AP Comp Sci Final 12.3

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

Consider the class showMe, shown below. What is printed as a result of showMe(10)? public static void showMe(int arg) { if (arg > 1) { showMe(arg - 1); } else { System.out.print(arg + " "); } } A. 1 B. 0 C. 10 9 8 7 6 5 4 3 2 1 D. 1 2 3 4 5 6 7 8 9 10 E. 10

A. 1

Consider the following method evens, which finds the number of even numbers present in an array. Which of the following segments of code would correctly replace [//TO BE COMPLETED]? public int evens(int [] arr) { int count = 0; for (int x : arr) { // TO BE COMPLETED } return count; } // I if (x % 2 == 0) count++; // II if (x % 2 == 1) count++; // III if (x / 2 == 0) count++; // IV if (x / 2 == 1) count++; A. I only B. II only C. III and IV only D. I and II only E. II and IV only

A. I only

Consider the classes Car and Minivan, shown below. If obj has been instantiated later in the class as a Minivan, what is printed as a result of obj.drive()? public class Car { public void drive() { System.out.print("Vroom vroom! "); } } public class Minivan extends Car { public void drive() { super.drive(); System.out.print(" Let's go! "); } } A. Vroom vroom! Let's go! B. Vroom vroom!C. Let's go! D. Let's go! Vroom vroom! E. This would result in a compile-time error.

A. Vroom vroom! Let's go!

Consider the method printString shown below. What is printed as a result of printString("sandwich")? public void printString(String s) { if (s.length() > 0) { (s.substring(1)); System.out.print(s.substring(0, 1)); } } A. hciwdnas B. sandwich C. andwichandwichndwichdwichwichichchhD. hchichwichdwichndwichandwich E. Nothing is printed because an infinite loop occurs

A. hciwdnas

Which statement is equivalent to !( (x > 7) && !(y < 12) )? A. (x <= 7) && (y < 12) B. (x <= 7) || (y < 12) C. (x > 7) || (y >= 12) D. (x > 7) && (y >= 12) E. (x <= 7) || (y >= 12)

B. (x <= 7) || (y < 12)

Consider the following code. What is printed as a result of executing this code? int sum = 0; for (int x = 0; x < 5; x++) { for (int y = x; y < 5; y++) { sum++; } } System.out.println(sum); A. 25 B. 15 C. 21 D. 36 E. 10

B. 15

II. int[][] arr = new int [7][9]; int count = 1; for(int i = 0; i < arr.length; i++) { for(int j = 0; j < arr[0].length; j++) { arr[i][j] = count * 2; count++; } }Which of the following code segments creates a 7 x 9 array of integers and fills every space in the array with multiples of two (not including the value 0)? I. int[][] arr = new int [7][9]; II. int[][] arr = new int [7][9]; int count = 1; for(int i = 0; i < arr.length; i++) { for(int j = 0; j < arr[0].length; j++) { arr[i][j] = count * 2; count++; } } III. int[][] arr = new int [7][9]; int count = 1; int row = 0; int col = 0; while (row < arr.length && col < arr[0].length) { arr[row][col] = count * 2; row++; col++; count++; } A. I only B. II only C. II and III only D. I and II only E. III only

B. II only

Consider the following method mystery. Assuming x is an integer greater than 1, in which case does mystery result in an infinite loop? public int mystery(int x, int y) { if (x <= y) return x; else return mystery(x, y * 10); } A. y is greater than 1 B. y is less than or equal to 0 C. y is greater than x D. all of the above E. none of the above

B. y is less than or equal to 0

Given the following code, what is returned by mystery(5364)? public static int mystery(int num) { if (num < 10) { return 1; } else { return 1 + mystery(num / 10); } } A. The method will produce an infinite loop. B. 19 C. 4 D. 3 E. 18

C. 4

Consider the following classes Cat and FluffyCat. What is the result of executing the following code? Cat obj = new FluffyCat(); obj.display(); public class Cat { public String display() { System.out.print("Cats! "); } } public class FluffyCat extends Cat { public String display() { System.out.print("Cool!"); } } A. Cats! B. Cats! Cool! C. Cool! D. Cool! Cats! E. The code results in an error.

C. Cool!

Which of these loops will output 02468? I. for (int i = 0; i <= 8; i++) { System.out.print(i); } II. int i = 0; while (i < 8) { i +=2; System.out.print(i); } III. for (int i = 0; i <= 8; i +=2) { System.out.print(i); } A. I only B. II only C. III only D. I and II only E. I, II, and III

C. III only

You are trying to write a method sumRow that finds the sum of the values in a specified row of a symmetrical 2-D matrix. Which of the following code segments could replace [//TO BE DETERMINED] to make the code work correctly? public int sumRow (int row, int[][] values) { int sum = 0; // TO BE DETERMINED return sum; } //I. for (int[] rowValues : values) { for (int x : rowValues) { sum += x; } } //II. for (int i = 0; i < values[0].length;i++) { sum += values[row][i]; } //III. int col = 0; while (col < values[0].length) { sum += values[row][col]; col++; } A. I only B. II only C. III only D. II and III E. I, II, and III

D. II and III

The Dog class is shown below. The GoldenRetriever class inherits from the Dog class. Which methods does the GoldenRetriever class inherit? public class Dog { private int numLegs = 4; private String name = "Spot"; public Dog(String theName) { // implementation not shown } public String bark() { return "Woof!"; } public String getName() { return name; } } I. public Dog(String theName) II. bark() III. getName() A. I only B. I and II only C. III only D. II and III only E. I, II, and III

D. II and III only

Consider an array of integers that contains [12, 8, 4, 6, 13, 29, 7]. If the array is sorted from smallest to largest using an insertion sort method, what will be the order of the array after the third iteration of the sorting method? A. [4, 6, 12, 8, 13, 29, 7] B. [4, 6, 7, 8, 13, 29, 12] C. [4, 8, 12, 6, 13, 29, 7] D. [4, 6, 8, 12, 13, 29, 7] E. [4, 6, 7, 8, 12, 13, 29]

D. [4, 6, 8, 12, 13, 29, 7]

Assume that list has been instantiated as an ArrayList of integers containing [6, 2, 9] . What are the contents of list after the code is executed? list.remove(2); list.add(1, 4); list.add(5); list.set(2, 7); A. [6, 2, 7, 5] B. [6, 4, 2, 7, 5] C. [4, 7, 9, 5] D. [6, 4, 7, 5] E. [4, 7, 6, 9, 5]

D. [6, 4, 7, 5]

You have an array values filled with 50 integers. Which of the following correctly produces a random index of values? A. (int) (Math.random() + 1) * 50 B. (int) (Math.random() * 50) + 1 C. (int) (Math.random() + 1 * 50) D. (int) Math.random() * 50 E. (int) (Math.random() * 50)

E. (int) (Math.random() * 50)

A sorted array of integers containing 2000 elements is to be searched for key using a binary search method. Assuming key is in the array, what is the maximum number of iterations needed to find key? A. 8 B. 10 C. 100 D. 2000 E. 11

E. 11

Consider the method findMax, which uses sequential search to find the index of the largest value of an array. In which case would findMax not work properly? public int findMax(int[] arr) { int maxVal = 0; int index = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > maxVal) { index = i; maxVal = arr[i]; } } return index; } A. This method will work correctly for all arrays. B. The first value in the array is less than 0. C. The first value in the array is equal to 0. D. Every value in the array is greater than 0. E. Every value in the array is less than 0.

E. Every value in the array is less than 0.

Consider the Animal, Fish, and Goldfish classes shown below. Which of the following object declarations will compile without error? public class Animal { // no constructors } public class Fish extends Animal { // no constructors } public class Goldfish extends Fish { //no constructors } I. Goldfish glub = new Fish(); II. Animal glub = new Fish(); III. Fish glub = new Goldfish(); A. I only B. II only C. III only D. I and II only E. II and III only

E. II and III only

Consider the following method changeArray. An array is created that contains [2, 8, 10, 9, 6] and is passed to changeArray. What are the contents of the array after the changeArray method executes? public void changeArray(int[] data) { for (int k = data.length - 1; k > 0; k--) data[k - 1] = data[k] + data[k - 1]; } A. [2, 6, 2, -1, -3] B. [-23, -21, -13, -3, 6] C. [10, 18, 19, 15, 6] D. This method creates an IndexOutOfBounds exception. E. [35, 33, 25, 15, 6]

E. [35, 33, 25, 15, 6]


Set pelajaran terkait

Astronomy Test 2 Lecture Tutorials

View Set