Java Methods Quiz: 20 MCQ Questions

Welcome to the Java Methods MCQ Quiz! In this quiz, we have compiled the top 20 multiple-choice questions (MCQs) on Java methods, along with detailed answers and explanations.

The quiz covers all important concepts related to Java methods, including method declaration, method calling, parameters, return types, and many more.

Whether you are a student, beginner, or experienced programmer, this quiz will help you test your understanding of Java methods and strengthen your programming fundamentals. By practicing these MCQs, you can improve your logical thinking, coding skills, and preparation for interviews, academic exams, certification tests, and competitive programming.

There is no time limit for completing the quiz. Once you finish all the questions, you will receive a scorecard showing your performance and overall score.

Java Methods MCQ Quiz

Let’s begin the quiz and see how well you understand Java methods. Can you score 20/20 and achieve a perfect 100%? Good luck!

What is the return type of a method that does not return any value?
A. null
B. int
C. void
D. empty
void
In Java, the keyword ‘void’ is used as the return type when a method does not return any value. ‘null’ is a value, not a type.
Which of the following is a valid method declaration in Java?
A. void myMethod[];
B. void myMethod() {}
C. void myMethod;
D. myMethod() void {}
void myMethod() {}
A valid Java method must have a return type, method name, parentheses (with optional parameters), and a body enclosed in braces.

What will be the output of the following code?

public class Test {
    static int add(int a, int b) { return a + b; }
    public static void main(String[] args) {
        System.out.println(add(5, 3));
    }
}
A. 53
B. 8
C. 0
D. Compilation error
8
  • The add() method takes two integers and returns their sum.
  • add(5, 3) returns 5 + 3 = 8.
Which keyword is used to call a method of the same class?
A. self
B. this
C. super
D. No keyword needed (direct call)
No keyword needed (direct call)
  • In Java, you can call another method in the same class directly by its name.
  • ‘this’ is optional but not required.
What is method signature in Java?
A. Method name + return type
B. Method name + parameter list
C. Return type + parameter list
D. Method name + return type + parameter list
Method name + parameter list
  • In Java, the method signature consists of the method name and its parameter list (types and order).
  • The return type is NOT part of the signature, which matters for overloading resolution.

What is the output of the following program?

public class A {
    void show() { System.out.print("A "); }
    void show(int x) { System.out.print("A" + x + " "); }
    public static void main(String[] args) {
        A obj = new A();
        obj.show();
        obj.show(10);
    }
}
A. A A10
B. Compilation error
C. A A 10
D. A10 A
A A10
  • The first call invokes show() which prints “A “, the second call invokes show(int) which prints “A10 “.
  • This is method overloading — two methods with the same name but different parameter lists.
Can a Java method have multiple return statements?
A. No
B. Yes, but only one executes
C. Yes, and all execute
D. Yes, but only in void methods
Yes, but only one executes
  • A method can have multiple return statements (e.g., in if-else branches), but only one executes per method call.
  • Once a return is hit, the method exits immediately.
Java passes arguments to methods by:
A. Call by Reference only
B. Call by Value only
C. Value for primitives, reference for objects
D. Reference for primitives, value for objects
Call by Value only
  • Java is strictly pass-by-value.
  • For primitives, the value itself is copied.
  • For objects, the reference (memory address) is copied — so the object’s fields can be mutated, but reassigning the reference inside the method does not affect the caller’s reference.

What is the output of the following program code?

public class Test {
    static void change(int x) { x = 100; }
    public static void main(String[] args) {
        int a = 10;
        change(a);
        System.out.println(a);
    }
}
A. 100
B. 10
C. 0
D. Compilation error
10
  • Java passes ‘a’ by value, so inside change(), ‘x’ is a copy of ‘a’.
  • Modifying ‘x’ does not affect ‘a’.
  • So ‘a’ remains 10.

What is the output of the following program?

public class Test {
    static void modify(int[] arr) { arr[0] = 99; }
    public static void main(String[] args) {
        int[] nums = {1, 2, 3};
        modify(nums);
        System.out.println(nums[0]);
    }
}
A. 1
B. 99
C. 0
D. Compilation error
99
  • Although Java passes the array reference by value, both the caller and the method point to the same array object.
  • Modifying arr[0] inside modify() changes the actual array, so nums[0] becomes 99.
What is a variable argument (varargs) method in Java?
A. A method with no parameters
B. A method that can take a variable number of arguments of the same type
C. A method with optional parameters
D. A method with default parameter values
A method that can take a variable number of arguments of the same type
  • A varargs parameter is declared using ‘…’, e.g., void sum(int… nums).
  • It is internally treated as an array.
  • Only one varargs parameter is allowed per method, and it must be the last parameter.

What is the output of the following program code?

public class Test {
    static void print(int... nums) {
        System.out.println(nums.length);
    }
    public static void main(String[] args) {
        print(1, 2, 3, 4, 5);
    }
}
A. 5
B. 1
C. 0
D. Compilation error
5
  • The varargs parameter ‘nums’ collects all passed arguments into an array.
  • Passing 1, 2, 3, 4, 5 creates an array of length 5.

What is the output of the following program?

public class Test {
    static int fact(int n) {
        if (n == 0) return 1;
        return n * fact(n - 1);
    }
    public static void main(String[] args) {
        System.out.println(fact(5));
    }
}
A. 120
B. 24
C. 5
D. StackOverflowError
120
  • This is a classic recursive factorial method.
  • The base case is n==0 returning 1, which stops the recursion.
  • fact(5) = 5 × 4 × 3 × 2 × 1 × 1 = 120.
What happens if a recursive method has no base case?
A. Returns 0
B. Returns null
C. Throws StackOverflowError at runtime
D. Compilation error
Throws StackOverflowError at runtime
Without a base case, recursion continues indefinitely, consuming stack frames until the call stack overflows, throwing java.lang.StackOverflowError at runtime.

What is the output of the following program if no errors?

public class Test {
    static int count = 0;
    static void recurse(int n) {
        if (n <= 0) return;
        count++;
        recurse(n - 2);
    }
    public static void main(String[] args) {
        recurse(7);
        System.out.println(count);
    }
}
A. 7
B. 4
C. 3
D. 5
4
  • Call chain: recurse(7) → recurse(5) → recurse(3) → recurse(1) → recurse(-1).
  • count increments 4 times (for n = 7, 5, 3, 1).
  • When n = -1, the base case n <= 0 triggers and returns. Output: 4.

What is the output?

public class Test {
    int x = 10;
    void show(int x) {
        System.out.println(x + " " + this.x);
    }
    public static void main(String[] args) {
        new Test().show(20);
    }
}
A. 10 10
B. 20 10
C. 10 20
D. 20 20
20 10
  • Inside show(), the parameter ‘x’ shadows the instance variable ‘x’, so plain ‘x’ refers to the parameter (20).
  • ‘this.x’ explicitly refers to the instance variable, which is 10. Output: 20 10.

What will the output print?

public class Test {
    static void check(int n) {
        if (n < 0) return;
        System.out.println(n);
    }
    public static void main(String[] args) {
        check(-5);
        check(10);
    }
}
A. -5 10
B. 10
C. Compilation error
D. Nothing printed
10
  • ‘return’ in a void method exits the method immediately without executing the remaining statements.
  • check(-5) hits the return immediately and prints nothing.
  • check(10) does not satisfy n < 0, so it prints 10.
Which of the following is true about private methods?
A. Private methods can be overridden.
B. Private methods are inherited by subclasses.
C. Private methods are not visible outside their class; they are hidden.
D. Private methods cannot call other private methods.
Private methods are not visible outside their class; they are hidden.
  • Private methods are accessible only within the declaring class.
  • They are not inherited, so subclasses cannot override them.
  • If a subclass defines a method with the same name, it is a completely new method, not an override.

Predict the output of the following program.

public class Test {
    static int mystery(int n) {
        return n == 1 ? 1 : n + mystery(n - 1);
    }
    public static void main(String[] args) {
        System.out.println(mystery(4) - mystery(2));
    }
}
A. 7
B. 8
C. 6
D. 4
7
  • mystery(4) = 4 + 3 + 2 + 1 = 10.
  • mystery(2) = 2 + 1 = 3.
  • 10 − 3 = 7. The trap is computing both sides independently before subtracting.

What will the Java program print?

public class Test {
    static int val = 5;

    static int compute(int x) {
        val = val * 2;
        return x + val;
    }
    public static void main(String[] args) {
        int a = compute(val);
        System.out.println(a);
    }
}
A. 15
B. 20
C. 10
D. 25
15
  • The argument val is evaluated as 5 before the method runs.
  • Inside compute(5): val becomes 5 × 2 = 10, then returns 5 + 10 = 15.
  • The key trap: the argument is evaluated first, then the method body executes — so x holds the old value of val (5), not 10.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.