Which statement best describes the relationship between a Java class and an object?
09 Classes and Class Design Online Quiz Questions
Use this free practice quiz with 20 questions to review 09 Classes and Class Design, test your knowledge, and prepare for your next test or exam.
True or false: A Java constructor must have the same name as its class and cannot declare a return type, including void.
- A
True
- B
False
Complete the statement: In a Java class, fields represent the object's , and methods represent its .
What design principle keeps an object's implementation details together while controlling access to its state?
Which situation is an example of constructor overloading?
- A
A class has multiple constructors with identical parameter lists but different names.
- B
A class has one constructor and several overloaded methods.
- C
A class has multiple constructors with parameter lists that differ in number or type.
- D
A class has a constructor that returns different types depending on the arguments.
Which statements correctly describe a Java static method? Select all that apply.
- A
It can directly access a static field.
- B
It can directly call another static method.
- C
It can directly read an instance field without an object reference.
- D
It cannot use
this.
True or false: If a Java class declares no constructor, Java supplies a no-argument default constructor.
- A
True
- B
False
Complete the statement: In a Java constructor, this(...) must be the and calls .
How many direct superclasses can a Java class extend?
Which design best uses composition to model a car that relies on an engine?
- A
Make
CarextendEngine, because every car is an engine. - B
Store an
Enginefield insideCarand delegatestart()to it. - C
Copy the source code of
EngineintoCar. - D
Make
EngineextendCar, because an engine is a specialized car.
Which activities belong to the recommended process for converting a problem into Java classes? Select all that apply.
- A
Extract nouns as possible classes.
- B
Extract verbs as possible methods.
- C
Make every field public so that responsibilities are visible.
- D
Define invariants that should always remain true.
- E
Choose among relationships such as composition and inheritance.
Design the public behavior and main rules for a BankAccount class. Explain what state should be protected, what its constructor and money-moving methods should validate, and how the class should prevent an invalid balance.
A BankAccount has a balance of 50. What happens when withdraw(70) is called according to the class design in the material?
- A
It returns
trueand changes the balance to-20. - B
It returns
falseand changes the balance to-20. - C
It returns
falseand leaves the balance at50. - D
It throws an exception because every unsuccessful withdrawal is invalid input.
Which expression creates an object of the Temperature class?
- A
Temperature - B
new Temperature(21.5) - C
toFahrenheit - D
roomTemperature
A BankAccount must prevent deposits of zero or negative amounts. Which design best preserves that rule?
- A
Make
balancepublic so every client can update it directly. - B
Keep
balanceprivate and provide a validatingdepositmethod. - C
Make
balancea local variable inside every method. - D
Remove all checks so clients can decide whether an update is valid.
What happens when a Java class declares no constructor of its own?
- A
Java always supplies both a no-argument and a parameterized constructor.
- B
Java supplies a no-argument constructor even after a parameterized constructor is declared.
- C
Java supplies an automatic no-argument constructor only when the class declares no constructor.
- D
Java never supplies constructors automatically.
Why can MathTools.square be declared static without an object, and what restriction follows from that choice?
- A
It has no particular object and uses only its parameter, so it cannot directly use instance fields, instance methods, or this.
- B
It can directly use any private instance field in the class because static methods belong to that class.
- C
It automatically receives the most recently created object and can use that object's instance state.
- D
It can use this to select which object to modify, even though it is called through the class name.
True or false: In a Java constructor, a call to another constructor in the same class using this(...) must be the first statement.
- A
True
- B
False
Which Java keyword refers to the current object inside an instance method or constructor?
What design term describes a class whose data and behavior are closely related and organized around one central responsibility?