What is the effect of the Java declaration BankAccount checking;?
7 Objects and Classes Online Quiz Questions
Use this free practice quiz with 20 questions to review 7 Objects and Classes, test your knowledge, and prepare for your next test or exam.
Which statement correctly describes a Java constructor?
- A
It must be declared static and return the new object.
- B
It must have a return type of void.
- C
It has the same name as the class and no return type.
- D
It can be called only after an object already exists.
Why is the balance field declared private in the BankAccount example?
- A
It prevents direct outside access to balance so methods can enforce valid changes.
- B
It makes balance shared by every BankAccount object.
- C
It causes balance to be initialized automatically to zero in every case.
- D
It allows any class to change balance through the dot operator.
Which two statements apply to the balance field in the given BankAccount class? Select all correct choices.
- A
Each BankAccount object has its own balance value.
- B
All BankAccount objects automatically use one shared balance.
- C
Changing one object's balance normally leaves another object's balance unchanged.
- D
The balance must be declared static to belong to an object.
A new BankAccount is constructed with an opening balance, and then deposit(50.00) is called. Which two effects are supported by the class definition? Select all correct choices.
- A
It creates a second BankAccount object.
- B
It initializes the new object's balance from openingBalance.
- C
It makes the balance shared by all accounts.
- D
It can increase the object's balance when a positive amount is deposited.
True or false: If BankAccount account = null;, calling account.getBalance() causes a NullPointerException at run time.
- A
True
- B
False
True or false: In this.owner = owner;, the left-hand owner is the constructor parameter and the right-hand owner is the object's field.
- A
True
- B
False
Which Java keyword would declare accountCount as one field shared by all BankAccount objects?
What Java exception is caused by calling an instance method through a reference variable that contains null?
Complete the statement: In an object, fields represent , while methods represent .
Complete the statement: The special code that initializes a new object's fields when new creates it is called a .
Using the Student class in the material, explain why student.getCredits() returns 15 and student.isFullTime() returns true after Student student = new Student("Avery"); student.addCredits(15);.
Given public static void printBalance(BankAccount account), what may the parameter account refer to when the method is called?
- A
The parameter must be a primitive number containing the balance.
- B
The parameter can refer to a BankAccount object or a compatible subclass object.
- C
The parameter is automatically a newly constructed BankAccount object.
- D
The parameter can refer only to the BankAccount class definition, never an object.
In the no-argument Point constructor, what does this(0, 0); do?
- A
It calls another constructor in the same class.
- B
It refers to the current object's x field.
- C
It creates a new Point object outside the class.
- D
It invokes the superclass constructor with two arguments.
Which statement best distinguishes a Java class from an object?
- A
A class is one particular entity created at run time, while an object is its blueprint.
- B
A class defines the structure and behavior of objects, while an object is a particular instance.
- C
A class stores only shared data, while an object stores only methods.
- D
A class is a method that creates objects, while an object is a variable type.
True or false: In Java, the declaration BankAccount checking; by itself creates a new BankAccount object.
- A
True
- B
False
Given BankAccount first = new BankAccount("Maya", 500.00); and BankAccount second = new BankAccount("Luis", 800.00);, what normally happens if first changes its balance?
- A
Both objects must always have the same balance because they share a class.
- B
Only the first object can have a balance because balance is declared in the class.
- C
Each object has its own balance value because balance is an instance field.
- D
The balance belongs to the class because every object can access the field.
Which Java keyword refers to the current object inside an instance method or constructor?
Which declaration rule identifies a Java constructor?
- A
It must have a different name from the class and return the new object.
- B
It must be declared static so that new can invoke it.
- C
It must return void and may have any name chosen by the programmer.
- D
It has the same name as the class and has no return type.
What Java exception occurs when BankAccount account = null; is followed by account.getBalance();?