Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Starting Out with Java From Control

Structures through Objects 7th Edition


Gaddis Test Bank
Visit to download the full and correct content document: https://testbankdeal.com/dow
nload/starting-out-with-java-from-control-structures-through-objects-7th-edition-gaddis
-test-bank/
Starting Out with Java: From Control Structures through Objects 7e (Gaddis)
Chapter 8 A Second Look at Classes and Objects

TRUE/FALSE

1. The key word this is the name of a reference variable that an object can use to refer to itself.

ANS: T

2. The key word this is the name of a reference variable that is available to all static methods.

ANS: F

3. The names of the enum constants in an enumerated data type must be enclosed in quotation marks.

ANS: F

4. An enumerated data type is actually a special type of class.

ANS: T

5. enum constants have a toString method.

ANS: T

6. An instance of a class does not have to exist in order for values to be stored in a class's static fields.

ANS: T

7. A class's static methods do not operate on the fields that belong to any instance of the class.

ANS: T

8. You can declare an enumerated data type inside a method.

ANS: F

9. If a class has a method named finalize, it is called automatically just before an instance of the class
is destroyed by the garbage collector.

ANS: T

10. If a class has a method named finalize, it is called automatically just before a data member that has
been identified as final of the class is destroyed by the garbage collector.

ANS: F
11. If you write a toString method for a class, Java will automatically call the method any time you
concatenate an object of the class with a string.

ANS: T

12. When an object is passed as an argument, it is actually a reference to the object that is passed.

ANS: T

13. Both instance fields and instance methods are associated with a specific instance of a class, and they
cannot be used until an instance of the class is created.

ANS: T

14. A single copy of a class's static field is shared by all instances of the class.

ANS: T

15. When an object reference is passed to a method, the method may change the values in the object.

ANS: T

16. If you write a toString method to display the contents of an object, object1, for a class,
Class1, then the following two statements are equivalent:

System.out.println(object1);
System.out.println(object1.toString());

ANS: T

MULTIPLE CHOICE

1. __________ is the term for the relationship created by object aggregation.


a. "Has a" c. "Is a"
b. Inner class d. One-to-many
ANS: A

2. The only limitation that static methods have is __________.


a. they must be declared outside of the class
b. they cannot refer to nonstatic members of the class
c. they can only be called from static members of the class
d. they can refer only to nonstatic members of the class
ANS: B

3. A class that is defined inside another class is called a(n) __________.


a. nested class c. inner class
b. enumerated class d. helper class
ANS: C
4. Static methods can only operate on __________ fields.
a. instance c. global
b. static d. local
ANS: B

5. When you make a copy of the aggregate object and of the objects that it references, __________.
a. you are performing a shallow copy
b. you are performing a nested copy
c. you are performing a deep copy
d. a compiler error will occur
ANS: C

6. A deep copy of an object __________.


a. is an assignment of that object to another object
b. is an operation that copies an aggregate object and all the objects that it references
c. is a bogus term and means nothing
d. is always a private method
ANS: B

7. Which of the following is not true about static methods?


a. It is not necessary for an instance of the class to be created to execute a static method.
b. They are called by placing the key word static after the access specifier in the method
header.
c. They are called from an instance of the class.
d. They are often used to create utility classes that perform operations on data but have no
need to collect and store data.
ANS: C

8. The "has a" relationship is sometimes called a(n) __________ because one object is part of a greater
whole.
a. enterprise c. mutual relationship
b. possession d. whole-part relationship
ANS: D

9. The whole-part relationship created by object aggregation is more often called a(n) __________
relationship.
a. "has a" c. extra class
b. inner class d. inside class
ANS: A

10. The JVM periodically performs the __________ process to remove unreferenced objects from
memory.
a. memory shuffling c. garbage collection
b. system restore d. memory sweeping
ANS: C
11. CRC stands for __________.
a. Class, Recyclability, Collaborations c. Class, Responsibilities, Collaborations
b. Class, Redundancy, Collections d. Code, Reuse, Constancy
ANS: C

12. Enumerated types have the __________ method which returns the position of an enum constant in the
declaration list.
a. position c. ordinal
b. location d. index
ANS: C

13. Java automatically stores a __________ value in all uninitialized static member variables.
a. 0 b. -1 c. null d. false
ANS: A

14. You cannot use the fully-qualified name of an enum constant for ___________.
a. a case expression c. a boolean expression
b. an argument to a method d. Any of these
ANS: A

15. When the this variable is used to call a constructor__________.


a. it must be the first statement in the constructor making the call
b. it can be anywhere in the constructor making the call
c. it must be the last statement in the constructor making the call
d. None of these. You cannot use the this variable in a constructor call.
ANS: A

16. When a reference variable is passed as an argument to a method __________.


a. a copy of the variable's value is passed into the method's parameter
b. the method has access to the object that the variable references
c. the method becomes a static method
d. the program terminates
ANS: B

17. When a method's return type is a class, what is actually returned to the calling program?
a. an object of that class
b. a reference to an object of that class
c. the values in the object that the method accessed
d. nothing - the return type is simply for documentation in this situation
ANS: B

18. In Java it is possible to write a method that will return __________.


a. a whole number c. a string of characters
b. a reference to an object d. Any of these
ANS: D

19. You cannot use the == operator to compare the contents of __________.
a. objects c. integers
b. strings d. Boolean values
ANS: A

20. An object's __________ is simply the data that is stored in the object's fields at any given moment.
a. value c. record
b. assessment d. state
ANS: D

21. A static field is created by placing the key word static __________.
a. after the access specifier and the field's data type
b. after the access specifier and before the field's data type
c. after the field name
d. in brackets, before the field's data type
ANS: B

22. A declaration for an enumerated type begins with the __________ key word.
a. enumerated c. ENUM
b. enum type d. enum
ANS: D

23. When a field is declared static there will be __________.


a. a copy of the field for each method in the class
b. a copy of the field in each class object
c. only one copy of the field in memory
d. two reference copies of the field for each method in the class
ANS: C

24. If the this variable is used to call a constructor, __________.


a. a compiler error will result if it is not the first statement of the constructor
b. a compiler error will result if it is the first statement of the constructor
c. nothing will happen
d. the this variable cannot be used as a constructor call
ANS: A

25. To compare two objects in a class, __________.


a. use the == operator (for example, object1 == object2)
b. write a method to do a byte-by-byte compare of the two objects
c. write an equals method that will make a field by field compare of the two objects
d. This cannot be done since objects consist of several fields.
ANS: C
26. Which of the following is not true about static methods?
a. They are created by placing the key word static after the access specifier in the method
header.
b. It is necessary for an instance of the class to be created to execute the method.
c. They are called directly from the class.
d. They are often used to create utility classes that perform operations on data but have no
need to store and collect data.
ANS: B

27. If you attempt to perform an operation with a null reference variable __________.
a. the resulting operation will always be zero
b. the results will be unpredictable
c. the program will terminate
d. Java will create an object to reference the variable
ANS: C

28. If object1 and object2 are objects of the same class, to make object2 a copy of object1
__________.
a. write a method for the class that will make a field by field copy of object1 data
members into object2 data members
b. use the copy method that is a part of the Java language
c. use the default constructor to create object2 with object1 data members
d. use an assignment statement to make object2 a copy of object1
ANS: A

29. If you have defined a class, SavingsAccount, with a public static method,
getNumberOfAccounts, and created a SavingsAccount object referenced by the variable
account20, which of the following will call the getNumberOfAccounts method?
a. account20.getNumberOfAccounts();
b. SavingsAccount.getNumberOfAccounts();
c. getNumberOfAccounts();
d. SavingsAccount.account20.getNumberOfAccounts();
ANS: B

30. If you have defined a class, SavingsAccount, with a public static data member named
numberOfAccounts, and created a SavingsAccount object referenced by the variable
account20, which of the following will assign numberOfAccounts to numAccounts?
a. numAccounts = account20.numAccounts;
b. numAccounts = numOfAccounts;
c. numAccounts = SavingsAccount.numberOfAccounts;
d. numAccounts = account20;
ANS: C

31. Assume the class BankAccount has been created and the following statement correctly creates an
instance of the class.
BankAccount account = new BankAccount(5000.00);
What is true about the following statement?
System.out.println(account);
a. A runtime error will occur.
b. The method will display unreadable binary data on the screen.
c. The account object's toString method will be implicitly called.
d. A compiler error will occur.
ANS: C

32. Given the following declaration:


enum Tree ( OAK, MAPLE, PINE )
What is the fully-qualified name of the PINE enum constant?
a. enum.PINE c. Tree.PINE
b. PINE d. enum.Tree.PINE
ANS: C

33. Given the following declaration:


enum Tree ( OAK, MAPLE, PINE )
What is the ordinal value of the MAPLE enum constant?
a. 0 b. 1 c. 2 d. 3
ANS: B

34. If the following is from the method section of a UML diagram, which of the statements below is true?
+ equals(object2:Stock) : boolean
a. This is a public method that accepts a Stock object as its argument and returns a
boolean value.
b. This is a public method that returns a reference to a String object.
c. This is a private method that receives two objects from the Stock class and returns a
boolean value.
d. This is a private method that returns a boolean value.
ANS: A

35. If the following is from the method section of a UML diagram, which of the statements below is true?
+ add(object2:Stock) : Stock
a. This is a private method named add that accepts and returns objects of the Stock class.
b. This is a private method named Stock that adds two objects.
c. This is a public method named add that accepts and returns references to objects in the
Stock class.
d. This is a public method named Stock that adds two objects.

ANS: C

36. Given the following method header, what will be returned from the method?
public Rectangle getRectangle()
a. the address of an object of the Rectangle class
b. the values stored in the data members of the Rectangle object
c. a graph of a rectangle
d. an object of the class Rectangle
ANS: A

You might also like