Java OOPs Concepts Test 2

    Question 1JAVA OOPS - Default Constructor

    What does Java provide if no constructor is defined in a class?

    class Person {}

    Question 2JAVA OOPS - Constructor Overloading

    What will be printed?

    class Demo { Demo() { System.out.println("Default"); } Demo(int x) { System.out.println("Param"); } } public class Main { public static void main(String[] args) { Demo d1 = new Demo(); Demo d2 = new Demo(10); } }

    Question 3JAVA OOPS - Hierarchical Inheritance

    Which is true about hierarchical inheritance?

    Question 4JAVA OOPS - Runtime Polymorphism

    What will be printed?

    class A { void show() { System.out.println("A"); } } class B extends A { void show() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.show(); } }

    Question 5JAVA OOPS - Abstract Class Constructor

    Which statement is true about constructors in abstract classes?

    Question 6JAVA OOPS - Access Modifier Importance

    Why are private access modifiers important in encapsulation?

    Question 7JAVA OOPS - Multiple Interface Implementation

    Which of the following is correct?

    interface A { void showA(); } interface B { void showB(); } class C implements A, B { public void showA() { System.out.println("A"); } public void showB() { System.out.println("B"); } }

    Question 8JAVA OOPS - Creating a Package

    Which of the following correctly creates a package?

    package com.example; public class Test {}

    Question 9JAVA OOPS - Final Method

    Which of the following statements about final methods is correct?

    Question 10JAVA OOPS - Overriding Basics

    Which statement about method overriding is true?

    Question 11JAVA OOPS - Object Reference Assignment

    What will be printed?

    class Demo { int a = 5; } public class Main { public static void main(String[] args) { Demo d1 = new Demo(); Demo d2 = d1; d2.a = 10; System.out.println(d1.a); } }

    Question 12JAVA OOPS - Constructor Without `void`

    Which is true about constructors?

    Question 13JAVA OOPS - Method Overriding in Inheritance

    What will be printed?

    class A { void show() { System.out.println("Parent"); } } class B extends A { void show() { System.out.println("Child"); } } public class Main { public static void main(String[] args) { A a = new B(); a.show(); } }

    Question 14JAVA OOPS - Polymorphism with Parameters

    Which of the following is true about polymorphism in Java?

    Question 15JAVA OOPS - Multiple Abstract Classes

    Which statement is correct?

    Question 16JAVA OOPS - Read-only Property

    How can you make a class property read-only?

    class Demo { private int value; public int getValue() { return value; } }

    Question 17JAVA OOPS - Default Methods in Interface

    Which is true about default methods in Java interfaces?

    Question 18JAVA OOPS - Importing a Package

    How do you import the `Test` class from `com.example` package?

    Question 19JAVA OOPS - Final Method Example

    What will happen when compiling the following code?

    class A { final void show() { System.out.println("A"); } } class B extends A { // void show() { System.out.println("B"); } }

    Question 20JAVA OOPS - Overriding Example

    What will be printed?

    class A { void show() { System.out.println("A"); } } class B extends A { void show() { System.out.println("B"); } } public class Main { public static void main(String[] args) { A obj = new B(); obj.show(); } }