Free Practice Quiz Question List

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.

20 questions
01
Choose one
1 point

Which statement best distinguishes a Java class from an object?

  1. A

    A class is an object with no state.

  2. B

    A class defines the fields and methods that its objects have.

  3. C

    A class is a reference variable that stores null.

  4. D

    A class can be created only once.

02
Choose one
1 point

Which combination correctly describes a Java constructor?

  1. A

    It must return void.

  2. B

    It must be declared static.

  3. C

    It has the same name as its class and no return type.

  4. D

    It can run only after an instance method.

03
Choose one
1 point

Given BankAccount original = new BankAccount("Maya", 100.00); followed by BankAccount alias = original; and alias.deposit(25.00);, what does original.getBalance() return?

  1. A

    100.00

  2. B

    25.00

  3. C

    125.00

  4. D

    It causes a compilation error because references cannot be assigned.

04
True or false
1 point

True or false: If BankAccount account = null;, then calling account.getBalance() causes a NullPointerException.

  1. A

    True

  2. B

    False

05
True or false
1 point

True or false: Declaring a balance field private can help a class enforce the invariant that the balance must not become negative.

  1. A

    True

  2. B

    False

06
Written response
1 point

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?

07
Written response
1 point

Which Java keyword refers to the current object inside an instance method or constructor?

08
Fill in the blank
1 point

A initializes a newly created object and can establish its initial valid state.

09
Fill in the blank
1 point

In BankAccount account = new BankAccount("Maya", 100.00);, the expression after new creates an , while account stores a to it.

10
Choose all
1 point

Select all statements that correctly describe Java constructors.

  1. A

    A constructor has the same name as its class.

  2. B

    A constructor must be declared static.

  3. C

    A constructor has no return type, not even void.

  4. D

    A constructor runs only when an existing object explicitly invokes it.

11
Choose all
1 point

Which practices support effective encapsulation in a Java class? Select all that apply.

  1. A

    Declare state fields private when outside code should not change them directly.

  2. B

    Use public methods to control and validate state changes.

  3. C

    Provide a public setter for every field regardless of the class's invariants.

  4. D

    Hide implementation details behind a controlled public interface.

12
Open ended
1 point

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.

13
Choose one
1 point

A BankAccount has a balance of 60 dollars. What happens when withdraw(75.00) is called?

  1. A

    The balance becomes negative and the method returns true.

  2. B

    The balance is unchanged and the method returns false.

  3. C

    The balance becomes zero and the method returns true.

  4. D

    The program automatically creates a second account.

14
Choose one
1 point

A Temperature object starts at 20.0 degrees Celsius. After increase(5.0) is called, what does toFahrenheit() return?

  1. A

    68.0 degrees Fahrenheit

  2. B

    72.0 degrees Fahrenheit

  3. C

    77.0 degrees Fahrenheit

  4. D

    85.0 degrees Fahrenheit

15
Choose one
1 point

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);?

  1. A

    Both accounts will have a balance of 125.0 because they use the same class.

  2. B

    Only the first account will have a balance of 125.0; the second will remain at 75.0.

  3. C

    The program will not compile because a class can be instantiated only once.

  4. D

    Both accounts will have a balance of 75.0 because the deposit is local to the method.

16
Choose one
1 point

Which statement correctly describes the constructor public BankAccount(String owner, double startingBalance)?

  1. A

    It must have the return type void.

  2. B

    It must have the return type of the field it initializes.

  3. C

    It has no return type.

  4. D

    It must return the newly created object explicitly.

17
Choose one
1 point

If BankAccount a = new BankAccount("A", 20.00); and BankAccount b = new BankAccount("B", 90.00);, what does b.deposit(10.00); change?

  1. A

    It changes the state of the object referenced by b.

  2. B

    It changes the state of every BankAccount object.

  3. C

    It changes the state of the object referenced by a because a was declared first.

  4. D

    It changes the class definition rather than an object.

18
True or false
1 point

True or false: In Java, assigning one object reference variable to another creates an independent copy of the referenced object.

  1. A

    True

  2. B

    False

19
Written response
1 point

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.

20
Written response
1 point

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?