Unit 9 Test Practice

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

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.

Answer A Correct. At compile time, the correctness of a non-static method call is determined by the declared type of a variable. Since the variable sec is declared as data type First, and the First class does not contain the method output, a compile time error occurs. Declaring sec to be of type Second in line 1 will correct this error since the class Second does contain the method output.

Consider the following class definitions. public class Robot { private int servoCount; public int getServoCount() { return servoCount; } public void setServoCount(int in) { servoCount = in; } } public class Android extends Robot { private int servoCount; public Android(int initVal) { setServoCount(initVal); } public int getServoCount() { return super.getServoCount(); } public int getLocal() { return servoCount; } public void setServoCount(int in) { super.setServoCount(in); } public void setLocal(int in) { servoCount = in; } } The following code segment appears in a method in another class. int x = 10; int y = 20; /* missing code */ Which of the following code segments can be used to replace /* missing code */ so that the value 20 will be printed? A) Android a = new Android(x); a.setServoCount(y); System.out.println(a.getServoCount()); B) Android a = new Android(x); a.setServoCount(y); System.out.println(a.getLocal()); C) Android a = new Android(x); a.setLocal(y); System.out.println(a.getServoCount()); D) Android a = new Android(y); a.setServoCount(x); System.out.println(a.getLocal()); E) Android a = new Android(y); a.setLocal(x); System.out.println(a.getLocal());

Answer A Correct. The Android constructor calls setServoCount, which in turn calls super.setServoCount with the parameter passed to the constructor, which in this case is x. The statement a.setServoCount(y) also calls super.setServoCount with its parameter, which in this case is y, thereby setting the instance variable servoCount of the Robot superclass to the value of y. The value that is printed is returned from the call a.getServoCount(), which calls super.getServoCount(), which returns the value of the instance variable servoCount off the Robot superclass.

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);

Answer A Correct. The code segment creates a new BallCap object, assigning "Denver" to myTeam and using super(mySize) to assign "L" to the size variable inherited from the Hat class. The toString method in the BallCap class uses super.toString() to call the toString method in the Hat class, generating the string "Size L hat". When control flow resumes in the toString method in the BallCap class, " with Denver logo" is appended to the generated string and the result is returned.

Consider the following partial class definitions. public class Membership { private String id; public Membership(String input) { id = input; } // Rest of definition not shown } public class FamilyMembership extends Membership { private int numberInFamily = 2; public FamilyMembership(String input) { super(input); } public FamilyMembership(String input, int n) { super(input); numberInFamily = n; } // Rest of definition not shown } public class IndividualMembership extends Membership { public IndividualMembership(String input) { super(input); } // Rest of definition not shown } The following code segment occurs in a class other than Membership, FamilyMembership, or IndividualMembership. FamilyMembership m1 = new Membership("123"); // Line 1 Membership m2 = new IndividualMembership("456"); // Line 2 Membership m3 = new FamilyMembership("789"); // Line 3 FamilyMembership m4 = new FamilyMembership("987", 3); // Line 4 Membership m5 = new Membership("374"); // Line 5 Which of the following best explains why the code segment does not compile? A) In line 1, m1 cannot be declared as type FamilyMembership and instantiated as a Membership object. B) In line 2, m2 cannot be declared as type Membership and instantiated as an IndividualMembership object. C) In line 3, m3 cannot be declared as type Membership and instantiated as a FamilyMembership object. D) In line 4, m4 cannot be declared as type FamilyMembership and instantiated as a FamilyMembership object. E) In line 5, m5 cannot be declared as type Membership and instantiated as a Membership object.

Answer A Correct. The reference m1 is declared to be of type FamilyMembership. Membership is a superclass of FamilyMembership, so m1 cannot be instantiated as a Membership object. Only objects of type FamilyMembership (or its subclasses) can be assigned to a FamilyMembership reference.

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.

Answer B Correct. At run time, the method in the actual object type is executed for a non-static method call. Since the variable thing has an object type of Bclass, the code line thing.methodX(); will invoke the methodX method defined in the Bclass class. This uses the keyword super to invoke the methodX method of the Aclass superclass, which correctly displays the first part of the output, "Super X ", and then invokes the methodY method. Since the methodY method is overridden in the Bclass subclass, and thing has an object type of that subclass, the methodY in the subclass is invoked, which incorrectly displays the second part of the output, "Sub Y ". To access only the methods in the superclass, the variable thing must be instantiated as an object of type Aclass.

Consider the following class definitions. public class Game { private String name; public Game(String n) { name = n; } // Rest of definition not shown } public class BoardGame extends Game { public BoardGame(String n) { super(n); } // Rest of definition not shown } The following code segment appears in a class other than Game or BoardGame. Game g1 = new BoardGame("checkers"); BoardGame g2 = new Game("chess"); ArrayList<Game> My_Games = new ArrayList(); My_Games.add(g1); My_Games.add(g2); Which of the following best explains why the code segment does not compile? A) A BoardGame object cannot be assigned to the Game reference g1. B) A Game object cannot be assigned to the BoardGame reference g2. C) The My_Games object cannot contain elements of different types. D) The object referenced by g1 cannot be added to My_Games since g1 was instantiated by a call to the BoardGame constructor. E) The object referenced by g2 cannot be added to My_Games since g2 was declared to be of type BoardGame.

Answer B Correct. In the inheritance relationship between Game and BoardGame, Game is the superclass and BoardGame is the subclass. A reference of type Game can be used to refer to an object of type Game or BoardGame, but a reference of type BoardGame can only refer to an object of type BoardGame, and not to an object of type Game.

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.

Answer B Correct. In the statement, myDog uses the one-argument Poodle constructor, which makes an implicit call to the no-argument Dog constructor. This in turn sets name to "NoName". The one-argument Poodle constructor also sets the instance variable size to "toy".

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" ? when obj has been declared as type ParentClass when obj has been declared as type SubClass 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

Answer B Correct. Objects of type ParentClass print "round and round" when the wheelsOnTheBus method is called. The SubClass class overrides the wheelsOnTheBus method, causing objects of type SubClass to print "are flat" when the wheelsOnTheBus method is called. The SubSubClass class inherits the wheelsOnTheBus method from the ParentClass class, causing objects of type SubSubClass to print "round and round" when the wheelsOnTheBus method is called.

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

Answer B Correct. Statement I will print false because t1 and t2 refer to different objects. Statement II will print true. The equals method compares t1 (1 hour and 10 minutes) to t2 (70 minutes), evaluating to true. Statement III causes a compile-time error because the equals method must be called on an object and only takes one parameter.

Consider the following class definitions. public class Computer { private String memory; public Computer() { memory = "RAM"; } public Computer(String m) { memory = m; } public String getMemory() { return memory; } } public class Smartphone extends Computer { private double screenWidth, screenHeight; public SmartPhone(double w, double h) { super("flash"); screenWidth = w; screenHeight = h; } public double getScreenWidth() { return screenWidth; } public double getScreenHeight() { return screenHeight; } } The following code segment appears in a class other than Computer or Smartphone. Computer myPhone = new SmartPhone(2.55, 4.53); System.out.println("Device has memory: " + myPhone.getMemory() + ", screen area: " + myPhone.getScreenWidth() * myPhone.getScreenHeight() + " square inches."); The code segment is intended to produce the following output. Device has memory: flash, screen area: 11.5515 square inches. Which of the following best explains why the code segment does not work as intended? A) An error occurs during compilation because a Smartphone object cannot be assigned to the Computer reference variable myPhone. B) An error occurs during compilation because the Smartphone class has no getMemory method. C) An error occurs during compilation because the getScreenWidth and getScreenHeight methods are not defined for the Computer object myPhone. D) An error occurs at runtime because the Smartphone class has no getMemory method. E) An error occurs at runtime because the getScreenWidth and getScreenHeight methods are not defined for the Computer object myPhone.

Answer C Correct. During compilation, the declared reference type determines the correctness of a method call. The declared type of myPhone is Computer and an error occurs during compilation because the Computer class does not contain the getScreenWidth and getScreenHeight methods.

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 ? A) s1.equals(s2) B) s2.equals(s3) C) s3.equals(s4) D) s4.equals(s5) E) s5.equals(s1)

Answer C Correct. In the given code segment, var1 for s3 is equal to var1 for s4, and var1 for s3 is equal to the length of var2 for s3, and the length of var2 for s3 is equal to the length of var2 for s4. Therefore, according to the equals method for the Silly class, s3.equals(s4) returns true.

Consider the following class definitions. public class Road { private String roadName; public Road(String name) { roadName = name; } } public class Highway extends Road { private int speedLimit; public Highway(String name, int limit) { super(name); speedLimit = limit; } } The following code segment appears in a method in another class. Road r1 = new Highway("Interstate 101", 55); // line 1 Road r2 = new Road("Elm Street"); // line 2 Highway r3 = new Road("Sullivan Street"); // line 3 Highway r4 = new Highway("New Jersey Turnpike", 65); // line 4 Which of the following best explains the error, if any, in the code segment? A) Line 1 will cause an error because a Road variable cannot be instantiated as an object of type Highway. B) Line 2 will cause an error because the Road constructor is not properly called. C) Line 3 will cause an error because a Highway variable cannot be instantiated as an object of type Road. D) Line 4 will cause an error because the Highway constructor is not properly called. E) The code segment compiles and runs without error.

Answer C Correct. Object r3 is declared as type Highway, so it must be instantiated as type Highway (or as a subclass of Highway)

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);

Answer C Correct. The ClosedRange constructor calls the Range constructor through the use of super(low), which sets the instance variable lowValue to low. Then the ClosedRange constructor sets the instance variable highValue to high. The print statement invokes the toString method of the ClosedRange class, which concatenates the string returned from the toString method of the Range class to another string and the instance variable highValue to produce the required output.

public class Book { private String bookTitle; public Book() { bookTitle = ""; } public Book(String title) { bookTitle = title; } } public class TextBook extends Book { private String subject; public TextBook(String theSubject) { subject = theSubject; } } The following code segment appears in a method in a class other than Book or TextBook. Book b = new TextBook("Psychology"); Which of the following best describes the effect of executing the code segment? A The TextBook constructor initializes the instance variable subject with the value of the parameter theSubject, and then invokes the zero-parameter Book constructor, which initializes the instance variable bookTitle to "". B The TextBook constructor initializes the instance variable subject with the value of the parameter theSubject, and then invokes the one-parameter Book constructor with theSubject as the parameter, which initializes the instance variable bookTitle to the value of the parameter theSubject. C There is an implicit call to the zero-parameter Book constructor. The instance variable bookTitle is then initialized to "". Then, the instance variable subject is initialized with the value of the parameter theSubject. D The code segment will not execute because the TextBook constructor does not contain an explicit call to one of the Book constructors. E The code segment will not execute because the TextBook constructor does not have a parameter for the title of the book.

Answer C Correct. The call new TextBook("Psychology") invokes the TextBook constructor with a value of "Psychology" for the parameter theSubject. The TextBook constructor implicitly invokes the zero-parameter Book constructor. The Book constructor then initializes the instance variable bookTitle to "". Finally, the TextBook constructor initializes the instance variable subject to the value passed in the parameter theSubject

Consider the following class definitions. public class Drink { // implementation not shown } public class Coffee extends Drink { // There may be instance variables and constructors that are not shown. // No methods are defined for this class. } The following code segment appears in a method in a class other than Drink or Coffee. Coffee myCup = new Coffee(); myCup.setSize("large"); Which of the following must be true so that the code segment will compile without error? A) The Drink class must have a public method named getSize that takes a String value as its parameter. B) The Drink class must have a public method named getSize that takes no parameters. C) The Drink class must have a public method named setSize that takes a String value as its parameter. D) The Drink class must have a public method named setSize that takes no parameters. E) The Drink class must have a String instance variable named size.

Answer C Correct. The statement myCup.setSize("large") represents a call to a method named setSize with a single String parameter. The method must be found in either the Coffee class or the Drink class. The Coffee class definition specifies that no methods are defined, so there must be a setSize method with a single String parameter inherited from the Drink class.

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.

Answer C Correct. In line 1, arr is declared to be an array of Thing objects. However, when it is instantiated in line 1, it is instantiated as an array of 3 MoreThing objects. Therefore, in line 5, t1, which is a Thing object, cannot be stored as an object of type MoreThing. This will cause an exception to be thrown when the code is executed.

Consider the following two class definitions. public class Bike { private int numOfWheels = 2; public int getNumOfWheels() { return numOfWheels; } } public class EBike extends Bike { private int numOfWatts; public EBike(int watts) { numOfWatts = watts; } public int getNumOfWatts() { return numOfWatts; } } The following code segment occurs in a class other than Bike or EBike. Bike b = new EBike(250); System.out.println(b.getNumOfWatts()); System.out.println(b.getNumOfWheels()); Which of the following best explains why the code segment does not compile? A) The Bike superclass does not have a constructor. B) There are too many arguments to the EBike constructor call in the code segment. C) The first line of the subclass constructor is not a call to the superclass constructor. D) The getNumOfWatts method is not found in the Bike class. E) The getNumOfWheels method is not found in the EBike class.

Answer D Correct. At compile time, the declared type of b determines where the compiler looks for methods during method calls. In this case, b is declared to be of type Bike, but the getNumOfWatts method does not appear in the Bike class.

Consider the following class definitions. public class C1 { public C1() { /* implementation not shown */ } public void m1() { System.out.print("A"); } public void m2() { System.out.print("B"); } } public class C2 extends C1 { public C2() { /* implementation not shown */ } public void m2() { System.out.print("C"); } } The following code segment appears in a class other than C1 or C2. C1 obj1 = new C2(); obj1.m1(); obj1.m2(); The code segment is intended to produce the output AB. Which of the following best explains why the code segment does not produce the intended output? A) A compile-time error occurs because obj1 is declared as type C1 but instantiated as type C2. B) A runtime error occurs because method m1 does not appear in C2. C) Method m1 is not executed because it does not appear in C2. D) Method m2 is executed from the subclass instead of the superclass because obj1 is instantiated as a C2 object. E) Method m2 is executed twice (once in the subclass and once in the superclass) because it appears in both classes.

Answer D Correct. At runtime, the method in the class containing the constructor called to instantiate the object is executed. In this case, obj1 is instantiated by a call to the C2 constructor. The method m1 is not found in C2 so the superclass method m1 is executed, printing "A". The method m2 is found in C2 so it is executed, printing "C".

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" ? When alice has been declared as type Person When alice has been declared as type EvilPerson 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

Answer D Correct. Objects of type Person print "Hahaha" when the laugh method is called. The EvilPerson class overrides Person's laugh method, causing objects of type EvilPerson to print "Mwahahaha" when the laugh method is called. The Henchman class inherits EvilPerson's laugh method, causing objects of type Henchman to print "Mwahahaha" when the laugh method is called.

Consider the following class definitions. public class Bird { private int beakStrength; public Bird(int input) { beakStrength = input; } public void setBeakStrength(int strength) { beakStrength = strength; } } public class Hawk extends Bird { private int talonStrength; public Hawk(int talon, int beak) { super(beak); talonStrength = talon; } } The following statement appears in a method in another class. Bird b = new Hawk(5, 8); Which of the following best describes the effect of executing the statement? A) The Bird variable b is instantiated as a Hawk. The instance variable talonStrength is initialized with the value from the parameter talon. The Hawk constructor cannot set the instance variable beakStrength because a subclass does not have access to a private variable in its superclass. B) The Bird variable b is instantiated as a Hawk. The call super(beak) returns a value from the instance variable beakStrength in the superclass and makes it accessible in the subclass. The instance variable talonStrength is then initialized with the value from the parameter talon. C) The Bird variable b is instantiated as a Hawk. The instance variable talonStrength is initialized with the value from the parameter talon. No other initializations are made to any instance variables. D) The Bird variable b is instantiated as a Hawk. The call super(beak) invokes the Bird constructor and initializes the instance variable beakStrength with the value from the parameter beak. The instance variable talonStrength is then initialized with the value from the parameter talon. E) The code segment will not execute because the Bird variable b cannot be instantiated as a Hawk.

Answer D Correct. The call new Hawk(5, 8) invokes the Hawk constructor with a value of 5 for talon and a value of 8 for beak. The Hawk constructor invokes the Bird constructor by calling super(beak), which sets the instance variable beakStrength to the value passed in the parameter. The Hawk constructor then sets the instance variable talonStrength to the value passed in the parameter talon.

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" ? 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();

Answer D Correct. The variable d is instantiated as a Grandchild object. When the call d.first() is invoked, it calls super.first(), which invokes the first method of the Child class, which in turn calls super.first(), invoking the first method of the Parent class. This method prints "P" and then calls second. Because the object that invoked the original call to first is of type Grandchild, the second method of the Grandchild class is invoked. Through a similar series of calls to super.second(), the second method of the Parent class prints "Q", then the second method of the Child class prints "R", and finally the second method of the Grandchild class prints "T". When these calls have completed execution, control returns to the first method of the Grandchild class and it completes execution by printing "S".

Consider the following class definitions. public class Book { private String author; private String title; public Book(String the_author, String the_title) { author = the_author; title = the_title; } } public class Textbook extends Book { private String subject; public Textbook(String the_author, String the_title, String the_subject) { /* missing implementation */ } } Which of the following can be used to replace /* missing implementation */ so that the Textbook constructor compiles without error? A) author = the_author; title = the_title; subject = the_subject; B) super(the_author, the_title); super(the_subject); C) subject = the_subject; super(the_author, the_title); D) super(the_author, the_title); subject = the_subject; E) super(the_author, the_title, the_subject);

Answer D Correct. When the constructor of a subclass is invoked, the first thing it must do is invoke a constructor of its immediate superclass using the keyword super. In this case, the Book class has only one constructor defined and it takes two parameters, so the call super(author, title) is correct. The Textbook constructor must also set the value of the instance variable subject, which it does using the statement subject = the_subject;.

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));

Answer D Correct. The constructor is called correctly and creates an object of type Motorized. The first parameter sets the numOfWheels instance variable to 4, and the second parameter sets the speed instance variable to 55. When the parameter to the println method is an object, the toString method is invoked to display the object. Since the object type is Motorized, the toString method in that class is invoked. It uses the superclass toString method to display the information about the number of wheels. Based on the value of speed, 55, the string "Fast" is displayed.

onsider 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

Answer E Correct. Classes Duck, Chicken, and Rooster are all subclasses of Bird. Class Duck overrides Bird's sing method. Class Chicken inherits Bird's sing method. Class Rooster overrides Chicken's sing method, which is inherited from Bird.

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? 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.

Answer E Correct. The ArrayList contains objects of type MultiTool. Even though objects of type DeluxeMultiTool can be added to the ArrayList, they can only access methods that are known to the MultiTool class.

Consider the following class definitions. public class Pet { public void speak() { System.out.print("pet sound"); } } public class Dog extends Pet { public void bark() { System.out.print("woof woof"); } public void speak() { bark(); } } public class Cat extends Pet { public void speak() { System.out.print("meow meow"); } } The following statement appears in a method in another class. myPet.speak(); Under which of the following conditions will the statement compile and run without error? When myPet is an object of type Pet When myPet is an object of type Dog When myPet is an object of type Cat A) I only B) I and II only C) I and III only D) II and III only E) I, II, and III

Answer E Correct. The Pet class implements the speak method. Classes Dog and Cat are both subclasses of Pet and both override the speak method of the Pet class. The Dog class implements the speak method by calling the bark method, which is defined in the Dog class.

Consider the following class definitions. public class Artifact { private String title; private int year; public Artifact(String t, int y) { title = t; year = y; } public void printInfo() { System.out.print(title + " (" + year + ")"); } } public class Artwork extends Artifact { private String artist; public Artwork(String t, int y, String a) { super(t, y); artist = a; } public void printInfo() { /* missing implementation */ } } The following code segment appears in a method in another class. Artwork starry = new Artwork("The Starry Night", 1889, "Van Gogh"); starry.printInfo(); The code segment is intended to produce the following output. The Starry Night (1889) by Van Gogh Which of the following can be used to replace /* missing implementation */ in the printInfo method in the Artwork class so that the code segment produces the intended output? A) System.out.print(title + " (" + year + ") by " + artist); B) super.printInfo(artist); C) System.out.print(super.printInfo() + " by " + artist); D) super(); System.out.print(" by " + artist); E) super.printInfo(); System.out.print(" by " + artist);

Answer E Correct. The call super.printInfo() calls the printInfo method in the parent class, printing "The Starry Night (1889)". The call System.out.print(" by " + artist) prints " by Van Gogh".

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 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".


Set pelajaran terkait

Organisational structure ( A level)

View Set

ADV 436 Promotions & Sponsorships

View Set

Longitude and Latitude and Important Lines on the Globe

View Set

A&P Chapter 4- Skins and Body Membranes

View Set

Honan-Chapter 10: Nursing Management: Patients With Chest and Lower Respiratory Tract Disorders

View Set