Java Methods

Last Updated :
Discuss
Comments

Question 1

What is the default return type of a method in Java if none is specified?


  • void

  • int

  • Object

  • It is compilation error when return type is not present.

Question 2

Which statement about instance methods is correct?

  • They can only be accessed from static methods.

  • They require an object to be called.

  • They are automatically synchronized.

  • They must be declared final.

Question 3

What happens if we declare a method both static and final?


  • Compilation error

  • The method cannot be overridden

  • The method can be overridden

  • Runtime error


Question 4

What will happen when calling a static method using an object?

Java
class Demo {
    static void show() {
        System.out.println("Static Method");
    }
    public static void main(String[] args) {
        Demo obj = new Demo();
        obj.show();
    }
}


  • Compilation error


  • Static Method

  • NullPointerException

  • Runtime error

Question 5

Which of the following will cause a compile-time error in method overriding?

  • Overriding a method and narrowing its access modifier

  • Overriding a method with the same return type

  • Overriding a method using @Override annotation

  • Overriding a method and throwing fewer checked exceptions

Question 6

What is method overloading?


  • Two methods with the same name and parameters

  • Two methods with the same name but different parameters

  • Two methods with the same name but different return types

  • Defining methods inside a loop


Question 7

What is the correct syntax to call a method named display() from the same class?

  • display();

  • class.display();

  • void display();

  • call display();


Question 8

What is the use of varargs in Java?

  • To return multiple values

  • To accept a variable number of arguments

  • To call a method multiple times


  • To pass a list


Question 9

Which method is called when a Java program starts execution?


  • execute()

  • main()

  • run()

  • start()

There are 9 questions to complete.

Take a part in the ongoing discussion