AP Computer Science A, Test 7A Name__________________________ MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. For the questions below, use the following partial class definitions: public class A1 { public int a; private int b; protected int c; } public class A2 extends A1 { protected int x; private int y; } public class A3 extends A2 { private int z; } 1) Which of the following is most true with respect to A1, A2 and A3? A) A1, A2 and A3 are all subclasses of the class A B) A3 is a superclass of A2 and A2 is a superclass of A1 C) A3 is a subclass of A2 and A2 is a subclass of A1 D) A2 and A3 are both subclasses of A1 E) A1 and A2 are both subclasses of A3 1) 2) Which of the following lists of instance data are accessible in class A2? A) x, y B) c, x, y C) a, b, x, y D) a, c, x, y E) a, b , c, x, y 2) 3) Which of the following lists of instance data are accessible in A3? A) z B) x, z C) x, y, z D) a, c, x, z E) a, b, c, x, y, z 3) 4) Which of the following is true regarding the use of instance variable y of class A1? A) It is accessible only in A2 B) It is accessible only in A3 C) It is accessible in A1 and A2 D) It is accessible in A2 and A3 E) It is not accessible to any of the three classes 4) 5) The instruction super( ); does which of the following? A) calls the constructor as defined in the current class B) calls the constructor as defined in the current class' parent class C) calls the method super as defined in the current class D) calls the method super as defined in the current class's parent class E) creates an instance of the current class's superclass 5) 6) Aside from permitting inheritance, the visibility modifier protected is also used to A) ensure that the class can not throw a NullPointerException B) permit access to the protected item by any static class C) define abstract elements of an interface D) permit access to the protected item by any parent class E) permit access to the protected item by any class defined in the same package 6) 7) Which of the following is an example of multiple inheritance? A) OSX and Windows PCs are both types of personal computers B) An Ultrabook is a thin laptop C) A cell phone can be a PC and mainframe D) A PC can be a desktop or a laptop E) A cell phone is both a computing device and a communication device 7) 1 8) If a programmer wants a class to be extended by another programmer, then this programmer must A) change the class to be public B) change public methods and instance data to be protected C) change appropriate private methods to be public D) change all methods to be either protected or private E) none of the above, the programmer does not have to change anything 8) For the next questions, consider the following complete class definition: public class Q9_10 { private int x; public Q9_10(int newValue) { x = newValue; } } 9) Which of the following is true about the class Q9_10? A) Its parent class is Java B) It can not be extended C) It has no parent class D) It has a default child called Object E) Its parent class is Object 10) If q1 and q2 are objects of Q9_10, then q1.equals(q2) A) is never true B) is true if q1 and q2 both store the same value of x C) throws a NullPointerException D) is true if the same Q9_10 object is referenced by q1 and q2 E) generates a syntax error, because equals() is not defined in the Q9_10 class 9) 10) TRUE/FALSE. Write 'T' if the statement is true and 'F' if the statement is false. 11) The public visibility modifier provides the best encapsulation that permits inheritance. 11) 12) The reserved word, extends, is used to denote a is-a relationship. 12) Consider the following class hierarchy and answer these questions. 13) A is a derived class of X. 13) 14) If A, B, Y and Z all contain the same instance data d, then d should be declared in X and inherited into Y, Z, A and B. 14) For the questions below, assume that Poodle is a derived class of Dog and that Dog d = new Dog(...) and Poodle p = new Poodle(...) where the ... are the necessary parameters for the two classes. 15) The assignment statement d = p; is legal even though d is not a Poodle. 15) 16) The assignment statement p = d; is legal even though p is not a Dog. 16) 2 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 17) If you instantiate an Abstract class, the class or object you wind up with A) is also an Abstract class B) is a reference to an Object C) is a normal class D) is an Interface E) is a figment of your imaginationyou cannot instantiate an Abstract class 17) For the questions below, consider the following class definition: public class AClass { public int x, y; public AClass(int a, int b) { x = a; y = b; } public int addEm( ) { return x + y; } public void changeEm( ) { x++; y--; } public String toString( ) { return x + ", " + y; } } 18) Consider that you want to extend AClass to BClass. BClass will have a third instance data, int z. Which of the following would best define BClass'constructor? 18) A) public BClass(int a, int b, int c) { super( ); } B) public BClass(int c) { z = c; } C) public BClass(int a, int b, int c) { x = a; y = b; z = c; } D) public BClass(int a, int b, int c) { super(a, b); z = c; } E) public BClass(int a, int b, int c) { super(a, b, c); } 19) You now want addEm to add all three values and return the sum, and changeEm to change x and y, but leave z alone. Which should you do? A) Redefine addEm to return the value of z + super.addEm( ), but leave changeEm alone B) Redefine changeEm to call super.changeEm( ) and then set z = x + y, but leave addEm alone C) Redefine addEm to return the value of z + super.addEm( ) and redefine changeEm to call super.changeEm( ) and then set z = x + y D) Redefine addEm and changeEm without referencing super.addEm( ) or super.changeEm( ) E) Redefine changeEm to call super.changeEm( ) without doing anything to z, and redefine addEm to return super.addEm( ) 3 19) 20) A variable declared to be of one class can later reference an extended class of that class. This variable is known as A) protected B) polymorphic C) superclass D) cloneable E) none of the above, a variable declared to be of one class can never reference any other type of class, even an extended class 20) 21) To determine the type that a polymorphic variable refers to, the decision is made A) by the programmer at the time the program is written B) by the user at run time C) by the operating system when the program is loaded into memory D) by the compiler at compile time E) by the Java run-time environment at run-time 21) For the questions below, assume that Student, Employee and Retired are all extended classes of Person, and all four classes have different implementations of the method getMoney (e.g. they may get different amounts of money). Consider the following code where ... are the required parameters for the constructors: Person p = new Person(...); int m1 = p.getMoney( ); p = new Student(...); int m2 = p.getMoney( ); if (m2 < 100000) p = new Employee(...); else if (m1 > 50000) p = new Retired(...); int m3 = p.getMoney( ); // assignment 1 // assignment 2 // assignment 3 22) The reference to getMoney( ) in assignment 3 is to the class A) Student B) Employee C) Person D) Retired E) unknown; it cannot be determined by examining the code 4 22) Answer Key Testname: AP_CS_TEST_7A 1) C 2) D 3) D 4) A 5) B 6) E 7) E 8) C 9) E 10) D 11) FALSE 12) TRUE 13) FALSE 14) TRUE 15) TRUE 16) FALSE 17) E 18) D 19) A 20) B 21) E 22) E 5