JAVA-SE-Tutorial-codeswithpankaj icon indicating copy to clipboard operation
JAVA-SE-Tutorial-codeswithpankaj copied to clipboard

Core Java MCQ

Open Pankaj-Str opened this issue 5 months ago • 0 comments

1. Which of the following best demonstrates the concept of Encapsulation in Java?

A) Using inheritance to share common behavior between classes
B) Hiding the data members of a class by declaring them private and providing public getter and setter methods
C) Overloading a method within the same class
D) Implementing an interface in a class


2. You have a superclass Animal and a subclass Dog. Which of the following statements about Inheritance is true?

A) Dog inherits only the private members of Animal
B) Dog inherits the public and protected members of Animal
C) Dog cannot override methods in Animal
D) Dog inherits the constructors of Animal


3. What concept is demonstrated by the following code snippet?

class Shape {
    public void draw() {
        System.out.println("Drawing a shape");
    }
}
class Circle extends Shape {
    @Override
    public void draw() {
        System.out.println("Drawing a circle");
    }
}

A) Abstraction
B) Encapsulation
C) Polymorphism
D) Inheritance


4. Which of the following is NOT a feature of Abstraction?

A) Hiding implementation details
B) Exposing only the essential functionalities
C) Using interfaces and abstract classes
D) Reusing code between classes


5. What will be the output of the following code, demonstrating Constructor Chaining?

class Parent {
    Parent() {
        System.out.println("Parent Constructor");
    }
}

class Child extends Parent {
    Child() {
        System.out.println("Child Constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        new Child();
    }
}

A) Parent Constructor
B) Child Constructor
C) Parent Constructor
Child Constructor
D) Child Constructor
Parent Constructor


6 What will be the output of the following Java program?

class Base {
    public void display() {
        System.out.println("Base display");
    }
}

class Derived extends Base {
    public void display() {
        System.out.println("Derived display");
    }
}

public class Test {
    public static void main(String[] args) {
        Base obj = new Derived();
        obj.display();
    }
}

A) Base display
B) Derived display
C) Compilation error
D) Runtime error


7 What will be the output of the following Java code?

class Test {
    public static void main(String[] args) {
        int x = 5;
        int y = 10;
        x = x + y - (y = x);
        System.out.println(x + " " + y);
    }
}

A) 15 10
B) 15 5
C) 10 5
D) 5 10


8 What will be the output of the following Java program?

class A {
    public A() {
        System.out.println("Class A Constructor");
    }
}

class B extends A {
    public B() {
        System.out.println("Class B Constructor");
    }
}

public class Test {
    public static void main(String[] args) {
        B obj = new B();
    }
}

A) Class A Constructor
B) Class B Constructor
C) Class A Constructor
Class B Constructor
D) Compilation error


9 What will be the output of the following Java program that uses a combination of final, static, and method overriding?

class A {
    public static void display() {
        System.out.println("Static method in Class A");
    }
    public void show() {
        System.out.println("Non-static method in Class A");
    }
}

class B extends A {
    public static void display() {
        System.out.println("Static method in Class B");
    }
    @Override
    public void show() {
        System.out.println("Non-static method in Class B");
    }
}

public class Test {
    public static void main(String[] args) {
        A obj = new B();
        obj.display();
        obj.show();
    }
}

A) Static method in Class A
Non-static method in Class A
B) Static method in Class B
Non-static method in Class B
C) Static method in Class A
Non-static method in Class B
D) Compilation error


10 What will be the output of the following program that demonstrates inner classes and their interaction with variables?

class Outer {
    private int data = 10;
    
    class Inner {
        int data = 20;
        
        void display() {
            int data = 30;
            System.out.println(data);
            System.out.println(this.data);
            System.out.println(Outer.this.data);
        }
    }
    
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();
        inner.display();
    }
}

A) 30
20
10
B) 30
10
20
C) 10
20
30
D) Compilation error


Pankaj-Str avatar Sep 21 '24 12:09 Pankaj-Str