AP Computer Science Sample MC, wqqqwwwwwwwwww

¡Supera tus tareas y exámenes ahora con Quizwiz!

Consider the following code segment. double num = 9 / 4; System.out.print(num); System.out.print(" "); System.out.print((int) num); What is printed as a result of executing the code segment? Responses

2.0 2

Consider the following class declaration. public class Sample { private int a; private double b; public Sample(int x, double y) { a = x; b = y; } // No other constructors } The following method appears in a class other than Sample. public static void test() { Sample object = new /* missing constructor call */ ; } Which of the following could be used to replace /* missing constructor call */ so that the method will compile without error? Responses

Sample(10, 6.2)

Consider the following method. public int someCode(int a, int b, int c) { if ((a < b) && (b < c)) return a; if ((a >= b) && (b >= c)) return b; if ((a == b) || (a == c) || (b == c)) return c; } Which of the following best describes why this method does not compile? Responses

It is possible to reach the end of the method without returning a value.

Consider the definition of the Person class below. The class uses the instance variable adult to indicate whether a person is an adult or not. public class Person { private String name; private int age; private boolean adult; public Person (String n, int a) { name = n; age = a; if (age >= 18) { adult = true; } else { adult = false; } } } Which of the following statements will create a Person object that represents an adult person?

Person p = new Person ("Homer", 23);

Consider the following code segment, which is intended to print the sum of all elements of an array. int[] arr = {10, 5, 1, 20, 6, 25}; int sum = 0; for (int k = 0; k <= arr.length; k++) { sum += arr[k]; } System.out.println("The sum is " + sum); A runtime error occurs when the code segment is executed. Which of the following changes should be made so that the code segment works as intended?

The for loop header should be replaced with for (int k = 0; k < arr.length; k++).

Consider the following code segment. int x = 7; int y = 4; boolean a = false; boolean b = false; if (x > y) { if (x % y >= 3) { a = true; x -= y; } else { x += y; } } if (x < y) { if (y % x >= 3) { b = true; x -= y; } else { x += y; } } What are the values of a, b, and x after the code segment has been executed? Responses

a = true, b = false, x = 7

Consider the following code segment. String str = "abcdef"; for (int rep = 0; rep < str.length() - 1; rep++) { System.out.print(str.substring(rep, rep + 2)); } What is printed as a result of executing this code segment? Responses

abbccddeef

Consider the following code segment. int[] arr = {1, 2, 3, 4, 5, 6, 7}; for (int i = 1; i < arr.length; i += 2) { arr[i] = arr[i - 1]; } Which of the following represents the contents of the array arr after the code segment is executed? Responses

{1, 1, 3, 3, 5, 5, 7}

Consider the following code segment. int[] arr = {3, 1, 0, 4, 2}; for(int j = 0; j < arr.length; j++) { System.out.print(arr[j] + j + " "); } What, if anything, is printed as a result of executing the code segment? Responses

3 2 2 7 6

public void test(int x) { int y; if (x % 2 == 0) { y = 3; } else if (x > 9) { y = 5; } else { y = 1; } System.out.println("y equals " + y); } Which of the following test data sets would test each possible output for the method?

8, 9, 11

Consider the following code segment. int a = 5; int b = 4; int c = 2; a *= 3; b += a; b /= c; System.out.print(b); What is printed when the code segment is executed? Responses

9

Consider the following code segment. int num = 1; for (int k = 2; k < 10; k++) { num = 0; num = num + k; } What will be the value of num after the loop is executed?

9

Consider the following code segment. String str1 = new String("Advanced Placement"); String str2 = new String("Advanced Placement"); if (str1.equals(str2) && str1 == str2) { System.out.println("A"); } else if (str1.equals(str2) && str1 != str2) { System.out.println("B"); } else if (!str1.equals(str2) && str1 == str2) { System.out.println("C"); } else if (!str1.equals(str2) && str1 != str2) { System.out.println("D"); } What, if anything, is printed when the code segment is executed?

B

Consider the following while loop. Assume that the int variable k has been properly declared and initialized. while (k < 0) { System.out.print("*"); k++; } Which of the following ranges of initial values for k will guarantee that at least one "*" character is printed? l.k < 0 ll.k = 0 lll.k > 0

I only

Which of the following expressions evaluate to 3.5 ? l.(double) 2 / 4 + 3 ll.(double) (2 / 4) + 3 lll.(double) (2 / 4 + 3) Responses

I only

Consider the following class definition. public class Student { private int studentID; private int gradeLevel; private boolean honorRoll; public Student(int s, int g) { studentID = s; gradeLevel = g; honorRoll = false; } public Student(int s) { studentID = s; gradeLevel = 9; honorRoll = false; } } Which of the following code segments would successfully create a new Student object? l.Student one = new Student(328564, 11); ll.Student two = new Student(238783); lll.int id = 392349;i nt grade = 11; Student three = new Student(id, grade);

I, II, and III

Consider the following code segments. I. int k = 1; while (k < 20) { if (k % 3 == 1) System.out.print( k + " "); k = k + 3; } II. for (int k = 1; k < 20; k++) { if (k % 3 == 1) System.out.print( k + " "); } III. for (int k = 1; k < 20; k = k + 3) { System.out.print( k + " "); } Which of the code segments above will produce the following output? 1 4 7 10 13 16 19

I, II, and III

public boolean isPassing() { /* implementation not shown */ } A student can pass a programming course if at least one of the following conditions is met. The student has a test average that is greater than or equal to 90. The student has a test average that is greater than or equal to 75 and has at least 4 completed assignments. Consider the following proposed implementations of the isPassing method. I. if (testAverage >= 90) return true; if (testAverage >= 75 && assignmentsCompleted >= 4) return true; return false; II. boolean pass = false; if (testAverage >= 90) pass = true; if (testAverage >= 75 && assignmentsCompleted >= 4) pass = true; return pass; III. return (testAverage >= 90) || (testAverage >= 75 && assignmentsCompleted >= 4); Which of the implementations will correctly implement method isPassing?

I, II, and III

Assume that a, b, and c are variables of type int. Consider the following three conditions. I. (a == b) && (a == c) && (b == c) II. (a == b) || (a == c) || (b == c) III. ((a - b) * (a - c) * (b - c)) == 0 Assume that subtraction and multiplication never overflow. Which of the conditions above is (are) always true if at least two of a, b, and c are equal?

II and III

Which of the following expressions evaluate to 7 ? l.9 + 10 % 12 ll.(9 + 10) % 12 lll.9 - 2 % 12

II and III

Consider the following class definitions. public class Data { private int x; public void setX(int n) { x = n; } // ... other methods not shown } public class EnhancedData extends Data { private int y; public void setY(int n) { y = n: } // ... other methods not shown } Assume that the following declaration appears in a client program. EnhancedData item = new EnhancedData(); Which of the following statements would be valid? I. item.y = 16; II. item.setY(16); III. item.setX(25); Responses

II and III only

Consider the following code segment. for (int num = 0; num < 10; num += 2) { for (int val = 0; val < 5; val++) { System.out.println("hop"); } } How many times will System.out.println("hop") be executed?

25

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

Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0. for (int i = 0; i < m * n; i++) { System.out.print("A"); } for (int j = 1; j <= m; j++) { for (int k = 1; k < n; k++) { System.out.print("B"); } } Assume that the initial values of m and n are the same in code segment I as they are in code segment II. Which of the following correctly compares the number of times that "A" and "B" are printed when each code segment is executed? Responses

"A" is printed m more times than "B".

Consider the following two code segments. Assume that the int variables m and n have been properly declared and initialized and are both greater than 0. l.for (int i = 0; i < m * n; i++) { System.out.print("A"); } ll.for (int j = 1; j <= m; j++) { for (int k = 1; k < n; k++) { System.out.print("B"); } } Assume that the initial values of m and n are the same in code segment I as they are in code segment II. Which of the following correctly compares the number of times that "A" and "B" are printed when each code segment is executed?

"A" is printed m more times than "B".

Consider the following code segment. for (int x = 0; x <= 4; x++) // Line 1 { for (int y = 0; y < 4; y++) // Line 3 { System.out.print("a"); } System.out.println(); } Which of the following best explains the effect of simultaneously changing x <= 4 to x < 4 in line 1 and y < 4 to y <= 4 in line 3 ? Responses

"a" will be printed the same number of times because while the number of output lines will decrease by 1, the length of each line will increase by 1.

Assume that x and y have been declared and initialized with int values. Consider the following Java expression. (y > 10000) || (x > 1000 && x < 1500)Which of the following is equivalent to the expression given above?

(y > 10000 | | x > 1000) && (y > 10000 | | x < 1500)

Consider the following code segment. for (int outer = 0; outer < n; outer++) { for (int inner = 0; inner <= outer; inner++) { System.out.print(outer + " "); } } If n has been declared as an integer with the value 4, what is printed as a result of executing the code segment?

0 1 1 2 2 2 3 3 3 3

Vehicle size classTotal interior volumeMinicompactLess than 85 cubic feetSubcompact85 to 99 cubic feetCompact100 to 109 cubic feetMid-Size110 to 119 cubic feetLarge120 cubic feet or more The classify method, which does not work as intended, is shown below. public static String classify(int volume) { String carClass = ""; if (volume >= 120) { carClass = "Large"; } else if (volume < 120) { carClass = "Mid-Size"; } else if (volume < 110) { carClass = "Compact"; } else if (volume < 100) { carClass = "Subcompact"; } else { carClass = "Minicompact"; } return carClass; } The classify method works as intended for some but not all values of the parameter volume. For which of the following values of volume would the correct value be returned when the classify method is executed?

115

Consider the following class definition. 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? Responses

14

Consider the following code segment. int a = 4; int b = 5; a++; b++; int c = a + b; a -= 1; System.out.println(a + c); What is printed when the code segment is executed? Responses

15

Consider the following code segment. int x = 5; x += 6 * 2; x -= 3 / 2; What value is stored in x after the code segment executes? Responses

16

A pair of number cubes is used in a game of chance. Each number cube has six sides, numbered from 1 to 6, inclusive, and there is an equal probability for each of the numbers to appear on the top side (indicating the cube's value) when the number cube is rolled. The following incomplete statement appears in a program that computes the sum of the values produced by rolling two number cubes. int sum = / * missing code * / ; Which of the following replacements for /* missing code */ would best simulate the value produced as a result of rolling two number cubes?

2 + (int) (Math.random() * 6) + (int) (Math.random() * 6)

public class YourClassName { public static void main(String[] args) { for (int k = 0; k < 20; k += 2) { if (k % 3 == 1) { System.out.print(k + " "); } } } }What is printed as a result of executing the code segment?

4 10 16

What is printed as a result of executing the following statement? System.out.println(404 / 10 *10 +1);

401

Consider the following method. public int getTheResult(int n) { int product = 1; for (int number = 1; number < n; number++) { if (number % 2 == 0) product *= number; } return product; } What value is returned as a result of the call getTheResult(8) ?

48

Consider the following method. public void numberCheck(int maxNum) { int typeA = 0; int typeB = 0; int typeC = 0; for (int k = 1; k <= maxNum; k++) { if (k % 2 == 0 && k % 5 == 0) typeA++; if (k % 2 == 0) typeB++; if (k % 5 == 0) typeC++; } System.out.println(typeA + " " + typeB + " " + typeC); } What is printed as a result of the call numberCheck(50) ?

5 25 10

Consider the following method. public int sol(int lim) { int s = 0; for (int outer = 1; outer <= lim; outer++) { for (int inner = outer; inner <= lim; inner++) { s++; } } return s; } What value is returned as a result of the call sol(10)?

55

When designing a class hierarchy, which of the following should be true of a superclass?

A superclass should contain the data and functionality that are common to all subclasses that inherit from the superclass.

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; } } 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.

Consider the following code segments. Code segment 2 is a revision of code segment 1 in which the loop increment has been changed. Code Segment 1 int sum = 0; for (int k = 1; k <= 30; k++) { sum += k; } System.out.println("The sum is: " + sum); Code Segment 2 int sum = 0; for (int k = 1; k <= 30; k = k + 2) { sum += k; } System.out.println("The sum is: " + sum); Code segment 1 prints the sum of the integers from 1 through 30, inclusive. Which of the following best explains how the output changes from code segment 1 to code segment 2 ?

Code segment 2 will print the sum of only the odd integers from 1 through 30, inclusive because it starts k at one, increments k by twos, and terminates when k exceeds 30.

Consider the following Boolean expressions. I. A && B II. !A && !B Which of the following best describes the relationship between values produced by expression I and expression II?

Expression I and expression II evaluate to the same value only when A and B differ.

The following questions refer to the following classes: public class First { public String name() { return "First"; } } public class Second extends First { public void whoRules() { System.out.print(super.name() + " rules"); System.out.println(" but " + name() + " is even better"); } public String name() { return "Second"; } } public class Third extends Second { public String name() { return "Third"; } } Consider the following code segment. Second varSecond = new Second(); Third varThird = new Third(); varSecond.whoRules(); varThird.whoRules(); What is printed as a result of executing the code segment?

First rules but Second is even better First rules but Third is even better

Which of the following code segments will print all multiples of 5 that are greater than 0 and less than 100 ? I. for (int k = 1; k < 100; k++){ if (k % 5 == 0) { System.out.print(k + " "); }} II. for (int k = 1; k < 100; k++){ if (k / 5 == 0) { System.out.print(k + " "); }} III. int k = 5;while (k < 100){ System.out.print(k + " "); k = k + 5;}

I and III

At a certain high school students receive letter grades based on the following scale. of the following code segments will assign the correct string to grade for a given integer score ? l.if (score >= 93) { grade = "A"; } else if (score >= 84 && score <= 92) { grade = "B"; } else if (score >= 75 && score <= 83) { grade = "C"; } else if (score < 75) { grade = "F"; } ll.if (score >= 93) { grade = "A"; } else if (84 <= score && score <= 92) { grade = "B"; } else if (75 <= score && score <= 83) { grade = "C"; } else if (score < 75) { grade = "F"; } lll.if (score >= 93) { grade = "A"; } else if (score >= 84) { grade = "B"; } else if (score >= 75) { grade = "C"; } else { grade = "F"; }

I and III only

A teacher put three bonus questions on a test and awarded 5 extra points to anyone who answered all three bonus questions correctly and no extra points otherwise. Assume that the boolean variables bonusOne, bonusTwo, and bonusThree indicate whether a student has answered the particular question correctly. Each variable was assigned true if the answer was correct and false if the answer was incorrect. Which of the following code segments will properly update the variable grade based on a student's performance on the bonus questions? I. if (bonusOne && bonusTwo && bonusThree) grade += 5; II. if (bonusOne || bonusTwo || bonusThree) grade += 5; III. if (bonusOne) grade += 5; if (bonusTwo) grade += 5; if (bonusThree) grade += 5;

I only

Consider the following code segment. for (int k = 0; k < 9; k = k + 2) { if ((k % 2) != 0) { System.out.print(k + " "); } } What, if anything, is printed as a result of executing the code segment? Responses

Nothing is printed.

Consider an integer array, nums, which has been declared and initialized with one or more integer values. Which of the following code segments updates nums so that each element contains the square of its original value? I. int k = 0; while (k < nums.length) { nums[k] = nums[k] * nums[k]; } II. for (int k = 0; k < nums.length; k++) { nums[k] = nums[k] * nums[k]; } III. for (int n : nums) { n = n * n; }

II only

Number of Boxes Price per Box 1 up to but not including 5 $5.00 5 up to but not including 10 $3.00 10 or more $1.50 /* Precondition: numBoxes > 0 / public static double getCost(int numBoxes) { double totalCost = 0.0; / missing code / return totalCost; } Which of the following code segments can be used to replace / missing code /so that methodgetCost will work as intended? I. if (numBoxes >= 10) { totalCost = numBoxes * 1.50; } if (numBoxes >= 5) { totalCost = numBoxes * 3.00; } if (numBoxes > 0) { totalCost = numBoxes * 5.00; } II. if (numBoxes >= 10) { totalCost = numBoxes * 1.50; } else if (numBoxes >= 5) { totalCost = numBoxes * 3.00; } else { totalCost = numBoxes * 5.00; } III. if (numBoxes > 0) { totalCost = numBoxes * 5.00; } else if (numBoxes >= 5) { totalCost = numBoxes * 3.00; } else if (numBoxes >= 10) { totalCost = numBoxes * 1.50; }

II only

public class Book { private int numPages; private String bookTitle; public Book(int pages, String title) { numPages = pages; bookTitle = title; } public String toString() { return bookTitle + " " + numPages; } public int length() { return numPages; } }public class AudioBook extends Book { private int numMinutes; public AudioBook(int minutes, int pages, String title) { super(pages, title); numMinutes = minutes; } public int length() { return numMinutes; } public double pagesPerMinute() { return ((double) super.length()) / numMinutes; } }// Create an array of Book objects Book[] books = new Book[2]; // Initialize the array with Book and AudioBook objects books[0] = new AudioBook(100, 300, "The Jungle"); books[1] = new Book(400, "Captains Courageous"); // Access and print information

Line 4 will not compile because variables of type Book may only call methods in the Book class.

Consider the following methods, which appear in the same class. public void printSum(int x, double y) { System.out.println(x + y); } public void printProduct(double x, int y) { System.out.println(x * y); } Consider the following code segment, which appears in a method in the same class as printSum and printProduct. int num1 = 5; double num2 = 10.0; printSum(num1, num2); printProduct(num1, num2); What, if anything, is printed as a result of executing the code segment?

Nothing is printed because the code does not compile.

Consider the following code segment, which traverses two integer arrays of equal length. If any element of arr1 is smaller than the corresponding (i.e., at the same index) element of minArray, the code segment should replace the element of minArray with the corresponding element of arr1. After the code segment executes, minArray should hold the smaller of the two elements originally found at the same indices in arr1 and minArray and arr1 should remain unchanged. for (int c = 0; c < arr1.length; c++) { if (arr1[c] < minArray[c]) { arr1[c] = minArray[c]; } else { minArray[c] = arr1[c]; } } Which of the following changes will ensure that the code segment always works as intended?

Removing lines 5-8

Consider the following class declarations. public class Base { private int myVal; public Base() { myVal = 0; } public Base(int x) { myVal = x; } } public class Sub extends Base { public Sub() { super(0); } } Which of the following statements will NOT compile?

Sub s3 = new Sub(5);

Consider the following code segment. int w = 1; int x = w / 2; double y = 3; int z = (int) (x + y); Which of the following best describes the results of compiling the code segment? Responses

The code segment compiles without error.

Consider the following code segment, where k and count are properly declared and initialized int variables. k++; k++; count++; k--; count++; k--; Which of the following best describes the behavior of the code segment?

The code segment leaves k unchanged and increases count by 2.

In the code segment below, assume that the int variables a and b have been properly declared and initialized. int c = a; int d = b; c += 3; d--; double num = c; num /= d; Which of the following best describes the behavior of the code segment?

The code segment stores the value of (a + 3) / (b - 1) in the variable num.

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

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

Consider the following code segment. for (int j = 0; j < 3; j++) { for (int k = 0; k < 4; k++) { System.out.println("Fun"); } } Which of the following best explains how changing the outer for loop header to for (int j = 0; j <= 3; j++) affects the output of the code segment?

The string "Fun" will be printed more times because the outer loop will execute more times.

The question refer to the following data field and method. private int[] arr; // precondition: arr.length > 0public void mystery() { int sl = 0; int s2 = 0; for (int k = 0; k < arr.length; k++) { int num = arr[k]; if ((num > 0) && (num % 2 == 0)) sl += num; else if (num < 0) s2 += num; } System.out.println(s1); System.out.println(s2); } Which of the following best describes the value of s1 output by the method mystery ? Responses

The sum of all positive even values in arr

Consider the following code segment. num += num; num *= num; Assume that num has been previously declared and initialized to contain an integer value. Which of the following best describes the behavior of the code segment? Responses

The value of num is the square of twice its original value.

The class Worker is defined below. The class includes the method getEarnings, which is intended to return the total amount earned by the worker. public class Worker { private double hourlyRate; private double hoursWorked; private double earnings; public Worker(double rate, double hours) { hourlyRate = rate; hoursWorked = hours; } private void calculateEarnings() { double earnings = 0.0; earnings += hourlyRate * hoursWorked; } public double getEarnings() { calculateEarnings(); return earnings; } } The following code segment appears in a method in a class other than Worker. The code segment is intended to print the value 800.0, but instead prints a different value because of an error in the Worker class. Worker bob = new Worker(20.0, 40.0); System.out.println(bob.getEarnings()); Which of the following best explains why an incorrect value is printed?

The variable earnings in the calculateEarnings method is a local variable.

Consider the following class definition. public class Thing { public void talk() { System.out.print("Hello "); } public void name() { System.out.print("my friend"); } public void greet() { talk(); name(); } /* Constructors not shown */ } Which of the following code segments, if located in a method in a class other than Thing, will cause the message "Hello my friend" to be printed?

Thing a = new Thing();a.greet();

int x = 7; int y = 3; if ((x < 10) && (y < 0)) { System.out.println("Value is: " + x * y); } else { System.out.println("Value is: " + x / y); } What is printed as a result of executing the code segment?

Value is: 2

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

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

Assume obj1 and obj2 are object references. Which of the following best describes when the expression obj1 == obj2 is true?

When obj1 and obj2 refer to the same object

Consider the following class. public class WindTurbine { private double efficiencyRating; public WindTurbine() { efficiencyRating = 0.0; } public WindTurbine(double e) { efficiencyRating = e; } } Which of the following code segments, when placed in a method in a class other than WindTurbine, will construct a WindTurbine object wt with an efficiencyRating of 0.25 ?

WindTurbine wt = new WindTurbine(0.25);

Which of the following best describes the error in the method? public static int biggest(int a, int b, int c) { if ((a > b) && (a > c)) { return a; } else if ((b > a) && (b > c)) { return b; } else { return c; } }

biggest may not work correctly when a and b have equal values.

Consider the following statement, which assigns a value to b1. boolean b1 = true && (17 % 3 == 1); Which of the following assigns the same value to b2 as the value stored in b1 ? Responses

boolean b2 = false && (17 % 3 == 2);

Consider the following method. /** Precondition: bound >= 0 */ public int sum(int bound) { int answer = 0; for (int i = 0; i < bound; i++) { answer += bound; } return answer; } Assume that sum is called with a parameter that satisfies the precondition and that it executes without error. How many times is the test expression i < bound in the for loop header evaluated?

bound + 1

Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10, 10, 10, 15, 10, 10], the call findLongest(10) should return 3, the length of the longest consecutive block of 10s. private int[] nums; public int findLongest(int target) { int lenCount = 0; int maxLen = 0; Line 1: for (int val : nums) Line 2: { Line 3: if (val == target) Line 4: { Line 5: lenCount++; Line 6: } Line 7: else Line 8: { Line 9: if (lenCount > maxLen) Line 10: { Line 11: maxLen = lenCount; Line 12: } Line 13: } Line 14: } Line 15: if (lenCount > maxLen) Line 16: { Line 17: maxLen = lenCount; Line 18: } Line 19: return maxLen; } The method findLongest does not work as intended. Which of the following best describes the value returned by a call to findLongest?

c. It is the number of occurrences of the value target in nums.

Consider the following two code segments where the int variable choice has been properly declared and initialized. Code Segment A if (choice > 10) { System.out.println("blue"); } else if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Code Segment B if (choice > 10) { System.out.println("blue"); } if (choice < 5) { System.out.println("red"); } else { System.out.println("yellow"); } Assume that both code segments initialize choice to the same integer value. Which of the following best describes the conditions on the initial value of the variable choice that will cause the two code segments to produce different output? Responses

choice > 10

Consider the following output. 1 1 1 1 1 2 2 2 2 3 3 3 4 4 5 Which of the following code segments will produce this output?

d. for (int j = 1; j <= 5; j++) { for (int k = 5; k >= j; k--) { System.out.print(j + " "); } System.out.println(); }

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

Assume that x and y are boolean variables and have been properly initialized. (x && y) && !(x || y) Which of the following best describes the result of evaluating the expression above? Responses

false always

Consider the following code segment. for (int k = 1; k <= 7; k += 2) { System.out.print(k); } Which of the following code segments will produce the same output as the code segment above? Responses

for (int k = 1; k <= 8; k += 2) { System.out.print(k); }

Consider the following code segment. public class YourClassName { public static void main(String[] args) { for (int k = 1; k <= 100; k++) { if ((k % 4) == 0) { System.out.println(k); } } } } Which of the following code segments will produce the same output as the code segment above?

for (int k = 4; k <= 100; k += 4) { System.out.println(k); }

Consider the following code segment. int count = 0; for (int k = 0; k < 10; k++) { count++; } System.out.println(count); Which of the following code segments will produce the same output as the code segment above?

int count = 0; for (int k = 9; k >= 0; k--) { count++; } System.out.println(count);

Consider the following code segment. for (int outer = 0; outer < 3; outer++) { for (/* missing loop header */) { System.out.print(outer + "" + inner + "_"); } } Which of the following can be used as a replacement for /* missing loop header */ so that the code segment produces the output 00_01_02_11_12_22_ ?

int inner = outer; inner < 3; inner++

Consider the following code segment, which is intended to print the digits of the two-digit int number num in reverse order. For example, if num has the value 75, the code segment should print 57. Assume that num has been properly declared and initialized. /* missing code */ System.out.print(onesDigit); System.out.print(tensDigit); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? Responses

int onesDigit = num % 10; int tensDigit = num / 10;

Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ?

int rn = (int) (Math.random() * 10) + 1;

Which of the following statements stores the value 3 in x ?

int x = 8 % 5;

Consider the following methods, which appear in the same class. public int function1(int i, int j) { return i + j; } public int function2(int i, int j) { return j - i; } Which of the following statements, if located in a method in the same class, will initialize the variable x to 11?

int x = function1(4, 5) + function2(1, 3);

Consider the following partial class declaration. public class SomeClass { private int myA; private int myB; private int myC; // Constructor(s) not shown public int getA() { return myA; } public void setB(int value) { myB = value; } } The following declaration appears in another class. SomeClass obj = new SomeClass(); Which of the following code segments will compile without error? int x = obj.getA() int x; obj.getA(x); int x = obj.myA; int x = SomeClass.getA(); int x = getA(obj);

int x = obj.getA ( );

Consider the following method. public int[] addNum(int[] array, int first, int second, int num) { int[] newArray = new int[array.length]; newArray[first] = array[first] + num; newArray[second] = array[second] + num; return newArray; } Which of the following code segments, appearing in the same class as the addNum method, will result in array2 having the contents {0, 0, 13, 0, 9, 0, 0} ?

int[] array1 = {5, 2, 8, 6, 4, 3, 9}; int[] array2 = addNum(array1, 2, 4, 5);

Consider the following code segment. int num1 = 0; int num2 = 3; while ((num2 != 0) && ((num1 / num2) >= 0)) { num1 = num1 + 2; num2 = num2 - 1; } What are the values of numl and num2 after the while loop completes its execution?

num1 = 6, num2 = 0

Consider the following method, which is intended to return the product of 3 and the nonnegative difference between its two int parameters. public int threeTimesDiff (int num1, int num2) { return 3 * (num1 - num2); } Which, if any, precondition is required so that the method works as intended for all values of the parameters that satisfy the precondition? Responses

num1 >= num2

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)printSome(20, 5)

The Car class will contain two string attributes for a car's make and model. The class will also contain a constructor. public class Car { /* missing code */ } Which of the following replacements for /* missing code */ is the most appropriate implementation of the class? Responses

private String make; private String model; public Car(String myMake, String myModel) { /* implementation not shown */ }

The Player class below will contain two int attributes and a constructor. The class will also contain a method getScore that can be accessed from outside the class. public class Player { /* missing code */ } Which of the following replacements for /* missing code */ is the most appropriate implementation of the class?

private int score; private int id; public Player(int playerScore, int playerID) { /* implementation not shown */ } public int getScore() { /* implementation not shown */ }

Assume that the following declaration appears in a class other than Dog.Dog fido = new UnderDog ( ) ;What is printed as a result of the call fido.act() ? public class Dog { public void act() { System.out.print("run "); eat(); } public void eat() { System.out.println("eat "); } } public class UnderDog extends Dog { public void act() { super.act(); System.out.print("sleep "); } public void eat() { super.eat(); System.out.println("bark "); } }

run eat bark sleep

Assume that x and y are boolean variables and have been properly initialized. An expression reads as follows: (x || y) && x Which of the following always evaluates to the same value as the expression above? Responses

x

Consider the following class declarations. public class Shoe { private String shoeBrand; private String shoeModel; public Shoe(String brand, String model) { shoeBrand = brand; shoeModel = model; } // No other constructors } public class Boot extends Shoe { private double heelHeight; public Boot(String brand, String model, double height) { /* missing implementation */ } } Which of the following should be used to replace /* missing implementation */ so that all instance variables are initialized with parameters? Responses

super(brand, model); heelHeight = height;

Consider the following code segment. int a = 10; int b = 5 * 2; System.out.print(a == b); What is printed as a result of executing the code segment?

true

Consider the following code segment. int[] numbers = new int[5]; numbers[0] = 2; numbers[1] = numbers[0] + 1; numbers[numbers[0]] = numbers[1]; for (int x = 3; x < numbers.length; x++) { numbers[x] = numbers[x - 1] * 2; } Which of the following represents the contents of the array numbers after the code segment is executed?

{2, 3, 3, 6, 12}

Consider the following code segment. int[] numbers = new int[5]; numbers[0] = 2; numbers[1] = numbers[0] + 1; numbers[numbers[0]] = numbers[1]; for (int x = 3; x < numbers.length; x++) { numbers[x] = numbers[x - 1] * 2; } Which of the following represents the contents of the array numbers after the code segment is executed?

{2, 3, 3, 6, 12}

Assume that x and y are variables of type int. Which of the following Java expressions never results in a division by zero? Responses

(x != 0) && ((y / x) == 0)

public class TimeRecord { private int hours; private int minutes; // 0 < minutes < 60 /** Constructs a TimeRecord object. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: 0 < m < 60 */ public TimeRecord(int h, int m) { hours = h; minutes = m; } /** @return the number of hours */ public int getHours() { / implementation not shown / } /** @return the number of minutes * Postcondition: 0 < minutes < 60 */ public int getMinutes() { / implementation not shown / } /** Adds h hours and m minutes to this TimeRecord. * @param h the number of hours * Precondition: h > 0 * @param m the number of minutes * Precondition: m > 0 */ public void advance(int h, int m) { hours = hours + h; minutes = minutes + m; / missing code / } // Other methods not shown } Which of the following can be used to replace / missing code / so that advance will correctly update the time?

c. hours = hours + minutes / 60;minutes = minutes % 60;

Consider the following data field and method. private int[] seq; // precondition: seq.length > 0 public int lenIncreasing() { int k = 1; while ((k < seq.length) && (seq[k - 1] < seq[k])) { k++; } // assertion return k; } Which of the following assertions is true when execution reaches the line//assertioninlenIncreasing?

(k == seq.length) || (seq[k - 1] >= seq[k])

Consider the following code segment, which is intended to assign to num a random integer value between min and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than the value of min. double rn = Math.random(); int num = /* missing code */; Which of the following could be used to replace /* missing code */ so that the code segment works as intended? Responses

(int) (rn * (max - min + 1)) + min

Consider the following class declaration public class SomeClass { private int num; public SomeClass(int n) { num = n; } public void increment(int more) { num = num + more; } public int getNum() { return num; } } The following code segment appears in another class. SomeClass one = new SomeClass(100); SomeClass two = new SomeClass(100); SomeClass three = one; one.increment(200); System.out.println(one.getNum() + " " + two.getNum() + " " + three.getNum()); What is printed as a result of executing the code segment?

300 100 300

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.

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information. Consider the following partial class declaration. Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC ? Responses

Directions: Select the choice that best fits each statement. The following question(s) refer to the following information. Consider the following partial class declaration. Which of the following changes to SomeClass will allow other classes to access but not modify the value of myC ? Responses

Consider the following method. public void doSomething() { System.out.println("Something has been done"); } Each of the following statements appears in a method in the same class as doSomething. Which of the following statements are valid uses of the method doSomething ? l.doSomething(); ll.String output = doSomething(); ll.System.out.println(doSomething());

I only

Consider the following class declarations. Assume that each class has a no-argument constructor. public class Food { /* implementation not shown */ } public class Snack extends Food { /* implementation not shown */ } public class Pizza extends Snack { /* implementation not shown */ } Which of the following declarations will compile without error?

Food tacos = new Snack();

Consider the following code segment in which the int variable x has been properly declared and initialized. if (x % 2 == 1) { System.out.println("YES"); } else { System.out.println("NO"); } Assuming that x is initialized to the same positive integer value as the original, which of the following code segments will produce the same output as the original code segment? I. if (x % 2 == 1) { System.out.println("YES"); } if (x % 2 == 0) { System.out.println("NO"); } II. if (x % 2 == 1) { System.out.println("YES"); } else if (x % 2 == 0) { System.out.println("NO"); } else { System.out.println("NONE"); } III. boolean test = x % 2 == 0; if (test) { System.out.println("YES"); } else { System.out.println("NO"); }

I and II only

Consider the following code segment. int x = 5; int y = 6; /* missing code */ z = (x + y) / 2; Which of the following can be used to replace /* missing code */ so that the code segment will compile? l.int z = 0; ll.int z; lll.boolean z = false; Responses

I and II only

public class Item { private String itemName; private double regPrice; private double discountPercent; public Item (String name, double price, double discount) { itemName = name; regPrice = price; discountPercent = discount; } public Item (String name, double price) { itemName = name; regPrice = price; discountPercent = 0.25; } /* Other methods not shown */ } Which of the following code segments, found in a class other than Item, can be used to create an item with a regular price of $10 and a discount of 25% ? l.Item b = new Item("blanket", 10.0, 0.25); ll.Item b = new Item("blanket", 10.0); lll.Item b = new Item("blanket", 0.25, 10.0);

I and II only

public class Student { private String myName; private int myAge; public Student() { /* Implementation not shown */ } public Student(String name, int age) { /* Implementation not shown */ } // No other constructors. } Which of the following declarations will compile without error? l.Student a = new Student(); ll.Student b = new Student("Juan", 15); lll.Student c = new Student("Juan", "15");

I and II only

Consider the following attempts at method overloading. I. public class Overload { public int average(int x, int y) { /* implementation not shown */ } public int average(int value1, int value2) { /* implementation not shown */ } // There may be instance variables, constructors, // and methods that are not shown. } II. public class Overload { public int average(int x, int y) { /* implementation not shown */ } public int average(int x, int y, int z) { /* implementation not shown */ } // There may be instance variables, constructors // and methods that are not shown. } III. public class Overload { public int average(int x, int y) { /* implementation not shown */ } public int average(double x, double y) { /* implementation not shown */ } // There may be instance variables, constructors, // and methods that are not shown. } Which of the attempts at method overloading will compile without error?

II and III only

Consider the following class definitions. public class Data { private int x; public void setX(int n) { x = n; } // ... other methods not shown } public class EnhancedData extends Data { private int y; public void setY(int n) { y = n: } // ... other methods not shown } Assume that the following declaration appears in a client program. EnhancedData item = new EnhancedData(); Which of the following statements would be valid? I. item.y = 16; II. item.setY(16); III. item.setX(25);

II and III only

The variables years and months are used to represent the age of the item, and the value for months is always between 0 and 11, inclusive. Method updateAge is used to update these variables based on the parameter extraMonths that represents the number of months to be added to the age. private int years; private int months; // 0 <= months <= 11 // precondition: extraMonths >= 0 public void updateAge(int extraMonths) { /* body of updateAge */ } Which of the following code segments could be used to replace /* body of updateAge */ so that the method will work as intended? I. int yrs = extraMonths % 12; int mos = extraMonths / 12; years = years + yrs; months = months + mos; II. int totalMonths = years * 12 + months + extraMonths; years = totalMonths / 12; months = totalMonths % 12; III. int totalMonths = months + extraMonths; years = years + totalMonths / 12; months = totalMonths % 12;

II and III only

Consider the following incomplete method. public int someProcess(int n) { /* body of someProcess */ } The following table shows several examples of input values and the results that should be produced by calling someProcess. Input Value n Value Returned by someProcess(n) 3 30 6 60 7 7 8 80 9 90 11 11 12 120 14 14 16 160 Which of the following code segments could be used to replace /* body of someProcess */ so that the method will produce the results shown in the table? I. if ((n % 3 == 0) && (n % 4 == 0)) return n * 10; else return n; II. if ((n % 3 == 0) || (n % 4 == 0)) return n * 10; return n; III. if (n % 3 == 0) if (n % 4 == 0) return n * 10; return n; Responses

II only

Consider the following method. public String inRangeMessage(int value) { if (value < 0 || value > 100) return "Not in range"; else return "In range"; } Consider the following code segments that could be used to replace the body of inRangeMessage. I. if (value < 0) { if (value > 100) return "Not in range"; else return "In range"; } else return "In range"; II. if (value < 0) return "Not in range"; else if (value > 100) return "Not in range"; else return "In range"; III. if (value >= 0) return "In range"; else if (value <= 100) return "In range"; else return "Not in range"; Which of the replacements will have the same behavior as the original version of inRangeMessage ?

II only

The following questions refer to the following classes: public class First { public String name() { return "First"; } } public class Second extends First { public void whoRules() { System.out.print(super.name() + " rules"); System.out.println(" but " + name() + " is even better"); } public String name() { return "Second"; } } public class Third extends Second { public String name() { return "Third"; } } Consider the following code segment. /* SomeType1 */ varA = new Second();/* SomeType2 */ varB = new Third(); varA.whoRules();varB.whoRules(); Which of the following could be used to replace /* SomeType1 */ and /* SomeType2 */ so that the code segment will compile without error? /* SomeType1 *//* SomeType2 */I.FirstThirdII.SecondSecondIII.ThirdThird

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

Consider the following code segment, which is intended to find the average of two positive integers, x and y. int x; int y; int sum = x + y; double average = (double) (sum / 2); Which of the following best describes the error, if any, in the code segment? Responses

In the expression (double) (sum / 2), the cast to double is applied too late, so the average will be less than the expected result for odd values of sum.

When designing classes, which of the following would be the best reason to use inheritance?

Inheritance allows the creation of a subclass that can use the methods of its superclass without rewriting the code for those methods.

The following question refer to the following information. Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { Line 1: int max = maxHelper(numVals - 1); Line 2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following corrects the methodmaxHelperso that it works as intended?

Insert the following statement before Line 1. if (numVals == 1 return nums[0];

Method findLongest is intended to find the longest consecutive block of the value target occurring in the array nums; however, findLongest does not work as intended. For example, if the array nums contains the values [7, 10, 10, 15, 15, 15, 15, 10,10, 10, 15, 10, 10], the call findLongest (10) should return 3, the length of the longest consecutive block of 10s. private int[] nums; public int findLongest(int target) { int lenCount = 0; int maxLen = 0; Line 1: for (int val : nums) Line 2: { Line 3: if (val == target) Line 4: { Line 5: lenCount++; Line 6: } Line 7: else Line 8: { Line 9: if (lenCount > maxLen) Line 10: { Line 11: maxLen = lenCount; Line 12: } Line 13: } Line 14: } Line 15: if (lenCount > maxLen) Line 16: { Line 17: maxLen = lenCount; Line 18: } Line 19: return maxLen; } Which of the following changes should be made so that method findLongest will work as intended?

Insert the statement lenCount = 0; between lines 12 and 13.

In the Toy class below, the raisePrice method is intended to increase the value of the instance variable price by the value of the parameter surcharge. The method does not work as intended. public class Toy { private String name; private double price; public Toy(String n, double p) { name = n; price = p; } public void raisePrice(double surcharge) // Line 12 { return price + surcharge; // Line 14 } Which of the following changes should be made so that the class definition compiles without error and the method raisePrice works as intended?

Replace line 14 with price += surcharge;.

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

onsider the following code segment. double a = 1.1; double b = 1.2; if ((a + b) * (a - b) != (a * a) - (b * b)) { System.out.println("Mathematical error!"); } Which of the following best describes why the phrase "Mathematical error!" would be printed? (Remember that mathematically (a + b) * (a - b) = a2 - b2 .)

Roundoff error makes the if condition true.

The following question refer to the following information. Consider the following data field and method. Method maxHelper is intended to return the largest value among the first numVals values in an array; however, maxHelper does not work as intended. private int[] nums; // precondition: 0 < numVals <= nums.length private int maxHelper(int numVals) { Line 1: int max = maxHelper(numVals - 1); Line 2: if (max > nums[numVals - 1]) return max; else return nums[numVals - 1]; } Which of the following best describes the conditions under which maxHelper does not work as intended? Responses

Method maxHelper never works as intended.

Consider the following code segment. for (int k = 0; k < 9; k = k + 2) { if ((k % 2) != 0) { System.out.print(k + " "); } } What, if anything, is printed as a result of executing the code segment?

Nothing is printed.

Consider the following code segment. int x = /* some integer value */ ; int y = /* some integer value */ ; boolean result = (x < y); result = ( (x >= y) && !result ); Which of the following best describes the conditions under which the value of result will be true after the code segment is executed?

Only when x >= y

Consider the following Point2D class. public class Point2D { private double xCoord; private double yCoord; public Point2D(double x, double y) { xCoord = x; yCoord = y; } } Which of the following code segments, appearing in a class other than Point2D, will correctly create an instance of a Point2D object?

Point2D p = new Point2D(3.0, 4.0);

Consider the following classes. public class Ticket { private double price; public Ticket(double p) { price = p; } public double getPrice() { return price; } public String toString() { return "Price is " + getPrice(); } } public class DiscountTicket extends Ticket { public DiscountTicket(double p) { super(p); } public double getPrice() { return super.getPrice() / 2.0; } } The following code segment appears in a class other than Ticket or DiscountTicket. Ticket t = new DiscountTicket(10.0); System.out.println(t); What output, if any, is produced when the code segment is executed?

Price is 5.0

Consider the following classes. public class Ticket { private double price; public Ticket(double p) { price = p; } public double getPrice() { return price; } public String toString() { return "Price is " + getPrice(); } } public class DiscountTicket extends Ticket { public DiscountTicket(double p) { super(p); } public double getPrice() { return super.getPrice() / 2.0; } } The following code segment appears in a class other than Ticket or DiscountTicket. Ticket t = new DiscountTicket(10.0); System.out.println(t); What output, if any, is produced when the code segment is executed? Responses

Price is 5.0

Consider the following two code segments. Code segment II is a revision of code segment I in which the loop header has been changed. I. for (int k = 1; k <= 5; k++) { System.out.print(k); } II. for (int k = 5; k >= 1; k--) { System.out.print(k); } Which of the following best explains how the output changes from code segment I to code segment II? Responses

The code segments print the same values but in a different order, because code segment I iterates from 1 to 5 and code segment II iterates from 5 to 1.

Consider the following output. 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Which of the following code segments will produce the output shown above?

for (int j = 1; j <= 6; j++) { for (int k = 1; k <= j; k++) System.out.print(" " + k); System.out.println(); }

Consider the following code segment. for (int j = 1; j < 10; j += 2) { System.out.print(j); } Which of the following code segments will produce the same output as the code segment above? Responses

int j = 1; while (j < 10) { System.out.print(j); j += 2; }

Assume that an array of integer values has been declared as follows and has been initialized. int[] arr = new int[10]; Which of the following code segments correctly interchanges the value of arr[0] and arr[5] ?

int k = arr[0]; arr[0] = arr[5]; arr[5] = k;

public int compute(int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) { answer *= n; } return answer; }public int compute(int n, int k) { int answer = 1; for (int i = 1; i <= k; i++) { answer *= n; } return answer; } Which of the following represents the value returned as a result of the call compute (n, k) ? Responses

n^k

Consider the following class definition. public class Tester { private int num1; private int num2; /* missing constructor */ } The following statement appears in a method in a class other than Tester. It is intended to create a new Tester object t with its attributes set to 10 and 20. Tester t = new Tester(10, 20); Which of the following can be used to replace /* missing constructor */ so that the object t is correctly created?

public Tester(int first, int second) { num1 = first; num2 = second; }

Assume that the following variable declarations have been made. double d = Math.random(); double r; Which of the following assigns a value to r from the uniform distribution over the range 0.5 ≤ r < 5.5 ?

r = d * 5.0 + 0.5;

Consider the following code segment. boolean[] oldVals = {true, false, true, true}; boolean[] newVals = new boolean[4]; for (int j = oldVals.length - 1; j >= 0; j--) { newVals[j] = !(oldVals[j]); } What, if anything, will be the contents of newVals as a result of executing the code segment?

{false, true, false, false}


Conjuntos de estudio relacionados

ATI CLINICAL DECISION MAKING: Clinical Judgement Process, Managing Client Care, priority-setting framework

View Set

Module 8: Mappes, "Sexual Morality and the Concept of Using Another Person

View Set

5-5 Social Security and Medicare

View Set

Respiratyory practice questions

View Set

Anatomy and Physiology Lab Quizs Test 3

View Set

Holistics 2: unit 1-mind, body, and soul: understanding mental health

View Set