AP Computer Science Unit 8, AP Computer Science Unit 9

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

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 c

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

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

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

A two-dimensional array arr is to be created with the following contents. boolean[][] arr = {{false, true, false}, {false, false, true}}; Which of the following code segments can be used to correctly create and initialize arr ? A boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true; B boolean arr[][] = new boolean[2][3]; arr[1][2] = true; arr[2][3] = true; C boolean arr[][] = new boolean[3][2]; arr[0][1] = true; arr[1][2] = true; D boolean arr[][] = new boolean[3][2]; arr[1][0] = true; arr[2][1] = true; E boolean arr[][] = new boolean[3][2]; arr[2][1] = true; arr[3][2] = true;

A boolean arr[][] = new boolean[2][3]; arr[0][1] = true; arr[1][2] = true;

Consider the following method. public boolean checkIndexes(double[][] data, int row, int col) { int numRows = data.length; if (row < numRows) { int numCols = data[0].length; return col < numCols; } else { return false; } } Consider the following variable declaration and initialization, which appears in a method in the same class as checkIndexes. double[][] table = new double[5][6]; Which of the following method calls returns a value of true ? A checkIndexes(table, 4, 5) B checkIndexes(table, 4, 6) C checkIndexes(table, 5, 4) D checkIndexes(table, 5, 6) E checkIndexes(table, 6, 5)

A checkIndexes(table, 4, 5)

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 two-dimensional array definition. int[][] data = new int[5][10]; Consider the following code segment, where all elements in data have been initialized. for (int j = 0; j < data.length; j++) { for (int k = 0; k < data[0].length; k++) { if (j == k) { System.out.println(data[j][k]); } } } How many times is the println method called when the code segment is executed? A 4 B 5 C 9 D 10 E 15

B 5

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 canno

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

Consider the following code segment, where num is an integer variable. int[][] arr = {{11, 13, 14 ,15}, {12, 18, 17, 26}, {13, 21, 26, 29}, {14, 17, 22, 28}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] == num) { System.out.print(j + k + arr[j][k] + " "); } } } What is printed when num has the value 14 ? A 14 14 B 16 17 C 17 16 D 18 19 E 19 18

B 16 17

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)

B b1.equals(b2)

Consider the following code segment. int[][] arr = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {3, 2, 1}}; for (int j = 0; j < arr.length; j++) { for (int k = j; k < arr[0].length; k++) { System.out.print(arr[j][k] + " "); } System.out.println(); } What output is printed when the code segment is executed? A 2 3 6 B 1 2 3 4 5 7 C 1 2 3 5 6 9 D 1 4 7 5 8 9 E 1 2 3 5 6 9 1

C 1 2 3 5 6 9

Consider the following code segment. int[][] arr = {{6, 2, 5, 7}, {7, 6, 1, 2}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] > j + k) { System.out.println("!"); } } } How many times will "!" be printed when the code segment is executed? A 0 times B 2 times C 4 times D 6 times E 8 times

D 6 times

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

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 Li

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, whi

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

C hotChocolate.equals(coffee)

Consider the following code segment. String[][] arr = {{"Hello,", "Hi,", "Hey,"}, {"it's", "it is", "it really is"}, {"nice", "great", "a pleasure"}, {"to", "to get to", "to finally"}, {"meet", "see", "catch up with"}, {"you", "you again", "you all"}}; for (int j = 0; j < arr.length; j++) { for (int k = 0; k < arr[0].length; k++) { if (k == 1) { System.out.print(arr[j][k] + " "); } } } What, if anything, is printed when the code segment is executed? A Nothing is printed due to an ArrayIndexOutOfBoundsException. B Hello, it's nice to meet you C Hey, it really is a pleasure to finally catch up with you all D Hi, it is great to get to see you again E it's it is it really is

D Hi, it is great to get to see you again

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

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

Consider the following code segment, which is intended to declare and initialize the two-dimensional (2D) String array things. /* missing code */ = {{"spices", "garlic", "onion", "pepper"}, {"clothing", "hat", "scarf", "gloves"}, {"plants", "tree", "bush", "flower"}, {"vehicles", "car", "boat", "airplane"}}; Which of the following could replace /* missing code */ so that things is properly declared? A new String[][] things B new(String[][]) things C String[] String[] things D String[][] things E [][]String things

D String[][] things

Assume that a two-dimensional (2D) array arr of String objects with 3 rows and 4 columns has been properly declared and initialized. Which of the following can be used to print the elements in the four corner elements of arr ? A System.out.print(arr[0, 0] + arr[0, 3] + arr[2, 0] + arr[2, 3]); B System.out.print(arr[1, 1] + arr[1, 4] + arr[3, 1] + arr[3, 4]); C System.out.print(arr[0][0] + arr[0][2] + arr[3][0] + arr[3][2]); D System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]); E System.out.print(arr[1][1] + arr[1][4] + arr[3][1] + arr[3][4]);

D System.out.print(arr[0][0] + arr[0][3] + arr[2][0] + arr[2][3]);

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 code segment, where nums is a two-dimensional (2D) array of integers. The code segment is intended to print "test1234". System.out.print("test" + nums[0][0] + nums[1][0] + nums[1][1] + nums[0][1]); Which of the following code segments properly declares and initializes nums so that the code segment works as intended? A int[][] nums = {{1, 2}, {3, 4}}; B int[][] nums = {{1, 2}, {4, 3}}; C int[][] nums = {{1, 3}, {2, 4}}; D int[][] nums = {{1, 4}, {2, 3}}; E int[][] nums = {{1, 4}, {3, 2}};

D int[][] nums = {{1, 4}, {2, 3}};

Consider the following code segment. int[][] array2D = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; for (int[] i : array2D) { for (int x : i) { System.out.print(x + " "); } System.out.println(" "); } How many times will the statement System.out.print(x + " ") be executed? A 3 times B 4 times C 6 times D 12 times E 16 times

E 16 times

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

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

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

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 code segment, where letters is a two-dimensional (2D) array that contains possible letters. The code segment is intended to print "DIG". String[][] letters = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}}; System.out.println( /* missing code */ ); Which of the following could replace /* missing code */ so that the code segment works as intended? A letters[2][1] + letters[3][3] + letters[3][1] B letters[2][0] + letters[2][2] + letters[1][0] C letters[1][2] + letters[3][3] + letters[1][3] D letters[1][0] + letters[2][2] + letters[2][0] E letters[0][1] + letters[2][2] + letters[0][2]

D letters[1][0] + letters[2][2] + letters[2][0]

Consider the following code segment, where num is a properly declared and initialized integer variable. The code segment is intended to traverse a two-dimensional (2D) array arr looking for a value equal to num and then print the value. The code segment does not work as intended. int[][] arr = {{7, 3, 6, 4}, {9, 2, 0, 5}, {1, 4, 3, 8}}; for (int j = 0; j < arr.length - 1; j++) { for (int k = 0; k < arr[0].length; k++) { if (arr[j][k] == num) { System.out.println(arr[j][k]); } } } For which of the following values of num does the code segment not work as intended? A num = 5 B num = 6 C num = 7 D num = 8 E num = 9

D num = 8

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 method, count, which is intended to traverse all the elements in the two-dimensional (2D) String array things and return the total number of elements that contain at least one "a". public static int count(String[][] things) { int count = 0; for (int r = 0; r < things.length; r++) { for (int c = 0; c < things[r].length - 1; c++) { if (things[r][c].indexOf("a") >= 0) { count++; } } } return count; } For example, if things contains {{"salad", "soup"}, {"water", "coffee"}}, then count(things) should return 2. The method does not always work as intended. For which of the following two-dimensional array input values does count NOT work as intended? A {{"lemon"}, {"lime"}} B {{"tall", "short"}, {"up", "down"}} C {{"rabbit", "bird"}, {"cat", "dog"}, {"gecko", "turtle"}} D {{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}} E {{"math", "english", "physics"}, {"golf", "baseball", "soccer"}}

D {{"scarf", "gloves", "hat"}, {"shoes", "shirt", "pants"}}

Consider the following method, which is intended to return true if 0 is found in its two-dimensional array parameter arr and false otherwise. The method does not work as intended. public boolean findZero(int[][] arr) { for (int row = 0; row <= arr.length; row++) { for (int col = 0; col < arr[0].length; col++) { if (arr[row][col] == 0) { return true; } } } return false; } Which of the following values of arr could be used to show that the method does not work as intended? A {{30, 20}, {10, 0}} B {{4, 3}, {2, 1}, {0, -1}} C {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}} D {{5, 10, 15, 20}, {25, 30, 35, 40}} E {{10, 20, 0, 30, 40}, {60, 0, 70, 80, 90}}

D {{5, 10, 15, 20}, {25, 30, 35, 40}}

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


Set pelajaran terkait

Unit 28: Record Keeping Requirements under SEC 17a-3 & 4

View Set

Fundamentals of Networking Technologies Ch 5

View Set

Physical Science: Extension B (PEF)

View Set

2.3 Direct Participation Programs (DPP)

View Set

Real Estate Contracts & Practice - Earnest Money Agreements

View Set