Saturday, August 3, 2024

HashSet MCQs

 HashSet MCQs

What is the primary characteristic of a HashSet in Java?


A. It allows duplicate elements.

B. It maintains insertion order.

C. It does not allow duplicate elements.

D. It is synchronized.

Which interface does HashSet implement?


A. List

B. Set

C. Queue

D. Map

What will be the output of the following code?


java

Copy code

HashSet<String> set = new HashSet<>();

set.add("A");

set.add("B");

set.add("A");

System.out.println(set);

A. [A, B, A]

B. [A, B]

C. [B, A]

D. null

Which method is used to remove an element from a HashSet?


A. remove(element)

B. delete(element)

C. discard(element)

D. erase(element)

What is the time complexity of adding an element to a HashSet?


A. O(1)

B. O(n)

C. O(log n)

D. O(n^2)

How do you check if a HashSet contains a specific element?


A. contains(element)

B. has(element)

C. find(element)

D. includes(element)

What is the default initial capacity of a HashSet in Java?


A. 10

B. 16

C. 20

D. 32

Which method is used to get the size of a HashSet?


A. size()

B. length()

C. count()

D. getSize()

Which of the following operations is NOT supported by HashSet?


A. Adding elements

B. Removing elements

C. Retrieving elements by index

D. Checking if an element exists

What will be the output of the following code?


java

Copy code

HashSet<Integer> set = new HashSet<>();

set.add(1);

set.add(2);

set.add(3);

set.remove(2);

System.out.println(set);

A. [1, 2, 3]

B. [1, 3]

C. [2, 1, 3]

D. [1]

Which method is used to clear all elements from a HashSet?


A. clear()

B. deleteAll()

C. removeAll()

D. empty()

Which of the following statements is true about HashSet?


A. HashSet is synchronized.

B. HashSet maintains the order of elements.

C. HashSet does not guarantee any specific order of elements.

D. HashSet allows duplicate elements.

What will be the result of calling hashSet.isEmpty() on an empty HashSet?


A. true

B. false

C. null

D. 0

What does the HashSet method addAll(Collection c) do?


A. Adds all elements from the specified collection to the HashSet.

B. Removes all elements from the specified collection from the HashSet.

C. Replaces all elements in the HashSet with elements from the specified collection.

D. Checks if all elements in the specified collection are present in the HashSet.

Which method in HashSet allows you to iterate over its elements?


A. iterator()

B. forEach()

C. stream()

D. traverse()

What will hashSet.size() return for a HashSet with three elements?


A. 3

B. 2

C. 1

D. null

Which method is used to check if a HashSet is a subset of another collection?


A. containsAll(Collection c)

B. isSubsetOf(Collection c)

C. checkSubset(Collection c)

D. subsetOf(Collection c)

What will be the result of hashSet.contains(null) if null is not present in the HashSet?


A. false

B. true

C. null

D. 0

Which constructor parameter can be used to specify the initial capacity of a HashSet?


A. new HashSet(int initialCapacity)

B. new HashSet(int capacity)

C. new HashSet(int size)

D. new HashSet(int maxCapacity)

Which method in HashSet can be used to check for equality between two HashSet instances?


A. equals(Object o)

B. isEqual(Object o)

C. compareTo(Object o)

D. matches(Object o)

Answers:

C. It does not allow duplicate elements.

B. Set

B. [A, B]

A. remove(element)

A. O(1)

A. contains(element)

B. 16

A. size()

C. Retrieving elements by index

B. [1, 3]

A. clear()

C. HashSet does not guarantee any specific order of elements.

A. true

A. Adds all elements from the specified collection to the HashSet.

A. iterator()

A. 3

A. containsAll(Collection c)

A. false

A. new HashSet(int initialCapacity)

A. equals(Object o)

ArrayList MCQs

 ArrayList MCQs

What is the initial capacity of an ArrayList in Java when it is created with no arguments?


A. 5

B. 10

C. 15

D. 20

Which method is used to add an element at a specific index in an ArrayList?


A. addAt(index, element)

B. insert(index, element)

C. add(index, element)

D. set(index, element)

What will be the output of the following code?


java

Copy code

ArrayList<String> list = new ArrayList<>();

list.add("A");

list.add("B");

list.add("C");

list.remove(1);

System.out.println(list);

A. [A, B, C]

B. [A, C]

C. [B, C]

D. [A]

Which method is used to retrieve an element from an ArrayList at a specific index?


A. get(index)

B. fetch(index)

C. retrieve(index)

D. elementAt(index)

How do you check if an ArrayList contains a specific element?


A. contains(element)

B. has(element)

C. find(element)

D. includes(element)

Which method removes all elements from an ArrayList?


A. clear()

B. delete()

C. removeAll()

D. empty()

What is the time complexity of accessing an element at a specific index in an ArrayList?


A. O(1)

B. O(n)

C. O(log n)

D. O(n^2)

Which method is used to check the size of an ArrayList?


A. length()

B. size()

C. count()

D. getSize()

How can you add all elements of one ArrayList to another ArrayList?


A. addAll(Collection c)

B. insertAll(Collection c)

C. appendAll(Collection c)

D. merge(Collection c)

What will be the output of the following code?


java

Copy code

ArrayList<Integer> list = new ArrayList<>();

list.add(1);

list.add(2);

list.add(3);

list.set(1, 5);

System.out.println(list);

A. [1, 5, 3]

B. [1, 2, 3]

C. [5, 2, 3]

D. [1, 5]

Which method is used to remove an element from an ArrayList at a specific index?


A. remove(index)

B. delete(index)

C. discard(index)

D. erase(index)

What is the default behavior of the add() method when adding an element to an ArrayList?


A. Adds the element at the end of the list.

B. Adds the element at the beginning of the list.

C. Inserts the element at a specific position.

D. Replaces the element at the end of the list.

Which of the following statements is true about ArrayList in Java?


A. ArrayList is synchronized.

B. ArrayList is thread-safe.

C. ArrayList allows duplicate elements.

D. ArrayList does not allow null elements.

How do you check if an ArrayList is empty?


A. isEmpty()

B. empty()

C. isNull()

D. hasNoElements()

Which method is used to sort an ArrayList of elements?


A. sort(Comparator c)

B. order()

C. arrange()

D. organize()

What is the time complexity of removing an element by index in an ArrayList?


A. O(1)

B. O(n)

C. O(log n)

D. O(n^2)

What will be the output of the following code?


java

Copy code

ArrayList<String> list = new ArrayList<>();

list.add("A");

list.add("B");

list.add("C");

list.remove("B");

System.out.println(list);

A. [A, B, C]

B. [A, C]

C. [B, C]

D. [A, B]

Which method is used to ensure that a specified element is present in an ArrayList?


A. add(element)

B. ensure(element)

C. check(element)

D. confirm(element)

What will list.indexOf("C") return if "C" is the third element in the ArrayList?


A. 2

B. 3

C. 1

D. 0

Which method is used to create a new ArrayList from an existing one?


A. clone()

B. copy()

C. duplicate()

D. new ArrayList<>(existingList)

Answers:

B. 10

C. add(index, element)

B. [A, C]

A. get(index)

A. contains(element)

A. clear()

A. O(1)

B. size()

A. addAll(Collection c)

A. [1, 5, 3]

A. remove(index)

A. Adds the element at the end of the list.

C. ArrayList allows duplicate elements.

A. isEmpty()

A. sort(Comparator c)

B. O(n)

B. [A, C]

A. add(element)

A. 2

D. new ArrayList<>(existingList)

Object Class MCQs

 Object Class MCQs

Which class is the root class of all classes in Java?


A. Base

B. Super

C. Object

D. Root

Which of the following methods is defined in the Object class?


A. toString()

B. print()

C. getName()

D. printString()

What does the equals() method in the Object class do?


A. Compares the memory addresses of two objects

B. Compares the contents of two objects for equality

C. Compares the types of two objects

D. Retrieves the string representation of an object

What is the default implementation of the hashCode() method in the Object class?


A. Returns a unique integer for each object

B. Returns a hash code based on the object's memory address

C. Returns the length of the object

D. Returns a constant value

Which method of the Object class is used to obtain a string representation of an object?


A. toString()

B. getString()

C. getRepresentation()

D. printString()

What is the purpose of the clone() method in the Object class?


A. To create a new instance of the object with the same state

B. To create a copy of the object's class definition

C. To return a new class with modified attributes

D. To generate a unique identifier for the object

Which of the following is true about the finalize() method in the Object class?


A. It is called by the garbage collector before an object is destroyed

B. It is used to finalize the state of an object before serialization

C. It is invoked when an object is created

D. It is used to check if an object is ready for garbage collection

Which method is called when an object is printed using System.out.println()?


A. toString()

B. print()

C. getDescription()

D. getString()

What will be the output of the following code?


java

Copy code

class Test {

    @Override

    public String toString() {

        return "Test Object";

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test();

        System.out.println(t);

    }

}

A. Test Object

B. Test@<hashcode>

C. Test

D. Compilation error

Which of the following methods can be overridden in a subclass?


A. hashCode()

B. toString()

C. clone()

D. All of the above

What does the getClass() method in the Object class return?


A. The name of the class

B. The Class object representing the runtime class of the object

C. The superclass of the object

D. The class loader for the class

What will be the output of the following code?


java

Copy code

class Test {

    @Override

    public boolean equals(Object obj) {

        return this == obj;

    }

}


public class Main {

    public static void main(String[] args) {

        Test t1 = new Test();

        Test t2 = new Test();

        System.out.println(t1.equals(t2));

    }

}

A. true

B. false

C. Compilation error

D. Runtime exception

Which method of the Object class must be implemented when a class implements the Cloneable interface?


A. clone()

B. toString()

C. hashCode()

D. equals()

Which method is used to get a hash code value for an object?


A. getHashCode()

B. getCode()

C. hashCode()

D. getValue()

What will be the result of the following code?


java

Copy code

class Test {

    @Override

    public String toString() {

        return "Test Object";

    }

    

    @Override

    public int hashCode() {

        return 42;

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test();

        System.out.println(t.hashCode());

    }

}

A. 42

B. Test Object

C. A unique hash code value

D. Compilation error

What is the result of calling obj.getClass().getName() where obj is an instance of a class?


A. The name of the class as a String

B. The class loader for the class

C. The Class object for the class

D. The superclass of the class

Which method in the Object class is used to check whether two objects are the same?


A. compareTo()

B. equals()

C. isEqual()

D. compare()

What will be the result of the following code?


java

Copy code

class Test {

    @Override

    public void finalize() {

        System.out.println("Finalizing");

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test();

        t = null;

        System.gc();

    }

}

A. Finalizing (if the garbage collector runs)

B. Compilation error

C. No output

D. Runtime exception

Which of the following methods is not present in the Object class?


A. wait()

B. notify()

C. notifyAll()

D. sleep()

What will be the output of the following code?


java

Copy code

class Test {

    public int x;

    

    public Test(int x) {

        this.x = x;

    }

    

    @Override

    public String toString() {

        return "x = " + x;

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test(10);

        System.out.println(t);

    }

}

A. x = 10

B. Test@<hashcode>

C. Test Object

D. Compilation error

Answers:

C. Object

A. toString()

B. Compares the contents of two objects for equality

B. Returns a hash code based on the object's memory address

A. toString()

A. To create a new instance of the object with the same state

A. It is called by the garbage collector before an object is destroyed

A. toString()

A. Test Object

D. All of the above

B. The Class object representing the runtime class of the object

B. false

A. clone()

C. hashCode()

A. 42

A. The name of the class as a String

B. equals()

A. Finalizing (if the garbage collector runs)

D. sleep()

A. x = 10


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

Interface MCQs

 Interface MCQs

What is an interface in Java?


A. A reference type that can contain abstract methods and constants

B. A class that can be instantiated and contains only abstract methods

C. A type that contains only static methods and final variables

D. A type that contains only concrete methods

Which keyword is used to declare an interface in Java?


A. interface

B. abstract

C. class

D. implements

Can an interface extend another interface?


A. Yes, an interface can extend another interface

B. No, an interface cannot extend another interface

C. Yes, but only if the extending interface is abstract

D. No, an interface can only implement another interface

What will be the output of the following code?


java

Copy code

interface Animal {

    void makeSound();

}


class Dog implements Animal {

    public void makeSound() {

        System.out.println("Woof");

    }

}


public class Test {

    public static void main(String[] args) {

        Animal a = new Dog();

        a.makeSound();

    }

}

A. Woof

B. Animal sound

C. Compilation error

D. Woof Animal sound

Which of the following statements is true about interface methods?


A. Interface methods are abstract by default and cannot have a body (prior to Java 8)

B. Interface methods must be static

C. Interface methods must be private

D. Interface methods can have any access modifier

Can an interface have constructors in Java?


A. No, interfaces cannot have constructors

B. Yes, interfaces can have constructors

C. Yes, but only if they are static

D. Yes, but only if they are private

What is the output of the following code?


java

Copy code

interface Shape {

    void draw();

    

    default void color() {

        System.out.println("Default color");

    }

}


class Circle implements Shape {

    public void draw() {

        System.out.println("Drawing Circle");

    }

}


public class Main {

    public static void main(String[] args) {

        Shape s = new Circle();

        s.draw();

        s.color();

    }

}

A. Drawing Circle Default color

B. Default color Drawing Circle

C. Drawing Circle

D. Compilation error

Can an interface extend multiple interfaces?


A. Yes, an interface can extend multiple interfaces

B. No, an interface cannot extend multiple interfaces

C. Yes, but only if the interfaces have the same methods

D. No, an interface can only extend one interface

Which of the following is not allowed in an interface?


A. Instance variables

B. Static final variables

C. Abstract methods

D. Default methods

What will be the result of the following code?


java

Copy code

interface Animal {

    void eat();

    

    default void sleep() {

        System.out.println("Sleeping");

    }

}


class Cat implements Animal {

    public void eat() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        Animal a = new Cat();

        a.eat();

        a.sleep();

    }

}

A. Cat eating Sleeping

B. Sleeping Cat eating

C. Cat eating

D. Compilation error

What is the result of the following code?


java

Copy code

interface A {

    void methodA();

}


interface B {

    void methodB();

}


class C implements A, B {

    public void methodA() {

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

    }

    

    public void methodB() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        C c = new C();

        c.methodA();

        c.methodB();

    }

}

A. Method A Method B

B. Method B Method A

C. Method A

D. Method B

Which of the following can be included in an interface starting from Java 8?


A. Static methods

B. Constructors

C. Instance fields

D. Private methods

What is the output of the following code?


java

Copy code

interface Vehicle {

    default void start() {

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

    }

}


class Bike implements Vehicle {

    public void start() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        Vehicle v = new Bike();

        v.start();

    }

}

A. Vehicle started

B. Bike started

C. Compilation error

D. Bike started Vehicle started

Can an interface implement another interface?


A. No, an interface cannot implement another interface

B. Yes, but only if the second interface is abstract

C. Yes, but the interface must not have any methods

D. Yes, an interface can extend another interface but not implement it

What is the output of the following code?


java

Copy code

interface A {

    void show();

    

    default void defaultMethod() {

        System.out.println("Default Method in A");

    }

}


class B implements A {

    public void show() {

        System.out.println("Show in B");

    }

    

    public void defaultMethod() {

        System.out.println("Overridden Default Method in B");

    }

}


public class Main {

    public static void main(String[] args) {

        A a = new B();

        a.show();

        a.defaultMethod();

    }

}

A. Show in B Overridden Default Method in B

B. Show in B Default Method in A

C. Overridden Default Method in B Show in B

D. Compilation error

Which of the following cannot be used to define an interface method?


A. default

B. static

C. private

D. protected

What is the result of the following code?


java

Copy code

interface I {

    int x = 10;

}


public class Main {

    public static void main(String[] args) {

        System.out.println(I.x);

    }

}

A. 10

B. Compilation error

C. 0

D. Interface field cannot be accessed

Can an interface extend another interface that contains default methods?


A. Yes, the extending interface can inherit default methods

B. No, an interface cannot extend another interface with default methods

C. Yes, but the extending interface must override all default methods

D. No, only abstract methods can be inherited

What will be the output of the following code?


java

Copy code

interface I {

    void display();

}


abstract class A implements I {

    void show() {

        System.out.println("Show in A");

    }

}


class B extends A {

    public void display() {

        System.out.println("Display in B");

    }

}


public class Main {

    public static void main(String[] args) {

        B b = new B();

        b.display();

        b.show();

    }

}

A. Display in B Show in A

B. Show in A Display in B

C. Display in B

D. Compilation error

Which of the following is true about interface inheritance?


A. A class can implement multiple interfaces

B. A class can extend multiple interfaces

C. An interface can extend multiple classes

D. An interface can extend only one interface

Answers:

A. A reference type that can contain abstract methods and constants

A. interface

A. Yes, an interface can extend another interface

A. Woof

A. Interface methods are abstract by default and cannot have a body (prior to Java 8)

A. No, interfaces cannot have constructors

A. Drawing Circle Default color

A. Yes, an interface can extend multiple interfaces

A. Instance variables

A. Cat eating Sleeping

A. Method A Method B

A. Static methods

B. Bike started

D. Yes, an interface can extend another interface but not implement it

A. Show in B Overridden Default Method in B

D. protected

A. 10

A. Yes, the extending interface can inherit default methods

A. Display in

Abstract Class MCQs

 Abstract Class MCQs

What is an abstract class in Java?


A. A class that cannot be instantiated and may contain abstract methods

B. A class that can be instantiated and contains only abstract methods

C. A class with only static methods

D. A class that can be instantiated and contains no methods

Which of the following is true about abstract methods in an abstract class?


A. Abstract methods must be implemented by subclasses

B. Abstract methods can have a body

C. Abstract methods can only be static

D. Abstract methods can be private

Can an abstract class have a constructor?


A. Yes, an abstract class can have a constructor

B. No, an abstract class cannot have a constructor

C. Yes, but only if it is private

D. Yes, but only if the constructor is static

What will be the output of the following code?


java

Copy code

abstract class Animal {

    abstract void makeSound();

    

    void eat() {

        System.out.println("Eating");

    }

}


class Dog extends Animal {

    void makeSound() {

        System.out.println("Woof");

    }

}


public class Test {

    public static void main(String[] args) {

        Animal a = new Dog();

        a.eat();

        a.makeSound();

    }

}

A. Eating Woof

B. Woof Eating

C. Compilation error

D. Eating

Can an abstract class have fields (instance variables)?


A. Yes, an abstract class can have fields

B. No, an abstract class cannot have fields

C. Yes, but only static fields

D. No, an abstract class can only have methods

What happens if a subclass does not implement all abstract methods of an abstract superclass?


A. The subclass must also be declared abstract

B. The subclass will compile successfully

C. The subclass will throw a runtime exception

D. The subclass can be instantiated, but only with default implementations

Which keyword is used to declare an abstract class in Java?


A. abstract

B. abstractclass

C. class

D. super

What will be the output of the following code?


java

Copy code

abstract class Shape {

    abstract void draw();

    

    void display() {

        System.out.println("Displaying");

    }

}


class Circle extends Shape {

    void draw() {

        System.out.println("Drawing Circle");

    }

}


public class Main {

    public static void main(String[] args) {

        Shape s = new Circle();

        s.display();

        s.draw();

    }

}

A. Displaying Drawing Circle

B. Drawing Circle Displaying

C. Displaying

D. Compilation error

Can an abstract class implement an interface?


A. Yes, an abstract class can implement an interface

B. No, an abstract class cannot implement an interface

C. Yes, but only if the interface methods are abstract

D. Yes, but only if the abstract class does not have abstract methods

What is the output of the following code?


java

Copy code

abstract class Vehicle {

    abstract void start();

    

    void stop() {

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

    }

}


class Car extends Vehicle {

    void start() {

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

    }

}


public class Test {

    public static void main(String[] args) {

        Vehicle v = new Car();

        v.start();

        v.stop();

    }

}

A. Car started Vehicle stopped

B. Vehicle stopped Car started

C. Car started

D. Compilation error

Can an abstract class be declared final?


A. Yes, an abstract class can be declared final

B. No, an abstract class cannot be final

C. Yes, but only if it does not contain any abstract methods

D. No, an abstract class can be final only if it has no methods

What will be the output of the following code?


java

Copy code

abstract class Animal {

    void sleep() {

        System.out.println("Sleeping");

    }

    

    abstract void makeSound();

}


class Cat extends Animal {

    void makeSound() {

        System.out.println("Meow");

    }

}


public class Main {

    public static void main(String[] args) {

        Animal a = new Cat();

        a.makeSound();

        a.sleep();

    }

}

A. Meow Sleeping

B. Sleeping Meow

C. Meow

D. Sleeping

What is the purpose of an abstract class?


A. To provide a base for other classes to extend and enforce a contract

B. To create a concrete class that can be instantiated

C. To contain only static methods

D. To prevent inheritance

Can an abstract class be extended by another abstract class?


A. Yes, an abstract class can be extended by another abstract class

B. No, an abstract class cannot be extended by another abstract class

C. Yes, but only if the subclass implements all abstract methods

D. No, an abstract class can only be extended by concrete classes

What is the output of the following code?


java

Copy code

abstract class Base {

    abstract void method();

    

    void display() {

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

    }

}


class Derived extends Base {

    void method() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        Base b = new Derived();

        b.display();

    }

}

A. Derived method

B. Base display

C. Derived method Base display

D. Compilation error

Which of the following is true about abstract classes?


A. An abstract class can have both abstract and concrete methods

B. An abstract class can only have abstract methods

C. An abstract class can only have static methods

D. An abstract class can only have constructors

What will be the result of the following code?


java

Copy code

abstract class Vehicle {

    abstract void drive();

    

    void honk() {

        System.out.println("Honking");

    }

}


class Bike extends Vehicle {

    void drive() {

        System.out.println("Bike driving");

    }

}


public class Main {

    public static void main(String[] args) {

        Vehicle v = new Bike();

        v.drive();

        v.honk();

    }

}

A. Bike driving Honking

B. Honking Bike driving

C. Bike driving

D. Honking

Can an abstract class be used as a type for variables?


A. Yes, an abstract class can be used as a type for variables

B. No, an abstract class cannot be used as a type for variables

C. Yes, but only if the abstract class is instantiated

D. No, abstract classes cannot be assigned to variables

What is the result of the following code?


java

Copy code

abstract class Instrument {

    abstract void play();

    

    void tune() {

        System.out.println("Tuning");

    }

}


class Piano extends Instrument {

    void play() {

        System.out.println("Playing Piano");

    }

}


public class Main {

    public static void main(String[] args) {

        Instrument i = new Piano();

        i.tune();

        i.play();

    }

}

A. Tuning Playing Piano

B. Playing Piano Tuning

C. Tuning

D. Playing Piano

Which of the following statements is false regarding abstract classes?


A. An abstract class can be instantiated directly

B. An abstract class can have constructors

C. An abstract class can have instance variables

D. An abstract class can extend another abstract class

Answers:

A. A class that cannot be instantiated and may contain abstract methods

A. Abstract methods must be implemented by subclasses

A. Yes, an abstract class can have a constructor

A. Eating Woof

A. Yes, an abstract class can have fields

A. The subclass must also be declared abstract

A. abstract

A. Displaying Drawing Circle

A. Yes, an abstract class can implement an interface

A. Car started Vehicle stopped

B. No, an abstract class cannot be final

A. Meow Sleeping

A. To provide a base for other classes to extend and enforce a contract

A. Yes, an abstract class can be extended by another abstract class

B. Base display

A. An abstract class can have both abstract and concrete methods

A. Bike driving Honking

A. Yes, an abstract class can be used as a type for variables

A. Tuning Playing Piano

A. An abstract class can be instantiated directly

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

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

Inheritance MCQs

 Inheritance MCQs

What is inheritance in Java?


A. A way to provide default values to variables

B. A mechanism to create new classes using existing classes

C. A method to define multiple classes in a single file

D. A way to handle exceptions

Which keyword is used to inherit a class in Java?


A. extends

B. implements

C. inherits

D. super

What is the main purpose of inheritance?


A. To reuse code and establish a hierarchical relationship

B. To handle runtime errors

C. To create multiple instances of a class

D. To make a class final

Which of the following statements about inheritance is true?


A. A subclass can inherit both methods and variables from a superclass

B. A superclass can inherit methods and variables from a subclass

C. Inheritance allows a subclass to access private members of the superclass

D. A class can inherit multiple classes in Java

Which type of inheritance is not supported in Java?


A. Single inheritance

B. Multiple inheritance (through classes)

C. Hierarchical inheritance

D. Multilevel inheritance

What will be the output of the following code?


java

Copy code

class Parent {

    void show() {

        System.out.println("Parent");

    }

}


class Child extends Parent {

    void show() {

        System.out.println("Child");

    }

}


public class Main {

    public static void main(String[] args) {

        Parent p = new Child();

        p.show();

    }

}

A. Parent

B. Child

C. Compilation error

D. Runtime exception

What is the purpose of the super keyword in Java?


A. To call a method in the superclass

B. To create a new instance of the superclass

C. To access private members of the superclass

D. To define a subclass

Which of the following cannot be inherited?


A. Instance variables

B. Methods

C. Static variables

D. Constructors

What happens if you do not explicitly define a constructor in a subclass?


A. The subclass will automatically have a default constructor

B. The subclass will not compile

C. The superclass constructor is not called

D. The subclass will use the constructor of the superclass

Which of the following is true about method overriding in Java?


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 can have a different return type

C. The method in the subclass must be static

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

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

    }

}


public class Test {

    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 the role of the final keyword in inheritance?


A. It prevents a class from being subclassed

B. It allows a class to be subclassed

C. It prevents methods from being overridden

D. It allows methods to be overridden

Which of the following is true about Java's multiple inheritance support?


A. Java supports multiple inheritance through interfaces

B. Java supports multiple inheritance through classes

C. Java does not support multiple inheritance at all

D. Java supports multiple inheritance with the extends keyword

What will be the output of the following code?


java

Copy code

class Base {

    void display() {

        System.out.println("Base");

    }

}


class Derived extends Base {

    void display() {

        System.out.println("Derived");

    }

    

    void show() {

        super.display();

    }

}


public class Main {

    public static void main(String[] args) {

        Derived d = new Derived();

        d.show();

    }

}

A. Base

B. Derived

C. Base Derived

D. Derived Base

How can you call the superclass constructor from a subclass?


A. Using this()

B. Using super()

C. Using parent()

D. Using super keyword directly

Which keyword is used to refer to the superclass instance members?


A. this

B. super

C. parent

D. instance

What will be the output of the following code?


java

Copy code

class Parent {

    int value = 10;

}


class Child extends Parent {

    int value = 20;

    

    void display() {

        System.out.println(super.value);

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.display();

    }

}

A. 10

B. 20

C. Compilation error

D. Runtime exception

Which of the following can a subclass access from its superclass?


A. Private members

B. Protected members

C. Default (package-private) members in a different package

D. Static methods only

What is the output of the following code?


java

Copy code

class Parent {

    void method() {

        System.out.println("Parent");

    }

}


class Child extends Parent {

    void method() {

        System.out.println("Child");

    }

    

    void callParentMethod() {

        super.method();

    }

}


public class Main {

    public static void main(String[] args) {

        Child c = new Child();

        c.callParentMethod();

    }

}

A. Parent

B. Child

C. Parent Child

D. Child Parent

In which scenario is method overriding NOT possible?


A. When the method in the subclass has a different return type

B. When the method in the superclass is private

C. When the method in the superclass is static

D. When the method in the superclass is final

Answers:

B. A mechanism to create new classes using existing classes

A. extends

A. To reuse code and establish a hierarchical relationship

A. A subclass can inherit both methods and variables from a superclass

B. Multiple inheritance (through classes)

B. Child

A. To call a method in the superclass

D. Constructors

A. The subclass will automatically have a default constructor

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

B. Woof

A. It prevents a class from being subclassed

A. Java supports multiple inheritance through interfaces

A. Base

B. Using super()

B. super

A. 10

B. Protected members

A. Parent

B. When the method in the superclass is private

this Keyword MCQs

 this Keyword MCQs

What does the this keyword refer to in Java?


A. The class itself

B. The current instance of the class

C. The parent class

D. The static method in the class

Which of the following is a valid use of the this keyword?


A. To call a static method

B. To refer to instance variables and methods

C. To access private members of another class

D. To refer to the superclass constructor

How can the this keyword be used in a constructor?


A. To refer to another constructor in the same class (constructor chaining)

B. To call a method from a static context

C. To access variables in a static method

D. To refer to a superclass constructor

What happens if you use this in a static method?


A. It refers to the current instance of the class

B. It cannot be used in static methods

C. It refers to the static variables of the class

D. It refers to the parent class instance

Consider the following code snippet:


java

Copy code

class Example {

    int x;

    Example(int x) {

        this.x = x;

    }

    

    void printValue() {

        System.out.println(this.x);

    }

}

What does this.x refer to in the constructor?


A. The parameter x

B. The instance variable x

C. A local variable x

D. A static variable x

Which statement about the this keyword is true?


A. this can be used to call private methods in the same class

B. this can refer to a method in another class

C. this can be used to call static methods

D. this can be used to access variables from a superclass

How can this be used to resolve name conflicts?


A. By referring to static variables

B. By referring to instance variables when their names conflict with parameter names

C. By calling superclass methods

D. By accessing local variables

In which of the following scenarios is the use of this keyword mandatory?


A. To call a superclass constructor

B. To access instance variables when there are local variables with the same name

C. To access static variables

D. To call static methods

What is the output of the following code?


java

Copy code

class MyClass {

    int x = 10;

    

    void setValue(int x) {

        this.x = x;

    }

    

    void display() {

        System.out.println(x);

    }

}


public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass();

        obj.setValue(20);

        obj.display();

    }

}

A. 10

B. 20

C. 0

D. Compilation error

Can this be used to access methods of the superclass?


A. Yes, if the methods are not overridden

B. No, this cannot be used to access superclass methods

C. Yes, this can be used in a subclass

D. No, this is only for current instance methods

What will happen if this is used inside an instance method to refer to a static variable?


java

Copy code

class Test {

    static int count = 5;

    

    void display() {

        System.out.println(this.count);

    }

}

A. It will print the static variable value correctly

B. It will result in a compilation error

C. It will print null

D. It will throw a runtime exception

Which of the following statements correctly demonstrates constructor chaining using this?


java

Copy code

class Demo {

    Demo() {

        this(10); // Calls parameterized constructor

    }

    

    Demo(int x) {

        System.out.println(x);

    }

}

A. Yes, this demonstrates constructor chaining

B. No, constructor chaining cannot be done with this

C. No, this cannot be used in a constructor

D. Yes, but it will result in a compilation error

Can this be used to refer to the current instance in a static context?


A. Yes, if the instance is created inside the static method

B. No, this cannot be used in a static context

C. Yes, but only if this is explicitly passed as a parameter

D. No, because this is only for instance methods

What is the purpose of using this keyword in method chaining?


java

Copy code

class Chain {

    Chain method1() {

        System.out.println("Method1");

        return this;

    }

    

    Chain method2() {

        System.out.println("Method2");

        return this;

    }

}

A. To return the current instance for chaining method calls

B. To access static methods

C. To access methods of other classes

D. To call superclass methods

What does the following code print?


java

Copy code

class Person {

    String name;

    

    Person(String name) {

        this.name = name;

    }

    

    void showName() {

        System.out.println(this.name);

    }

}


public class Main {

    public static void main(String[] args) {

        Person p = new Person("John");

        p.showName();

    }

}

A. John

B. name

C. null

D. Compilation error

What is the result of using this in an inner class?


java

Copy code

class Outer {

    int x = 10;

    

    class Inner {

        void display() {

            System.out.println(Outer.this.x);

        }

    }

}

A. It will print 10

B. It will print 0

C. It will result in a compilation error

D. It will print null

How does this differ from super keyword?


A. this is used to refer to the current instance, super refers to the superclass

B. this is used to call static methods, super is for instance methods

C. this refers to static variables, super to instance variables

D. this is used to access superclass methods, super is for instance variables

What is the result of the following code snippet?


java

Copy code

class Test {

    void display() {

        System.out.println(this);

    }

}


public class Main {

    public static void main(String[] args) {

        Test t = new Test();

        t.display();

    }

}

A. Prints the memory address of the Test object

B. Prints null

C. Prints Test

D. Prints the class name only

Which statement is true about using this in a constructor?


A. this can be used to call another constructor in the same class

B. this can call constructors in other classes

C. this must be the first statement in a constructor

D. this cannot be used in constructors

Can this be used to access private members of a class?


A. Yes, this can access private members within the same class

B. No, this cannot access private members

C. Yes, but only if this is used in a different class

D. No, this can only access public members

Answers:

B. The current instance of the class

B. To refer to instance variables and methods

A. To refer to another constructor in the same class (constructor chaining)

B. It cannot be used in static methods

B. The instance variable x

A. this can be used to call private methods in the same class

B. By referring to instance variables when their names conflict with parameter names

B. By referring to instance variables when there are local variables with the same name

B. 20

C. Yes, this can be used in a subclass

A. It will print the static variable value correctly

A. Yes, this demonstrates constructor chaining

B. No, this cannot be used in a static context

A. To return the current instance for chaining method calls

A. John

A. It will print 10

A. this is used to refer to the current instance, super refers to the superclass

A. Prints the memory address of the Test object

A. this can be used to call another constructor in the same class

A. Yes, this can access private members within the same class

Constructors MCQs

 Constructors MCQs

What is a constructor in Java?


A. A special method used to initialize objects

B. A method that can be called explicitly to perform an action

C. A method that can have a return type

D. A method that can be inherited by subclasses

Which of the following statements is true about constructors?


A. Constructors can have any return type, including void

B. Constructors must have the same name as the class

C. Constructors cannot be overloaded

D. Constructors cannot be parameterized

What is the default constructor in Java?


A. A constructor that takes parameters

B. A constructor that is provided by the JVM if no constructor is defined

C. A constructor that must be explicitly defined

D. A constructor that initializes all instance variables to zero

Can a constructor be private in Java?


A. No, constructors cannot have access modifiers

B. Yes, but only in abstract classes

C. Yes, private constructors are used in singleton design patterns

D. No, constructors must be public or protected

How is a constructor invoked in Java?


A. By using the new keyword followed by the constructor name

B. By calling the constructor directly with the class name

C. By creating an instance of the class

D. By using a special syntax init()

Which of the following is true about constructor overloading in Java?


A. Constructors cannot be overloaded

B. Constructor overloading is not allowed with different parameter lists

C. Constructor overloading allows multiple constructors with different parameter lists

D. Constructor overloading requires all constructors to have the same parameters

What happens if you define a parameterized constructor but do not define a default constructor?


A. The default constructor is automatically provided by the JVM

B. The class will not compile unless a default constructor is also defined

C. Objects of the class cannot be created without passing parameters

D. The default constructor is implicitly created and takes no arguments

What is a copy constructor in Java?


A. A constructor that copies the properties of one object to another

B. A constructor that initializes objects with default values

C. A constructor that can be called with a single parameter

D. A constructor that is automatically provided by the JVM

Can a constructor call another constructor in the same class?


A. No, a constructor cannot call another constructor

B. Yes, using the this keyword

C. Yes, using the super keyword

D. Yes, but only if the constructors have different access modifiers

What is the purpose of the super() call in a constructor?


A. To call the constructor of the superclass

B. To call another constructor in the same class

C. To create a new instance of the class

D. To initialize static variables

What is the output of the following code snippet?


java

Copy code

class A {

    A() {

        System.out.println("A's constructor");

    }

}


class B extends A {

    B() {

        System.out.println("B's constructor");

    }

}


public class Test {

    public static void main(String[] args) {

        new B();

    }

}

A. A's constructor

B. B's constructor

C. A's constructor\nB's constructor

D. B's constructor\nA's constructor

Can a constructor return a value?


A. Yes, it can return any data type

B. No, constructors do not have a return type

C. Yes, but only primitive types

D. Yes, but only if the return type is void

What is the difference between a constructor and a method in Java?


A. Constructors can have any return type, methods cannot

B. Constructors are called implicitly, methods are called explicitly

C. Methods can have the same name as the class, constructors cannot

D. Methods can only initialize instance variables, constructors cannot

Which of the following is true about the default constructor provided by the JVM?


A. It initializes all instance variables to their default values

B. It must be explicitly defined in the class

C. It takes parameters

D. It cannot be overridden

Which keyword is used to call the superclass constructor in a subclass?


A. this

B. super

C. base

D. parent

What is constructor chaining in Java?


A. Calling multiple constructors one after another using the super keyword

B. The process of calling one constructor from another constructor

C. The act of overriding a constructor in the subclass

D. The process of having multiple constructors in a class

In what order are constructors called when an object is created?


A. Child class constructor first, then parent class constructor

B. Parent class constructor first, then child class constructor

C. They are called simultaneously

D. Constructors are called in the order they are defined in the class

Can a constructor be synchronized in Java?


A. Yes, constructors can be synchronized

B. No, constructors cannot be synchronized

C. Yes, but only static constructors

D. Yes, but only if they are private

Is it mandatory to define a constructor in a Java class?


A. Yes, at least one constructor must be defined

B. No, a default constructor is provided if none is defined

C. Yes, but only if the class has instance variables

D. No, constructors are optional and can be omitted

What will happen if you try to create an object using a constructor with private access?


A. The object will be created successfully

B. The object creation will fail at runtime

C. The code will not compile

D. The object can be created only within the same class

Answers:

A. A special method used to initialize objects

B. Constructors must have the same name as the class

B. A constructor that is provided by the JVM if no constructor is defined

C. Yes, private constructors are used in singleton design patterns

C. By creating an instance of the class

C. Constructor overloading allows multiple constructors with different parameter lists

C. Objects of the class cannot be created without passing parameters

A. A constructor that copies the properties of one object to another

B. Yes, using the this keyword

A. To call the constructor of the superclass

C. A's constructor\nB's constructor

B. No, constructors do not have a return type

B. Constructors are called implicitly, methods are called explicitly

A. It initializes all instance variables to their default values

B. super

B. The process of calling one constructor from another constructor

B. Parent class constructor first, then child class constructor

B. No, constructors cannot be synchronized

B. No, a default constructor is provided if none is defined

D. The object can be created only within the same class


Default Constructor MCQs

What is a default constructor in Java?


A. A constructor provided by the programmer with no parameters

B. A constructor provided by the compiler when no other constructors are defined

C. A constructor that must initialize all instance variables

D. A constructor that must take at least one parameter

When is a default constructor provided by the Java compiler?


A. Always, regardless of whether other constructors are defined

B. Only when no constructors are explicitly defined by the programmer

C. Only when a class has no instance variables

D. When a class is abstract

What does the default constructor do?


A. It sets all instance variables to zero

B. It initializes all instance variables to default values

C. It initializes all instance variables to null

D. It does nothing unless explicitly overridden

Can a default constructor be overloaded in Java?


A. Yes, by defining a constructor with the same name but different parameters

B. No, a default constructor cannot be overloaded

C. Yes, by defining a constructor with a different return type

D. No, because default constructors are always private

Which of the following statements is true about default constructors?


A. They can have parameters

B. They are automatically private

C. They are only provided if no other constructors are defined

D. They must be explicitly defined

What is the access modifier of a default constructor if none is specified?


A. public

B. protected

C. private

D. package-private (default access)

Can a class have a default constructor if a parameterized constructor is defined?


A. Yes, it will still have a default constructor

B. No, a default constructor is not provided if any constructors are defined

C. Yes, but it must be explicitly defined

D. No, unless the class is abstract

What is the primary purpose of the default constructor?


A. To provide a method for creating objects with specific parameters

B. To initialize objects when no specific initialization is required

C. To override methods from the superclass

D. To create abstract classes

In which scenario will the Java compiler not provide a default constructor?


A. When no constructors are defined in the class

B. When a class contains static methods only

C. When at least one parameterized constructor is defined in the class

D. When a class inherits from another class

If a class has both a default constructor and a parameterized constructor, which one will be called when an object is created without passing any arguments?


A. The parameterized constructor

B. The default constructor

C. A compilation error will occur

D. The constructor with the most parameters

What happens if you define a constructor with parameters but forget to define a default constructor?


A. The class will still have a default constructor

B. The class will not compile

C. You cannot create an instance of the class without passing arguments

D. The JVM will automatically create a default constructor

Can a default constructor throw an exception?


A. No, default constructors cannot throw exceptions

B. Yes, but only checked exceptions

C. Yes, default constructors can throw both checked and unchecked exceptions

D. Yes, but only if explicitly defined by the programmer

What is the return type of a default constructor in Java?


A. void

B. The class type itself

C. No return type

D. Object

How can you explicitly define a default constructor?


A. By creating a constructor with no parameters

B. By creating a constructor with default parameters

C. By creating a static method called defaultConstructor

D. By using the this keyword

What is the output of the following code?


java

Copy code

class MyClass {

    MyClass() {

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

    }

}


public class Main {

    public static void main(String[] args) {

        MyClass obj = new MyClass();

    }

}

A. Default Constructor

B. Compilation error

C. No output

D. NullPointerException

Can a default constructor be called explicitly in Java?


A. No, default constructors cannot be called explicitly

B. Yes, by using the new keyword followed by the class name

C. Yes, by using the super keyword

D. Yes, by using the this keyword

What happens if you don't provide any constructor in a class?


A. The class cannot be instantiated

B. The compiler provides a default constructor

C. The JVM throws a runtime exception

D. The class will automatically become abstract

Can a default constructor have a body?


A. No, default constructors cannot have a body

B. Yes, it can have a body defined by the programmer

C. Yes, but only if it overrides a method

D. No, it is automatically defined by the JVM

Is it possible to access instance variables from within a default constructor?


A. No, instance variables cannot be accessed from a constructor

B. Yes, but only if they are static

C. Yes, all instance variables can be accessed and initialized

D. No, default constructors are only for object creation

What will happen if a default constructor is explicitly defined with a private access modifier?


A. The class cannot be instantiated outside its own methods

B. The class can still be instantiated normally

C. The default constructor will be called twice

D. A compilation error will occur

Answers:

B. A constructor provided by the compiler when no other constructors are defined

B. Only when no constructors are explicitly defined by the programmer

B. It initializes all instance variables to default values

A. Yes, by defining a constructor with the same name but different parameters

C. They are only provided if no other constructors are defined

D. package-private (default access)

C. Yes, but it must be explicitly defined

B. To initialize objects when no specific initialization is required

C. When at least one parameterized constructor is defined in the class

B. The default constructor

C. You cannot create an instance of the class without passing arguments

C. Yes, default constructors can throw both checked and unchecked exceptions

C. No return type

A. By creating a constructor with no parameters

A. Default Constructor

B. Yes, by using the new keyword followed by the class name

B. The compiler provides a default constructor

B. Yes, it can have a body defined by the programmer

C. Yes, all instance variables can be accessed and initialized

A. The class cannot be instantiated outside its own methods


Types of Constructors MCQs

What is a default constructor in Java?


A. A constructor that initializes objects with custom values

B. A constructor that is automatically provided by the compiler if no constructors are defined

C. A constructor that takes arguments

D. A constructor that must be defined explicitly by the programmer

Which of the following is true about a parameterized constructor?


A. It is a constructor that takes no arguments

B. It allows different ways to initialize objects

C. It cannot be overloaded

D. It is provided automatically by the compiler

What is the primary purpose of a copy constructor in Java?


A. To initialize an object with default values

B. To create a new object as a copy of an existing object

C. To define a constructor without parameters

D. To provide multiple ways to create an object

How can a copy constructor be defined in Java?


A. By using the clone() method

B. By creating a constructor that takes an object of the same class as a parameter

C. By creating a constructor with no parameters

D. By using the this keyword within the constructor

What is the difference between a default constructor and a parameterized constructor?


A. A default constructor has parameters, a parameterized constructor does not

B. A parameterized constructor has a return type, a default constructor does not

C. A default constructor is provided by the JVM, a parameterized constructor is defined by the programmer

D. There is no difference; they are the same

Which of the following statements is true about default constructors in Java?


A. Default constructors are always public

B. Default constructors can have parameters

C. Default constructors are provided only if no other constructors are defined

D. Default constructors are called only when an object is created using the new keyword

What is a parameterized constructor used for?


A. To initialize objects with default values

B. To provide different ways to initialize an object with specific values

C. To define a constructor without parameters

D. To create multiple objects of the same class

Can a parameterized constructor call another constructor in the same class?


A. No, constructors cannot call each other

B. Yes, using the super keyword

C. Yes, using the this keyword

D. Yes, using the new keyword

What happens if a class does not define a constructor explicitly?


A. The class cannot be instantiated

B. A default constructor is provided by the JVM

C. The class will not compile

D. The class must be declared abstract

Which of the following is an example of a copy constructor?


java

Copy code

class Example {

    int x;

    Example(Example e) {

        this.x = e.x;

    }

}

A. Yes, this is a copy constructor

B. No, this is a parameterized constructor

C. No, this is a default constructor

D. No, this is an overloaded constructor

What is the result of the following code?


java

Copy code

class Test {

    Test() {

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

    }

    Test(int x) {

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

    }

}


public class Main {

    public static void main(String[] args) {

        Test t1 = new Test();

        Test t2 = new Test(10);

    }

}

A. Default Constructor

B. Parameterized Constructor

C. Default Constructor\nParameterized Constructor

D. Parameterized Constructor\nDefault Constructor

Is it possible to overload a copy constructor in Java?


A. No, copy constructors cannot be overloaded

B. Yes, but only with a different return type

C. Yes, with different parameter types

D. No, constructors cannot be overloaded

Which of the following correctly distinguishes a copy constructor from a parameterized constructor?


A. A copy constructor initializes an object with another object of the same type, a parameterized constructor can have any type of parameters

B. A parameterized constructor initializes objects with default values, a copy constructor does not

C. A copy constructor must have the same parameters as the default constructor

D. A copy constructor cannot be defined by the programmer

What will happen if a class has both a default constructor and a parameterized constructor?


A. The parameterized constructor overrides the default constructor

B. Both constructors can be used depending on how the object is created

C. Only the default constructor will be called

D. Only the parameterized constructor will be called

Can a constructor be declared as final in Java?


A. Yes, to prevent the constructor from being overridden

B. No, constructors cannot be declared as final

C. Yes, to prevent the class from being instantiated

D. No, constructors must always be static

Which constructor is called when an object is created without passing any arguments?


A. Parameterized constructor

B. Copy constructor

C. Default constructor

D. Static constructor

What is the purpose of a constructor in an abstract class?


A. To initialize abstract methods

B. To provide a way to initialize instance variables in subclasses

C. To prevent the class from being instantiated

D. To provide multiple ways to initialize an abstract class

Which of the following is NOT a type of constructor in Java?


A. Default constructor

B. Parameterized constructor

C. Copy constructor

D. Static constructor

What is the behavior of the default constructor provided by the JVM?


A. It sets all instance variables to default values

B. It calls the parameterized constructor with default values

C. It throws an exception if no parameters are provided

D. It initializes static variables only

Which of the following is true about constructors in an interface?


A. Interfaces cannot have constructors

B. Interfaces can have default constructors

C. Interfaces can have parameterized constructors

D. Interfaces can have only private constructors

Answers:

B. A constructor that is automatically provided by the compiler if no constructors are defined

B. It allows different ways to initialize objects

B. To create a new object as a copy of an existing object

B. By creating a constructor that takes an object of the same class as a parameter

C. A default constructor is provided by the JVM, a parameterized constructor is defined by the programmer

C. Default constructors are provided only if no other constructors are defined

B. To provide different ways to initialize an object with specific values

C. Yes, using the this keyword

B. A default constructor is provided by the JVM

A. Yes, this is a copy constructor

C. Default Constructor\nParameterized Constructor

C. Yes, with different parameter types

A. A copy constructor initializes an object with another object of the same type, a parameterized constructor can have any type of parameters

B. Both constructors can be used depending on how the object is created

B. No, constructors cannot be declared as final

C. Default constructor

B. To provide a way to initialize instance variables in subclasses

D. Static constructor

A. It sets all instance variables to default values

A. Interfaces cannot have constructors


Parameterized Constructor MCQs

What is a parameterized constructor in Java?


A. A constructor that initializes an object with default values

B. A constructor that takes arguments to initialize object attributes

C. A constructor with no parameters

D. A constructor that initializes static variables

Which of the following is true about parameterized constructors?


A. They are used to provide multiple ways to create an object

B. They cannot be overloaded

C. They always have a return type

D. They cannot be used to initialize instance variables

How is a parameterized constructor different from a default constructor?


A. A parameterized constructor takes arguments, while a default constructor does not

B. A default constructor can be overloaded, while a parameterized constructor cannot

C. A parameterized constructor initializes static variables, while a default constructor does not

D. A parameterized constructor is automatically provided by the JVM, while a default constructor is not

What happens if you define a parameterized constructor in a class without defining a default constructor?


A. The class will automatically have a default constructor

B. You cannot create an object of the class without passing arguments

C. The parameterized constructor will be overridden

D. The class will not compile

Which of the following statements is correct about parameterized constructors?


A. They must initialize all instance variables

B. They can be used to initialize object attributes with specific values

C. They cannot be called from another constructor

D. They are only used for object creation without initialization

How can you call a parameterized constructor from another constructor in the same class?


A. By using the super keyword

B. By using the this keyword

C. By using the new keyword

D. By using the parent keyword

What will be the output of the following code?


java

Copy code

class Test {

    int a;

    Test(int x) {

        a = x;

    }

    

    public static void main(String[] args) {

        Test t = new Test(5);

        System.out.println(t.a);

    }

}

A. 0

B. 5

C. Compilation error

D. NullPointerException

Which keyword is used to call a parameterized constructor from another constructor within the same class?


A. super

B. this

C. new

D. parent

What happens if you attempt to use a parameterized constructor without passing the required arguments?


A. The class will compile successfully, but default values will be used

B. A runtime exception will occur

C. The code will not compile

D. The parameterized constructor will be replaced by a default constructor

Can a parameterized constructor call a default constructor?


A. No, a parameterized constructor cannot call a default constructor

B. Yes, but only if the default constructor is explicitly defined

C. Yes, using the this keyword

D. Yes, using the super keyword

What is the purpose of using a parameterized constructor in a class?


A. To define methods for initializing objects with specific values

B. To provide different ways to initialize an object based on parameters

C. To enforce a fixed initialization process

D. To provide static methods for object initialization

What will be the output of the following code if you define a parameterized constructor but do not define a default constructor?


java

Copy code

class Example {

    Example(int x) {

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

    }

    

    public static void main(String[] args) {

        Example e = new Example();

    }

}

A. Parameterized Constructor

B. Compilation error

C. NullPointerException

D. No output

Can you overload parameterized constructors?


A. No, overloading constructors is not allowed

B. Yes, by changing the number or type of parameters

C. Yes, by changing the return type

D. No, a class can have only one parameterized constructor

Which of the following is NOT true about parameterized constructors?


A. They can have multiple parameters

B. They can be used to initialize objects with specific values

C. They can only initialize static variables

D. They can be used to set initial values of instance variables

What is the effect of passing different arguments to multiple parameterized constructors?


java

Copy code

class Person {

    String name;

    int age;

    

    Person(String name) {

        this.name = name;

    }

    

    Person(String name, int age) {

        this.name = name;

        this.age = age;

    }

}


public class Test {

    public static void main(String[] args) {

        Person p1 = new Person("Alice");

        Person p2 = new Person("Bob", 30);

    }

}

A. Only the constructor with two parameters will be called

B. The constructors will be called based on the number of arguments

C. Both constructors will result in a compilation error

D. Only the constructor with one parameter will be called

Which of the following is a valid way to define a parameterized constructor?


java

Copy code

class MyClass {

    int x;

    MyClass(int x) {

        this.x = x;

    }

}

A. Yes, this is a valid parameterized constructor

B. No, constructors cannot have parameters

C. No, constructors must be declared static

D. No, constructors must have a return type

How does a parameterized constructor help in object creation?


A. It provides a way to set default values for all instance variables

B. It helps in creating objects with specific initial values based on the parameters passed

C. It is only used to call other methods in the class

D. It is used to initialize static variables

Can a class have multiple parameterized constructors?


A. Yes, but only if they have different parameter lists

B. No, a class can have only one parameterized constructor

C. Yes, but they must have the same parameter list

D. No, parameterized constructors are not allowed in a class

What will happen if a parameterized constructor is defined but not used to create an object?


A. The parameterized constructor will be ignored

B. A default constructor will automatically be used

C. The code will not compile

D. The object will be created with default values

In which scenario will you typically use a parameterized constructor?


A. To provide default values for instance variables

B. To initialize objects with specific values provided by the user

C. To create static methods in the class

D. To override methods from the superclass

Answers:

B. A constructor that takes arguments to initialize object attributes

A. They are used to provide multiple ways to create an object

A. A parameterized constructor takes arguments, while a default constructor does not

B. You cannot create an object of the class without passing arguments

B. They can be used to initialize object attributes with specific values

B. By using the this keyword

B. 5

B. this

C. The code will not compile

C. Yes, using the this keyword

B. To provide different ways to initialize an object based on parameters

B. Compilation error

B. Yes, by changing the number or type of parameters

C. They can only initialize static variables

B. The constructors will be called based on the number of arguments

A. Yes, this is a valid parameterized constructor

B. It helps in creating objects with specific initial values based on the parameters passed

A. Yes, but only if they have different parameter lists

A. The parameterized constructor will be ignored

B. To initialize objects with specific values provided by the user

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