Free Practice Quiz Question List

2 Classes and Objects Online Quiz Questions

Use this free practice quiz with 20 questions to review 2 Classes and Objects, test your knowledge, and prepare for your next test or exam.

20 questions
01
Choose one
1 point

Which statement best distinguishes a class from an object in object-oriented programming?

  1. A

    The class is a particular instance, while the object is a template.

  2. B

    The class is a template, while the object is a particular instance.

  3. C

    The class stores only data, while the object stores only behavior.

  4. D

    The class exists only after an object has been created.

02
Choose all
1 point

Which two statements correctly describe fields in a class? Select all that apply.

  1. A

    An instance field can hold a different value for each object.

  2. B

    A method cannot change an object's state.

  3. C

    A static or class field may be shared by all objects of the class.

  4. D

    A constructor is an ordinary field used to store state.

03
True or false
1 point

True or false: After checking.deposit(100), the savings object in the example also has a balance of 600.

  1. A

    True

  2. B

    False

04
Written response
1 point

What programming term means creating an object from a class?

05
Fill in the blank
1 point

A is a special operation used to establish an object's initial state when the object is created.

06
Choose one
1 point

A class includes a method named withdraw that validates an amount and may change the balance. What is withdraw?

  1. A

    A value that uniquely identifies a memory address and cannot change

  2. B

    A storage location that always belongs to the class rather than an object

  3. C

    A function defined as part of a class that provides behavior

  4. D

    A required argument supplied only when a class is declared

07
Choose all
1 point

Which two responsibilities are appropriate for a constructor? Select all that apply.

  1. A

    It establishes the object's initial state.

  2. B

    It must return an ordinary result such as a Boolean.

  3. C

    It is used only after the object has finished initialization.

  4. D

    It can help ensure that class invariants hold for the new object.

08
True or false
1 point

True or false: If first = checking and second = checking, then second.deposit(50) can change the balance observed through first.

  1. A

    True

  2. B

    False

09
Written response
1 point

What programming term describes keeping fields private and exposing carefully designed methods to control access to object state?

10
Fill in the blank
1 point

Two account objects can have equal account numbers and balances yet still be different because their object differs.

11
Choose one
1 point

In the lifecycle described, which condition indicates that an object has lost reachability?

  1. A

    The object has just been allocated but has no fields.

  2. B

    The object has been initialized but has never had a method called.

  3. C

    The object has equal field values to another object.

  4. D

    No live part of the program can reach the object through a reference.

12
Open ended
1 point

Using the Counter example, explain why counterA.currentValue() prints 4 after counterB.increment(). Your answer should address references, object identity, the instance field value, and the effect of the method call.

13
Written response
1 point

In the Counter example, what integer does counterA.currentValue() return after counterB.increment()?

14
True or false
1 point

True or false: In a garbage-collected language, an object that is no longer reachable is necessarily reclaimed immediately.

  1. A

    True

  2. B

    False

15
Written response
1 point

In the pseudocode methods for BankAccount, what keyword refers to the current object in an expression such as this.balance?

16
Choose one
1 point

A BankAccount object must store its own balance independently of every other account. Which kind of member should represent that balance?

  1. A

    A field that stores a value shared by every object

  2. B

    A field that belongs separately to each object

  3. C

    A method that initializes an object

  4. D

    A reference that identifies the class definition

17
Choose one
1 point

Why should a constructor validate an account's opening balance when a new BankAccount is created?

  1. A

    To compare two existing objects for identity

  2. B

    To release all external resources immediately

  3. C

    To establish the new object's initial state

  4. D

    To make every field static

18
Choose one
1 point

Which statement correctly distinguishes an instance method from a class or static method?

  1. A

    An instance method operates on a particular object

  2. B

    An instance method must always modify a static field

  3. C

    A class method requires a particular object for every call

  4. D

    A class method can only return a Boolean

19
Choose one
1 point

A BankAccount has a balance of 400. Its withdrawal operation returns false when the amount is less than or equal to 0 or greater than the current balance; otherwise, it subtracts the amount and returns true. Which call succeeds?

  1. A

    withdraw(0)

  2. B

    withdraw(250)

  3. C

    withdraw(-20)

  4. D

    withdraw(450)

20
Choose one
1 point

A BankAccount starts with a balance of 500. It performs withdraw(150) and then withdraw(300), and both operations are permitted. What is the final balance?

  1. A

    350

  2. B

    200

  3. C

    50

  4. D

    -100