MCQs on Methods and Functions | Java Methods

Introduction to Java Methods & Functions

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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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

  1. 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
  2. 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
  3. 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
  4. 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() {}
  5. 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
  6. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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(); }
  6. 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

  1. 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
  2. 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
  3. How are objects passed to a method in Java?
    • A. By reference
    • B. By value
    • C. By pointer
    • D. By copy
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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[]) {}
  11. 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

QnoAnswer (Option with the text)
1D. All of the above
2A. The method does not return any value
3A. public int addNumbers() {}
4D. A method can return any type including objects
5C. To identify the method by its name and parameters
6D. None of the above
7A. public void calculate(int a, double b) {}
8A. Declaring multiple methods with the same name but different signatures
9C. Different return type
10B. No, overloading requires a change in the method’s parameter list
11A. int add(int a, int b) { return a + b; } int add(int a, double b) { return a + b; }
12A. Compilation error
13A. It increases readability by allowing methods with similar functions to share the same name
14A. A method that calls itself
15B. A base case to terminate the recursion
16A. To compute factorials
17C. The method will cause a stack overflow error
18A. public void recursiveMethod(int n) { if (n > 0) recursiveMethod(n - 1); }
19B. Recursion uses function calls to repeat the process, while iteration uses loops
20A. By value
21B. The method receives a copy of the primitive
22A. By reference
23D. Primitives are passed by value, objects are passed by reference
24A. 0
25B. By reference
26B. Varargs must be the last parameter in a method
27A. It will throw an exception
28A. Yes, it’s possible
29A. public void example(int... args) {}
30B. It will throw a runtime exception

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top