Java Methods Interview Questions

    Question 1JAVA - Method Basics

    Which of the following is a valid method declaration in Java?

    Question 2JAVA - Return Type

    What will be the output?

    int add(int a, int b) { return a + b; } System.out.println(add(3, 4));

    Question 3JAVA - Method Overloading

    Which statement about method overloading is true?

    Question 4JAVA - Method Overloading Example

    What will the following code print?

    class Test { void show(int x) { System.out.println("int: " + x); } void show(String s) { System.out.println("String: " + s); } } Test t = new Test(); t.show("Java");

    Question 5JAVA - Method Overriding

    Which of the following is true about method overriding?

    Question 6JAVA - Static Method

    Which statement is correct about static methods?

    Question 7JAVA - Recursion Example

    What will this code print?

    int fact(int n) { if(n == 1) return 1; return n * fact(n - 1); } System.out.println(fact(4));

    Question 8JAVA - Varargs Method

    What will the following code print?

    void display(int... nums) { System.out.println(nums.length); } display(10, 20, 30);

    Question 9JAVA - Main Method Signature

    Which is the correct main method declaration in Java?

    Question 10JAVA - Pass by Value

    What will be the output?

    void change(int x) { x = 50; } int a = 10; change(a); System.out.println(a);