Classes and Objects Interview Questions

    Question 1JAVA OOPS - Class and Object Creation

    Which of the following correctly creates an object of class Car?

    class Car {}

    Question 2JAVA OOPS - Accessing Object Members

    What will be the output of the following code?

    class Test { int x = 10; } public class Main { public static void main(String[] args) { Test t = new Test(); System.out.println(t.x); } }

    Question 3JAVA OOPS - Default Constructor

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

    class Person {}

    Question 4JAVA 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 5JAVA OOPS - Object Creation Syntax

    Which statement is invalid?

    class Car {}

    Question 6JAVA OOPS - Class Member Access

    What will be the output?

    class A { int x = 10; } class B { int y = 20; } public class Main { public static void main(String[] args) { A a = new A(); B b = new B(); System.out.println(a.x + b.y); } }

    Question 7JAVA 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 8JAVA 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 9JAVA OOPS - Object Initialization

    Which is true about objects in Java?

    Question 10JAVA OOPS - this Keyword Usage

    What does the this keyword represent in Java?

    class Test { int x; Test(int x) { this.x = x; } }