Java Constructors and Object Lifetime Practice Quiz (12 questions)

All questions and answers

1. When does a constructor prepare an object by initializing fields or performing setup?

When a method is called on an already-created object
When the program declares the class, before any object is created
When the program creates the object with `new` and a matching constructor call
When the program assigns a new value to one of the object's fields

When the program creates the object with `new` and a matching constructor call

Explanation

A constructor runs as part of creating an object with `new`, using a matching constructor call, and can initialize fields or perform setup. Declaring a class defines its structure but does not itself create an object or run its constructor.

2. Given `class Student { String name; Student() { name = "New Student"; } }`, what is the result of evaluating `new Student()`?

The `name` field is assigned `"Student"` when the class is declared
The `Student()` constructor runs and assigns `"New Student"` to `name`
A new `Student` object is created, but its constructor does not run
The constructor runs only after a method reads the `name` field

The `Student()` constructor runs and assigns `"New Student"` to `name`

Explanation

Creating the object with `new Student()` invokes `Student()`, which assigns `"New Student"` to the object's `name` field. Merely declaring the class does not perform that assignment.

3. Which description identifies a no-argument constructor?

It has the class name but includes one or more input parameters
It has no parameters, as in `Student() { }`
It accepts values that initialize fields, as in `Student(int id) { }`
It returns a value after creating an object

It has no parameters, as in `Student() { }`

Explanation

A no-argument constructor has an empty parameter list, as in `Student() { }`. A constructor that accepts input values is parameterized, so `Student(int id) { }` does not fit this definition.

4. Which constructor is parameterized?

`void Student(int id, String name) { }`
`Student() { }`
`Student { int id; String name; }`
`Student(int id, String name) { }`

`Student(int id, String name) { }`

Explanation

A parameterized constructor accepts input values through its parameter list, as `Student(int id, String name) { }` does. The empty parameter list in `Student() { }` makes that constructor a no-argument constructor.

5. What makes constructors in a class overloaded?

They have different names while creating objects of the same class
They have the same parameter list but different return types
They have different parameter lists
They use different access modifiers but share one parameter list

They have different parameter lists

Explanation

Constructor overloading means defining constructors with different parameter lists. Changing a return type does not distinguish constructors, which have no return type.

6. A constructor must invoke another constructor before executing its own statements. Which pairing correctly describes the calls?

Both `this()` and `super()` chain within the same class
`this()` chains within the same class, while `super()` chains across inheritance
`this()` chains across inheritance, while `super()` chains within the same class
Both `this()` and `super()` chain across inheritance

`this()` chains within the same class, while `super()` chains across inheritance

Explanation

`this()` invokes another constructor in the same class, whereas `super()` invokes a constructor across inheritance; either call must come before other statements. The plausible reversal assigns each call to the wrong kind of constructor chain.

7. What distinguishes a class from an object created from that class?

A class describes structure, while each object has its own state.
A class is created by a constructor, while each object is loaded by the JVM.
A class stores the state shared by all objects, while each object describes structure.
A class is a reference to memory, while each object is a method-call frame.

A class describes structure, while each object has its own state.

Explanation

A class defines structure, and separate objects created from it can hold their own state. The idea that a class stores state shared by all objects confuses the class definition with the state of individual instances.

8. Which sequence best describes an object's life cycle in Java?

A class is loaded, an object is created with `new` and a constructor, the object is used through a reference, and it may become eligible for collection when no live path reaches it.
A class is loaded, its object is used directly without a reference, and garbage collection begins as soon as a method returns.
An object is created when its class is loaded, its constructor is called after it becomes unreachable, and a reference is assigned during collection.
A reference is created first, the object is allocated on the stack, and the class is loaded after the object is no longer used.

A class is loaded, an object is created with `new` and a constructor, the object is used through a reference, and it may become eligible for collection when no live path reaches it.

Explanation

The sequence runs from class loading to object creation, reference-based use, and possible eligibility for garbage collection after the object loses all live paths. A constructor does not wait until an object becomes unreachable; it participates in creating the object.

9. In the usual Java memory model, where are method-call frames and local references generally associated, and where are objects generally allocated?

Frames are associated with the heap, while local references and objects are generally allocated in the stack.
Frames and local references are associated with the stack, while objects are generally allocated in the heap.
Frames and local references are associated with the heap, while objects are generally allocated in the stack.
Frames and objects are associated with the stack, while local references are generally allocated in the heap.

Frames and local references are associated with the stack, while objects are generally allocated in the heap.

Explanation

Method-call frames and local variables or references are commonly associated with the stack, whereas objects are generally allocated in the heap. Placing objects on the stack reverses the standard distinction tested here.

10. An object has no live reference path leading to it; what does this imply about its garbage-collection status?

It becomes unreachable only after the program terminates.
It remains reachable as long as its class is loaded.
It is guaranteed to be collected as soon as the reference disappears.
It is unreachable and may be eligible for garbage collection.

It is unreachable and may be eligible for garbage collection.

Explanation

An object is unreachable when no live reference path leads to it, and it may then be eligible for garbage collection. Unreachability does not guarantee immediate collection, so eligibility should not be confused with an immediate outcome.

11. After an object becomes eligible for garbage collection, what can be concluded about when it will be collected?

It will be collected as soon as the next method call finishes.
It may remain uncollected for some time because eligibility does not imply immediate collection.
It will be collected immediately if the program has called `System.gc()`.
It will remain allocated until the program explicitly deletes it.

It may remain uncollected for some time because eligibility does not imply immediate collection.

Explanation

Eligibility means the object can be collected, not that collection happens immediately. Calling `System.gc()` also does not guarantee immediate collection, so it cannot establish a specific collection time.

12. A Java program automatically manages object memory but opens a file and a database connection; what resource-handling approach is appropriate?

Close the file explicitly but rely on garbage collection to close the database connection.
Rely on garbage collection for object memory and explicitly close the file and database connection properly.
Explicitly release object memory while relying on garbage collection to close the file and database connection.
Rely on garbage collection to close the file and database connection when their objects become unreachable.

Rely on garbage collection for object memory and explicitly close the file and database connection properly.

Explanation

Java automatically manages object memory, but files, sockets, and database connections still require proper closing. Making these resources unreachable is not a substitute for closing them.

Study with flashcards

Lock in the answers with 30 flashcards on Java Constructors and Object Lifetime.

What does a constructor do when preparing a new object?

It initializes fields or performs setup.

When does a constructor run during object creation?

When the program creates an object with `new` and a matching constructor call.

What must a constructor’s name match?

Its class name.

See the flashcards →

Read the study guide

Go over the full study guide for Java Constructors and Object Lifetime.

Read the study guide →

Related classes

Make your own practice quizzes

Upload your class notes and Revizly writes a practice quiz with explained answers in about a minute.

Quiz maker