Which statement best distinguishes a class from an object in object-oriented programming?
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.
Which two statements correctly describe fields in a class? Select all that apply.
- A
An instance field can hold a different value for each object.
- B
A method cannot change an object's state.
- C
A static or class field may be shared by all objects of the class.
- D
A constructor is an ordinary field used to store state.
True or false: After checking.deposit(100), the savings object in the example also has a balance of 600.
- A
True
- B
False
What programming term means creating an object from a class?
A is a special operation used to establish an object's initial state when the object is created.
A class includes a method named withdraw that validates an amount and may change the balance. What is withdraw?
- A
A value that uniquely identifies a memory address and cannot change
- B
A storage location that always belongs to the class rather than an object
- C
A function defined as part of a class that provides behavior
- D
A required argument supplied only when a class is declared
Which two responsibilities are appropriate for a constructor? Select all that apply.
- A
It establishes the object's initial state.
- B
It must return an ordinary result such as a Boolean.
- C
It is used only after the object has finished initialization.
- D
It can help ensure that class invariants hold for the new object.
True or false: If first = checking and second = checking, then second.deposit(50) can change the balance observed through first.
- A
True
- B
False
What programming term describes keeping fields private and exposing carefully designed methods to control access to object state?
Two account objects can have equal account numbers and balances yet still be different because their object differs.
In the lifecycle described, which condition indicates that an object has lost reachability?
- A
The object has just been allocated but has no fields.
- B
The object has been initialized but has never had a method called.
- C
The object has equal field values to another object.
- D
No live part of the program can reach the object through a reference.
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.
In the Counter example, what integer does counterA.currentValue() return after counterB.increment()?
True or false: In a garbage-collected language, an object that is no longer reachable is necessarily reclaimed immediately.
- A
True
- B
False
In the pseudocode methods for BankAccount, what keyword refers to the current object in an expression such as this.balance?
A BankAccount object must store its own balance independently of every other account. Which kind of member should represent that balance?
- A
A field that stores a value shared by every object
- B
A field that belongs separately to each object
- C
A method that initializes an object
- D
A reference that identifies the class definition
Why should a constructor validate an account's opening balance when a new BankAccount is created?
- A
To compare two existing objects for identity
- B
To release all external resources immediately
- C
To establish the new object's initial state
- D
To make every field static
Which statement correctly distinguishes an instance method from a class or static method?
- A
An instance method operates on a particular object
- B
An instance method must always modify a static field
- C
A class method requires a particular object for every call
- D
A class method can only return a Boolean
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?
- A
withdraw(0)
- B
withdraw(250)
- C
withdraw(-20)
- D
withdraw(450)
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?
- A
350
- B
200
- C
50
- D
-100