Saturday, August 3, 2024

super Keyword MCQs

 super Keyword MCQs

What does the super keyword refer to in Java?


A. The current instance of the class

B. The superclass of the current class

C. The static methods of the class

D. The private members of the superclass

How can you use the super keyword to call a superclass constructor?


A. super()

B. super.constructor()

C. this()

D. parent()

Which of the following statements about super is true?


A. super can be used to access private members of the superclass

B. super can be used to call overridden methods in the superclass

C. super can be used to refer to the static variables of the superclass

D. super can be used to refer to local variables

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

    }

    

    void show() {

        super.display();

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.show();

    }

}

A. Parent

B. Child

C. Parent Child

D. Child Parent

Can super be used to access static methods of the superclass?


A. Yes, super can be used to access static methods

B. No, super cannot be used to access static methods

C. Yes, but only if the static method is overridden

D. No, super can only access instance methods

What happens if super is used to call a superclass method that is not overridden in the subclass?


A. The superclass method will be called directly

B. It will result in a compilation error

C. It will cause a runtime exception

D. The subclass method will be called

What is the correct way to call a superclass method from a subclass method?


java

Copy code

class SuperClass {

    void method() {

        System.out.println("SuperClass Method");

    }

}


class SubClass extends SuperClass {

    void method() {

        System.out.println("SubClass Method");

        // Code to call SuperClass method

    }

}

A. super.method();

B. this.method();

C. SuperClass.method();

D. parent.method();

Which constructor call is used to call a parameterized constructor of the superclass?


java

Copy code

class Parent {

    Parent(int x) {

        System.out.println("Parent " + x);

    }

}


class Child extends Parent {

    Child(int x) {

        // Constructor code

    }

}

A. super(x);

B. super();

C. this(x);

D. parent(x);

What will be the output of the following code?


java

Copy code

class A {

    int num = 10;

}


class B extends A {

    int num = 20;

    

    void print() {

        System.out.println(super.num);

    }

}


public class Main {

    public static void main(String[] args) {

        B b = new B();

        b.print();

    }

}

A. 10

B. 20

C. Compilation error

D. Runtime exception

Which of the following statements about super and this is true?


A. this refers to the current instance, while super refers to the superclass

B. this can only be used in static methods, while super can be used in instance methods

C. super can be used to call constructors of the current class

D. this can be used to call superclass methods

What is 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 callSuperSound() {

        super.sound();

    }

}


public class Main {

    public static void main(String[] args) {

        Dog d = new Dog();

        d.callSuperSound();

    }

}

A. Animal sound

B. Woof

C. Animal sound Woof

D. Woof Animal sound

Can you use super to access private members of the superclass?


A. Yes, super can access private members

B. No, super cannot access private members

C. Yes, but only if the private members are accessed through a method

D. No, super can only access protected members

What will be the output of the following code?


java

Copy code

class Parent {

    void method() {

        System.out.println("Parent");

    }

}


class Child extends Parent {

    void method() {

        super.method();

        System.out.println("Child");

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.method();

    }

}

A. Parent

B. Child

C. Parent Child

D. Child Parent

What is the output of the following code?


java

Copy code

class Base {

    Base() {

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

    }

}


class Derived extends Base {

    Derived() {

        super();

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

    }

}


public class Main {

    public static void main(String[] args) {

        Derived d = new Derived();

    }

}

A. Derived Constructor Base Constructor

B. Base Constructor Derived Constructor

C. Derived Constructor

D. Base Constructor

Which of the following is a correct statement about super and instance variables?


A. super can be used to access instance variables that are hidden by subclass variables

B. super can modify instance variables

C. super can only access static instance variables

D. super cannot access instance variables in the superclass

Can super be used in a static method?


A. Yes, super can be used in a static method

B. No, super cannot be used in a static method

C. Yes, but only if the method is final

D. No, super can only be used in instance methods

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

        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

Which of the following statements is true about the super keyword and method overriding?


A. super can be used to call overridden methods in the superclass

B. super cannot be used to call overridden methods

C. super can be used to override methods

D. super can be used to access methods in an interface

What is the result of the following code snippet?


java

Copy code

class Parent {

    void method() {

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

    }

}


class Child extends Parent {

    void method() {

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

    }

    

    void callParentMethod() {

        super.method();

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.callParentMethod();

    }

}

A. Parent method

B. Child method

C. Parent method Child method

D. Child method Parent method

Can you use super to access a method that is private in the superclass?


A. Yes, if the method is accessed through a protected method in the superclass

B. No, super cannot access private methods

C. Yes, if the private method is accessed via reflection

D. No, super can only access public methods

Answers:

B. The superclass of the current class

A. super()

B. super can be used to call overridden methods in the superclass

A. Parent

B. No, super cannot be used to access static methods

A. The superclass method will be called directly

A. super.method();

A. super(x);

A. 10

A. this refers to the current instance, while super refers to the superclass

A. Animal sound

B. No, super cannot access private members

C. Parent Child

B. Base Constructor Derived Constructor

A. super can be used to access instance variables that are hidden by subclass variables

B. No, super cannot be used in a static method

A. Parent Display

A. super can be used to call overridden methods in the superclass

A. Parent method

B. No, super cannot access private methods

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