Which statement best distinguishes a Java class from an object?
08 Objects and Encapsulation Online Quiz Questions
Use this free practice quiz with 20 questions to review 08 Objects and Encapsulation, test your knowledge, and prepare for your next test or exam.
Which combination correctly describes a Java constructor?
- A
It must return void.
- B
It must be declared static.
- C
It has the same name as its class and no return type.
- D
It can run only after an instance method.
Given BankAccount original = new BankAccount("Maya", 100.00); followed by BankAccount alias = original; and alias.deposit(25.00);, what does original.getBalance() return?
- A
100.00
- B
25.00
- C
125.00
- D
It causes a compilation error because references cannot be assigned.
True or false: If BankAccount account = null;, then calling account.getBalance() causes a NullPointerException.
- A
True
- B
False
True or false: Declaring a balance field private can help a class enforce the invariant that the balance must not become negative.
- A
True
- B
False
A BankAccount starts with a balance of 80 dollars. It receives a valid deposit of 45 dollars and then attempts to withdraw 200 dollars. What is the final balance?
Which Java keyword refers to the current object inside an instance method or constructor?
A initializes a newly created object and can establish its initial valid state.
In BankAccount account = new BankAccount("Maya", 100.00);, the expression after new creates an , while account stores a to it.
Select all statements that correctly describe Java constructors.
- A
A constructor has the same name as its class.
- B
A constructor must be declared static.
- C
A constructor has no return type, not even void.
- D
A constructor runs only when an existing object explicitly invokes it.
Which practices support effective encapsulation in a Java class? Select all that apply.
- A
Declare state fields private when outside code should not change them directly.
- B
Use public methods to control and validate state changes.
- C
Provide a public setter for every field regardless of the class's invariants.
- D
Hide implementation details behind a controlled public interface.
Explain the difference between assigning a reference variable and assigning a primitive variable in Java. Use the examples BankAccount b = a; and int second = first; to describe what is copied and how later changes behave.
A BankAccount has a balance of 60 dollars. What happens when withdraw(75.00) is called?
- A
The balance becomes negative and the method returns true.
- B
The balance is unchanged and the method returns false.
- C
The balance becomes zero and the method returns true.
- D
The program automatically creates a second account.
A Temperature object starts at 20.0 degrees Celsius. After increase(5.0) is called, what does toFahrenheit() return?
- A
68.0 degrees Fahrenheit
- B
72.0 degrees Fahrenheit
- C
77.0 degrees Fahrenheit
- D
85.0 degrees Fahrenheit
Given BankAccount first = new BankAccount("Maya", 100.00); and BankAccount second = new BankAccount("Luis", 75.00);, what are the balances after first.deposit(25.00);?
- A
Both accounts will have a balance of 125.0 because they use the same class.
- B
Only the first account will have a balance of 125.0; the second will remain at 75.0.
- C
The program will not compile because a class can be instantiated only once.
- D
Both accounts will have a balance of 75.0 because the deposit is local to the method.
Which statement correctly describes the constructor public BankAccount(String owner, double startingBalance)?
- A
It must have the return type
void. - B
It must have the return type of the field it initializes.
- C
It has no return type.
- D
It must return the newly created object explicitly.
If BankAccount a = new BankAccount("A", 20.00); and BankAccount b = new BankAccount("B", 90.00);, what does b.deposit(10.00); change?
- A
It changes the state of the object referenced by
b. - B
It changes the state of every
BankAccountobject. - C
It changes the state of the object referenced by
abecauseawas declared first. - D
It changes the class definition rather than an object.
True or false: In Java, assigning one object reference variable to another creates an independent copy of the referenced object.
- A
True
- B
False
A Temperature object starts at 20.0 degrees Celsius. After temperature.increase(5.0);, what value does temperature.toFahrenheit() return? Enter the numerical value in degrees Fahrenheit.
What is the technical term for a condition that should remain true for every valid object, such as a bank account balance that cannot become negative?