Java OOPs Concepts Test 4

    Question 1JAVA OOPS - Multiple Objects

    What does the following code demonstrate?

    class Counter { int count = 0; } public class Main { public static void main(String[] args) { Counter c1 = new Counter(); Counter c2 = new Counter(); c1.count++; System.out.println(c2.count); } }

    Question 2JAVA OOPS - Object Initialization

    Which is true about objects in Java?

    Question 3JAVA OOPS - Constructor Execution Order

    What will be printed?

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

    Question 4JAVA OOPS - Static Method and Polymorphism

    Which statement is correct?

    Question 5JAVA OOPS - Abstract Method Requirement

    Which of the following is true?

    Question 6JAVA OOPS - Changing Internal Representation

    Which is true?

    Question 7JAVA OOPS - Interface and Abstract Class

    Which is true?

    Question 8JAVA OOPS - Package with Multiple Classes

    What will be the output?

    package com.example; public class A { public void show() { System.out.println("A"); } } class B { void display() { System.out.println("B"); } }

    Question 9JAVA OOPS - Final with Parameters

    What is true about final parameters in Java?

    Question 10JAVA OOPS - Final Method Overriding

    Which statement is correct?

    Question 11JAVA OOPS - Object Null Reference

    What happens if we access a method via a null object?

    class Demo { void show() { System.out.println("Hello"); } } public class Main { public static void main(String[] args) { Demo d = null; d.show(); } }

    Question 12JAVA OOPS - Private Constructor

    What is true about a private constructor?

    class Demo { private Demo() {} }

    Question 13JAVA OOPS - Inheritance and Private Members

    Which of the following statements is true?

    Question 14JAVA OOPS - Constructor and Polymorphism

    Which statement is true?

    Question 15JAVA OOPS - Interface Implementation

    What will happen with the following code?

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

    Question 16JAVA OOPS - Encapsulation with Validation

    Which is correct for validating input using encapsulation?

    class Account { private int balance; public void setBalance(int b) { if(b >= 0) balance = b; } public int getBalance() { return balance; } }

    Question 17JAVA OOPS - Static Methods in Interface

    Which is valid in Java 8+?

    interface Demo { static void show() { System.out.println("Hello"); } } public class Main { public static void main(String[] args) { Demo.show(); } }

    Question 18JAVA OOPS - Java API Packages

    Which of the following is a standard Java API package?

    Question 19JAVA OOPS - Blank Final Variable

    Which statement is correct?

    Question 20JAVA OOPS - Object Class hashCode()

    Which statement is true about hashCode()?