AP CSA Unit Nine MCQ APCLASSROOM ANSWERS

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

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?

super(the_author, the_title); subject = the_subject;

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 Game object cannot be assigned to the BoardGame reference g2.

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?

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

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?

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.

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 */

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

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

I and III only

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.

I, II, and III

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?

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

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?

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

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?

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

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

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?

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

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

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

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?

b1.equals(b2)

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.

hotChocolate.equals(coffee)

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?

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


Set pelajaran terkait

1/ Fázové rovnováhy, základní podmínka fázové rovnováhy, Gibbsův zákon fází, jednosložkové fázové rovnováhy

View Set

6th Grade - Wordly Wise - Lesson 9

View Set

Psych Research Methods /Quiz 1-12 (Not 4 and 9)

View Set

Ch 2 - Choice in a World of Scarcity

View Set

Microsoft Excel and Access Test Review

View Set

Section 7, Unit 2: Interest and Ownership REAL ESTATE

View Set

Entrepreneurship/International Marketing Chapter 34 Vocabulary Terms by Gwen Davidson

View Set

Biology Chapter 5: Membrane Transport exam 2

View Set