Java Functions MCQs

1 min read

In Java, functions are formally known as methods and serve as modular blocks of reusable code encapsulated within classes. A method definition specifies a return type, a unique name, and an optional parameter list governing input values. Java strictly uses pass-by-value semantics when evaluating parameters, meaning method arguments copy variable values rather than mutating original references directly. Understanding method signatures, overloading, overriding, and static versus instance scope is vital for writing clean, structured code and succeeding in technical programming exams.

1. What is the primary purpose of a method in Java?

a) To define the blueprint of a class
b) To encapsulate a reusable block of code to perform a specific task
c) To manage JVM memory allocation
d) To replace package declarations
Correct Answer: b) To encapsulate a reusable block of code to perform a specific task
Explanation:
Methods modularize code by grouping related statements together, promoting code reuse and maintainability.

2. Which component is NOT a mandatory part of a standard Java method signature?

a) Method name
b) Return type
c) Access modifier
d) Parameter list (can be empty)
Correct Answer: c) Access modifier
Explanation:
Access modifiers (like public or private) are optional; default package-private visibility applies if omitted.

3. What happens if a method is declared with a return type other than void, but contains no return statement?

a) It returns null by default.
b) It causes a compile-time error ('missing return statement').
c) It returns zero automatically.
d) It throws a NullPointerException at runtime.
Correct Answer: b) It causes a compile-time error ('missing return statement').
Explanation:
Java enforces strict compile-time checking ensuring non-void methods guarantee a return path.

4. How does Java pass arguments to methods?

a) Always by reference
b) Always by value
c) By reference for objects and by value for primitives
d) By pointer sharing
Correct Answer: b) Always by value
Explanation:
Java is strictly pass-by-value. For object variables, the value passed is a copy of the object's reference address.

5. What is method overloading in Java?

a) Defining multiple methods with the same name in the same class with different parameter lists
b) Redefining a superclass method in a subclass with identical signature
c) Calling a method recursively without a base case
d) Overriding static methods
Correct Answer: a) Defining multiple methods with the same name in the same class with different parameter lists
Explanation:
Method overloading is compile-time polymorphism achieved by varying parameter count, types, or order.

6. Which of the following method declarations correctly implements variable-length arguments (varargs)?

a) void print(int... numbers)
b) void print(int numbers...)
c) void print(array... int numbers)
d) void print(int[]... numbers)
Correct Answer: a) void print(int... numbers)
Explanation:
Varargs syntax uses an ellipsis (...) after the data type, treating the parameter internally as an array within the method body.

7. Where must a varargs parameter be placed in a method's parameter list?

a) As the first parameter
b) In the middle of other parameters
c) As the absolute last parameter
d) Anywhere in the parameter list
Correct Answer: c) As the absolute last parameter
Explanation:
Java permits only one varargs parameter per method, and it must be positioned at the end of the parameter list.

8. What is recursion in Java methods?

a) A method calling itself directly or indirectly to solve a problem
b) A method executing asynchronously in a separate thread
c) A method defined inside an interface
d) A method that cannot accept parameters
Correct Answer: a) A method calling itself directly or indirectly to solve a problem
Explanation:
Recursion breaks complex problems down into smaller sub-problems by having a method invoke itself with a base case.

9. What exception occurs if a recursive method lacks a proper base case or terminates too late?

a) OutOfMemoryError
b) StackOverflowError
c) RecursionLimitException
d) ArithmeticException
Correct Answer: b) StackOverflowError
Explanation:
Each recursive call pushes a new frame onto the thread's call stack; infinite recursion exhausts stack memory.

10. Can a static method invoke an instance method directly without an object reference?

a) Yes, always
b) No, because static context has no implicit this reference
c) Only if declared in an abstract class
d) Only inside constructors
Correct Answer: b) No, because static context has no implicit this reference
Explanation:
Static methods belong to the class, not object instances, so they cannot directly access instance methods or variables.

11. What is the standard exact signature of the main method required by the JVM to launch a Java application?

a) public static void main(String[] args)
b) private void main(String args)
c) public void main(String[] args)
d) static int main(String[] args)
Correct Answer: a) public static void main(String[] args)
Explanation:
The JVM specifically looks for a public, static, void method named main accepting a String array argument.

12. Can the main method be overloaded in Java?

a) No, it causes a compile-time error.
b) Yes, you can define multiple overloaded versions of main, but the JVM will only execute the standard signature.
c) Only if marked as private.
d) Only in abstract classes.
Correct Answer: b) Yes, you can define multiple overloaded versions of main, but the JVM will only execute the standard signature.
Explanation:
The main method can be overloaded like any other method, but entry-point launching strictly invokes String[] args.

13. What does a void method return?

a) Zero
b) Null
c) Nothing (it does not return a value)
d) A boolean false
Correct Answer: c) Nothing (it does not return a value)
Explanation:
The void keyword specifies that the method performs actions without returning a resultant value to the caller.

14. Can a void method contain a return statement?

a) No, return statements are illegal in void methods.
b) Yes, an empty 'return;' statement can be used to exit the method early.
c) Only if returning null.
d) Only inside loop constructs.
Correct Answer: b) Yes, an empty 'return;' statement can be used to exit the method early.
Explanation:
An empty return statement can terminate execution of a void method prematurely.

15. What is a method signature composed of in Java?

a) Method name and parameter types (order and count)
b) Method name and return type
c) Access modifier and return type
d) Method name, return type, and exceptions thrown
Correct Answer: a) Method name and parameter types (order and count)
Explanation:
In Java, a method signature consists strictly of the method name and its parameter list. Return types are not part of the signature.

16. What happens if you define two methods in the same class with the exact same name and parameter list, but different return types?

a) It compiles successfully and resolves based on assignment context.
b) It causes a compile-time error ('method already defined').
c) It creates an overloaded method pair.
d) It throws a runtime linkage error.
Correct Answer: b) It causes a compile-time error ('method already defined').
Explanation:
Because return types are not part of the method signature, identical names and parameters result in duplicate method definitions.

17. What is method overriding?

a) Defining a method in a subclass with the exact same signature as a method in its superclass to provide specific implementation
b) Defining multiple methods with the same name in the same class
c) Hiding static methods
d) Calling a superclass constructor
Correct Answer: a) Defining a method in a subclass with the exact same signature as a method in its superclass to provide specific implementation
Explanation:
Method overriding enables runtime polymorphism by letting subclasses redefine inherited instance methods.

18. Can a static method be overridden in Java?

a) Yes, using dynamic dispatch.
b) No, static methods are hidden, not overridden, because resolution is determined at compile-time by reference type.
c) Only if marked as final.
d) Only inside interfaces.
Correct Answer: b) No, static methods are hidden, not overridden, because resolution is determined at compile-time by reference type.
Explanation:
Static methods participate in method hiding rather than overriding.

19. What is the role of the throws clause in a method signature?

a) To declare checked exceptions that might be thrown during method execution, requiring callers to handle them
b) To catch exceptions locally
c) To throw runtime exceptions automatically
d) To optimize method execution speed
Correct Answer: a) To declare checked exceptions that might be thrown during method execution, requiring callers to handle them
Explanation:
The throws keyword alerts callers that the method throws checked exceptions that must be handled or redeclared.

20. Can a method throw an unchecked exception (RuntimeException) without declaring it in a throws clause?

a) No, all exceptions must be declared.
b) Yes, unchecked exceptions are exempt from mandatory declaration or handling requirements.
c) Only if inside a try block.
d) Only in abstract methods.
Correct Answer: b) Yes, unchecked exceptions are exempt from mandatory declaration or handling requirements.
Explanation:
Unchecked exceptions (subclasses of RuntimeException) do not require explicit throws declarations.

21. What is a lambda expression in Java regarding methods?

a) An anonymous method passed as data, implementing a functional interface concisely
b) A method that runs on a GPU
c) A recursive function without a base case
d) A deprecated method wrapper
Correct Answer: a) An anonymous method passed as data, implementing a functional interface concisely
Explanation:
Lambda expressions provide functional programming syntax for single-method functional interfaces.

22. What is a method reference in Java (e.g., System.out::println)?

a) A shorthand syntax for a lambda expression that executes an existing method
b) A pointer to a memory address
c) A technique for dynamic downcasting
d) An alternative to constructor chaining
Correct Answer: a) A shorthand syntax for a lambda expression that executes an existing method
Explanation:
Method references (using `::`) make code cleaner when lambda bodies simply call an existing method.

23. Can an interface contain method implementations in Java 8 and later?

a) No, all interface methods must remain strictly abstract.
b) Yes, via default and static methods.
c) Only if declared as private.
d) Only in abstract classes.
Correct Answer: b) Yes, via default and static methods.
Explanation:
Java 8 introduced default and static methods in interfaces, allowing method bodies.

24. What is an abstract method?

a) A method declared without a body, ending with a semicolon, requiring subclasses to implement it
b) A method that executes asynchronously
c) A method with private visibility
d) A method that returns null
Correct Answer: a) A method declared without a body, ending with a semicolon, requiring subclasses to implement it
Explanation:
Abstract methods establish mandatory contracts for concrete subclasses.

25. Can a final method be overridden by a subclass?

a) Yes, always
b) No, the final modifier prevents subclasses from overriding the method
c) Only if the subclass is abstract
d) Only in static contexts
Correct Answer: b) No, the final modifier prevents subclasses from overriding the method
Explanation:
Declaring a method as final locks its implementation across subclasses.

26. What is method chaining in Java?

a) Calling multiple methods sequentially on the same object in a single statement by returning 'this'
b) Linking multiple methods recursively
c) Overloading constructors sequentially
d) Executing methods in parallel threads
Correct Answer: a) Calling multiple methods sequentially on the same object in a single statement by returning 'this'
Explanation:
Method chaining relies on methods returning the current object reference (`return this`) to allow fluent syntax.

27. What happens when an object reference is passed to a method and its internal state is modified inside that method?

a) The modifications affect the original object outside the method because the reference points to the same heap object.
b) The changes are discarded when the method returns.
c) It causes a ConcurrentModificationException.
d) It creates an independent clone automatically.
Correct Answer: a) The modifications affect the original object outside the method because the reference points to the same heap object.
Explanation:
Because Java passes object references by value (copy of the address), mutating object state inside a method reflects on the caller's object.

28. Can a primitive variable passed to a method have its original value changed by that method?

a) Yes, because primitives are passed by reference.
b) No, because primitives are passed by value, meaning the method receives an independent copy.
c) Only if declared as volatile.
d) Only inside recursive calls.
Correct Answer: b) No, because primitives are passed by value, meaning the method receives an independent copy.
Explanation:
Modifying a primitive parameter inside a method only alters the local copy; the caller's variable remains unchanged.

29. What is a native method in Java?

a) A method implemented in platform-dependent code (like C or C++) using JNI (Java Native Interface)
b) A method built into java.lang.Object
c) A method that executes on the stack
d) A method without parameters
Correct Answer: a) A method implemented in platform-dependent code (like C or C++) using JNI (Java Native Interface)
Explanation:
Native methods use the `native` keyword to interface with low-level system libraries.

30. What is a synchronized method?

a) A method whose execution is locked on the object instance (or class for static), allowing only one thread at a time
b) A method that executes simultaneously across multiple cores
c) A method that cannot throw exceptions
d) A method that runs in background daemon threads
Correct Answer: a) A method whose execution is locked on the object instance (or class for static), allowing only one thread at a time
Explanation:
The synchronized keyword ensures thread safety by serializing thread access to method execution.

31. What is method hiding?

a) When a subclass defines a static method with the same signature as a static method in its superclass
b) When a private method is inherited
c) When an abstract method lacks a body
d) When overloading methods with different return types
Correct Answer: a) When a subclass defines a static method with the same signature as a static method in its superclass
Explanation:
Static methods are hidden rather than overridden in subclasses.

32. What is a bridge method in Java?

a) A synthetic method generated by the compiler during type erasure to support polymorphism with generics
b) A method used to connect databases
c) A constructor helper method
d) An interface default method
Correct Answer: a) A synthetic method generated by the compiler during type erasure to support polymorphism with generics
Explanation:
Bridge methods ensure correct polymorphic method overriding when generic type parameters are erased.

33. Can a method return an array in Java?

a) Yes, methods can return arrays of primitive or reference types.
b) No, arrays cannot be returned from methods.
c) Only if converted to strings first.
d) Only inside static methods.
Correct Answer: a) Yes, methods can return arrays of primitive or reference types.
Explanation:
Methods can return any valid array type by specifying the array notation (e.g., `int[]`) in the return type.

34. Can a method return another method or function directly as a first-class citizen like in JavaScript?

a) No, Java methods cannot be returned directly, but methods can return functional interfaces or lambda expressions.
b) Yes, using the function return keyword.
c) Only in abstract classes.
d) Only when using varargs.
Correct Answer: a) No, Java methods cannot be returned directly, but methods can return functional interfaces or lambda expressions.
Explanation:
Java methods are not standalone first-class citizens, but lambda expressions and functional interfaces achieve similar factory behavior.

35. What is the scope of local variables declared inside a method body?

a) Restricted exclusively to the method block from their point of declaration onward
b) Accessible across the entire class
c) Accessible globally across all packages
d) Retained in memory across method invocations
Correct Answer: a) Restricted exclusively to the method block from their point of declaration onward
Explanation:
Local variables have stack scope limited to the method execution context and are destroyed when the method returns.

36. What is method signature ambiguity during overloading?

a) When compiler cannot determine which overloaded method to invoke due to ambiguous type widening conversions
b) When a method has duplicate parameter names
c) When return types differ
d) When exceptions are unhandled
Correct Answer: a) When compiler cannot determine which overloaded method to invoke due to ambiguous type widening conversions
Explanation:
Ambiguous method overloading causes a compile-time error when multiple overloads match argument types equally well.

37. What is a strictfp method?

a) A method that enforces strict IEEE 754 floating-point calculations across all platforms
b) A method restricted to strict integer math
c) A method that optimizes string concatenation
d) A method with strict exception checking
Correct Answer: a) A method that enforces strict IEEE 754 floating-point calculations across all platforms
Explanation:
The strictfp modifier ensures floating-point calculations produce identical results across different hardware architectures.

38. Can constructor methods be overloaded?

a) Yes, classes frequently provide multiple overloaded constructors to allow different ways of initializing object state.
b) No, constructors cannot be overloaded.
c) Only if declared as static.
d) Only in interfaces.
Correct Answer: a) Yes, classes frequently provide multiple overloaded constructors to allow different ways of initializing object state.
Explanation:
Constructor overloading is a standard practice for supporting flexible object instantiation.

39. What is the return type of a constructor method in Java?

a) Constructors have no return type whatsoever (not even void).
b) void
c) The class type itself
d) int
Correct Answer: a) Constructors have no return type whatsoever (not even void).
Explanation:
Declaring a return type on a constructor turns it into a regular method with the same name as the class.

40. Can a method be both abstract and final?

a) No, abstract methods must be overridden by subclasses, whereas final methods cannot be overridden.
b) Yes, for utility classes.
c) Only in interfaces.
d) Only when using default methods.
Correct Answer: a) No, abstract methods must be overridden by subclasses, whereas final methods cannot be overridden.
Explanation:
Abstract and final are mutually exclusive modifiers; combining them causes a compile-time error.

41. Can a method be both abstract and static?

a) No, static methods belong to the class and cannot be overridden, whereas abstract methods require subclass overriding.
b) Yes, in interfaces.
c) Only in final classes.
d) Only when returning void.
Correct Answer: a) No, static methods belong to the class and cannot be overridden, whereas abstract methods require subclass overriding.
Explanation:
Abstract and static modifiers conflict conceptually and cannot be combined on a method.

42. What is the result of invoking a method on a null object reference?

a) NullPointerException
b) ClassCastException
c) ArithmeticException
d) StackOverflowError
Correct Answer: a) NullPointerException
Explanation:
Attempting to invoke an instance method on a reference pointing to null throws a NullPointerException at runtime.

43. Can static methods be invoked using an object reference variable in Java?

a) Yes, though it is discouraged and generally recommended to call them via the class name.
b) No, it causes a compile-time error.
c) Only inside constructors.
d) Only if the object is null.
Correct Answer: a) Yes, though it is discouraged and generally recommended to call them via the class name.
Explanation:
Java permits calling static methods via object references, though compilers evaluate them based on the reference type.

44. What is a helper method?

a) A private or auxiliary method created to break down complex logic within a class into smaller, readable parts
b) A method that handles GUI events
c) A built-in JVM garbage collection method
d) A method that returns error codes
Correct Answer: a) A private or auxiliary method created to break down complex logic within a class into smaller, readable parts
Explanation:
Helper methods encapsulate internal support logic, keeping public methods clean and cohesive.

45. What is a callback method pattern in Java?

a) Passing an interface reference or lambda to a method so it can notify the caller when an event occurs
b) Calling a method recursively
c) Overloading constructors
d) Handling checked exceptions
Correct Answer: a) Passing an interface reference or lambda to a method so it can notify the caller when an event occurs
Explanation:
Callbacks enable decoupled event-driven architecture and asynchronous communication.

46. Can a method have multiple parameters of different types?

a) Yes, methods can accept any number of parameters of mixed primitive and reference types.
b) No, all parameters must share the same data type.
c) Only if wrapped in an array.
d) Only in static methods.
Correct Answer: a) Yes, methods can accept any number of parameters of mixed primitive and reference types.
Explanation:
Method parameters can combine any valid Java types in any order.

47. What is parameter shadowing in methods?

a) When a method parameter shares the name of an instance field, temporarily hiding the field within the method scope
b) When a method overrides a parent method
c) When a variable is garbage collected
d) When exceptions are suppressed
Correct Answer: a) When a method parameter shares the name of an instance field, temporarily hiding the field within the method scope
Explanation:
Parameter shadowing requires using `this.field` to distinguish the instance variable from the parameter.

48. What is a pure function in programming principles often applied in Java?

a) A function that produces no side effects and always returns the same output given the same input parameters
b) A method that modifies global static variables
c) A method with a void return type
d) A method that throws no exceptions
Correct Answer: a) A function that produces no side effects and always returns the same output given the same input parameters
Explanation:
Pure functions enhance predictability, testability, and concurrency safety.

49. Can a method be declared with default access modifier explicitly written as a keyword?

a) No, 'default' is not an access modifier keyword for methods (it is package-private by default; 'default' is used for interface methods).
b) Yes, by writing 'default void method()'
c) Only in abstract classes
d) Only in final classes
Correct Answer: a) No, 'default' is not an access modifier keyword for methods (it is package-private by default; 'default' is used for interface methods).
Explanation:
Package-private visibility requires omitting access modifiers entirely; writing 'default' on a class method is a syntax error.

50. What is a factory method?

a) A static method that creates and returns instances of a class, encapsulating object creation logic
b) A method that destroys objects
c) A method that generates random numbers
d) A constructor with public access
Correct Answer: a) A static method that creates and returns instances of a class, encapsulating object creation logic
Explanation:
Factory methods provide flexible alternatives to direct constructor instantiation.

51. What is a utility method?

a) A public static method providing general-purpose helper tasks (e.g., Math.sqrt)
b) A method that manages file I/O streams
c) A method restricted to private scope
d) An abstract interface method
Correct Answer: a) A public static method providing general-purpose helper tasks (e.g., Math.sqrt)
Explanation:
Utility methods are typically static and stateless, housed within utility classes.

52. Can a method accept an array as a parameter?

a) Yes, methods can accept 1D and multi-dimensional arrays as parameters.
b) No, arrays cannot be passed to methods.
c) Only if converted to ArrayList first.
d) Only in main methods.
Correct Answer: a) Yes, methods can accept 1D and multi-dimensional arrays as parameters.
Explanation:
Arrays are reference types and can be passed seamlessly into method parameter lists.

53. What happens if a method parameter is declared as final?

a) The parameter value cannot be reassigned within the method body.
b) The object referenced by the parameter becomes immutable.
c) The method cannot be overloaded.
d) The method executes faster.
Correct Answer: a) The parameter value cannot be reassigned within the method body.
Explanation:
Declaring a parameter final prevents reassigning its reference or primitive value inside the method.

54. What is dynamic method dispatch?

a) The mechanism by which Java resolves overridden method calls at runtime based on the actual object type
b) Loading classes dynamically via reflection
c) Executing static methods concurrently
d) Allocating method frames on heap
Correct Answer: a) The mechanism by which Java resolves overridden method calls at runtime based on the actual object type
Explanation:
Dynamic method dispatch underpins runtime polymorphism in Java.

55. Can a method declaration include checked exceptions that it does not actually throw?

a) Yes, a method can declare checked exceptions in its throws clause even if code inside does not currently throw them.
b) No, it causes a compile-time error.
c) Only for runtime exceptions.
d) Only in abstract methods.
Correct Answer: a) Yes, a method can declare checked exceptions in its throws clause even if code inside does not currently throw them.
Explanation:
Subclass implementations or interface contracts may throw declared exceptions, so declaring unused checked exceptions is permitted.

56. What is a generic method in Java?

a) A method featuring its own type parameter(s) declared before the return type (e.g., public T identity(T arg))
b) A method that accepts Object parameters
c) An abstract method in an interface
d) A method with varargs
Correct Answer: a) A method featuring its own type parameter(s) declared before the return type (e.g., public T identity(T arg))
Explanation:
Generic methods introduce type safety and reusability across different data types.

57. Can a non-generic class contain generic methods?

a) Yes, non-generic classes can define generic methods independently.
b) No, generic methods require the enclosing class to be generic.
c) Only if declared as static.
d) Only in abstract classes.
Correct Answer: a) Yes, non-generic classes can define generic methods independently.
Explanation:
Generic methods can exist in any standard class by declaring their own type parameters.

58. What is method return type covariance?

a) Allowing an overriding method in a subclass to return a subtype of the superclass method's return type
b) Returning primitive types instead of objects
c) Returning multiple values from a method
d) Returning void from abstract methods
Correct Answer: a) Allowing an overriding method in a subclass to return a subtype of the superclass method's return type
Explanation:
Covariant return types provide type-safe narrowing of return types during method overriding.

59. What is a default method conflict in interfaces?

a) When a class implements two interfaces containing default methods with identical signatures, requiring explicit override resolution
b) When a method returns null
c) When static methods clash
d) When exceptions are unhandled
Correct Answer: a) When a class implements two interfaces containing default methods with identical signatures, requiring explicit override resolution
Explanation:
Diamond inheritance of default methods forces the implementing class to resolve ambiguity explicitly.

60. Can a method be annotated with @Override if it does not actually override a superclass or interface method?

a) No, the compiler generates an error if the method does not correctly override a parent method.
b) Yes, it is just a documentation comment.
c) Only in abstract classes.
d) Only when returning void.
Correct Answer: a) No, the compiler generates an error if the method does not correctly override a parent method.
Explanation:
The @Override annotation enforces a compile-time check to prevent typo-based overriding bugs.

61. What is method argument promotion during overload resolution?

a) Widening primitive conversions (e.g., int to long or float) when an exact parameter match is absent
b) Converting objects to primitives
c) Boxing wrapper objects
d) Casting subclasses to superclasses
Correct Answer: a) Widening primitive conversions (e.g., int to long or float) when an exact parameter match is absent
Explanation:
If an exact overload match is missing, Java promotes smaller primitive types to wider types automatically.

62. Can a method accept another method as a direct parameter in traditional Java versions prior to Java 8?

a) No, methods could not be passed as parameters without using object wrappers or anonymous inner classes.
b) Yes, always.
c) Only in static methods.
d) Only in abstract classes.
Correct Answer: a) No, methods could not be passed as parameters without using object wrappers or anonymous inner classes.
Explanation:
Before Java 8 introduced lambdas, passing behavior required verbose anonymous inner classes implementing interfaces.

63. What is the lifespan of parameters and local variables declared inside a method?

a) They are created when the method is invoked and destroyed when the method execution completes.
b) They persist for the entire program execution.
c) They remain in heap memory until garbage collected.
d) They persist across thread switches.
Correct Answer: a) They are created when the method is invoked and destroyed when the method execution completes.
Explanation:
Stack frame allocation ensures local variables exist exclusively during the method's active call duration.

64. Can a method be declared with multiple return statements?

a) Yes, methods can contain multiple return statements branching across conditional blocks.
b) No, exactly one return statement is permitted per method.
c) Only in void methods.
d) Only in recursive methods.
Correct Answer: a) Yes, methods can contain multiple return statements branching across conditional blocks.
Explanation:
Methods can exit at different points using return statements based on conditional logic.

65. What is a recursive base case?

a) The condition in a recursive method that stops further self-calls and initiates return unwinding
b) The initial method call from main
c) The method return type
d) The exception handler block
Correct Answer: a) The condition in a recursive method that stops further self-calls and initiates return unwinding
Explanation:
A base case is mandatory to prevent infinite recursion and StackOverflowError.

66. Can an exception be thrown inside a method without a try-catch block?

a) Yes, provided any checked exceptions are declared in the throws clause or are unchecked RuntimeExceptions.
b) No, all exceptions must be caught locally inside the method.
c) Only in main methods.
d) Only in abstract classes.
Correct Answer: a) Yes, provided any checked exceptions are declared in the throws clause or are unchecked RuntimeExceptions.
Explanation:
Exceptions propagate up the call stack if not caught locally, as long as method signature rules are satisfied.

67. What is a method's return value used for?

a) To pass computation results back to the calling code context
b) To terminate program threads
c) To allocate heap memory
d) To declare package dependencies
Correct Answer: a) To pass computation results back to the calling code context
Explanation:
Return values provide the output data produced by method execution to the caller.

68. Can a static method access non-static variables through an object reference passed as a parameter?

a) Yes, because the static method has an explicit object reference to access instance members.
b) No, static methods can never access instance members under any circumstance.
c) Only if declared in the same line.
d) Only if returning void.
Correct Answer: a) Yes, because the static method has an explicit object reference to access instance members.
Explanation:
While static methods lack an implicit `this`, they can freely access instance members of explicitly passed object references.

69. What is the purpose of the assert keyword statement inside a method?

a) To test assumptions about program state during debugging, throwing AssertionError if false
b) To handle checked exceptions
c) To optimize loop compilation
d) To replace return statements
Correct Answer: a) To test assumptions about program state during debugging, throwing AssertionError if false
Explanation:
Assertions validate internal assumptions during testing and debugging phases.

70. Can a method be overloaded based solely on its return type?

a) No, return types alone cannot distinguish overloaded methods because ambiguous call sites cannot be resolved.
b) Yes, always.
c) Only for primitive types.
d) Only in interfaces.
Correct Answer: a) No, return types alone cannot distinguish overloaded methods because ambiguous call sites cannot be resolved.
Explanation:
Java overload resolution relies on method name and arguments, ignoring return types.

71. What is the effect of placing the synchronized keyword on a static method?

a) It locks on the Class object (Class.class), serializing access across all instances of that class
b) It locks on the current object instance ('this')
c) It locks on the JVM heap
d) It prevents multithreading entirely
Correct Answer: a) It locks on the Class object (Class.class), serializing access across all instances of that class
Explanation:
Static synchronized methods acquire a lock on the Class object rather than individual object instances.

72. Can a method be declared with both public and private access modifiers simultaneously?

a) No, access modifiers are mutually exclusive; combining them causes a compile-time error.
b) Yes, to allow dual visibility.
c) Only in abstract classes.
d) Only in interface default methods.
Correct Answer: a) No, access modifiers are mutually exclusive; combining them causes a compile-time error.
Explanation:
A method can have only one access modifier (public, protected, private, or default).

73. What is a recursive helper method pattern?

a) A public wrapper method that calls a private recursive method with extra accumulator or index parameters
b) A method that calls main recursively
c) A method without return values
d) An interface default method
Correct Answer: a) A public wrapper method that calls a private recursive method with extra accumulator or index parameters
Explanation:
Helper methods shield recursive implementation details and extra parameters from public API users.

74. Can a method contain inner local classes defined inside its body?

a) Yes, Java permits defining local inner classes within method bodies.
b) No, classes can only be defined at the package level.
c) Only in static methods.
d) Only when returning void.
Correct Answer: a) Yes, Java permits defining local inner classes within method bodies.
Explanation:
Local classes can be declared inside methods, with access to effectively final local variables.

75. What happens if a method throws an unhandled exception?

a) The exception propagates up the call stack to the calling method; if unhandled at top-level, the JVM prints the stack trace and terminates the thread.
b) The program automatically restarts.
c) The exception is ignored silently.
d) The method returns null.
Correct Answer: a) The exception propagates up the call stack to the calling method; if unhandled at top-level, the JVM prints the stack trace and terminates the thread.
Explanation:
Uncaught exceptions bubble up the call stack until handled or reaching the JVM uncaught exception handler.

76. Can an annotation be applied to method parameters?

a) Yes, annotations (like @NonNull or @RequestParam) can be applied to method parameters.
b) No, annotations are restricted to classes and methods.
c) Only on return types.
d) Only in interfaces.
Correct Answer: a) Yes, annotations (like @NonNull or @RequestParam) can be applied to method parameters.
Explanation:
Java supports annotations on local variables, fields, methods, parameters, and types.

77. What is method inlining performed by the JIT compiler?

a) An optimization where the JIT compiler replaces a method call site with the actual body of the method to eliminate call overhead
b) Writing methods on a single line of source code
c) Deleting unused methods automatically
d) Converting methods to static
Correct Answer: a) An optimization where the JIT compiler replaces a method call site with the actual body of the method to eliminate call overhead
Explanation:
Method inlining is a key JVM Just-In-Time optimization that boosts execution speed.

78. Can a method call itself indirectly through another method in mutual recursion?

a) Yes, mutual recursion occurs when method A calls method B, and method B calls method A.
b) No, recursion must be strictly direct.
c) Only if declared as static.
d) Only in abstract classes.
Correct Answer: a) Yes, mutual recursion occurs when method A calls method B, and method B calls method A.
Explanation:
Indirect or mutual recursion involves a cycle of method invocations achieving recursive problem solving.

79. What is the purpose of the return type in a method declaration?

a) To specify the data type of the value the method will return to the caller
b) To determine method execution speed
c) To define the access level
d) To name the parameters
Correct Answer: a) To specify the data type of the value the method will return to the caller
Explanation:
The return type informs the compiler and caller what type of data the method outputs.

80. Can a method be marked as strictfp without a return type?

a) No, strictfp is a modifier that can accompany return types on methods and classes, but methods require a return type.
b) Yes, like constructors.
c) Only in interfaces.
d) Only in void methods.
Correct Answer: a) No, strictfp is a modifier that can accompany return types on methods and classes, but methods require a return type.
Explanation:
Methods always require a specified return type (or void), unlike constructors.

81. What is the difference between a method parameter and a method argument?

a) Parameters are variable placeholders defined in the method signature; arguments are the actual values passed during method invocation.
b) They are exact synonyms with no distinction.
c) Arguments are used in static methods, parameters in instance methods.
d) Parameters belong to constructors, arguments to methods.
Correct Answer: a) Parameters are variable placeholders defined in the method signature; arguments are the actual values passed during method invocation.
Explanation:
Parameters are formal definitions in declarations, while arguments are actual values supplied when calling methods.

82. Can a method have zero parameters?

a) Yes, methods can have empty parameter lists represented by empty parentheses '()'.
b) No, at least one parameter is mandatory.
c) Only in abstract classes.
d) Only when returning void.
Correct Answer: a) Yes, methods can have empty parameter lists represented by empty parentheses '()'.
Explanation:
Parameterless methods are common for getter routines or action triggers.

83. What happens if a method overrides a superclass method but changes the access modifier from public to private?

a) It causes a compile-time error because overriding methods cannot reduce accessibility.
b) It compiles and restricts visibility successfully.
c) It hides the method instead.
d) It throws a runtime exception.
Correct Answer: a) It causes a compile-time error because overriding methods cannot reduce accessibility.
Explanation:
Overriding rules state that visibility cannot be restricted below the access level of the superclass method.

84. Can a method be called before it is declared in the source file in Java?

a) Yes, Java compiler scans the entire class, so method declaration order within a class does not matter.
b) No, methods must be declared before they are called.
c) Only in static blocks.
d) Only in interfaces.
Correct Answer: a) Yes, Java compiler scans the entire class, so method declaration order within a class does not matter.
Explanation:
Unlike some single-pass languages, Java's compiler processes entire class definitions regardless of method ordering.

85. What is the role of the stack memory frame during a method invocation?

a) It stores local variables, parameters, return address, and intermediate computation states for that specific method call.
b) It stores heap-allocated objects.
c) It manages garbage collection threads.
d) It compiles bytecode to native machine instructions.
Correct Answer: a) It stores local variables, parameters, return address, and intermediate computation states for that specific method call.
Explanation:
Each method invocation allocates a stack frame on the thread's call stack, which is popped upon method return.

86. Can an interface have a method with a method body without using the default or static keyword?

a) No, all traditional interface methods are implicitly abstract and cannot have a body unless marked default or static (or private in Java 9+).
b) Yes, always.
c) Only in abstract classes.
d) Only in functional interfaces.
Correct Answer: a) No, all traditional interface methods are implicitly abstract and cannot have a body unless marked default or static (or private in Java 9+).
Explanation:
Interface method bodies require explicit modifiers like default, static, or private.

87. What is a private interface method introduced in Java 9?

a) A helper method with a body inside an interface used to share code between default or other private interface methods
b) A method that cannot be implemented by classes
c) A constructor inside an interface
d) An abstract method requiring implementation
Correct Answer: a) A helper method with a body inside an interface used to share code between default or other private interface methods
Explanation:
Java 9 introduced private interface methods to reduce code duplication among default methods.

88. Can a method return a functional interface instance?

a) Yes, methods can return lambda expressions or method references matching a functional interface return type.
b) No, functional interfaces cannot be returned from methods.
c) Only in abstract classes.
d) Only in static methods.
Correct Answer: a) Yes, methods can return lambda expressions or method references matching a functional interface return type.
Explanation:
Methods can act as factories returning lambda expressions or method references.

89. What is the purpose of method overloading when designing robust APIs?

a) To provide intuitive method names for similar operations taking different argument types or counts
b) To reduce the number of classes in a package
c) To bypass type checking
d) To enforce inheritance
Correct Answer: a) To provide intuitive method names for similar operations taking different argument types or counts
Explanation:
Overloading (like `println` taking int, String, double, etc.) improves API readability and usability.

90. Can a method be declared with multiple varargs parameters?

a) No, Java permits strictly one varargs parameter per method, and it must be the final parameter.
b) Yes, up to three varargs parameters.
c) Only if enclosed in arrays.
d) Only in abstract methods.
Correct Answer: a) No, Java permits strictly one varargs parameter per method, and it must be the final parameter.
Explanation:
Allowing multiple varargs would create ambiguity in parameter matching during invocation.

91. What happens when a method invokes another method?

a) A new stack frame is pushed onto the thread's call stack, and execution control transfers to the called method.
b) The current method is destroyed immediately.
c) Heap memory is cleared.
d) A new thread is spawned automatically.
Correct Answer: a) A new stack frame is pushed onto the thread's call stack, and execution control transfers to the called method.
Explanation:
Method invocation manages execution flow using stack frames.
← Previous: Java Exception Handling MCQs
Next →: Java Inheritance and Polymorphism MCQs
Java Collections Frame Work MCQs

Java Collections Frame Work MCQs

The Java Collections Framework provides a unified architecture consisting of interfaces (such as List, Set, and Map) and implementation classes…

By MCQs Generator
No Image

400+ C++ Interview Questions MCQ

1. What is the primary purpose of a method in Java? a) To define the blueprint of a class b)…

By MCQs Generator
Latest C++ Basics MCQs

Latest C++ Basics MCQs

Created by Bjarne Stroustrup as an extension of the C programming language, C++ is a powerful, high-performance, statically typed language…

By MCQs Generator