Java Methods

Last Updated :
Discuss
Comments

Question 1

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


  • A

    void

  • B

    int

  • C

    Object

  • D

    It is compilation error when return type is not present.

Question 2

Which statement about instance methods is correct?

  • A

    They can only be accessed from static methods.

  • B

    They require an object to be called.

  • C

    They are automatically synchronized.

  • D

    They must be declared final.

Question 3

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


  • A

    Compilation error

  • B

    The method cannot be overridden

  • C

    The method can be overridden

  • D

    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();
    }
}


  • A

    Compilation error


  • B

    Static Method

  • C

    NullPointerException

  • D

    Runtime error

Question 5

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

  • A

    Overriding a method and narrowing its access modifier

  • B

    Overriding a method with the same return type

  • C

    Overriding a method using @Override annotation

  • D

    Overriding a method and throwing fewer checked exceptions

Question 6

What is method overloading?


  • A

    Two methods with the same name and parameters

  • B

    Two methods with the same name but different parameters

  • C

    Two methods with the same name but different return types

  • D

    Defining methods inside a loop


Question 7

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

  • A

    display();

  • B

    class.display();

  • C

    void display();

  • D

    call display();


Question 8

What is the use of varargs in Java?

  • A

    To return multiple values

  • B

    To accept a variable number of arguments

  • C

    To call a method multiple times


  • D

    To pass a list


Question 9

Which method is called when a Java program starts execution?


  • A

    execute()

  • B

    main()

  • C

    run()

  • D

    start()

There are 9 questions to complete.

Take a part in the ongoing discussion