Saturday, August 3, 2024

Method Overriding MCQs

 Method Overriding MCQs

What is method overriding in Java?


A. Providing a new implementation of a method in a subclass with the same name and parameters as in the superclass

B. Defining a method with a different name in the subclass

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

D. Overloading a method in the superclass

Which of the following is necessary for method overriding?


A. The method in the subclass must have the same name, return type, and parameters as the method in the superclass

B. The method in the subclass must have a different name

C. The method in the superclass must be private

D. The method in the subclass must be static

What will be the output of the following code?


java

Copy code

class Parent {

    void display() {

        System.out.println("Parent");

    }

}


class Child extends Parent {

    void display() {

        System.out.println("Child");

    }

}


public class Main {

    public static void main(String[] args) {

        Parent p = new Child();

        p.display();

    }

}

A. Parent

B. Child

C. Parent Child

D. Compilation error

Which access modifier can be used in the overridden method in the subclass?


A. It can be the same or more accessible than the method in the superclass

B. It must be more restrictive than the method in the superclass

C. It must be private

D. It must be static

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");

    }

    

    void makeSound() {

        super.sound();

    }

}


public class Test {

    public static void main(String[] args) {

        Dog d = new Dog();

        d.makeSound();

    }

}

A. Animal sound

B. Woof

C. Animal sound Woof

D. Woof Animal sound

Which of the following statements is false about method overriding?


A. Overridden methods can have different return types

B. The overridden method cannot be static

C. The method in the subclass should have the same name and parameters

D. Overriding allows for runtime polymorphism

What is the purpose of using @Override annotation in Java?


A. To indicate that a method is overriding a superclass method

B. To define a new method in the subclass

C. To make a method static

D. To make a method private

What will be the output of the following code?


java

Copy code

class Base {

    void method() {

        System.out.println("Base");

    }

}


class Derived extends Base {

    void method() {

        System.out.println("Derived");

    }

}


public class Main {

    public static void main(String[] args) {

        Base b = new Derived();

        b.method();

    }

}

A. Base

B. Derived

C. Base Derived

D. Compilation error

Which method cannot be overridden in Java?


A. Static methods

B. Non-static methods

C. Protected methods

D. Public methods

What will be the result 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() {

        super.display();

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.show();

    }

}

A. Parent display

B. Child display

C. Parent display Child display

D. Child display Parent display

Can you override a method in a subclass if it is declared final in the superclass?


A. Yes

B. No

C. Only if the method is protected

D. Only if the method is private

What is the output of the following code?


java

Copy code

class Animal {

    void eat() {

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

    }

}


class Cat extends Animal {

    void eat() {

        System.out.println("Cat eating");

    }

}


public class Test {

    public static void main(String[] args) {

        Animal a = new Cat();

        a.eat();

    }

}

A. Animal eating

B. Cat eating

C. Animal eating Cat eating

D. Compilation error

Can a constructor be overridden in Java?


A. Yes

B. No

C. Only if it is protected

D. Only if it is public

What will be the result 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");

    }

}


public class Main {

    public static void main(String[] args) {

        Parent p = new Child();

        p.display();

    }

}

A. Parent display

B. Child display

C. Parent display Child display

D. Compilation error

Which of the following can be overridden?


A. Non-static method in a subclass

B. Static method in a subclass

C. Constructor in a subclass

D. Private method in a subclass

What is the output of the following code?


java

Copy code

class Base {

    void method() {

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

    }

}


class Derived extends Base {

    void method() {

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

    }

    

    void callMethod() {

        super.method();

    }

}


public class Main {

    public static void main(String[] args) {

        Derived d = new Derived();

        d.callMethod();

    }

}

A. Base method

B. Derived method

C. Derived method Base method

D. Base method Derived method

Which keyword is used to call an overridden method in the superclass?


A. this

B. super

C. parent

D. base

What is the result of the following code?


java

Copy code

class A {

    void show() {

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

    }

}


class B extends A {

    void show() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        A a = new B();

        a.show();

    }

}

A. A show

B. B show

C. A show B show

D. Compilation error

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() {

        display();

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.show();

    }

}

A. Parent display

B. Child display

C. Parent display Child display

D. Compilation error

In which case is method overriding not possible?


A. When the method in the subclass has the same name, return type, and parameters as the method in the superclass

B. When the method in the superclass is static

C. When the method in the superclass is final

D. When the method in the superclass is private

Answers:

A. Providing a new implementation of a method in a subclass with the same name and parameters as in the superclass

A. The method in the subclass must have the same name, return type, and parameters as the method in the superclass

B. Child

A. It can be the same or more accessible than the method in the superclass

A. Animal sound

A. Overridden methods can have different return types

A. To indicate that a method is overriding a superclass method

B. Derived

A. Static methods

A. Parent display

B. No

B. Cat eating

B. No

B. Child display

A. Non-static method in a subclass

A. Base method

B. super

B. B show

B. Child display

B. When the method in the superclass is static

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