Mastering Java methods and functions is key to developing efficient and clean Java applications. This topic covers essential concepts like method declaration, overloading, recursion, and passing parameters for more effective programming. Below are 30 multiple-choice questions (MCQs) to test your understanding of these crucial Java programming concepts.
Java Methods
Method Declaration and Definition
Which of the following is required to declare a method in Java?
A. Method name
B. Return type
C. Parameters
D. All of the above
What does the void keyword in a method declaration signify?
A. The method does not return any value
B. The method returns a string value
C. The method is private
D. The method is abstract
Which of the following is a correct way to declare a method in Java?
A. public int addNumbers() {}
B. int addNumbers public {}
C. addNumbers() public int {}
D. void addNumbers{} public int
Which of the following statements is true about method return types in Java?
A. A method must always return a value
B. A method must never return a value
C. A method can return only primitive data types
D. A method can return any type including objects
What is the purpose of a method signature in Java?
A. To define the method’s return type
B. To define the method’s access level
C. To identify the method by its name and parameters
D. To declare the method’s exceptions
Which keyword is used to call a method from within another method in Java?
A. this
B. super
C. return
D. None of the above
What is the correct syntax for declaring a method with parameters in Java?
A. public void calculate(int a, double b) {}
B. public void calculate() {int a, double b;}
C. public calculate() {int a, double b;}
D. public void calculate(int, double) {}
Method Overloading
What is method overloading in Java?
A. Declaring multiple methods with the same name but different signatures
B. Changing the return type of a method
C. Overriding a method in a subclass
D. Declaring a method that throws an exception
Which of the following is NOT a condition for method overloading in Java?
A. Same method name
B. Same parameter list
C. Different return type
D. Different parameter types or number of parameters
Can we overload a method by changing only its return type in Java?
A. Yes, we can overload based on return type
B. No, overloading requires a change in the method’s parameter list
C. Yes, if the method name is different
D. No, overloading is not allowed in Java
Which of the following is a valid example of method overloading?
A. int add(int a, int b) { return a + b; } int add(int a, double b) { return a + b; }
B. int add(int a) { return a; }int add() { return 0; }
C. int add(int a) { return a; }double add(int a) { return a; }
D. void add() {}void add() {}
What will happen if we try to overload a method by changing only the method’s name but keeping the parameters and return type the same?
A. Compilation error
B. Runtime error
C. The program will run correctly
D. It will create a new method
Which of the following is an advantage of method overloading in Java?
A. It increases readability by allowing methods with similar functions to share the same name
B. It reduces memory usage
C. It decreases method invocation time
D. It hides the internal implementation of a method
Recursion in Java
What is recursion in Java?
A. A method that calls itself
B. A method that executes an infinite loop
C. A method that executes once
D. A method that returns a value
What is required for a recursive method to work properly in Java?
A. A return statement
B. A base case to terminate the recursion
C. A non-return value
D. A loop to control the recursion
In which of the following situations is recursion commonly used?
A. To compute factorials
B. To loop through an array
C. To count the number of elements in a list
D. All of the above
What happens when a recursive method does not have a base case?
A. The method will return null
B. The method will throw an exception
C. The method will cause a stack overflow error
D. The method will run infinitely without any result
Which of the following is the correct syntax for a recursive method in Java?
A. public void recursiveMethod(int n) { if (n > 0) recursiveMethod(n - 1); }
B. public int recursiveMethod() { recursiveMethod(); }
C. public int recursiveMethod(int n) { recursiveMethod(n - 1); }
D. public recursiveMethod() { if (n > 0) recursiveMethod(); }
How does recursion differ from iteration in Java?
A. Recursion uses a loop, while iteration uses function calls
B. Recursion uses function calls to repeat the process, while iteration uses loops
C. Recursion can only be used for sorting, iteration is used for calculations
D. Both recursion and iteration are the same
Passing Parameters
Which of the following types of parameter passing is used in Java for primitive data types?
A. By value
B. By reference
C. By pointer
D. By address
What happens when you pass a primitive type to a method in Java?
A. The method receives the reference to the primitive
B. The method receives a copy of the primitive
C. The original value is modified in the calling method
D. The primitive is converted to a reference
How are objects passed to a method in Java?
A. By reference
B. By value
C. By pointer
D. By copy
Which of the following is true about parameter passing in Java?
A. Primitive data types are passed by reference
B. Objects are passed by value
C. Both objects and primitives are passed by reference
D. Primitives are passed by value, objects are passed by reference
What is the default value of a parameter in a Java method if not initialized?
A. 0
B. Null
C. Undefined
D. It depends on the data type
Which type of parameter passing is used when modifying an object inside a method?
A. By value
B. By reference
C. By pointer
D. Both by value and by reference
Which of the following statements about varargs is true in Java?
A. Varargs can only be used with primitive types
B. Varargs must be the last parameter in a method
C. Varargs does not allow multiple parameters of the same type
D. Varargs must be used for all method parameters
What will happen if a method is called with fewer parameters than required in Java?
A. It will throw an exception
B. The default values of parameters will be used
C. It will compile but give wrong output
D. The program will terminate unexpectedly
Can you pass a method call as a parameter in Java?
A. Yes, it’s possible
B. No, only variables can be passed
C. Only primitive types can be passed
D. It depends on the method signature
Which of the following correctly defines a method with variable-length arguments?
A. public void example(int... args) {}
B. public void example(args...) {}
C. public void example(int[] args) {}
D. public void example(int args[]) {}
What happens when a method is invoked with more arguments than the defined parameters in Java?
A. It will compile with a warning
B. It will throw a runtime exception
C. It will automatically resize the parameter list
D. It will cause a compile-time error
Answers Table
Qno
Answer (Option with the text)
1
D. All of the above
2
A. The method does not return any value
3
A. public int addNumbers() {}
4
D. A method can return any type including objects
5
C. To identify the method by its name and parameters
6
D. None of the above
7
A. public void calculate(int a, double b) {}
8
A. Declaring multiple methods with the same name but different signatures
9
C. Different return type
10
B. No, overloading requires a change in the method’s parameter list
11
A. int add(int a, int b) { return a + b; }int add(int a, double b) { return a + b; }
12
A. Compilation error
13
A. It increases readability by allowing methods with similar functions to share the same name
14
A. A method that calls itself
15
B. A base case to terminate the recursion
16
A. To compute factorials
17
C. The method will cause a stack overflow error
18
A. public void recursiveMethod(int n) { if (n > 0) recursiveMethod(n - 1); }
19
B. Recursion uses function calls to repeat the process, while iteration uses loops
20
A. By value
21
B. The method receives a copy of the primitive
22
A. By reference
23
D. Primitives are passed by value, objects are passed by reference