AP Computer Science Unit 9

Ace your homework & exams now with Quizwiz!

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.

B A Game object cannot be assigned to the BoardGame reference g2.

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

E I, II, and III

Consider the following class definitions. public class Bike { private int numWheels = 2; // No constructor defined } public class EBike extends Bike { private int numBatteries; public EBike(int batteries) { numBatteries = batteries; } } The following code segment appears in a method in a class other than Bike or EBike. EBike eB = new EBike(4); Which of the following best describes the effect of executing the code segment? A An implicit call to the zero-parameter Bike constructor initializes the instance variable numWheels. The instance variable numBatteries is initialized using the value of the parameter batteries. B An implicit call to the one-parameter Bike constructor with the parameter passed to the EBike constructor initializes the instance variable numWheels. The instance variable numBatteries is initialized using the value of the parameter batteries. C Because super is not explicitly called from the EBike constructor, the instance variable numWheels is not initialized. The instance variable numBatteries is initialized using the value of the parameter batteries. D The code segment will not execute because the Bike class is a superclass and must have a constructor. E The code segment will not execute because the constructor of the EBike class is missing a second parameter to use to initialize the numWheels instance variable.

A An implicit call to the zero-parameter Bike constructor initializes the instance variable numWheels. The instance variable numBatteries is initialized using the value of the parameter batteries.

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

A Android a = new Android(x); a.setServoCount(y); System.out.println(a.getServoCount());

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.

A In line 1, m1 cannot be declared as type FamilyMembership and instantiated as a Membership object.

Consider the following class definition. public class Document { private int pageCount; private int chapterCount; public Document(int p, int c) { pageCount = p; chapterCount = c; } public String toString() { return pageCount + " " + chapterCount; } } The following code segment, which is intended to print the page and chapter counts of a Document object, appears in a class other than Document. Document d = new Document(245, 16); System.out.println( /* missing code */ ); Which of the following can be used as a replacement for /* missing code */ so the code segment works as intended? A d.toString() B toString(d) C d.pageCount + " " + d.chapterCount D d.getPageCount() + " " + d.getChapterCount() E Document.pageCount + " " + Document.chapterCount

A d.toString()

Consider the following class definition. public class Backyard { private int length; private int width; public Backyard(int l, int w) { length = l; width = w; } public int getLength() { return length; } public int getWidth() { return width; } public boolean equals(Object other) { if (other == null) { return false; } Backyard b = (Backyard) object; return (length == b.getLength() && width == b.getWidth()); } } The following code segment appears in a class other than Backyard. It is intended to print true if b1 and b2 have the same lengths and widths, and to print false otherwise. Assume that x, y, j, and k are properly declared and initialized variables of type int. Backyard b1 = new Backyard(x, y); Backyard b2 = new Backyard(j, k); System.out.println( /* missing code */ ); Which of the following can be used as a replacement for /* missing code */ so the code segment works as intended? A b1 == b2 B b1.equals(b2) C equals(b1, b2) D b1.equals(b2.getLength(), b2.getWidth()) E b1.length == b2.length && b1.width == b2.width

B b1.equals(b2)

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

C An error occurs during compilation because the getScreenWidth and getScreenHeight methods are not defined for the Computer object m

Consider the following class definitions. public class Apple { public void printColor() { System.out.print("Red"); } } public class GrannySmith extends Apple { public void printColor() { System.out.print("Green"); } } public class Jonagold extends Apple { // no methods defined } The following statement appears in a method in another class. someApple.printColor(); Under which of the following conditions will the statement print "Red" ? When someApple is an object of type Apple When someApple is an object of type GrannySmith When someApple is an object of type Jonagold A I only B I and II only C I and III only D II and III only E I, II, and III

C I and III only

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.

C Line 3 will cause an error because a Highway variable cannot be instantiated as an object of type Road.

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.

C The Drink class must have a public method named setSize that takes a String value as its parameter.

Consider the following class definitions. 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.

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.

Consider the following class definition. public class Beverage { private int temperature; public Beverage(int t) { temperature = t; } public int getTemperature() { return temperature; } public boolean equals(Object other) { if (other == null) { return false; } Beverage b = (Beverage) other; return (b.getTemperature() == temperature); } } The following code segment appears in a class other than Beverage. Assume that x and y are properly declared and initialized int variables. Beverage hotChocolate = new Beverage(x); Beverage coffee = new Beverage(y); boolean same = /* missing code */; Which of the following can be used as a replacement for /* missing code */ so that the boolean variable same is set to true if and only if the hotChocolate and coffee objects have the same temperature values? A (hotChocolate = coffee) B (hotChocolate == coffee) C hotChocolate.equals(coffee) D hotChocolate.equals(coffee.getTemperature()) E hotChocolate.getTemperature().equals(coffee.getTemperature())

C hotChocolate.equals(coffee)

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.

D Method m2 is executed from the subclass instead of the superclass because obj1 is instantiated as a C2 object.

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.

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.

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.

D The getNumOfWatts method is not found in the Bike class.

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

D super(the_author, the_title); subject = the_subject;

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

E super.printInfo(); System.out.print(" by " + artist);


Related study sets

Paediatrics - Questions and Problems

View Set

MCN - Unit 1 - Chapter 2: Family-Centered Community-Based Care

View Set

Final Exam Review Questions Fall 2023

View Set

Chapter 35 Geriatric Emergencies

View Set

Insurance Ch 7 Long Term Care Insurance

View Set

Multiplying Numbers in Scientific Notation, Dividing Numbers in Scientific Notation

View Set