Java Classes, Objects & Constructors Quiz
40 deep and challenging questions that explore every corner of object creation, constructor behavior, the true meaning of this, the subtle differences between instance and static members, and the complete lifecycle of objects in Java.
Question 1
When you write a class with no explicit constructor, what constructor is automatically provided by the compiler?
Question 2
Consider this class with multiple constructors. In what order are the instance initializers and constructor bodies executed when creating a new Child object?
class Parent {
{ System.out.println("Parent instance block"); }
Parent() { System.out.println("Parent()"); }
}
class Child extends Parent {
{ System.out.println("Child instance block"); }
Child() { System.out.println("Child()"); }
}Question 3
Why does this constructor cause a compilation error?
public class Bad {
private Bad(int x) {
this();
}
public Bad() {
this(10);
}
}Question 4
What is the primary purpose of the this keyword when used as this.x in a method or constructor?
Question 5
Examine this class. How many times is 'Static block' printed when the class is loaded and two objects are created?
class Demo {
static { System.out.println("Static block"); }
{ System.out.println("Instance block"); }
Demo() { System.out.println("Constructor"); }
}
// Somewhere: new Demo(); new Demo();Question 6
In a constructor, when must this() or super() be the very first statement?
Question 7
What value does name hold immediately after this object is constructed?
class Person {
String name = "Default";
Person(String name) {
name = name;
}
}
// Person p = new Person("Alice");Question 8
Why is it useful to return this from setter methods?
Question 9
What happens when you attempt to access a static member using an instance reference?
class Example {
static int count = 100;
}
Example ex = new Example();
System.out.println(ex.count);Question 10
Observe this initialization sequence. What is the complete output when new Complex() is executed?
class Complex {
static { System.out.println("1"); }
{ System.out.println("2"); }
Complex() { System.out.println("3"); }
static { System.out.println("4"); }
}Question 11
Why can a static method not directly call this or super?
Question 12
What is displayed when this carefully crafted class is instantiated?
class Tricky {
Tricky t = new Tricky();
{ System.out.println("Instance block"); }
Tricky() { System.out.println("Constructor"); }
}
// new Tricky();Question 13
In which situations is an object eligible for garbage collection?
Question 14
What is the output when this fluent-style class is used?
class Builder {
private String name;
public Builder setName(String name) {
this.name = name;
return this;
}
public void print() { System.out.println(name); }
}
// new Builder().setName("Java").print();Question 15
Why does this class prevent instantiation from outside?
public class Singleton {
private static final Singleton INSTANCE = new Singleton();
private Singleton() {}
public static Singleton getInstance() { return INSTANCE; }
}Question 16
What is displayed when this inheritance chain is instantiated?
class A { A() { System.out.println("A"); } }
class B extends A { B() { System.out.println("B"); } }
class C extends B { C() { System.out.println("C"); } }
// new C();Question 17
Can an instance variable be accessed inside a static method?
Question 18
What is the result of executing this code snippet?
class Counter {
static int count = 0;
Counter() { count++; }
static int getCount() { return count; }
}
new Counter(); new Counter();
System.out.println(Counter.getCount());Question 19
Why might a developer intentionally declare a class with only private constructors?
Question 20
After this method finishes, is the created object eligible for garbage collection?
public void create() {
Object obj = new Object();
// no reference escapes
}Question 21
Which of these is true about the implicit super() call in constructors?
Question 22
What is the exact output of this initialization-heavy example?
class Init {
static String s1 = assign("Static field");
String s2 = assign("Instance field");
static { assign("Static block"); }
{ assign("Instance block"); }
Init() { assign("Constructor"); }
static String assign(String val) { System.out.println(val); return val; }
}
// new Init();Question 23
Why does this code compile successfully even though no explicit constructor is defined for the subclass?
class Parent { public Parent(int x) {} }
class Child extends Parent {}Question 24
What is the practical benefit of instance initializers over putting the same code in every constructor?
Question 25
When does an object become eligible for garbage collection in this scenario?
class Holder {
static Object ref;
void keep(Object o) { ref = o; }
}
// Holder h = new Holder(); h.keep(new Object()); h = null;Question 26
Which keyword is implicitly passed as the first parameter to every instance method?
Question 27
Why is it considered good practice to make utility classes non-instantiable?
Question 28
What is the output when this class with overloaded constructors is used?
class Overload {
Overload() { this("hello"); System.out.println("no-arg"); }
Overload(String s) { System.out.println(s); }
}
// new Overload();Question 29
Can a constructor call an instance method that is overridden in a subclass?
Question 30
What is the most important takeaway about object initialization order in Java?
Question 31
Why does this seemingly correct code produce 'null' instead of the expected name?
class User {
private String name = initName();
User() { name = "Alice"; }
private String initName() { return name; }
}
// System.out.println(new User().name);Question 32
Which of these is NOT a valid use of the this keyword?
Question 33
When is the best time to null out large object references in long-lived objects?
Question 34
Why does this class have exactly one instance throughout the application?
public enum Color { RED, GREEN, BLUE }Question 35
What is the final value of x after object creation?
class Value {
int x = 10;
{ x = 20; }
Value() { x = 30; }
{ x = 40; }
}
// Value v = new Value();Question 36
Which of these statements about static members is false?
Question 37
Why is this constructor considered dangerous in a subclassable class?
public class Base {
public Base() {
doSomething(); // overridable
}
void doSomething() { /* default */ }
}Question 38
What is the output of this chained constructor example?
class Chain {
Chain() { this(5); System.out.println("no-arg"); }
Chain(int x) { this("hello"); System.out.println(x); }
Chain(String s) { System.out.println(s); }
}
// new Chain();Question 39
When is the memory for an object actually allocated?
Question 40
After completing this quiz, what is the single most important principle you now fully understand about object creation in Java?
