Java OOPs Concepts Test 4

    Question 1: JAVA 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 2: JAVA OOPS - Object Initialization

    Which is true about objects in Java?

    Question 3: JAVA 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 4: JAVA OOPS - Static Method and Polymorphism

    Which statement is correct?

    Question 5: JAVA OOPS - Abstract Method Requirement

    Which of the following is true?

    Question 6: JAVA OOPS - Changing Internal Representation

    Which is true?

    Question 7: JAVA OOPS - Interface and Abstract Class

    Which is true?

    Question 8: JAVA 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 9: JAVA OOPS - Final with Parameters

    What is true about final parameters in Java?

    Question 10: JAVA OOPS - Final Method Overriding

    Which statement is correct?

    Question 11: JAVA 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 12: JAVA OOPS - Private Constructor

    What is true about a private constructor?

    class Demo { private Demo() {} }

    Question 13: JAVA OOPS - Inheritance and Private Members

    Which of the following statements is true?

    Question 14: JAVA OOPS - Constructor and Polymorphism

    Which statement is true?

    Question 15: JAVA 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 16: JAVA 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 17: JAVA 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 18: JAVA OOPS - Java API Packages

    Which of the following is a standard Java API package?

    Question 19: JAVA OOPS - Blank Final Variable

    Which statement is correct?

    Question 20: JAVA OOPS - Object Class hashCode()

    Which statement is true about hashCode()?