Saturday, August 3, 2024

Polymorphism MCQs

Polymorphism MCQs

What is polymorphism in Java?


A. The ability of a class to have multiple constructors

B. The ability of a method to operate on different types of objects

C. The ability of an object to inherit methods from multiple classes

D. The ability to create multiple instances of a class

Which of the following is an example of compile-time (static) polymorphism?


A. Method overriding

B. Method overloading

C. Dynamic method dispatch

D. Interface implementation

What will be the output of the following code?


java

Copy code

class Animal {

    void sound() {

        System.out.println("Animal sound");

    }

}


class Dog extends Animal {

    void sound() {

        System.out.println("Woof");

    }

}


public class Main {

    public static void main(String[] args) {

        Animal a = new Dog();

        a.sound();

    }

}

A. Animal sound

B. Woof

C. Compilation error

D. Runtime exception

What is method overloading?


A. Defining multiple methods with the same name but different parameters within the same class

B. Defining multiple methods with the same name and parameters within the same class

C. Overriding a method from a superclass

D. Implementing a method from an interface

What is method overriding?


A. Defining a method in a subclass with the same signature as a method in its superclass

B. Defining a method in a superclass with a different signature

C. Defining multiple methods with the same name in the same class

D. Implementing multiple methods in an interface

Which of the following statements is true about polymorphism?


A. Polymorphism allows objects to be treated as instances of their parent class

B. Polymorphism allows a method to have multiple implementations in the same class

C. Polymorphism requires multiple constructors in a class

D. Polymorphism allows a class to implement multiple interfaces

What will be the output of the following code?


java

Copy code

class Base {

    void show() {

        System.out.println("Base");

    }

}


class Derived extends Base {

    void show() {

        System.out.println("Derived");

    }

}


public class Main {

    public static void main(String[] args) {

        Base b = new Derived();

        b.show();

    }

}

A. Base

B. Derived

C. Compilation error

D. Runtime exception

Which keyword is used for method overriding in Java?


A. override

B. new

C. super

D. None of the above

What will be the output of the following code?


java

Copy code

class Parent {

    void display() {

        System.out.println("Parent display");

    }

}


class Child extends Parent {

    void display() {

        System.out.println("Child display");

    }

    

    void show() {

        System.out.println("Child show");

    }

}


public class Main {

    public static void main(String[] args) {

        Parent p = new Child();

        p.display();

        // p.show(); // Uncommenting this line will cause a compilation error

    }

}

A. Parent display

B. Child display

C. Parent display Child show

D. Compilation error

What type of polymorphism is demonstrated by the following code?


java

Copy code

class Calculator {

    int add(int a, int b) {

        return a + b;

    }

    

    double add(double a, double b) {

        return a + b;

    }

}


public class Main {

    public static void main(String[] args) {

        Calculator calc = new Calculator();

        System.out.println(calc.add(5, 10));

        System.out.println(calc.add(5.5, 10.5));

    }

}

A. Compile-time polymorphism

B. Runtime polymorphism

C. Method overriding

D. Constructor overloading

What is the output of the following code?


java

Copy code

class Animal {

    void sound() {

        System.out.println("Animal sound");

    }

}


class Cat extends Animal {

    void sound() {

        System.out.println("Meow");

    }

}


public class Main {

    public static void main(String[] args) {

        Animal a = new Cat();

        a.sound();

    }

}

A. Animal sound

B. Meow

C. Compilation error

D. Runtime exception

Which of the following is not a benefit of polymorphism?


A. Code reusability

B. Increased code readability

C. Flexibility in code design

D. Increased code execution speed

What is the result of the following code?


java

Copy code

class Base {

    void display() {

        System.out.println("Base display");

    }

}


class Derived extends Base {

    void display() {

        System.out.println("Derived display");

    }

}


public class Main {

    public static void main(String[] args) {

        Base b = new Derived();

        b.display();

    }

}

A. Base display

B. Derived display

C. Compilation error

D. Runtime exception

What does the super keyword do in method overriding?


A. Calls the superclass's method from the subclass

B. Overrides the superclass's method

C. Refers to the current object's method

D. Creates a new method in the superclass

Which of the following allows a subclass to use the methods of a superclass?


A. Method overloading

B. Method overriding

C. Polymorphism

D. Inheritance

What is the result of the following code?


java

Copy code

class Parent {

    void greet() {

        System.out.println("Hello from Parent");

    }

}


class Child extends Parent {

    void greet() {

        System.out.println("Hello from Child");

    }

    

    void greet(String name) {

        System.out.println("Hello " + name);

    }

}


public class Main {

    public static void main(String[] args) {

        Parent p = new Child();

        p.greet();

        // p.greet("John"); // Uncommenting this line will cause a compilation error

    }

}

A. Hello from Parent

B. Hello from Child

C. Hello from Child Hello John

D. Compilation error

Which of the following is an example of runtime (dynamic) polymorphism?


A. Method overloading

B. Method overriding

C. Constructor overloading

D. Interface implementation

What will be the output of the following code?


java

Copy code

class A {

    void method() {

        System.out.println("A");

    }

}


class B extends A {

    void method() {

        System.out.println("B");

    }

}


public class Main {

    public static void main(String[] args) {

        A a = new B();

        a.method();

    }

}

A. A

B. B

C. Compilation error

D. Runtime exception

What is the purpose of polymorphism in object-oriented programming?


A. To enable a single method to handle different types of inputs

B. To allow a class to inherit from multiple classes

C. To provide multiple constructors in a class

D. To restrict a method to a single implementation

What will be the result of the following code?


java

Copy code

class Vehicle {

    void start() {

        System.out.println("Vehicle started");

    }

}


class Car extends Vehicle {

    void start() {

        System.out.println("Car started");

    }

}


public class Main {

    public static void main(String[] args) {

        Vehicle v = new Car();

        v.start();

    }

}

A. Vehicle started

B. Car started

C. Compilation error

D. Runtime exception

Answers:

B. The ability of a method to operate on different types of objects

B. Method overloading

B. Woof

A. Defining multiple methods with the same name but different parameters within the same class

A. Defining a method in a subclass with the same signature as a method in its superclass

A. Polymorphism allows objects to be treated as instances of their parent class

B. Derived

D. None of the above

A. Parent display

A. Compile-time polymorphism

B. Meow

D. Increased code execution speed

B. Derived display

A. Calls the superclass's method from the subclass

D. Inheritance

A. Hello from Parent

B. Method overriding

B. B

A. To enable a single method to handle different types of inputs

B. Car started

No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...