Java Polymorphism and Packages

Here are 10 essential multiple-choice questions on Java Polymorphism and Packages, covering key concepts.


Last Updated :
Discuss
Comments

Question 1

What is the key difference between compile-time and runtime polymorphism in Java?

  • Compile-time polymorphism is implemented using method overriding, while runtime polymorphism is implemented using method overloading.


  • Compile-time polymorphism is determined during compilation, while runtime polymorphism is resolved at runtime.

  • Runtime polymorphism is faster than compile-time polymorphism.

  • Compile-time polymorphism is achieved using interfaces.

Question 2

What will be the output of the following code?

Java
class A {
    void display() {
        System.out.println("Class A");
    }
}
class B extends A {
    void display() {
        System.out.println("Class B");
    }
}
public class Main {
    public static void main(String[] args) {
        A obj = new B();
        obj.display();
    }
}


  • Class A

  • Class B

  • Compilation error

  • Runtime error

Question 3

Which of the following statements about method overloading is true?

  • Method overloading occurs only in different classes.


  • Method overloading is achieved by changing the method name.

  • Method overloading allows multiple methods with the same name but different parameters.

  • Overloaded methods must have the same return type.


Question 4

What will be the output of this code?

Java
class Parent {
    static void show() {
        System.out.println("Parent");
    }
}
class Child extends Parent {
    static void show() {
        System.out.println("Child");
    }
}
public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        p.show();
    }
}


  • Parent

  • Child

  • Compilation error


  • Runtime error

Question 5

What is the primary use of packages in Java?


  • To group related classes together.

  • To speed up Java execution.

  • To enable multiple inheritance.

  • To improve memory allocation.

Question 6

What will happen if two classes in different packages have the same name and are imported in a Java file?


  • Compilation error due to ambiguity.

  • The last imported class is used.


  • The first imported class is used.

  • Java automatically renames one class.

Question 7

Which access modifier allows a member to be accessible within the same package but not from outside?


  • private

  • protected

  • default

  • public

Question 8

What will be the output of the following program?

Java
package pack1;
public class A {
    public void display() {
        System.out.println("Hello from A");
    }
}

package pack2;
import pack1.A;
public class B {
    public static void main(String args[]) {
        A obj = new A();
        obj.display();
    }
}


  • Compilation error

  • Hello from A

  • Runtime error

  • No output

Question 9

Which of the following correctly defines method overriding?

  • Changing the method name while keeping the parameters the same.

  • Defining a method with the same signature in the child class as in the parent class.

  • Overriding a static method in the child class.

  • Changing the return type of a method in a subclass.


Question 10

Which of the following best demonstrates runtime polymorphism in Java?

  • Calling an overloaded method from the same class

  • Overriding a method in a subclass and calling it through a superclass reference

  • Using static methods with the same name in parent and child classes

  • Accessing private methods from subclasses

There are 10 questions to complete.

Take a part in the ongoing discussion