AP CSA

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Assume that a, b, and c are boolean variables that have been properly declared and initialized. Which of the following boolean expressions is equivalent to !(a && b) || c ?

!a || !b || c

int a = 3 + 2 * 3; int b = 4 + 3 / 2; int c = 7 % 4 + 3; double d = a + b + c; What is the value of d after the code segment is executed?

20.0

int[][] values = {{1, 2, 3}, {4, 5, 6}}; int x = 0; for (int j = 0; j < values.length; j++) { for (int k = 0; k < values[0].length; k++) { if (k == 0) { values[j][k] *= 2; } x += values[j][k]; } } What is the value of x after the code segment is executed?

26

Consider the following code segment. Assume that a is greater than zero. int a = /* value not shown */; int b = a + (int) (Math.random() * a); Which of the following best describes the value assigned to b when the code segment is executed?

A random integer between a and 2 * a - 1, inclusive

public static String rearrange(String str) { String temp = ""; for (int i = str.length() - 1; i > 0; i--) { temp += str.substring(i - 1, i); } return temp; } What, if anything, is returned by the method call rearrange("apple") ?

"lppa"

public static String abMethod(String a, String b) { int x = a.indexOf(b); while (x >= 0) { a = a.substring(0, x) + a.substring(x + b.length()); x = a.indexOf(b); } return a; } What, if anything, is returned by the method call abMethod("sing the song", "ng") ?

"si the so"

public class SomeClass { private int x = 0; private static int y = 0; public SomeClass(int pX) { x = pX; y++; } public void incrementY() { y++; } public void incrementY(int inc) { y += inc; } public int getY() { return y; } } The following code segment appears in a class other than SomeClass. SomeClass first = new SomeClass(10); SomeClass second = new SomeClass(20); SomeClass third = new SomeClass(30); first.incrementY(); second.incrementY(10); System.out.println(third.getY()); What is printed as a result of executing the code segment if the code segment is the first use of a SomeClass object?

14

public static String[] strArrMethod(String[] arr) { String[] result = new String[arr.length]; for (int j = 0; j < arr.length; j++) { String sm = arr[j]; for (int k = j + 1; k < arr.length; k++) { if (arr[k].length() < sm.length()) { sm = arr[k]; // Line 12 } } result[j] = sm; } return result; } Consider the following code segment. String[] testTwo = {"last", "day", "of", "the", "school", "year"}; String[] resultTwo = strArrMethod(testTwo); How many times is the line labeled // Line 12 in the strArrMethod executed as a result of executing the code segment?

4 times

public static int calcMethod(int num) { if (num == 0) { return 10; } return num + calcMethod(num / 2); } What value is returned by the method call calcMethod(16) ?

41

public class Hero { private String name; private int power; public Hero(String n, int p) { name = n; power = p; } public void powerUp(int p) { power += p; } public int showPower() { return power; } } public class SuperHero extends Hero { public SuperHero(String n, int p) { super(n, p); } public void powerUp(int p) { super.powerUp(p * 2); } } The following code segment appears in a class other than Hero and SuperHero. Hero j = new SuperHero("JavaHero", 50); j.powerUp(10); System.out.println(j.showPower()); What is printed as a result of executing the code segment?

70

public class ClassA { public String getValue() { return "A"; } public void showValue() { System.out.print(getValue()); } } public class ClassB extends ClassA { public String getValue() { return "B"; } } The following code segment appears in a class other than ClassA or ClassB. ClassA obj = new ClassB(); obj.showValue(); What, if anything, is printed when the code segment is executed?

B

String[][] letters = {{"A", "B", "C", "D"}, {"E", "F", "G", "H"}, {"I", "J", "K", "L"}}; for (int col = 1; col < letters[0].length; col++) { for (int row = 1; row < letters.length; row++) { System.out.print(letters[row][col] + " "); } System.out.println(); } What is printed as a result of executing this code segment?

F J G K H L

Which of the following code segments can replace /* missing code */ so the getCategory method works as intended? I. String cat; if (density > 3000) { cat = "urban"; } else if (density > 999) { cat = "suburban"; } else { cat = "rural"; } return cat; II. String cat; if (density > 3000) { cat = "urban"; } if (density > 999) { cat = "suburban"; } cat = "rural"; return cat; III. if (density > 3000) { return "urban"; } if (density > 999) { return "suburban"; } return "rural";

I and III only

Consider an integer array nums, which has been properly declared and initialized with one or more values. Which of the following code segments counts the number of negative values found in nums and stores the count in counter ? I. int counter = 0; int i = -1; while (i <= nums.length - 2) { i++; if (nums[i] < 0) { counter++; } } II. int counter = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] < 0) { counter++; } } III. int counter = 0; for (int i : nums) { if (nums[i] < 0) { counter++; } }

I only

Which of the following expressions evaluate to 3.5 ? I. (double) 2 / 4 + 3 II. (double) (2 / 4) + 3 III. (double) (2 / 4 + 3)

I only

Consider the following class definition. public class Points { private double num1; private double num2; public Points(int n1, int n2) // Line 6 { num1 = n1; // Line 8 num2 = n2; // Line 9 } public void incrementPoints(int value) // Line 12 { n1 += value; // Line 14 n2 += value; // Line 15 } } The class does not compile. Which of the following identifies the error in the class definition?

In lines 14 and 15, the variables n1 and n2 are not defined.

The following method is intended to remove all elements of an ArrayList of integers that are divisible by key and add the removed elements to a new ArrayList, which the method returns. public static ArrayList<Integer> match(ArrayList<Integer> numList, int key) { ArrayList<Integer> returnList = new ArrayList<Integer>(); int i = 0; while (i < numList.size()) { int num = numList.get(i); if (num % key == 0) { numList.remove(i); returnList.add(num); } i++; } return returnList; } As an example, if the method is called with an ArrayList containing the values [5, 2, 10, 20, 16] and the parameter key has the value 5, then numList should contain [2, 16] at the end of the method and an ArrayList containing [5, 10, 20] should be returned.

The method skips some elements of numList during the traversal.

public class A { public String message(int i) { return "A" + i; } } public class B extends A { public String message(int i) { return "B" + i; } } The following code segment appears in a class other than A or B. A obj1 = new B(); // Line 1 B obj2 = new B(); // Line 2 System.out.println(obj1.message(3)); // Line 3 System.out.println(obj2.message(2)); // Line 4 Which of the following best explains the difference, if any, in the behavior of the code segment that will result from removing the message method from class A ?

The statement in line 3 will cause a compiler error because the message method for obj1 cannot be found.

Consider the following code segment. Assume that num3 > num2 > 0. int num1 = 0; int num2 = /* initial value not shown */; int num3 = /* initial value not shown */; while (num2 < num3) { num1 += num2; num2++; } Which of the following best describes the contents of num1 as a result of executing the code segment?

The sum of all integers from num2 to num3 - 1, inclusive

/** Precondition: a > 0 and b > 0 */ public static int methodOne(int a, int b) { int loopCount = 0; for (int i = 0; i < a / b; i++) { loopCount++; } return loopCount; } /** Precondition: a > 0 and b > 0 */ public static int methodTwo(int a, int b) { int loopCount = 0; int i = 0; while (i < a) { loopCount++; i += b; } return loopCount; } Which of the following best describes the conditions under which methodOne and methodTwo return the same value?

When a % b is equal to zero

Consider the following statement. Assume that a and b are properly declared and initialized boolean variables. boolean c = (a && b) || (!a && b); Under which of the following conditions will c be assigned the value false ?

When b has the value false

Consider the following code segment. Assume num is a properly declared and initialized int variable. if (num > 0) { if (num % 2 == 0) { System.out.println("A"); } else { System.out.println("B"); } } Which of the following best describes the result of executing the code segment?

When num is a positive even integer, "A" is printed; when num is a positive odd integer, "B" is printed; otherwise, nothing is printed.

public static void message(int a, int b, int c) { if (a < 10) { if (b < 10) { System.out.print("X"); } System.out.print("Y"); } if (c < 10) { if (b > 10) { System.out.print("Y"); } else { System.out.print("Z"); } } } What is printed as a result of the call message(5, 15, 5) ?

YY

Consider the following code segment. ArrayList<Integer> numList = new ArrayList<Integer>(); numList.add(3); numList.add(2); numList.add(1); numList.add(1, 0); numList.set(0, 2); System.out.print(numList); What is printed by the code segment?

[2, 0, 2, 1]

int num = /* initial value not shown */; boolean b1 = true; if (num > 0) { if (num >= 100) { b1 = false; } } else { if (num >= -100) { b1 = false; } } Which of the following statements assigns the same value to b2 as the code segment assigns to b1 for all values of num ?

boolean b2 = (num < -100) || (num > 0 && num < 100);

Consider the following implementation of getHours. public static double getHours(int marker1, int marker2) { /* missing statement */ return hours; } Which of the following statements can replace /* missing statement */ so getHours works as intended?

double hours = Math.abs(marker1 - marker2) / 60.0;

Consider the following method, which is intended to print the values in its two-dimensional integer array parameter in row-major order. public static void rowMajor(int[][] arr) { /* missing code */ } As an example, consider the following code segment. int[][] theArray = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; rowMajor(theArray); When executed, the code segment should produce the following output. 1 2 3 4 5 6 7 8 Which of the following code segments can replace /* missing code */ so that the rowMajor method works as intended?

for (int[] j : arr) { for (int k : j) { System.out.print(k + " "); } }

Consider the following class definition. public class Book { private int pages; public int getPages() { return pages; } // There may be instance variables, constructors, and methods not shown. } The following code segment is intended to store in maxPages the greatest number of pages found in any Book object in the array bookArr. Book[] bookArr = { /* initial values not shown */ }; int maxPages = bookArr[0].getPages(); for (Book b : bookArr) { /* missing code */ } Which of the following can replace /* missing code */ so the code segment works as intended?

if (b.getPages() > maxPages) { maxPages = b.getPages(); }

Which of the following code segments produces the output "987654321" ?

int num = 10; while (num > 1) { num--; System.out.print(num); }

Consider the following method, which is intended to return the number of local maximum values in an array. Local maximum values are array elements that are greater than both adjacent array elements. The first and last elements of an array have only a single adjacent element, so neither the first nor the last array element is counted by this method. For example, an array containing the values {3, 9, 7, 4, 10, 12, 3, 8} has two local maximum values: 9 and 12. public static int countPeaks(int[] data) { int numPeaks = 0; for ( /* missing loop header */ ) { if (data[p - 1] < data[p] && data[p] > data[p + 1]) { numPeaks++; } } return numPeaks; } Which of the following can replace /* missing loop header */ so the method countPeaks works as intended?

int p = 1; p < data.length - 1; p++

Consider the following method. public static void printSome(int num1, int num2) { for (int i = 0; i < num1; i++) { if (i % num2 == 0 && i % 2 == 0) { System.out.print(i + " "); } } } Which of the following method calls will cause "0 10 " to be printed?

printSome(20, 5)

public class Bird { private String species; private String color; private boolean canFly; public Bird(String str, String col, boolean cf) { species = str; color = col; canFly = cf; } } Which of the following constructors, if added to the Bird class, will cause a compilation error?

public Bird(String col, String str, boolean cf) { species = str; color = col; canFly = cf; }

public class Rectangle { private int height; private int width; public Rectangle() { height = 1; width = 1; } public Rectangle(int x) { height = x; width = x; } public Rectangle(int h, int w) { height = h; width = w; } // There may be methods that are not shown. } public class Square extends Rectangle { public Square(int x) { /* missing code */ } } Which of the following code segments can replace /* missing code */ so that the Square class constructor initializes the Rectangle class instance variables height and width to x ?

super(x);

public static String[] strArrMethod(String[] arr) { String[] result = new String[arr.length]; for (int j = 0; j < arr.length; j++) { String sm = arr[j]; for (int k = j + 1; k < arr.length; k++) { if (arr[k].length() < sm.length()) { sm = arr[k]; // Line 12 } } result[j] = sm; } return result; } Consider the following code segment. String[] testOne = {"first", "day", "of", "spring"}; String[] resultOne = strArrMethod(testOne); What are the contents of resultOne when the code segment has been executed?

{"of", "of", "of", "spring"}


Kaugnay na mga set ng pag-aaral

Chapter 34: Assessment and Management of Patients with Inflammatory Rheumatic Disorders

View Set

Marketing: An Introduction - Chapter 5

View Set

Combo with "APUSH Chapter 9" and 6 others

View Set