Java Constructors and Object Lifetime Flashcards (30 cards)

All cards

1Question

What does a constructor do when preparing a new object?

Answer

It initializes fields or performs setup.

2Question

When does a constructor run during object creation?

Answer

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

3Question

What must a constructor’s name match?

Answer

Its class name.

4Question

What return type does a constructor have?

Answer

None.

5Question

What defines a no-argument constructor?

Answer

It has no parameters.

6Question

What does a parameterized constructor accept?

Answer

Input values.

7Question

What does constructor overloading mean?

Answer

Creating constructors with different parameter lists.

8Question

Which call chains constructors within the same class?

Answer

`this()`.

9Question

Which call chains constructors across inheritance?

Answer

`super()`.

10Question

Where must a `this()` or `super()` call appear in a constructor?

Answer

Before any other statement.

11Question

What does a class describe, and what does each object created from it have?

Answer

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

12Question

What event makes an object eligible for garbage collection?

Answer

No live path reaches the object.

13Question

Which steps precede object use in an object life cycle?

Answer

Class loading, then object creation with `new` and a constructor.

14Question

Where are method-call frames and local variables commonly associated?

Answer

The stack.

15Question

Where are objects generally allocated?

Answer

The heap.

16Question

When is an object reachable?

Answer

When a live reference path leads to it.

17Question

When is an object unreachable?

Answer

When no live reference path reaches it.

18Question

What does garbage-collection eligibility mean about when an object will be collected?

Answer

It does not mean the object will be collected immediately.

19Question

What must Java programs do with files, sockets, and database connections?

Answer

Close them properly.

20Question

How does Java manage object memory?

Answer

Automatically.

21Question

What does `new Student()` assign to `name` in the given class?

Answer

`New Student`.

22Question

Which constructor runs when the given class evaluates `new Student()`?

Answer

`Student()`.

23Question

Which modifiers cannot be used to declare a constructor?

Answer

`static`, `final`, `abstract`, and `synchronized`.

24Question

What should a programmer expect after declaring a custom constructor?

Answer

A default constructor should not be expected.

25Question

What must a constructor call match?

Answer

The number and types of arguments accepted by the constructor.

26Question

What is the scope of a variable name?

Answer

The part of a program where the name can be accessed.

27Question

In `Student s = new Student();`, what does `s` refer to?

Answer

The `Student` object created by `new Student()`.

28Question

What does assigning `null` to a reference mean?

Answer

That reference no longer points to the object.

29Question

What does calling `System.gc()` guarantee about collecting a specific object?

Answer

It does not guarantee immediate collection.

30Question

What common programming error involves calling a method through a null reference?

Answer

Calling a method through a null reference.

Test yourself with the quiz

Answer 12 questions on Java Constructors and Object Lifetime.

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

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

Take the quiz →

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 flashcards

Upload your class notes and Revizly writes your flashcards in about a minute.

Flashcard maker