Comp said semester 1 finak

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

Consider the following code segment. int k = 0; while (k < 10) { System.out.print((k % 3) + " "); if ((k % 3) == 0) k = k + 2; else k++; }

0 2 0 2 0 2 0

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 code segment. if (false && true || false) { if (false || true && false) { System.out.print("First"); } else { System.out.print("Second"); } } if (true || true && false) { System.out.print("Third"); } What is printed as a result of executing the code segment?

Third

Consider the following code segment. ArrayList<Integer> oldList = new ArrayList(); oldList.add(100); oldList.add(200); oldList.add(300); oldList.add(400); ArrayList<Integer> newList = new ArrayList(); newList.add(oldList.remove(1)); newList.add(oldList.get(2)); System.out.println(newList); What, if anything, is printed as a result of executing the code segment?

[200, 400]

Consider the following statement. boolean x = (5 < 8) == (5 == 8); What is the value of x after the statement has been executed?

false

Consider the following Boolean expression in which the int variables x and y have been properly declared and initialized. (x <= 10) == (y > 25) Which of the following values for x and y will result in the expression evaluating to true ?

x = 10 and y = 30

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?

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

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?

2.0 2

Consider the following code segment. String str = "a black cat sat on a table"; int counter = 0; for (int i = 0; i < str.length() - 1; i++) { if (str.substring(i, i + 1).equals("a") && !str.substring(i + 1, i + 2).equals("b")) { counter++; } } System.out.println(counter);

5

I. 5 0 5 II. 7 4 9 III. 3 5 2

II. only

Consider the following recursive method. public static void stars(int num) { if (num == 1) { return; } stars(num - 1); for (int i = 0; i < num; i++) { System.out.print("*"); } System.out.println(); } What is printed as a result of the method call stars(5) ?

** *** **** *****

Consider the following code segment. String str = "0"; str += str + 0 + 8; System.out.println(str); What is printed as a result of executing the code segment?

0008

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?

16

In the following code segment, assume that the ArrayList wordList has been initialized to contain the String values ["apple", "banana", "coconut", "lemon", "orange", "pear"]. int count = 0; for (String word : wordList) { if (word.indexOf("a") >= 0) { count++; } } System.out.println(count); What is printed as a result of executing the code segment?

4

Consider the following code segment. String oldStr = "ABCDEF"; String newStr = oldStr.substring(1, 3) + oldStr.substring(4); System.out.println(newStr); What is printed as a result of executing the code segment?

BCEF

Consider the following method. public void conditionalTest(int a, int b) { if ((a > 0) && (b > 0)) { if (a > b) System.out.println("A"); else System.out.println("B"); } else if ((b < 0) || (a < 0)) System.out.println("C"); else System.out.println("D"); } What is printed as a result of the call conditionalTest(3, -2)?

C

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?

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

Consider the following methods. /** 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

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 ?

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

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

int rn = (int)(Math.random() * 36) + 25;

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

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 correct implementation of the selection sort algorithm. public static void selectionSort(int[] elements) { for (int j = 0; j < elements.length - 1; j++) { int minIndex = j; for (int k = j + 1; k < elements.length; k++) { if (elements[k] < elements[minIndex]) { minIndex = k; } } if (j != minIndex) { int temp = elements[j]; elements[j] = elements[minIndex]; elements[minIndex] = temp; // Line 19 } } } The following declaration and method call appear in a method in the same class as selectionSort. int[] arr = {9, 8, 7, 6, 5}; selectionSort(arr); How many times is the statement elements[minIndex] = temp; in line 19 of the method executed as a result of the call to selectionSort ?

2

Consider the following code segment. ArrayList<String> numbers = new ArrayList<String>(); numbers.add("one"); numbers.add("two"); numbers.add(0, "three"); numbers.set(2, "four"); numbers.add("five"); numbers.remove(1); Which of the following represents the contents of numbers after the code segment has been executed?

["three", "four", "five"]

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 ?

"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 the boolean variables a, b, c, and d have been declared and initialized. Consider the following expression. !( !( a && b ) || ( c || !d )) Which of the following is equivalent to the expression?

( a && b) && (!c&&d)

The question refer to the following code segment. int k = a random number such that 1 ≤ k ≤ n ; for (int p = 2; p <= k; p++) for (int r = 1; r < k; r++) System.out.println("Hello"); What is the maximum number of times that Hello will be printed?

(n-1)^2

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

Consider the following code segment. int num = 1; int count = 0; while (num <= 10) { if (num % 2 == 0 && num % 3 == 0) { count++; } num++; } What value is stored in the variable count as a result of executing the code segment?

1

Consider the following code segment. 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 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?

5

Consider the following code segment. int start = 4; int end = 5; boolean keepGoing = true; if (start < end && keepGoing) { if (end > 0) { start += 2; end++; } else { end += 3; } } if (start < end) { if (end == 0) { end += 2; start++; } else { end += 4; } } What is the value of end after the code segment is executed?

6

Consider the following code segment. String str = "CompSci"; System.out.println(str.substring(0, 3)); int num = str.length(); What is the value of num when the code segment is executed?

7

Consider the following code segment. int[][] anArray = new int[10][8]; for (int j = 0; j < 8; j++) { for (int k = 0; k < 10; k++) { anArray[j][k] = 5; } } The code segment causes an ArrayIndexOutOfBoundsException to be thrown. How many elements in anArray will be set to 5 before the exception is thrown?

8

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?

9

Consider the following statement, which is intended to create an ArrayList named theater_club to store elements of type Student. Assume that the Student class has been properly defined and includes a no-parameter constructor. ArrayList<Student> theater_club = new /* missing code */; Which choice can replace /* missing code */ so that the statement compiles without error?

ArrayList<Student>()

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 ? doSomething(); String output = doSomething(); System.out.println(doSomething());

I only

In the code segment below, assume that the int variable n has been properly declared and initialized. The code segment is intended to print a value that is 1 more than twice the value of n. /* missing code */ System.out.print(result); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? int result = 2 * n; result = result + 1; int result = n + 1; result = result * 2; int result = (n + 1) * 2;

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

Assume that the boolean variables a and b have been declared and initialized. Consider the following expression. (a && (b || !a)) == a && b Which of the following best describes the conditions under which the expression will evaluate to true?

Only when be is true

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.

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;

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

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

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

Consider the following code segment. int val = 1; while (val <= 6) { for (int k = 0; k <= 2; k++) { System.out.println("Surprise!"); } val++; } How many times is the string "Surprise!" printed as a result of executing the code segment?

18

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

The following incomplete method is intended to sort its array parameter arr in increasing order. // postcondition: arr is sorted in increasing order public static void sortArray(int[] arr) { int j, k; for (j = arr.length - 1; j > 0; j--) { int pos = j; for ( /* missing code */ ) { if (arr[k] > arr[pos]) { pos = k; } } swap(arr, j, pos); } } Assume that swap(arr, j, pos) exchanges the values of arr[j] and arr[pos]. Which of the following could be used to replace /* missing code */ so that executing the code segment sorts the values in array arr?

k = j -1; k>= 0; k- -

Consider the following code segment. 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

Consider the following class definition. public class Value { private int num; public int getNum() { return num; } // There may be instance variables, constructors, and methods not shown. } The following method appears in a class other than Value. It is intended to sum all the num instance variables of the Value objects in its ArrayList parameter. /** Precondition: valueList is not null */ public static int getTotal(ArrayList<Value> valueList) { int total = 0; /* missing code */ return total; } Which of the following code segments can replace /* missing code */ so the getTotal method works as intended? for (int x = 0; x < valueList.size(); x++) { total += valueList.get(x).getNum(); } for (Value v : valueList) { total += v.getNum(); } for (Value v : valueList) { total += getNum(v); }

I and II

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 code segment. ArrayList<Double> conditionRating = new ArrayList<Double>(); conditionRating.add(9.84); conditionRating.add(8.93); conditionRating.add(7.65); conditionRating.add(6.24); conditionRating.remove(2); conditionRating.set(2, 7.63); System.out.println(conditionRating); What is printed when this code segment is executed?

[9.84, 8.93, 7.63]

Consider the following code segment. ArrayList<Integer> nums = new ArrayList<Integer>(); nums.add(new Integer(37)); nums.add(new Integer(3)); nums.add(new Integer(0)); nums.add(1, new Integer(2)); nums.set(0, new Integer(1)); nums.remove(2); System.out.println(nums); What is printed as a result of executing the code segment?

[1, 2, 0]

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]


Set pelajaran terkait

Chpt 12: Interest Groups and Civic and Political Engagement

View Set

PSYCH 316 FINAL EXAM STUDY GUIDE

View Set

CIS 310 Management Information Systems

View Set

Honors Biology: Chp 15 Study Guide

View Set

Chapter 2: The marketing environment and market analysis

View Set

Exam 4: Cardiovascular Dysfunction NCLEX Questions

View Set