Java Polymorphism and Packages

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 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 3

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 4

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


  • private

  • protected

  • default

  • public

Question 5

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 6

Which type of polymorphism is achieved through method overloading?

  • Compile-time

  • Runtime

  • Dynamic

  • Static binding


Question 7

Which of the following allows method overriding in Java?

  • Static methods

  • Final methods

  • Instance methods

  • Private methods

Question 8

Which keyword is used to prevent method overriding?

  • static


  • final

  • protected

  • abstract

There are 8 questions to complete.

Take a part in the ongoing discussion