Java Classes, Objects & Constructors Quiz

Java
0 Passed
0% acceptance

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.

40 Questions
~80 minutes
1

Question 1

When you write a class with no explicit constructor, what constructor is automatically provided by the compiler?

A
A public no-arg constructor that calls super()
B
A private no-arg constructor
C
No constructor at all
D
A protected constructor with Object parameter
2

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?

java
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()"); }
}
A
Parent instance block → Parent() → Child instance block → Child()
B
Parent() → Parent instance block → Child() → Child instance block
C
Child instance block → Child() → Parent instance block → Parent()
D
Static blocks first, then everything else
3

Question 3

Why does this constructor cause a compilation error?

javascript
public class Bad {
    private Bad(int x) {
        this();
    }
    public Bad() {
        this(10);
    }
}
A
Recursive constructor invocation — each calls the other
B
Private constructors cannot be overloaded
C
this() must be the first statement
D
No error — it compiles
4

Question 4

What is the primary purpose of the this keyword when used as this.x in a method or constructor?

A
To disambiguate instance variables from parameters or local variables with the same name
B
To improve performance
C
To create a new object
D
Only required in static contexts
5

Question 5

Examine this class. How many times is 'Static block' printed when the class is loaded and two objects are created?

java
class Demo {
    static { System.out.println("Static block"); }
    { System.out.println("Instance block"); }
    Demo() { System.out.println("Constructor"); }
}
// Somewhere: new Demo(); new Demo();
A
Once
B
Twice
C
Three times
D
Never
6

Question 6

In a constructor, when must this() or super() be the very first statement?

A
Always — the compiler enforces it
B
Only when calling another constructor in the same class
C
Only in subclasses
D
Never required
7

Question 7

What value does name hold immediately after this object is constructed?

java
class Person {
    String name = "Default";
    Person(String name) {
        name = name;
    }
}
// Person p = new Person("Alice");
A
"Default"
B
"Alice"
C
null
D
Compilation error
8

Question 8

Why is it useful to return this from setter methods?

A
Enables method chaining for fluent APIs
B
Improves garbage collection
C
Required by the compiler
D
Makes the method static
9

Question 9

What happens when you attempt to access a static member using an instance reference?

java
class Example {
    static int count = 100;
}
Example ex = new Example();
System.out.println(ex.count);
A
Compiles and runs, but the compiler issues a warning
B
Compilation error
C
Runtime exception
D
Always returns null
10

Question 10

Observe this initialization sequence. What is the complete output when new Complex() is executed?

java
class Complex {
    static { System.out.println("1"); }
    { System.out.println("2"); }
    Complex() { System.out.println("3"); }
    static { System.out.println("4"); }
}
A
1\n4\n2\n3
B
1\n2\n3\n4
C
4\n1\n2\n3
D
2\n3\n1\n4
11

Question 11

Why can a static method not directly call this or super?

A
Static methods belong to the class, not to any instance
B
It would cause a memory leak
C
The compiler prevents it for performance
D
Only in final classes
12

Question 12

What is displayed when this carefully crafted class is instantiated?

java
class Tricky {
    Tricky t = new Tricky();
    { System.out.println("Instance block"); }
    Tricky() { System.out.println("Constructor"); }
}
// new Tricky();
A
StackOverflowError at runtime
B
Instance block\nConstructor
C
Compilation error
D
Nothing — infinite silent loop
13

Question 13

In which situations is an object eligible for garbage collection?

A
When no live thread can reach it via any reference
B
Immediately after nulling the reference
C
Only after System.gc() is called
D
Never — Java has no garbage collection
14

Question 14

What is the output when this fluent-style class is used?

java
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();
A
Java
B
null
C
Compilation error
D
Exception
15

Question 15

Why does this class prevent instantiation from outside?

javascript
public class Singleton {
    private static final Singleton INSTANCE = new Singleton();
    private Singleton() {}
    public static Singleton getInstance() { return INSTANCE; }
}
A
Private constructor cannot be called from outside the class
B
Static final field is immutable
C
Both A and B are required
D
Only the static field matters
16

Question 16

What is displayed when this inheritance chain is instantiated?

java
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();
A
A\nB\nC
B
C\nB\nA
C
A\nC\nB
D
Only C
17

Question 17

Can an instance variable be accessed inside a static method?

A
No — there is no instance in static context
B
Yes, directly
C
Only if the method is also final
D
Only via this reference
18

Question 18

What is the result of executing this code snippet?

java
class Counter {
    static int count = 0;
    Counter() { count++; }
    static int getCount() { return count; }
}
new Counter(); new Counter();
System.out.println(Counter.getCount());
A
2
B
0
C
1
D
Compilation error
19

Question 19

Why might a developer intentionally declare a class with only private constructors?

A
To force all instantiation through static factory methods
B
To improve performance
C
To make the class abstract
D
To prevent inheritance
20

Question 20

After this method finishes, is the created object eligible for garbage collection?

java
public void create() {
    Object obj = new Object();
    // no reference escapes
}
A
Yes — as soon as the method returns
B
No — it lives forever
C
Only after System.gc()
D
Never — local references prevent GC
21

Question 21

Which of these is true about the implicit super() call in constructors?

A
It is automatically inserted if no explicit this() or super() call exists
B
It calls Object() constructor only
C
It can be omitted completely
D
It is only needed in abstract classes
22

Question 22

What is the exact output of this initialization-heavy example?

java
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();
A
Static field\nStatic block\nInstance field\nInstance block\nConstructor
B
Static field\nInstance field\nStatic block\nInstance block\nConstructor
C
Instance field\nConstructor\nStatic field\nStatic block
D
Constructor\nStatic field\nInstance field
23

Question 23

Why does this code compile successfully even though no explicit constructor is defined for the subclass?

java
class Parent { public Parent(int x) {} }
class Child extends Parent {}
A
It does NOT compile — Child has no constructor that calls super(int)
B
The compiler adds a no-arg constructor that calls super(int)
C
Parent has a default constructor
D
Child inherits Parent’s constructor
24

Question 24

What is the practical benefit of instance initializers over putting the same code in every constructor?

A
Guarantees the code runs regardless of which constructor is used
B
Improves runtime performance
C
Reduces bytecode size
D
Makes the class thread-safe
25

Question 25

When does an object become eligible for garbage collection in this scenario?

java
class Holder {
    static Object ref;
    void keep(Object o) { ref = o; }
}
// Holder h = new Holder(); h.keep(new Object()); h = null;
A
Never — static reference prevents collection
B
Immediately after h = null
C
Only after ref = null
D
After System.gc()
26

Question 26

Which keyword is implicitly passed as the first parameter to every instance method?

A
this
B
super
C
instance
D
No hidden parameter
27

Question 27

Why is it considered good practice to make utility classes non-instantiable?

A
Private constructor clearly communicates that the class is not meant to be instantiated
B
Improves performance
C
Required by the JVM
D
Prevents inheritance
28

Question 28

What is the output when this class with overloaded constructors is used?

java
class Overload {
    Overload() { this("hello"); System.out.println("no-arg"); }
    Overload(String s) { System.out.println(s); }
}
// new Overload();
A
hello\nno-arg
B
no-arg\nhello
C
Only hello
D
Compilation error
29

Question 29

Can a constructor call an instance method that is overridden in a subclass?

A
Yes — the overridden version in the subclass is called
B
No — only the superclass version runs
C
Only if the method is static
D
Compilation error
30

Question 30

What is the most important takeaway about object initialization order in Java?

A
Superclasses are fully initialized before subclasses, and fields/blocks before constructors
B
Constructors run before any fields are initialized
C
Static initialization happens after instance creation
D
Order is undefined
31

Question 31

Why does this seemingly correct code produce 'null' instead of the expected name?

javascript
class User {
    private String name = initName();
    User() { name = "Alice"; }
    private String initName() { return name; }
}
// System.out.println(new User().name);
A
Field initializers run before constructor body
B
initName() is called recursively
C
name is private
D
Compilation error
32

Question 32

Which of these is NOT a valid use of the this keyword?

A
this() to call another constructor
B
this.field to access shadowed field
C
return this for method chaining
D
this inside a static method
33

Question 33

When is the best time to null out large object references in long-lived objects?

A
When the reference is no longer needed, to help GC
B
Never — the JVM handles it automatically
C
Only inside finalize()
D
Only for static fields
34

Question 34

Why does this class have exactly one instance throughout the application?

java
public enum Color { RED, GREEN, BLUE }
A
Enums are implicitly static and final with private constructors
B
The compiler creates a singleton
C
Because it extends Enum
D
All of the above
35

Question 35

What is the final value of x after object creation?

java
class Value {
    int x = 10;
    { x = 20; }
    Value() { x = 30; }
    { x = 40; }
}
// Value v = new Value();
A
40
B
30
C
20
D
10
36

Question 36

Which of these statements about static members is false?

A
They can be accessed without any instance
B
They are initialized before any instance exists
C
They belong to each object individually
D
They live as long as the class is loaded
37

Question 37

Why is this constructor considered dangerous in a subclassable class?

java
public class Base {
    public Base() {
        doSomething(); // overridable
    }
    void doSomething() { /* default */ }
}
A
Subclasses may override doSomething() and access uninitialized fields
B
It prevents inheritance
C
It causes memory leak
D
No issue
38

Question 38

What is the output of this chained constructor example?

java
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();
A
hello\n5\nno-arg
B
no-arg\n5\nhello
C
hello\nno-arg\n5
D
Compilation error
39

Question 39

When is the memory for an object actually allocated?

A
When the new keyword is executed
B
When the constructor finishes
C
When the class is loaded
D
Only after all fields are initialized
40

Question 40

After completing this quiz, what is the single most important principle you now fully understand about object creation in Java?

A
Object initialization follows a strict, predictable order that guarantees superclass completion before subclass work begins
B
Constructors are optional and rarely needed
C
this and super are interchangeable
D
Static and instance members behave identically

QUIZZES IN Java