AP Computer Science A Unit 9 Progress Check: MCQ

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following class declarations. public class Hat { private String size; public Hat(String s) { size = s; } public String toString() { return "Size " + size + " hat"; } } public class BallCap extends Hat { private String team; public BallCap(String mySize, String myTeam) { super(mySize); team = myTeam; } public String toString() { return super.toString() + " with " + team + " logo"; } } A code segment located in a different class is intended to produce the following output. Size L hat with Denver logo Which of the following code segments will produce this output? A BallCap myHat = new BallCap("L", "Denver"); System.out.println(myHat); B BallCap myHat = new BallCap("Denver", "L"); System.out.println(myHat); C BallCap myHat = new BallCap("L"); myHat.team = "Denver"; System.out.println(myHat); D Hat myHat = new Hat("L", "Denver"); System.out.println(myHat); E Hat myHat = new Hat("L"); myHat.team = "Denver"; System.out.println(myHat);

A. BallCap myHat = new BallCap("L", "Denver"); System.out.println(myHat);

Consider the following class definitions. public class First { public void output1() { output2(); } public void output2() { output3(); } public void output3() { System.out.print("First"); } } public class Second extends First { public void output() { output1(); output2(); output3(); } } public class Third extends Second { public void output3() { System.out.print("Third"); } } The following code segment appears in a class other than First, Second, or Third. First sec = new Second(); // Line 1 Second thr = new Third(); // Line 2 sec.output(); // Line 3 thr.output(); // Line 4 Which of the following best explains why the code segment will not compile? A Line 3 causes a compile-time error because the variable sec should be declared as type Second. B Line 4 causes a compile-time error because the variable thr should be declared as type Third. C Line 3 causes a compile-time error because the Second class is missing the output1 method. D Line 3 causes a compile-time error because the Second class is missing the output2 method. E Line 4 causes a compile-time error because the Third class is missing the output method.

A. Line 3 causes a compile-time error because the variable sec should be declared as type Second.

Consider the following class declarations. public class ParentClass { public void wheelsOnTheBus() { System.out.println("round and round"); } } public class SubClass extends ParentClass { public void wheelsOnTheBus() { System.out.println("are flat"); } } public class SubSubClass extends ParentClass { public void wheelsOnTheBus() { // No methods defined } } The following code segment appears in a method in another class. obj.wheelsOnTheBus(); Under which of the following conditions will the code segment print "are flat" ? I. when obj has been declared as type ParentClass II. when obj has been declared as type SubClass III. when obj has been declared as type SubSubClass A I only B II only C I and II only D II and III only E I, II, and III

B. II only

Consider the following class definition. public class Time { private int hours; private int minutes; public Time(int h, int m) { hours = h; minutes = m; } public boolean equals(Object other) { if (other == null) { return false; } Time t = (Time) other; return (hours * 60 + minutes == t.hours * 60 + t.minutes); } } The following code segment appears in a class other than Time. Time t1 = new Time(1, 10); Time t2 = new Time(0, 70); Which of the following statements will print true ? System.out.println(t1 == t2); System.out.println(t1.equals(t2)); System.out.println(equals(t1, t2); A I only B II only C III only D I and II E I and III

B. II only

Consider the following class declarations. public class Tree { private String treeVariety; public Tree() { treeVariety = "Oak"; } public Tree(String variety) { treeVariety = variety; } } public class DeciduousTree extends Tree { public DeciduousTree(String variety) { super(); } } public class EvergreenTree extends Tree { public EvergreenTree(String variety) { super(variety); } } The following code segment appears in a method in another class. DeciduousTree tree1 = new DeciduousTree("Maple"); EvergreenTree tree2 = new EvergreenTree("Fir"); Which of the following best describes the result of executing the code segment? A Object tree1 is created using the DeciduousTree constructor, which uses super to set tree1's treeVariety attribute to "Maple". Object tree2 is created using the EvergreenTree constructor, which uses super to set tree2's treeVariety attribute to "Fir". B Object tree1 is created using the DeciduousTree constructor, which uses super to set tree1's treeVariety attribute to "Oak". Object tree2 is created using the EvergreenTree constructor, which uses super to set tree2's treeVariety attribute to "Fir". C Object tree1 is created using the DeciduousTree constructor, which uses super to set tree1's treeVariety attribute to "Oak". Object tree2 is created using the EvergreenTree constructor, which uses super to set tree2's treeVariety attribute to "Oak". D The code segment does not compile because the DeciduousTree and EvergreenTree constructors should not take a parameter. E The code segment does not compile because the DeciduousTree and EvergreenTree constructors do not correctly call a Tree constructor.

B. Object tree1 is created using the DeciduousTree constructor, which uses super to set tree1's treeVariety attribute to "Oak". Object tree2 is created using the EvergreenTree constructor, which uses super to set tree2's treeVariety attribute to "Fir".

Consider the following class declarations. public class Dog { private String name; public Dog() { name = "NoName"; } } public class Poodle extends Dog { private String size; public Poodle(String s) { size = s; } } The following statement appears in a method in another class. Poodle myDog = new Poodle("toy"); Which of the following best describes the result of executing the statement? A The Poodle variable myDog is instantiated as a Poodle. The instance variable size is initialized to "toy". The instance variable name is not assiged a value. B The Poodle variable myDog is instantiated as a Poodle. The instance variable size is initialized to "toy". An implicit call to the no-argument Dog constructor is made, initializing the instance variable name to "NoName". C The Poodle variable myDog is instantiated as a Poodle. The instance variable size is initialized to "toy". An implicit call to the no-argument Dog constructor is made, initializing the instance variable name to "toy". D A runtime error occurs because super is not used to call the no-argument Dog constructor. E A runtime error occurs because there is no one-argument Dog constructor.

B. The Poodle variable myDog is instantiated as a Poodle. The instance variable size is initialized to "toy". An implicit call to the no-argument Dog constructor is made, initializing the instance variable name to "NoName".

Consider the following class definitions. public class Aclass { public void methodX() { System.out.print("Super X "); methodY(); } public void methodY() { System.out.print("Super Y "); methodZ(); } public void methodZ() ( System.out.print("Super Z"); } } public class Bclass extends Aclass { public void methodX() { super.methodX(); } public void methodY() { System.out.print("Sub Y "); methodZ(); } } The following code segment appears in a class other than Aclass or Bclass. Aclass thing = new Bclass(); thing.methodX(); The code segment is intended to display the following. Super X Super Y Super Z Which of the following best explains why the code segment does not work as intended? A The variable thing should be declared as a Bclass data type because thing is instantiated as a Bclass object. B The variable thing should be instantiated as an Aclass object because methodY is overridden in Bclass. C The method methodX should be removed from the Aclass definition because methodX is overridden in Bclass. D The method methodY should be removed from the Aclass definition because methodY is overridden in Bclass. E The method methodZ should be overridden in the Bclass definition because methodZ appears only in Aclass.

B. The variable thing should be instantiated as an Aclass object because methodY is overridden in Bclass.

Consider the following class declarations. public class Range { private int lowValue; public Range(int low) { lowValue = low; } public String toString() { return "This range starts with " + lowValue; } } public class ClosedRange extends Range { private int highValue; public ClosedRange(int low, int high) { super(low); highValue = high; } public String toString() { return super.toString() + " and ends with " + highValue; } } A code segment appearing in a method in another class is intended to produce the following output. This range starts with 1 and ends with 10 Which of the following code segments will produce this output? A Range r1 = new Range(1); System.out.println(r1); B Range r2 = new Range(1, 10); System.out.println(r2); C ClosedRange r3 = new ClosedRange(1, 10); System.out.println(r3); D ClosedRange r4 = new ClosedRange(10, 1); System.out.println(r4); E ClosedRange r5 = new ClosedRange(10); System.out.println(r5);

C. ClosedRange r3 = new ClosedRange(1, 10); System.out.println(r3);

Consider the following class definitions. public class Thing { /* implementation not shown */ } public class MoreThing extends Thing { /* implementation not shown */ } The following code segment appears in a class other than Thing or MoreThing. Thing[] arr = new MoreThing[3]; // line 1 Thing t1 = new Thing(); Thing t2 = new MoreThing(); // line 3 MoreThing t3 = new MoreThing(); arr[0] = t1; // line 5 arr[1] = t2; // line 6 arr[2] = t3; // line 7 Which of the following best explains the error in the code segment? A Line 1 will cause an error because the type used to declare arr and the type used to instantiate arr are different. B Line 3 will cause an error because the type used to declare t2 and the type used to instantiate t2 are different. C Line 5 will cause an error because the types of arr[0] and t1 are different. D Line 6 will cause an error because the types of arr[1] and t2 are different. E Line 7 will cause an error because the types of arr[2] and t3 are different.

C. Line 5 will cause an error because the types of arr[0] and t1 are different.

Consider the following class declarations. public class Publication { private String title; public Publication() { title = "Generic"; } public Publication(String t) { title = t; } } public class Book extends Publication { public Book() { super(); } public Book(String t) { super(t); } } The following code segment appears in a method in another class. Book myBook = new Book("Adventure Story"); // Line 1 Book yourBook = new Book(); // Line 2 Which of the following best describes the result of executing the code segment? A Object myBook is created using the one-argument Book constructor, which uses super to set myBook's title attribute to "Adventure Story". Object yourBook is created using the Book constructor, which uses super to set yourBook's title attribute to an empty string. B Object myBook is created using the no-argument Book constructor, which uses super to set myBook's title attribute to "Generic". Object yourBook is created using super to call to the Publication no-argument constructor to set yourBook's title attribute to "Generic". C Object myBook is created using the one-argument Book constructor, which uses super to set myBook's title attribute to "Adventure Story". Object yourBook is created using super to call to the Publication no-argument constructor to set yourBook's title attribute to "Generic". D A runtime error occurs in line 1 because the one-argument Publication constructor cannot be called from the one-argument Book constructor. E A runtime error occurs in line 2 because the no-argument Publication constructor cannot be called from the no-argument Book constructor.

C. Object myBook is created using the one-argument Book constructor, which uses super to set myBook's title attribute to "Adventure Story". Object yourBook is created using super to call to the Publication no-argument constructor to set yourBook's title attribute to "Generic".

Consider the following class definition. public class Silly { private int var1; private String var2; public Silly(int v1, String v2) { var1 = v1; var2 = v2; } public boolean equals(Object other) { if (other == null) { return false; } Silly s = (Silly) other; return (var1 == s.var1 && var1 == var2.length() && var2.length() == s.var2.length()); } } The following code segment appears in a class other than Silly. Silly s1 = new Silly(3, "abcd"); Silly s2 = new Silly(3, "abcd"); Silly s3 = new Silly(5, "vwxyz"); Silly s4 = new Silly(5, "aaaaa"); Silly s5 = new Silly(5, "efg"); Which of the following Boolean expressions will evaluate to true ? Which of the following Boolean expressions will evaluate to true ? A s1.equals(s2) B s2.equals(s3) C s3.equals(s4) D s4.equals(s5) E s5.equals(s1)

C. s3.equals(s4)

Consider the following class declarations. public class Parent { public void first() { System.out.print("P"); second(); } public void second() { System.out.print("Q"); } } public class Child extends Parent { public void first() { super.first(); } public void second() { super.second(); System.out.print("R"); } } public class Grandchild extends Child { public void first() { super.first(); System.out.print("S"); } public void second() { super.second(); System.out.print("T"); } } Which of the following code segments, if located in another class, will produce the output "PQRTS" ? Which of the following code segments, if located in another class, will produce the output "PQRTS" ? A Parent a = new Parent(); a.first(); B Child b = new Child(); b.first(); C Child c = new Child(); c.second(); D Grandchild d = new Grandchild(); d.first(); E Grandchild e = new Grandchild(); e.second();

D. Grandchild d = new Grandchild(); d.first();

Consider the following class declarations. public class Person { public void laugh() { System.out.print("Hahaha"); } } public class EvilPerson extends Person { public void laugh() { System.out.print("Mwahahaha"); } } public class Henchman extends EvilPerson { // No methods defined } The following code segment appears in a method in another class. alice.laugh(); Under which of the following conditions will the code segment print "Mwahahaha" ? I. When alice has been declared as type Person II. When alice has been declared as type EvilPerson III. When alice has been declared as type Henchman A II only B I and II only C I and III only D II and III only E I, II, and III

D. II and III only

Consider the following class definitions. public class Appliance { private int id; private String brand; public Appliance(int aId, String aBrand) { /* implementation not shown */ } public String display() { /* implementation not shown */ } } public class Refrigerator extends Appliance { private int numOfDoors; public Refrigerator(int rId, String rBrand, int rNumOfDoors) { /* implementation not shown */ } } The following code segment appears in a class other than Appliance or Refrigerator. public static void displayFeatures(Refrigerator r) { System.out.println(r.display()); // Line 3 } Appliance a1 = new Refrigerator(456, "AllBrand", 2); // Line 6 Refrigerator a2 = new Refrigerator(789, "Xtreme", 3); // Line 7 displayFeatures(a1); // Line 8 displayFeatures(a2); // Line 9 Which of the following best explains why the code segment will not compile? A Line 3 causes a compile-time error because the Refrigerator class is missing the display() method. B Line 6 causes a compile-time error because the variable a1 is incorrectly instantiated. C Line 7 causes a compile-time error because the variable a2 is incorrectly instantiated. D Line 8 causes a compile-time error because the parameter a1 in the call displayFeatures(a1) has the incorrect data type. E Line 9 causes a compile-time error because the parameter a2 in the call displayFeatures(a2) has the incorrect data type.

D. Line 8 causes a compile-time error because the parameter a1 in the call displayFeatures(a1) has the incorrect data type.

Consider the following class definitions. public class Person { private String firstName; private String lastName; public Person(String pFirstName, String pLastName) { firstName = pFirstName; lastName = pLastName; } public void personInfo() { System.out.println("My name is " + firstName + " " + lastName); } } pubic class Teacher extends Person { private String school; private String subject; public Teacher(String tFN, String tLN, String tSchool, String tSubject) { super(tFN, tLN); school = tSchool; subject = tSubject; } public void teacherInfo() { personInfo(); System.out.println("I teach " + subject + " at " + school); } } The following code segment appears in a class other than Person or Teacher. Person teach = new Teacher("Henry", "Lowe", "PS 150", "English"); teach.teacherInfo(); Which of the following best explains why the code segment will not compile? A The call to personInfo(); in the teacherInfo method should be this.personInfo(); because personInfo is a method in the Person class. B The personInfo method should be moved to the Teacher class because personInfo is a method only in the Person class. C The teacherInfo method should be moved to the Person class because teacherInfo is a method in the Teacher class. D The variable teach should be declared as a Teacher data type because teacherInfo is a method in the Teacher class. E The variable teach should be instantiated as a Person object because teach has been declared as type Person.

D. The variable teach should be declared as a Teacher data type because teacherInfo is a method in the Teacher class.

Consider the following class definitions. public class Vehicle { private int numOfWheels; public Vehicle(int nNumOfWheels) { numOfWheels = nNumOfWheels; } public String toString() { return "Number of Wheels: " + numOfWheels; } } public class Motorized extends Vehicle { private int maxSpeed; public Motorized(int nNumOfWheels, nMaxSpeed) { super(nNumOfWheels); maxSpeed = nMaxSpeed; } public String toString() { String s = super.toString() + " Max Speed: "; if (maxSpeed <= 10) { s += "Slow"; } else if (maxSpeed > 10 && maxSpeed <= 100) { s += "Fast"; } else { s += "Super Speedy"; } return s; } } Which of the following code segments, when executed in a class other than Vehicle or Motorized, will display Number of Wheels: 4 Max Speed: Fast ? A Vehicle obj = new Vehicle(55); System.out.println(obj); B Vehicle obj = new Vehicle(55); System.out.println(toString(obj)); C Motorized obj = new Motorized(55); System.out.println(obj); D Vehicle obj = new Motorized(4, 55); System.out.println(obj); E Motorized obj = new Motorized(4, 55); System.out.println(toString(obj));

D. Vehicle obj = new Motorized(4, 55); System.out.println(obj);

Consider the following classes. public class Bird { public void sing() { System.out.println("Cheep"); } } public class Duck extends Bird { public void sing() { System.out.println("Quack"); } } public class Chicken extends Bird { // No methods defined } public class Rooster extends Chicken { public void sing() { System.out.println("Cockadoodle doo"); } } The following statement appears in a method in another class. someBird.sing(); Under which of the following conditions will the statement compile and run without error? When someBird has been declared as type Duck When someBird has been declared as type Chicken When someBird has been declared as type Rooster A I only B III only C I and II only D I and III only E I, II, and III

E. I, II, and III

Consider the following class declarations. public class MultiTool { private int blade; private int screwdriver; public MultiTool(int b, int s) { blade = b; screwdriver = s; } } public class DeluxeMultiTool extends MultiTool { private boolean compass; public DeluxeMultiTool(int b, int s, boolean c) { super(b, s); compass = c; } public String getCompass() { return compass + ""; } } The following code segment appears in a method in another class. ArrayList<MultiTool> toolList = new ArrayList<MultiTool>(); MultiTool tool1 = new DeluxeMultiTool(4, 2, false); // Line 2 DeluxeMultiTool tool2 = new DeluxeMultiTool(3, 1, true); // Line 3 toolList.add(tool1); // Line 4 toolList.add(tool2); // Line 5 for (MultiTool tool : toolList) { System.out.println(tool.getCompass()); // Line 8 } The code segment does not compile. Which of the following best explains the cause of the error? The code segment does not compile. Which of the following best explains the cause of the error? A Line 2 causes a compile-time error because the variable tool1 is declared as type MultiTool but is instantiated as a DeluxeMultiTool. B Line 3 causes a compile-time error because the variable tool2 is declared as type MultiTool and is instantiated as a DeluxeMultiTool. C In line 4, tool2 cannot be added to the ArrayList because it was instantiated as a DeluxeMultiTool. D In line 5, tool2 cannot be added to the ArrayList because it was declared to be of type DeluxeMultiTool. E Line 8 causes a compile-time error because the getCompass method is not defined for objects of type MultiTool.

E. Line 8 causes a compile-time error because the getCompass method is not defined for objects of type MultiTool.


Conjuntos de estudio relacionados

Interpersonal Communication Final Exam

View Set

Vocabulary workshop level c unit 4 ** antonyms

View Set

Bio 206 Ch 35: Water and Sugar Transport in Plants

View Set

Empathy & Patient Communication - Comm&Ethics - Exam 2

View Set

Texas Property & Casualty Insurance

View Set