Combo with "Java quiz chapter 4" and 1 other

Ace your homework & exams now with Quizwiz!

2. Each object of a class has its own set of ___. A) methods B) instance variables C) constructors D) classes

Ans: B Section Ref: 3.1 Instance Variables and Encapsulation Title: Each object of a class has its own set of: Difficulty: Eas

29) What is the result when the following code is run? double x = 1; double y = 1; int i = 0; do { y = x / 2; x = x + y; i = i + 1; } while (x < 2.5); System.out.print(i + " "); a) 1 b) 2 c) 3 d) 4

Answer: c) 3

88) How many times is the text "Let's have fun with Java." printed when this code snippet is run? int i = 0; do { System.out.println("Let's have fun with Java."); i++; if (i % 2 == 0) { i = 10; } } while (i <= 10); a) 1 b) 2 c) 3 d) 10

Answer: c) 3

97) How many times does the following loop run? int i = 0; int j = 1; do { System.out.println("" + i + ";" + j); i++; if (i % 3 == 0) { j--; } } while (j >= 1); a) 1 time b) 2 times c) 3 times d) 4 times

Answer: c) 3 times

15) Which loop does not check a condition at the beginning of the loop? I. The do loop II. The while loop III. The for loop a) I and II b) I and III c) I only d) III only

Answer: c) I only

73) Which of the following loop(s) should be used when you need to ask a user to enter one data item and repeat the prompt if the data is invalid? I. for loop II. while loop III. do loop a) I only b) I and II c) III only d) II only

Answer: c) III only

74) Which of the loop(s) test the condition after the loop body executes? I. for loop II. while loop III. do loop a) I only b) II only c) III only d) II and III

Answer: c) III only

94) Which statement is correct about the execution of the loop in the following code fragment? double num; int incr = 0; Scanner reader = new Scanner(System.in); do { incr = incr + 1; System.out.println("Please enter a number (0 when done): "); num = reader.nextDouble(); } while (num != 0); System.out.println("" + incr); a) The loop will execute only when 0 is entered. b) The execution of the loop is independent of user input. c) The program prints the count of positive inputs. d) The loop will execute at least once even if the user has entered the sentinel value.

Answer: d) The loop will execute at least once even if the user has entered the sentinel value.

77. When hand-tracing a portion of code, which statement about Boolean conditions is true? a) They typically are too complex to be evaluated. b) They do not need to be monitored because their result usually is not stored in a variable. c) It is rare to encounter a Boolean condition. d) They are crucial to evaluate since they determine if-statement conditions and looping.

Answer: d) They are crucial to evaluate since they determine if-statement conditions and looping.

Which of the following operators is used to invert a conditional statement? a) ! b) != c) || d) ?

a) !

99) What is the output of the following code? int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; System.out.println(counts[3].length); a) 2 b) 3 c) 4 d) 5

a) 2

When drawing flowcharts, unconstrained branching and merging can lead to a) So-called "spaghetti code" b) A better design c) Intuitive understanding of the flow of control d) Clear visualization of the problem solution

a) So-called "spaghetti code"

78) What is the value of the cnt variable after the execution of the code snippet below? ArrayList<Integer> somenum = new ArrayList<Integer>(); somenum.add(1); somenum.add(2); somenum.add(1); int cnt = 0; for (int index = 0; index < somenum.size(); index++) { if (somenum.get(index) % 2 == 0) { cnt++; } } a) 1 b) 2 c) 3 d) 0

a. 1

What is the output of the following code snippet? double income = 45000; double cutoff = 55000; double minIncome = 30000; if (minIncome > income) { System.out.println("Minimum income requirement is not met."); } if (cutoff < income) { System.out.println("Maximum income limit is exceeded."); } else { System.out.println("Income requirement is met."); } a) Minimum income requirement is not met. b) Maximum income limit is exceeded. c) Income requirement is met. d) There is no output.

c) Income requirement is met.

61) Consider the following code snippet: int[][] arr = { { 13, 23, 33 }, { 14, 24, 34 } }; Identify the appropriate statement to display the value 24 from the given array? a) System.out.println(arr[1][2]); b) System.out.println(arr[2][2]); c) System.out.println(arr[1][1]); d) System.out.println(arr[2][1]);

c) System.out.println(arr[1][1]);

Assuming that the user inputs "twenty" as the input, what is the output of the following code snippet? String numEmployeesStr; Scanner in = new Scanner(System.in); System.out.println("Please enter the number of your employees: "); numEmployeesStr = in.next(); int numEmployees = Integer.parseInt(numEmployeesStr); if (numEmployees < 10) { System.out.println("Very small business!"); } else { System.out.println("Small business"); if (numEmployees > 100) { System.out.println("Mid size business"); } else { System.out.println("Large business"); } } a) Very small business! b) Small business c) Mid size business d) Run-time error

d) Run-time error

1) Consider the following line of code: int[] somearray = new int[30]; Which one of the following options is a valid line of code for displaying the twenty-eighth element of somearray? a) System.out.println(somearray[28]); b) System.out.println(somearray(28)); c) System.out.println(somearray(27)); d) System.out.println(somearray[27]);

d) System.out.println(somearray[27]);

51) What is the output of the following code snippet? public static void main(String[] args) { String[] arr = { "aaa", "bbb", "ccc" }; mystery(arr); System.out.println(arr[0] + " " + arr.length); } public static void mystery(String[] arr) { arr = new String[5]; arr[0] = "ddd"; } a) ddd 5 b) ddd 3 c) aaa 5 d) aaa 3

d) aaa 3

The flow chart shows the order in which steps should be executed, and the diamond-shaped boxes indicate a) input b) algorithms c) tasks d) conditional tests

d) conditional tests

Which of the following conditions tests whether the user enters the single digit 5? String s; Scanner in = new Scanner(System.in); System.out.print("Enter a single digit: "); s = in.next(); a) if (s == "5") b) if (s == '5') c) if (s = "5") d) if (s.equals("5"))

d) if (s.equals("5"))

54) Consider the following 2-dimensional array. Select the statement that gives the number of columns in the third row. int[][] counts = { { 0, 0, 1 }, { 0, 1, 1, 2 }, { 0, 0, 1, 4, 5 }, { 0, 2 } }; a) int cols = counts[2].size(); b) int cols = counts.length[2]; c) int cols = counts.length; d) int cols = counts[2].length;

d) int cols = counts[2].length;

49) Which one of the following is the correct header for a method named arrMeth that is called like this: arrMeth(intArray); // intArray is an integer array of size 3 a) public static void arrMeth(int[] ar, int n) b) public static void arrMeth(r[], n) c) public static void arrMeth(int n , int[] ar) d) public static void arrMeth(int[] ar)

d) public static void arrMeth(int[] ar)

The operator !> stands for a) not less than. b) not greater than. c) not equal to. d) this is not an operator in Java

d) this is not an operator in Java

1. What does an object store its data in? A) files B) methods C) instance variables D) access specifiers

Ans: C Section Ref: 3.1 Instance Variables and Encapsulation Title: What does an object store its data in? Difficulty: Easy

56) What is the output of the following code snippet? int counter = 1; for (double i = 0.01; i <= 1.0; i = i + 0.01) { counter++; } System.out.println(counter); a) 100 b) 49.50 c) 60.5 d) 10

Answer: a) 100

52) Choose the loop that is equivalent to this loop. int n = 1; double x = 0; double s; do { s = 1.0 / (n * n); x = x + s; n++; } while (s > 0.01); a) double x = 0; double s = 1; for (int k = 1; s > 0.01; k++) { s = 1.0 / (k * k); x = x + s; } b) double x = 0; double s = 1; for (int k = 1; k < 100; k++) { s = 1.0 / (k * k); x = x + s; } c) double x = 0; double s = 1; int k = 10; while (s > 0.01) { s = 1.0 / (k * k); x = x + s; k++; } d) double x = 0; double s = 10; int k = 1; while (s > 0.01) { s = 1.0 / (k * k); x = x + s; k++;

Answer: a) double x = 0; double s = 1; for (int k = 1; s > 0.01; k++) { s = 1.0 / (k * k); x = x + s; }

71) Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the following code snippets simulates that random number? a) (long) (Math.random() * 1000000 + 1) b) (long) (Math.random() * 100000 + 1) c) (long) (Math.random() * 9999990 + 1) d) (long) (Math.random() * 1000001 + 1)

Answer: a) (long) (Math.random() * 1000000 + 1)

18) What is the first and last value of i to be displayed by the following code snippet? int n = 20; for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { System.out.println("" + i); } } a) 0 and 20 b) 1 and 20 c) 0 and 19 d) 1 and 19

Answer: a) 0 and 20

106) What is the output of this code snippet? int s = 1; int n = 1; do { s = s + n; System.out.print(s + " "); n++; } while (s < 3 * n); a) 2 4 7 11 16 22 b) 1 3 5 7 9 c) 2 3 5 6 7 d) 2 4 6 8

Answer: a) 2 4 7 11 16 22

69) What is the output of the following loop? int s = 1; int n = 1; do { s = s + n; n++; } while (s < 10 * n); System.out.println(s); a) 211 b) 210 c) 120 d) 123

Answer: a) 211

98) How many times does the following loop execute? int upperCaseLetters = 0; String str = "abcdefgHIJKL"; boolean found = false; for (int i = str.length() - 1; i >= 0 && !found; i--) { char ch = str.charAt(i); if (Character.isUpperCase(ch)) { upperCaseLetters; } else { found = false; } } a) 6 times b) 8 times c) 9 times d) 12 times

Answer: a) 6 times

83) Which statement about storyboards is true? a) A storyboard can help prevent potential user confusion early in the design process. b) Storyboards are used primarily to understand how implemented programs work. c) The storyboard helps to train users about how to use software. d) Storyboards have no relationship to the structure of an actual working program.

Answer: a) A storyboard can help prevent potential user confusion early in the design process.

58) What does the following code snippet display? char ch1 = '\ u0000'; char ch2 = '\ uffff'; for (int i = 0; i < 1000; i++) { if (i % 50 == 0) { System.out.println(); } System.out.print((char)(ch1 + Math.random() * (ch2 - ch1 + 1))); } a) It displays random Unicode characters. b) It displays random ASCII characters. c) Nothing because it has compilation error. d) It displays the hexadecimal characters between '0' and 'F'

Answer: a) It displays random Unicode characters.

81. When designing storyboards, it is a good idea to use different colors to a) Make it easy to distinguish between user input and program output. b) Match the colors your program will use when it is finally designed. c) Emphasize the difference between numbers and words. d) Draw lines to divide up panels into different regions.

Answer: a) Make it easy to distinguish between user input and program output.

90) Which for loop prints data across each row in the following code snippet? int i; int j; for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { System.out.print("X"); } System.out.println(""); } a) The inner for loop b) The outer for loop c) Both for loops d) Another missing for loop

Answer: a) The inner for loop

76. When hand tracing, drawing a line through the value stored in a variable means that a) The value stored there has changed to something new b) The variable is the wrong data type for the code being executed c) The expression being evaluated uses that variable d) The variable must be inside a loop

Answer: a) The value stored there has changed to something new

92) In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop? int i; int j; for (i = 0; i <= 9; i++) { for (j = 1; j < 5; j++) { System.out.println("Hello"); } } a) When the value of j becomes 5 b) When the program executes completely c) When the condition for the outer loop is met d) When the value of i is incremented

Answer: a) When the value of j becomes 5

28) In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop? int i; int j; for (i = 0; i <= 9; i++) { for (j = 1; j < 5; j++) { System.out.println("Hello"); if (j == 2) { j = 6; } } } a) When the value of j becomes 6 b) When the program executes completely c) When the condition for the outer loop is met d) When the value of i is incremented

Answer: a) When the value of j becomes 6

38) What is the data type of the number generated by the Math.random() function? a) double b) float c) int d) String

Answer: a) double

44) Which of the following is considered a loop with a bound that could be problematic? a) for (i = 1; i != n; i++) b) for (years = n; years < 0; years--) c) for (i = 1; i <= n; i++) d) for (int i = 1; i <= 10; i++)

Answer: a) for (i = 1; i != n; i++)

66) Which of the following is considered a loop with a problematic condition? a) for(i = 1; i != n; i++) b) for (years = n; years > 0; years++) c) for(i = 1; i <= n; i++) d) for (int i = 20; i >= 10; i--)

Answer: a) for(i = 1; i != n; i++)

8) What are the values of i and j after the following code fragment runs? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i=" + i + ", j=" + j); a) i = 1951, j = 0 b) i = 1951, j = 45 c) i = 65, j = 1 d) i = 65, j = 45

Answer: a) i = 1951, j = 0

85) Which of the following code snippets displays the output exactly 10 times? a) int i = 0; while (i < 10); { System.out.println("This is example 1."); i++; } b) int i = 0; while (i < 10) { System.out.println("This is example 2."); i++; } c) int i = 0; while (i < 10) { System.out.println("This is example 3."); } d) int i = 1; while (i < 10) { System.out.println("This is example 4."); i++;

Answer: b) int i = 0; while (i < 10) { System.out.println("This is example 2."); i++; }

63) Which code snippet produces the sum of the first n even numbers? (0 is not considered an even number.) a) int sum = 0; for (int i = 1; i <= n; i++) { if (i % 2 == 0) { sum = sum + i; } } b) int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i * 2; } c) int sum = 0; for (int i = 0; i < n; i++) { if (i % 2 == 0) { sum = sum + i; } } d) int sum; for (int i = 1; i <= n; i++) { sum = sum + i * 2;

Answer: b) int sum = 0; for (int i = 1; i <= n; i++) { sum = sum + i * 2; }

53) What is the output of the following code snippet? int f1 = 0; int f2 = 1; int fRes; System.out.print(f1 + " "); System.out.print(f2 + " "); for (int i = 1; i < 10; i++) { fRes = f1 + f2; System.out.print(fRes + " "); f1 = f2; f2 = fRes; } System.out.println(); a) 0 1 5 7 9 11 13 15 17 19 b) 0 1 1 2 3 5 8 13 21 34 c) 0 1 4 6 8 10 12 14 16 18 d) 0 1 6 7 9 12 14 17 19 21

Answer: b) 0 1 1 2 3 5 8 13 21 34

40) What is the output of the following code snippet? int i = 1; while (i < 20) { System.out.print(i + " "); i = i + 2; if (i == 15) { i = 19; } } a) 1 3 5 7 9 11 13 15 17 19 b) 1 3 5 7 9 11 13 19 c) 1 3 5 7 9 11 13 15 17 d) 1 3 5 7 9 11 13 17 19

Answer: b) 1 3 5 7 9 11 13 19

6) What is the output of the following code snippet? int i = 1; while (i < 10) { System.out.print(i + " "); i = i + 2; if (i == 5) { i = 9; } } a) 1 3 5 b) 1 3 9 c) 1 3 5 7 9 d) 1 3 5 9

Answer: b) 1 3 9

46) What values does counter variable i assume when this loop executes? for (int i = 20; i >= 2; i = i - 6) { System.out.print(i + ","); } a) 20, 14, 8, 2 b) 20, 14, 8, 2, -4 c) 20, 14, 8 d) 14, 8, 2

Answer: b) 20, 14, 8, 2, -4

43) What is the last output line of the code snippet given below? int i = 0; while (i < 10) { int num = 1; for (int j = i; j > 1; j--) { System.out.print(j + " "); num = num * 2; } System.out.println("***"); i++; } a) 3 2 *** b) 9 8 7 6 5 4 3 2 *** c) 8 7 6 5 4 3 2 *** d) 2 ***

Answer: b) 9 8 7 6 5 4 3 2 ***

17) A loop inside another loop is called: a) A sentinel loop b) A nested loop c) A parallel loop d) A do/while loop

Answer: b) A nested loop

95) What will be the output of the following code snippet? boolean token1 = true; while (token1) { for (int i = 0; i < 10; i++) { System.out.println("Hello"); } token1 = false; } a) No output. b) Hello will be displayed 10 times. c) Hello will be displayed 9 times. d) Hello will be displayed infinite times.

Answer: b) Hello will be displayed 10 times.

72) Which of the following loop(s) could possibly not enter the loop body at all? I. for loop II. while loop III. do loop a) I only b) I and II only c) II and III only d) I and III only

Answer: b) I and II only

50) What does the following code do? int sum = 0; final int count = 1000; for (int i = 1; i <= count; i++) { sum = sum + (int) (Math.random() * 101); } System.out.println(sum / count); a) It simulates the outcome of throwing a coin. b) It calculates the average of 1000 random numbers between 0 and 100. c) It performs Monte Carlo simulation of fluid dynamics. d) It calculates the average of 1000 random numbers between 1 and 101.

Answer: b) It calculates the average of 1000 random numbers between 0 and 100.

34) What is the output of the code snippet given below? String s = "abcde"; int i = 1; do { if (i > 1) { System.out.print(s.substring(i, i + 1)); } } while (i < 5); a) No output b) No output (infinite loop) c) abcde d) bcde

Answer: b) No output (infinite loop)

36) What is the output of the code snippet given below? String s = "12345"; int i = 1; do { if (i > 1) { System.out.print(s.substring(i, i + 1)); } } while (i < 5); a) No output b) No output (infinite loop) c) 12345 d) 2345

Answer: b) No output (infinite loop)

21) What will be the final output of the following code snippet when a user enters input values in the order 10, 20, 30, 40, 50, and -1? public static void main(String[] args) { double sum = 0; int count = 0; double salary = 0; double average = 0; Scanner reader = new Scanner(System.in); System.out.println("Enter salaries (-1 to stop): "); while (salary != -1) { salary = reader.nextInt(); if (salary != -1) { sum = sum + salary; count++; } } if (count > 0) { average = sum / count; System.out.println("The Average Salary: " + average); } else { System.out.println ("No data!"); } return 0; } a) The Average Salary: 0 b) The Average Salary: 30 c) The Average Salary: 24.83333 d) There will be no output as the code snippet will not compile.

Answer: b) The Average Salary: 30

84) What will be the result of running the following code fragment? int year = 0; double rate = 5; double principal = 10000; double interest = 0; while (year < 10) { interest = (principal * year * rate) / 100; System.out.println("Interest " + interest); } a) The code fragment will display the interest calculated for nine years. b) The code fragment will continue to display the calculated interest forever because the loop will never end. c) The code fragment will not display the calculated interest and halt abruptly. d) The code fragment will not display any output because it will not compile. Answer: B

Answer: b) The code fragment will continue to display the calculated interest forever because the loop will never end.

7) The code snippet below checks whether a given number is a prime number. What will be the result of executing it? public static void main(String[] args) { int j = 2; int result = 0; int number = 0; Scanner reader = new Scanner(System.in); System.out.println("Please enter a number: "); number = reader.nextInt(); while (j <= number / 2) { if (number % j == 0) { result = 1; } j++; } if (result == 1) { System.out.println("Number: " + number + " is Not Prime."); } else { System.out.println("Number: " + number + " is Prime. "); } } a) The code snippet will not compile. b) The code snippet will display the desired result. c) The code snippet will display an incorrect result. d) The code snippet will loop forever.

Answer: b) The code snippet will display the desired result.

11) What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 11) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); a) The value of sum is 65 b) The value of sum is 66 c) The value of sum is 55 d) The value of sum is 56

Answer: b) The value of sum is 66

75) When hand-tracing the loop in the code snippet below, which variables are important to evaluate? int i = 10; int j = 5; int k = -10; int sum = 0; while (i > 0) { sum = sum + i + j; i--; System.out.println("Iteration: " + i); } a) The variables i and j b) The variables i and sum c) The variables i, j, and k d) The variables j and k

Answer: b) The variables i and sum

89) Is the following code snippet legal? boolean b = false; do { System.out.println("Do you think in Java?"); } while (b != b); a) Yes, it is legal but does not print anything. b) Yes, it is legal and prints "Do you think in Java?" once. c) Yes, it is legal and prints "Do you think in Java?" twice. d) No, it is not legal and gives a compilation error.

Answer: b) Yes, it is legal and prints "Do you think in Java?" once.

59) Which of the following for loops is illegal? a) for (int i = 0; ; ) { } b) for (int i = 0) { } c) for (int i = 0, k = 1; ; i++) { } d) for ( ; ; )

Answer: b) for (int i = 0) { }

41) What are the values of i and j after the following code snippet is run? int i = 10; int j = 20; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println("i = " + i + ", j = " + j); a) i = 45, j = 1 b) i = 351, j = 0 c) i = 351, j = 2 d) i = 1311, j = 35

Answer: b) i = 351, j = 0

45) In the __________ loop header, you can include multiple update expressions, separated by commas, but it is not recommended. a) do b) while c) for d) do

Answer: b) while

104) Select the statement that correctly completes the loop in this code snippet. int years = 20; double balance = 10000; while (years > 0) { __________ double interest = balance * rate / 100; balance = balance + interest; } a) years++; b) years--; c) balance++; d) balance--;

Answer: b) years--;

31) Which of the following is the correct code snippet for throwing a pair of dice to get a sum of the numbers on two dice between 2 and 12 with different probabilities? a) (int) (Math.random() * 6 + 1) b) (int) (Math.random() * 12 + 1) c) (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1) d) (int) (Math.random() * 12 - 2)

Answer: c) (int) (Math.random() * 6 + 1) + (int) (Math.random() * 6 + 1)

86) What is the output of the following code snippet? int i = 1; while (i != 9) { System.out.print(i + " "); i++; if (i == 9) { System.out.println("End"); } } a) 1 End b) 1 End (infinite loop) c) 1 2 3 4 5 6 7 8 End d) 1 2 3 4 5 6 7 8 End (infinite loop)

Answer: c) 1 2 3 4 5 6 7 8 End

62) How many times does the following loop execute? int i = 0; boolean found = false; while (i < 100 && !found) { i++; System.out.print(i + " "); int j = i * i; if ((i * i * i) % j == j) { found = true; } } a) 10 times b) 20 times c) 100 times d) An infinite number of times

Answer: c) 100 times

10) How many times does the following code fragment display "Hi"? int i = 10; while (i >= 0) { System.out.println("Hi"); i--; } a) 9 times b) 10 times c) 11 times d) 12 times

Answer: c) 11 times

12) How many times does the loop execute in the following code fragment? int i; for (i = 0; i < 50; i = i + 4) { System.out.println(i); } a) 11 b) 12 c) 13 d) 14

Answer: c) 13

26) How many times does the code snippet below display "Hello"? int i = 0; while (i != 15) { System.out.println("Hello"); i++; } a) Infinite times b) 14 times c) 15 times d) 16 times

Answer: c) 15 times

16) How many times does the following loop run? int i = 0; int j = 1; do { System.out.println("" + i + ";" + j); i++; if (i % 2 == 0) { j--; } } while (j >= 1); a) 0 times b) 1 times c) 2 times d) 4 times

Answer: c) 2 times

68) What is the output of this code snippet? String str = "ABCabc"; char ch; int i = 0; while (i < str.length()) { ch = str.charAt(i); if (Character.isLowerCase(ch)) { System.out.print(i + " "); } else { i++; } } a) 3 4 5 b) 3 c) 3 3 3 3 3 ... (infinite loop) d) 0 1 2

Answer: c) 3 3 3 3 3 ... (infinite loop)

57) What does the following code snippet print? int a = 120; int b = 90; int n1 = Math.abs(a); int n2 = Math.abs(b); int result = 1; for (int k = 1; k <= n1 && k <= n2; k++) { if (n1 % k == 0 && n2 % k == 0) { result = k; } } System.out.println(result); a) 10 b) 20 c) 30 d) 40

Answer: c) 30

27) How many times does the following loop execute? int upperCaseLetters = 0; String str = "abcdEfghI"; boolean found = false; for (int i = 0; i < str.length() && !found; i++) { char ch = str.charAt(i); if (Character.isUpperCase(ch)) { found = true; } } a) 9 times b) 8 times c) 5 times d) 1 time

Answer: c) 5 times

39) What is the output of the code fragment given below? int i = 0; int j = 0; while (i < 125) { i = i + 2; j++; } System.out.println(j); a) 0 b) 62 c) 63 d) The code fragment displays no output because it does not compile.

Answer: c) 63

4) How many times does the code snippet given below display "Loop Execution"? int i = 1; while (i != 10) { System.out.println ("Loop Execution"); i++; } a) Infinite times b) 8 times c) 9 times d) 10 times

Answer: c) 9 times

82. Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedals of a bicycle based on its gear combinations). The user must provide the gear sizes, which must be converted into roll-out for all different gear combinations. How can the flow of user interaction for this problem be designed? a) Hand-tracing can confirm code that implements gear selection. b) Pseudocode can guide algorithm design through divide-and-conquer strategy. c) A storyboard can be used. d) The physical gears can lead to ideas for the correct algorithm to use.

Answer: c) A storyboard can be used.

54) How many times does the following loop execute? double d; double x = Math.random() * 100; do { d = Math.sqrt(x) * Math.sqrt(x) - x; System.out.println(d); x = Math.random() * 10001; } while (d != 0); a) Exactly once b) Exactly twice c) Can't be determined d) Always infinite loop

Answer: c) Can't be determined

22) What will be the output of the following code snippet? boolean token = false; while (token) { System.out.println("Hello"); } a) "Hello" will be displayed infinite times. b) No output because of compilation error. c) No output after successful compilation. d) "Hello" will be displayed only once.

Answer: c) No output after successful compilation.

55) What is the output of this loop? int i = 0; boolean found; while (i < 20 && !found) { int sum = i * 2 + i * 3; System.out.print(sum + " "); if (sum > 50) { found = true; } i++; } a) 0 5 10 15 20 25 30 35 40 45 50 55 b) 0 c) No output, compilation error d) 0 5 10

Answer: c) No output, compilation error

101) Is the code snippet written below legal? String s = "1234"; for (int i = 0; i <= 5; i++) { System.out.print(s.substring(i, i + 1)); } a) Yes. b) No; there should be a semicolon at the end of line 2. c) No; for i = 3, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error. d) No; line 4 should have no semicolon at the end.

Answer: c) No; for i = 3, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error.

9) Which error type does the "off-by-one" error belong to? a) Syntax error b) Compile-time error c) Run-time error d) Infinite loop

Answer: c) Run-time error

80. Storyboards are a helpful part of the design process because the storyboard develops a) A pseudocode description of the algorithm being designed b) The mathematical formulas required for computing a correct answer c) The information needed to solve the problem, and how to present that information d) The amount of time and space needed to find a solution

Answer: c) The information needed to solve the problem, and how to present that information

105) Which statement about this code snippet is accurate? int years = 50; double balance = 10000; double targetBalance = 20000; double rate = 3; for (int i = 1; i <= years; i++) { if (balance >= targetBalance) { i = years + 1; } else { double interest = balance * rate / 100; balance = balance + interest; } } a) The loop will run 50 times. b) The loop will never stop. c) The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance. d) There is a compilation error.

Answer: c) The loop will run at most 50 times, but may stop earlier when balance exceeds or equals targetBalance.

42) What is the output of the following code fragment? int i = 1; int sum = 0; while (i <= 15) { sum = sum + i; i++; } System.out.println("The value of sum is " + sum); a) The value of sum is 0 b) The value of sum is 105 c) The value of sum is 120 d) The value of sum is 136

Answer: c) The value of sum is 120

48) What is the output of the code snippet given below? String s = "abcdefghijkl"; int i = 1; do { if (i > 2) { System.out.print(s.substring (1, i)); } i++; } while (i < 5); a) abcd b) bcde c) bcbcd d) cdef

Answer: c) bcbcd

32) Given the following code snippet, what should we change to have 26 alphabet characters in the string str? String str = ""; for (char c = 'A'; c < 'Z'; c++) { str = str + c; } a) int c = 'A' b) str = c + str; c) c <= 'Z' d) Must change to use a do loop

Answer: c) c <= 'Z

1) Which of the following loops executes the statements inside the loop before checking the condition? a) for b) while c) do d) do-for

Answer: c) do

78. What are the values of i and j after the following code snippet executes? int i = 60; int j = 50; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System.out.println(i); System.out.println(j); a) i = 65, j = 1 b) i = 65, j = 45 c) i = 1951, j = 0 d) i = 1951, j = 45

Answer: c) i = 1951, j = 0

67) How do you fix this code snippet to make it print out the sum when the user enters Q? System.out.print("Enter a value, Q to quit: "); double sum = 0; Scanner in = new Scanner(System.in); boolean hasData = true; do { double value = in.nextDouble(); sum = sum + value; System.out.print("Enter a value, Q to quit: "); } while (in.hasNext()); System.out.println("sum " + sum); a) while (in.hasData()); b) while (!in.hasEnded()); c) while (in.hasNextDouble()); d) while (hasData);

Answer: c) while (in.hasNextDouble());

23) What will be the output of the following code snippet? boolean token = false; do { System.out.println("Hello"); } while (token); a) "Hello" will be displayed infinite times. b) No output because of compilation error. c) No output after successful compilation. d) "Hello" will be displayed only once.

Answer: d) "Hello" will be displayed only once.

24) What is the output of the following code snippet? int i = 1; while (i <= 10) { System.out.println("Inside the while loop"); i = i + 10; } a) No output because of compilation error. b) "Inside the while loop" will be displayed 10 times. c) No output after successful compilation. d) "Inside the while loop" will be displayed only once.

Answer: d) "Inside the while loop" will be displayed only once.

70) Which of the following is correct for simulating the toss of a pair of coins to get 0 (head) or 1 (tail) with different probabilities? a) (int) (Math.random() * 0 + 1) b) (int) (Math.random() * 1 + 1) c) (int) (Math.random() * 2) d) (int) (Math.random() * 2) + (int) (Math.random() * 2)

Answer: d) (int) (Math.random() * 2) + (int) (Math.random() * 2)

20) What is the sentinel value in the following code snippet? public static void main(String[] args) { int age = 0; int sumOfAges = 0; int stop = 1; Scanner reader = new Scanner(System.in); System.out.println("Enter an age (-1 to stop): "); age = reader.nextInt(); while (age != -1) { sumOfAges = sumOfAges + age; System.out.println("Enter an age (-1 to stop): "); age = reader.nextInt(); } System.out.println("Sum of ages " + sumOfAges); return 0; } a) 0 b) 1 c) 2 d) -1

Answer: d) -1

96) What is the output of the code snippet given below? int i = 0; while (i != 11) { System.out.print(" " + i); i = i + 2; } a) No output b) 0 2 4 6 8 c) 10 12 14 16 18 .... (infinite loop) d) 0 2 4 6 8 10 12 14 .... (infinite loop)

Answer: d) 0 2 4 6 8 10 12 14 .... (infinite loop)

int i = 0; while (i != 9) { System.out.println("" + i); i = i + 2; } a) No output b) 0 2 4 6 8 c) 10 12 14 16 18 .... (infinite loop) d) 0 2 4 6 8 10 12 14 .... (infinite loop)

Answer: d) 0 2 4 6 8 10 12 14 .... (infinite loop)

102) What is the output of the code snippet given below? int i = 0; while (i != 11) { System.out.print(i + " "); i = i + 3; } a) No output b) 0 3 6 9 12 15 18 c) 0 1 3 5 7 9 d) 0 3 6 9 .... (infinite loop)

Answer: d) 0 3 6 9 .... (infinite loop)

103) What is the last output line of the code snippet given below? int i = 5; while (i >= 1) { int num = 1; for (int j = 1; j <= i; j++) { System.out.print(num + "**"); num = num * 2; } System.out.println(); i = i - 1; } a) No output b) 1**2 c) 1**2**3 d) 1**

Answer: d) 1**

2) How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; } a) 0 b) 8 c) 9 d) 10

Answer: d) 10

49) What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5? double total = 0; boolean hasValidNumber = true; Scanner in = new Scanner(System.in); while (in.hasNextDouble() && hasValidNumber) { double input = in.nextDouble(); if (input < 0) { hasValidNumber = false; } else { total = total + input; } } System.out.println(total); a) 15.0 b) 14.0 c) 12.0 d) 10.0

Answer: d) 10.0

35) What is the output of the code snippet given below? String s = "12345"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; } a) No output b) 1234 c) 12345 d) 2345

Answer: d) 2345

51) What is the output of the following code snippet? double a = 2; int n = 16; double r = 1; double b = a; int i = n; while (i > 0) { if (i % 2 == 0) // n is even { b = b * b; i = i / 2; } else { r = r * b; i--; } } System.out.println("r = " + r); a) 16.0 b) 128.0 c) 4096.0 d) 65536.0

Answer: d) 65536.0

93) Which of the following statements is correct about a sentinel? a) A sentinel is a value that creates a bridge between a data set and unrelated input. b) A sentinel is a value that is part of the data to be processed by the program. c) A sentinel is a value that terminates a program. d) A sentinel is a value that indicates the end of an input sequence.

Answer: d) A sentinel is a value that indicates the end of an input sequence.

64) Suppose that a program asks a user to enter multiple integers, either positive or negative, to do some calculation. The data entry will stop when the user enters a certain value to indicate the end of the data. What value should the code use as the sentinel? a) 0 b) -1 c) 999 d) An alphabetic character

Answer: d) An alphabetic character

61) How many times does the following loop execute? for (double d = 1; d != 10; d++) { d = d / 3; System.out.print(d + " "); } a) 10 b) 9 c) 8 d) An infinite number of times

Answer: d) An infinite number of times

91) What will be the output of the following code snippet? int i; int j; for (i = 0; i < 6; i++) { for (j = 7; j > i; j--) { System.out.print("*"); } System.out.println(""); } a) A rectangle with six rows and seven columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop. b) A right triangle with six rows and seven columns of asterisks. The number of columns increments by one on completion of one iteration of the inner loop. c) A rectangle with seven rows and six columns of asterisks. The number of rows increments by one on completion of one iteration of the inner loop. d) An inverted right triangle with six rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.

Answer: d) An inverted right triangle with six rows and seven columns of asterisks. The number of columns decrements by one on completion of one iteration of the inner loop.

30) What will be the range of the random numbers generated by the following code snippet? int r1 = (int) (Math.random() * 50) + 1; a) Between 1 and 49 b) Between 0 and 50 c) Between 0 and 49 d) Between 1 and 50

Answer: d) Between 1 and 50

37) Which of the following activities can be simulated using a computer? I. Waiting time in a line at a restaurant II. Tossing a coin III. Shuffling cards for a card game a) I only b) II only c) I and II only d) I, II, and III

Answer: d) I, II, and III

65) Which of the following statements expresses why the following code is considered bad form? for (rate = 5; years-- > 0; System.out.println(balance)) . . . I. Unrelated expressions in loop header II. Doesn't match expected for loop idiom III. Loop iteration is not clear a) II and III only b) I and II only c) I and III only d) I, II, and III

Answer: d) I, II, and III

107) When will the loop in the following code snippet stop? java.util.Scanner in = new java.util.Scanner(System.in); double sum = 0; int count = 0; System.out.print("Enter values, Q to quit: "); do { double value = in.nextDouble(); sum = sum + value; count++; System.out.print("Enter values, Q to quit: "); } while (in.hasNextDouble() && count < 100); I. When the user enters an integer II. When the user enters an alphabetic character III. After the user enters 100 numbers a) I or II b) II only c) III only d) II or III

Answer: d) II or III

79. The process of hand-tracing code is valuable because a) It is usually faster than just running the code. b) It is the best way to design an algorithm. c) You must already have a working program in order to do it. d) It gives valuable insight that you do not get by running the code.

Answer: d) It gives valuable insight that you do not get by running the code.

13) How many times does the following code snippet display "Loop Execution"? for (int i = 0; i < 10; i++); { System.out.println("Loop Execution"); } a) Ten times. b) The code snippet does not run because of a compile error. c) Infinite loop. d) Only one time.

Answer: d) Only one time.

100) What is the output of the code snippet given below? String s = "aeiou"; int i = 0; do { System.out.print(s.substring(i, i + 1)); i++; if (i >= 3) { i = 5; } } while (i < 5); a) a b) ae c) aeiou d) aei

Answer: d) aei

33) What is the output of the code snippet given below? String s = "abcde"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; } a) No output b) abcd c) abcde d) bcde

Answer: d) bcde

47) Insert a statement that will correctly terminate this loop when the end of input is reached. boolean done = false; while (!done) { String input = in.next(); if (input.equalsIgnoreCase("Q")) { __________ } else { double x = Double.parseDouble(input); data.add(x); } } a) stop; b) done = 1; c) exit; d) done = true;

Answer: d) done = true;

60) Which of the following loops executes exactly 10 times? a) for (int i = 0; i <= 10; i++) { } b) int i = 0; boolean found = false; do { i++; if (i % 10 == 0) { found = true; } } while (i < 10 && !found); c) int i = 0; while (i <= 10) { i++; } d) for (int i = 1; i <= 10; i++)

Answer: d) for (int i = 1; i <= 10; i++)

14) What is the output of the code snippet given below? int i; int j = 0; for (i = 0; i < 5; i++) { if (i % 2 == 0) { i = i + 2; j++; } else { i++; j = j + 2; } j++; } System.out.println("i=" + i + ", j=" + j); a) i=7, j =7 b) i =7, j =6 c) i =6, j =7 d) i =5, j =5

Answer: d) i =5, j =5

87) What changes do you need to make in the following code snippet to display "Let us learn Java" exactly 10 times? int i = 0; while (i <= 10) { System.out.println("Let us learn Java"); i++; } a) while (i < 9) b) while (i < 11) c) while (i < 12) d) int i = 1;

Answer: d) int i = 1;

99) Which of the following code snippets will generate a random number between 0 and 79? a) int val = (int) Math.random() % 80; b) int val = (int) Math.random() * 80 - 1; c) int val = (int) Math.random() % 79; d) int val = (int) Math.random() * 80;

Answer: d) int val = (int) Math.random() * 80;

5) What is the output of the code fragment given below? int i = 0; int j = 0; while (i < 27) { i = i + 2; j++; } System.out.println("j=" + j); a) j=27 b) j=12 c) j=13 d) j=14

Answer: d) j=14

25) What is the outcome of the following code snippet? boolean val1 = true; boolean val2 = false; while (val1) { if (val1) { System.out.println("Hello"); } val1 = val2; } a) No output will be displayed because of compilation error. b) "Hello" will be displayed only once. c) "Hello" will be displayed infinite times. d) No output will be displayed even after successful compilation of the code snippet.

Answer:b) "Hello" will be displayed only once.

19) How many times will the output line be printed in the following code snippet? for (int num2 = 1; num2 <= 3; num2++) { for (int num1 = 0; num1 <= 2; num1++) { System.out.println("" + num2 + " " + num1); } } a) 3 times b) 6 times c) 9 times d) 12 times

Answer:c) 9 times

79) Which one of the following code snippets accepts the integer input in an array list named num1 and stores the even integers of num1 in another array list named evennum? a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } } b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { evennum.add(num1.get(i)); } } c) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { evennum[i] = num1[i]; } } d) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum[i] = num1[i]; } }

a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> evennum = new ArrayList<Integer>(); Scanner in = new Scanner(System.in); int data; for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { evennum.add(num1.get(i)); } }

Assuming that a user enters 45 as the brightness of a lamp, which of the following hand-trace tables is valid for the given code snippet? int brightness = 0; String description = ""; Scanner in = new Scanner(System.in); System.out.print("Enter your lamp brightness (in watts): "); brightness = in.nextInt(); if (brightness >= 120) { description = "very bright"; if (brightness >= 100) { description = "bright"; } } else { description = "pleasant"; if (brightness <= 50) { description = "dim"; } } a) brightness description 0 "" 45 "pleasant" "dim" b) brightness description 0 "very bright" 45 "bright" c) brightness description 0 "" 45 "bright" "pleasant" d) brightness description 0 "bright" 45 "pleasant"

a) brightness description 0 "" 45 "pleasant" "dim"

Assuming that a user enters 22 as the price of an object, which of the following hand-trace tables is valid for the given code snippet? int price = 0; String status = ""; Scanner in = new Scanner(System.in); System.out.print("Please enter object's price: "); price = in.nextInt(); if (price >= 50) { status = "reasonable"; if (price >= 75) { status = "costly"; } } else { status = "inexpensive"; if (price <= 25) { status = "reasonable"; } } a) price status 0 "" 22 "inexpensive" "reasonable" b) price status 0 "inexpensive" 22 "reasonable" c) price status 0 "" 22 "reasonable" "costly" d) price Status 0 "reasonable" 22 "costly"

a) price status 0 "" 22 "inexpensive" "reasonable"

40) What is the value of the count variable after the execution of the given code snippet? ArrayList<Integer> num = new ArrayList<Integer>(); num.add(1); num.add(2); num.add(1); int count = 0; for (int i = 0; i < num.size(); i++) { if (num.get(i) % 2 == 0) { count++; } } a) 1 b) 2 c) 0 d) 3

a) 1

8) What is the output of the given code snippet? int[] mynum = new int[5]; for (int i = 1; i < 5; i++) { mynum[i] = i + 1; System.out.print(mynum[i]); } a) 2345 b) 1234 c) 1345 d) 1111

a) 2345

What is the output of the following code snippet if the input is 25? int i = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); i = in.nextInt(); if (i > 25) { i++; } else { i--; } System.out.print(i); a) 24 b) 25 c) 26 d) 27

a) 24

What is the output of the following code snippet? int x = 50; if (x > 100) { x++; } else { x--; } System.out.println(x); a) 49 b) 50 c) 51 d) 52

a) 49

What is the output of the following code snippet? int num = 100; if (num > 100); { num = num - 10; } System.out.println(num); a) 90 b) 100 c) 99 d) 101

a) 90

What are the two parts of an if statement? a) A condition and a body b) A check and an increment c) An increment and a body d) An increment and a return value

a) A condition and a body

16) Which statements are true about the buffer overrun attack launched over the Internet in 1988? I. The buffer overrun exploited a program that was written in C running on the Unix operating system II. The Java programming language generates a run-time exception when buffer overrun occurs III. In recent years computer attacks have lessened a) I, II b) I, III c) II, III d) I, II, III

a) I, II

Consider the following code snippet. Assuming that the user inputs 75 as the age, what is the output? int age = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter your age: "); age = in.nextInt(); if (age < 10) { System.out.print("Child "); } if (age < 30) { System.out.print("Young adult "); } if (age < 70) { System.out.print("Old "); } if (age < 100) { System.out.print("Impressively old "); } a) Impressively old b) Child Young adult Old c) Young adult Old d) Child Young adult Old Impressively old

a) Impressively old

In Java, which of the following orderings is used to compare strings? a) Lexicographic b) Semantic c) Alphabetic d) Syntactic

a) Lexicographic

What can be done to improve the following code fragment? if ((counter % 10) == 0) { System.out.println("Counter is divisible by ten: " + counter); counter++; } else { System.out.println("Counter is not divisible by ten: " + counter); counter++; } a) Move the duplicated code outside of the if statement b) Shorten variable names c) Move the brackets to save several lines of code d) Add semicolons after the if condition and the else reserved word

a) Move the duplicated code outside of the if statement

60) Which one of the following statements is correct for displaying the value in the second row and the third column of a two-dimensional, size 3 by 4 array? a) System.out.println(arr[1][2]); b) System.out.println(arr[2][3]); c) System.out.println(arr[2][1]); d) System.out.println(arr[3][2]);

a) System.out.println(arr[1][2]);

84) Which one of the following statements is correct for displaying the value in the third row and the fourth column of a two-dimensional 5 by 6 array? a) System.out.println(arr[2][3]); b) System.out.println(arr[3][4]); c) System.out.println(arr[3][2]); d) System.out.println(arr[4][3]);

a) System.out.println(arr[2][3]);

76) What should you check for when calculating the largest value in an array list? a) The array list contains at least one element. b) The array list contains at least two elements. c) The array list contains the minimum value in the first element. d) The array list contains the maximum value in the first element.

a) The array list contains at least one element

96) What should you check for when calculating the smallest value in an array list? a) The array list contains at least one element. b) The array list contains at least two elements. c) The array list contains the maximum value in the first element. d) The array list contains the minimum value in the first element.

a) The array list contains at least one element.

68) Consider the following code snippet: public static void check(ArrayList<Integer> chknum1) { int cnt = 0; for(int i = 0; i < chknum1.size(); i++) { if(chknum1.get(i) == 0) { cnt++; } } System.out.println("The total 0 values in the list are: " + cnt); } Which one of the following is true about the check method in the given code snippet? a) The check method counts all the elements with value 0 in an array list passed as a parameter to the method. b) The check method removes all the elements with value 0 from an array list passed as a parameter to the method. c) The check method counts all the elements with value 0 in an array list passed as a parameter to a method and also returns the count. d) The check method adds 0 to the elements of an array list as a parameter to a method and also returns the array list.

a) The check method counts all the elements with value 0 in an array list passed as a parameter to the method.

Which statement about an if statement is true? a) The condition in an if statement using relational operators will evaluate to a Boolean result b) The condition in an if statement should make exact comparisons to floating-point numbers c) The condition in an if statement should always evaluate to true d) The condition in an if statement should never include integer variables

a) The condition in an if statement using relational operators will evaluate to a Boolean result

88) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); friends.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

a) The final size of names is 2; the final size of friends is 3

Assuming that the valid cost should be between 100 and 200, does the following code snippet test this condition correctly? final int MIN_COST = 100; final int MAX_COST = 200; int cost = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter the cost: "); cost = in.nextInt(); if (cost < MIN_COST) { System.out.println("Error: The cost is too low."); } else if (cost > MAX_COST) { System.out.println("Error: The cost is too high."); } else { System.out.println("The cost entered is in the valid cost range."); } a) This code snippet ensures that the cost value is between 100 and 200. b) This code snippet only ensures that the cost value is greater than 100. c) This code snippet only ensures that the cost value is less than 200. d) This code snippet ensures that the cost value is either less than 100 or greater than 200.

a) This code snippet ensures that the cost value is between 100 and 200.

Assuming that a valid price should be between 30 and 50, does the following code snippet test this condition correctly? final int MIN_PRICE = 30; final int MAX_PRICE = 50; int price = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter the price: "); price = in.nextInt(); if (price < MIN_PRICE) { System.out.println("Error: The price is too low."); } else if (price > MAX_PRICE) { System.out.println("Error: The price is too high."); } else { System.out.println("The price entered is in the valid price range."); } a) This code snippet ensures that the price value is between 30 and 50. b) This code snippet only ensures that the price value is greater than 30. c) This code snippet only ensures that the price value is less than 50. d) This code snippet ensures that the price value is either less than 30 or greater than 50.

a) This code snippet ensures that the price value is between 30 and 50.

Consider the following code snippet. What is the potential problem with the if statement? double average; average = (g1 + g2 + g3 + g4) / 4.0; if (average == 90.0) { System.out.println("You earned an A in the class!"); } a) Using == to test the double variable average for equality is error-prone. b) The conditional will not evaluate to a Boolean value. c) The assignment operator should not be used within an if-statement conditional. d) Literals should never be used in if statement conditionals.

a) Using == to test the double variable average for equality is error-prone.

Assuming that the user provides 99 as input, what is the output of the following code snippet? int a; int b; a = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter a number: "); b = in.nextInt(); if (b > 300) { a = b; } else { a = 0; } System.out.println("a: " + a); a) a: 0 b) a: 99 c) a: 100 d) a: 300

a) a: 0

45) Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; Using Java 6 or later, which statement grows the data array to twice its size? a) data = Arrays.copyOf(data, 2 x data.length); b) data = Arrays.copyOf(data, 2); c) data = Arrays.copyOf(data, 2 * data); d) data = Arrays.copyOf(data, 2 * data.size());

a) data = Arrays.copyOf(data, 2 * data.length);

15) Complete the following code snippet with the correct enhanced for loop so it iterates over the array without using an index variable. String[] arr = { "abc", "def", "ghi", "jkl" }; ___________________ { System.out.print(str); } a) for (String str : arr) b) for (str : String arr) c) for (str[] : arr) d) for (arr[] : str)

a) for (String str : arr)

What is the output of the following code snippet? String str1 = "her"; String str2 = "cart"; if (str1.compareTo(str2) < 0) { System.out.print(str2); } else { System.out.print(str1); } a) her b) hercart c) cart d) carther

a) her

What is the output of the following code snippet? String someString1 = "his"; String someString2 = "cycle"; if (someString1.compareTo(someString2) < 0) { System.out.println(someString2); } else { System.out.println(someString1); } a) his b) hiscycle c) cycle d) There is no output due to compilation errors.

a) his

Which of the following options is a legally correct expression for inverting a condition? a) if (!(a == 10)) b) if (!a == 10) c) if (a !== 10) d) if (a ! 10)

a) if (!(a == 10))

Which of the following options correctly represents a "nested if" structure? a) if (cost < 70) { if (tax_rate < 0.10) { . . . } } b) if (cost < 70) { . . . } if (tax_rate < 0.10) { . . . } c) if (cost < 70) { . . . } else { . . . } if (tax_rate < 0.10) { . . . } d) if (cost < 70) { . . . } { else { if (tax_rate < 0.10) { . . . } } }

a) if (cost < 70) { if (tax_rate < 0.10) { . . . } }

Which of the following statements can be used to validate that the user input for the floor variable is between 0 and 20 inclusive? a) if (floor >= 0 && floor <= 20) b) if (floor >= 0 || floor <= 20) c) if (floor <= 0 && floor >= 20) d) if (floor <= 0 || floor >= 20)

a) if (floor >= 0 && floor <= 20)

Consider the following code snippet: int number = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); number = in.nextInt(); if (number > 30) { . . . } else if (number > 20) { . . .. } else if (number > 10) { . . . } else { . . . } Assuming that the user input is 40, which block of statements is executed? a) if (number > 30) { . . . } b) else if (number > 20) { . . . } c) else if (number > 10) { . . . } d) else { . . . }

a) if (number > 30) { . . . }

Write an if-statement condition that is true if the length of string s1 is greater than 42. a) if (s1.length() > 42) b) if (s1.length() != 42) c) if (42 > s1.length()) d) if (42 != s1.length())

a) if (s1.length() > 42)

3) Which one of the following statements is a valid initialization of an array named somearray of ten elements? a) int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; b) int somearray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; c) int[10] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; d) int somearray[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

a) int[] somearray = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

57) Which one of the following statements is the correct definition for a two-dimensional array of 20 rows and 2 columns of the type integer? a) int[][] num = new int[20][2]; b) int[][] num = new int[2][20]; c) int[][] num = new int[20,2]; d) int[][] num = new int[2,20];

a) int[][] num = new int[20][2];

19) The enhanced for loop a) is convenient for traversing all elements in an array b) is convenient for traversing elements in a partially filled array c) is only used for arrays of integers d) is used to initialize all array elements to a common value

a) is convenient for traversing all elements in an array

The switch statement in Java a) is like a sequence of if statements that compares a single integer value against several constant alternatives. b) is a compound statement that tests all branches against different variables. c) makes the break statement optional. d) requires compound Boolean expressions as alternatives.

a) is like a sequence of if statements that compares a single integer value against several constant alternatives.

64) Which one of the following is a valid signature of a method with an integer two-dimensional array parameter of size 10 x 10? a) public static void func(int[][] arr) b) public static void func(int[10][] arr) c) public static void func(int[][10] arr) d) public static void func(int[10][10] arr)

a) public static void func(int[][] arr)

14) In a partially filled array, the number of slots in the array that are not currently used is a) the length of the array minus the number of elements currently in the array b) the number of elements currently in the array minus the length of the array c) the length of the array plus the number of elements currently in the array d) the number of elements currently in the array

a) the length of the array minus the number of elements currently in the array

22) It may be necessary to "grow" an array when reading inputs because a) the number of inputs may not be known in advance. b) arrays in Java must be resized after every 100 elements. c) arrays are based on powers of two. d) the only alternative is a bounds exception.

a) the number of inputs may not be known in advance.

38) The following statement gets an element from position 4 in an array: x = a[4]; What is the equivalent operation using an array list? a) x = a.get(4); b) x = a.get(); c) x = a.get[4]; d) x = a[4];

a) x = a.get(4);

29) Which code snippet calculates the sum of all the even elements in an array values? a) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum += values[i]; } } b) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] % 2) == 0) { sum++; } } c) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum += values[i]; } } d) int sum = 0; for (int i = 0; i < values.length; i++) { if ((values[i] / 2) == 0) { sum++; } }

a.

41) Which one of the following code snippets accepts the integer input in an array named num1 and stores the odd integers of num1 in another array named oddnum? a) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1.get(i)); } } b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } } c) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 == 0) { oddnum.add(num1[i]); } } d) ArrayList<Integer> num1; ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1[i] % 2 != 0) { oddnum.add(num1[i]); } } Answer: b

b) ArrayList<Integer> num1 = new ArrayList<Integer>(); ArrayList<Integer> oddnum = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 10; i++) { data = in.nextInt(); num1.add(data); if (num1.get(i) % 2 != 0) { oddnum.add(num1.get(i)); } }

59) Which one of the following is the correct definition for initializing data in a two-dimensional array of three rows and two columns? a) int[][] arr = { { 1, 1, 1 }, { 2, 2, 2 }, }; b) int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } }; c) int[][] arr = { { 1, 1 } { 2, 2 } { 3, 3 } }; d) int[][] arr = { { 1, 1, 1 } { 2, 2, 2 } { 3, 3, 3 } };

b) int[][] arr = { { 1, 1 }, { 2, 2 }, { 3, 3 } };

77) Which one of the following is the correct code snippet for calculating the largest value in an integer array list arrList of size 10? a) int maximum = 0; for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } b) int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } c) int maximum = arrList.get(1); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } } d) int maximum = arrList.get(0); for (int counter = 1; counter > arrList.size(); counter++) { if (arrList.get(counter) < maximum) { maximum = arrList.get(counter); } }

b) int maximum = arrList.get(0); for (int counter = 1; counter < arrList.size(); counter++) { if (arrList.get(counter) > maximum) { maximum = arrList.get(counter); } }

) The two strings "Aardvark" and "Aardvandermeer" are exactly the same up to the first six letters. What is their correct lexicographical ordering? a) They cannot be compared lexicographically unless they are the same length b) "Aardvandermeer" is first, then "Aardvark" c) "Aardvark is first, then "Aardvandermeer" d) The shorter word is always first

b) "Aardvandermeer" is first, then "Aardvark"

Which of the following operators is NOT a relational operator? a) <= b) += c) != d) ==

b) +=

72) Consider the following code snippet: ArrayList<Double> somedata = new ArrayList<Double>(); somedata.add(10.5); What is the size of the array list somedata after the given code snippet is executed? a) 0 b) 1 c) 10 d) 2

b) 1

Assuming that a user enters 45, 78, and then 12, what is the output of the following code snippet? int num1 = 0; int num2 = 0; int num3 = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num1 = in.nextInt(); System.out.print("Enter a number: "); num2 = in.nextInt(); System.out.print("Enter a number: "); num3 = in.nextInt(); if (!(num1 > num2 && num1 > num3)) { System.out.println(num1); } else if (!(num2 > num1 && num2 > num3)) { System.out.println(num2); } else if (!(num3 > num1 && num3 > num2)) { System.out.println(num3); } a) 12 b) 45 c) 78 d) There is no output due to compilation errors

b) 45

55) Consider the following code snippet: int cnt = 0; int[][] numarray = new int[2][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { numarray[j][i] = cnt; cnt++; } } What is the value of numarray[1][2] after the code snippet is executed? a) 2 b) 5 c) 3 d) 4

b) 5

What is the output of the following code snippet? final int MIN_SPEED = 45; final int MAX_SPEED = 65; int speed = 55; if (!(speed < MAX_SPEED)) { speed = speed - 10; } if (!(speed > MIN_SPEED)) { speed = speed + 10; } System.out.println(speed); a) 45 b) 55 c) 65 d) 50

b) 55

85) What is the output of the code snippet below? int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int val = arr[1][2] + arr[1][3]; System.out.println(val); a) 5 b) 6 c) 7 d) 9

b) 6

86) Consider the following code snippet: int[][] arr = { { 1, 2, 3, 0 }, { 4, 5, 6, 0 }, { 0, 0, 0, 0 } }; int[][] arr2 = arr; System.out.println(arr2[2][1] + arr2[1][2]); What is the output of the given code snippet on execution? a) 5 b) 6 c) 7 d) 9

b) 6

81) What is the value of myArray[1][2] after this code snippet is executed? int count = 0; int[][] myArray = new int[4][5]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 4; j++) { myArray[j][i] = count; count++; } } a) 8 b) 9 c) 19 d) 11

b) 9

Which of the following operators is used as a relational operator? a) =< b) <= c) = d) ! Answer: b

b) <=

74) Your program needs to store a sequence of integers of unknown length. Which of the following is most suitable to use? a) An array declared as int[] marks; b) A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>(); c) An array declared as int marks[10000]; d) An array declared as int marks[10];

b) A array list declared as ArrayList<Integer> marks = new ArrayList<Integer>();

46) Consider the following code snippet: String[] data = { "123", "ghi", "jkl", "def", "%&*" }; Which statement sorts the data array in ascending order? a) data = Arrays.sort(); b) Arrays.sort(data); c) data = Arrays.sort(data.length); d) data = Arrays.sort(data);

b) Arrays.sort(data);

24) Which statements about array algorithms are true? I. The array algorithms are building blocks for many programs that process arrays II. Java contains ready-made array algorithms for every problem situation III. It is inefficient to make multiple passes through an array if you can do everything in one pass a) I, II b) I, III c) II, III d) I, II, III

b) I, III

39) Which statements are true regarding the differences between arrays and array lists? I. Arrays are better if the size of a collection will not change II. Array lists are more efficient than arrays III. Array lists are easier to use than arrays a) I, II b) I, III c) II, III d) I, II, III

b) I, III

When testing code for correctness, it always makes sense to a) Test all cases b) Identify boundary cases and test them c) Check all cases by hand d) Assume invalid input will never occur

b) Identify boundary cases and test them

9) Which one of the following statements is correct for the given code snippet? int[] number = new int[3]; // Line 1 number[3] = 5; // Line 2 a) Line 2 declares and initializes an array of three elements with value 5. b) Line 2 causes a bounds error because 3 is an invalid index number. c) Line 2 initializes the third element of the array with value 5. d) Line 2 initializes all the elements of the array with value 5.

b) Line 2 causes a bounds error because 3 is an invalid index number

What is the output of the following code snippet? int golfScore = 64; if (golfScore < 60) { System.out.println("Astounding!"); } if (golfScore >= 60 && golfScore < 70) { System.out.println("Professional!"); } if (golfScore >= 70 && golfScore < 80) { System.out.println("Pretty good!"); } if (golfScore >= 80 && golfScore < 90) { System.out.println("Not so hot!"); } if (golfScore >= 90) { System.out.println("Keep your day job!"); } a) Astounding! b) Professional! c) Pretty good! d) Keep your day job!

b) Professional!

20) When the order of the elements is unimportant, what is the most efficient way to remove an element from an array? a) Delete the element and move each element after that one to a lower index. b) Replace the element to be deleted with the last element in the array. c) Replace the element to be deleted with the first element in the array. d) Replace the element with the next element

b) Replace the element to be deleted with the last element in the array.

What is the value of the magicPowers variable after executing the following code snippet? String magicPowers = ""; int experienceLevel = 9; if (experienceLevel > 10) { magicPowers = magicPowers + "Golden sword "; } if (experienceLevel > 8) { magicPowers = magicPowers + "Shining lantern "; } if (experienceLevel > 2) { magicPowers = magicPowers + "Magic beans "; } a) Golden sword Shining lantern Magic beans b) Shining lantern Magic beans c) Magic beans d) An empty string

b) Shining lantern Magic beans

Consider the following code snippet: int score = 0; double price = 100; if (score > 0 && price < 200 && price / score > 10) { System.out.println("buy"); } Which of the following statements is true on the basis of this code snippet? a) The output is buy. b) The code snippet compiles and runs, but there is no output. c) The code snippet doesn't compile. d) The code snippet causes a divide-by-zero error.

b) The code snippet compiles and runs, but there is no output.

What is the problem with the following if statement? double count = 15.0; if (count / 3.0) { System.out.println("The value of count is "); } a) There should be an "else" condition b) The condition does not evaluate to a Boolean value c) The variable count should be part of the string d) It is never possible to use the "/" operator in an if statement

b) The condition does not evaluate to a Boolean value

) Suppose you want to write an if statement with multiple alternatives to print out the single tax bracket that someone is in, based on their income. Assume the integer variable income holds the annual income. What is wrong with the following if statement? if (income < 10000) { System.out.println("Lowest tax bracket"); } if (income < 20000) { System.out.println("Low-Middle tax bracket"); } if (income < 30000) { System.out.println("Middle tax bracket"); } System.out.println("High tax bracket"); a) The conditions are in the wrong order; the check for the highest bracket should be first b) The conditions should use an if else/if else sequence, not just independent if statements c) The conditions should be a switch statement instead d) Nothing is wrong - the if statement will correctly print out the tax brackets

b) The conditions should use an if else/if else sequence, not just independent if statements

91) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = new ArrayList<String>(names); names.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

b) The final size of names is 3; the final size of friends is 2

7) Which one of the following statements is correct about the given code snippet? int[] somearray = new int[6]; for (int i = 1; i < 6; i++) { somearray[i] = i + 1; } a) The for loop initializes all the elements of the array. b) The for loop initializes all the elements except the first element. c) The for loop initializes all the elements except the last element. d) The for loop initializes all the elements except the first and last elements

b) The for loop initializes all the elements except the first element.

35) If a programmer confuses the method required for checking the length of a string and uses size() instead of length(), what will happen? a) The program will crash. b) The program will not compile. c) The program will run but will produce an uncertain result. d) The compiler will automatically correct the error.

b) The program will not compile

What is the output of the following code snippet? boolean passed = false; String someStr = "Unknown"; passed = !(passed); if (!passed) { someStr = "False"; } if (passed) { passed = false; } if (!passed) { someStr = "True"; } else { someStr = "Maybe"; } System.out.println(some_str); a) False b) True c) Unknown d) Maybe

b) True

Which of the following statements is correct about an if statement? a) You must use braces if the body of an if statement contains only a single statement. b) You can omit an else statement if there is no task defined in the else branch. c) You cannot use braces if the body of an if statement contains only a single statement. d) The number of opening braces can be different from the number of closing braces.

b) You can omit an else statement if there is no task defined in the else branch.

43) Consider the following code snippet: String[] data = { "abc", "def", "ghi", "jkl" }; String [] data2; In Java 6 and later, which statement copies the data array to the data2 array? a) data2 = Arrays.copyOf(data, data2.length); b) data2 = Arrays.copyOf(data, data.length); c) data2 = Arrays.copyOf(data, data.size()); d) data2 = Arrays.copyOf(data);

b) data2 = Arrays.copyOf(data, data.length);

A store provides 10 percent discount on all items with a price of at least $100. No discount is otherwise applicable. Which of the following DOES NOT correctly compute the discount? a) double discount = 0; if (price >= 100) { discount = 0.10 * price; } b) double discount = 0.10 * price; if (price <= 100) { discount = 0; } c) double discount; if (price < 100) { discount = 0; } else { discount = 0.10 * price; } d) double discount = 10; if (price >= 100) { discount = 0.1 * price; } else { discount = 0; }

b) double discount = 0.10 * price; if (price <= 100) { discount = 0; }

A store applies a 15 percent service charge on all items with a price of at least $150. No service charge is otherwise applicable. Which of the following DOES NOT correctly compute the service charge? a) double serviceCharge = 0; if (cost >= 150) { serviceCharge = 0.15 * cost; } b) double serviceCharge = 0.15 * cost; if (cost <= 150) { serviceCharge = 0; } c) double serviceCharge; if (cost < 150) { serviceCharge = 0; } else { serviceCharge = 0.15 * cost; } d) double serviceCharge = 15; if (cost >= 150) { serviceCharge = 0.15 * cost; } else { serviceCharge = 0; }

b) double serviceCharge = 0.15 * cost; if (cost <= 150) { serviceCharge = 0; }

Which of the following options checks that character ch is neither a letter nor a white space? a) if (!Character.isLetter(ch) || !Character.isWhiteSpace(ch)) b) if !(Character.isLetter(ch) || Character.isWhiteSpace(ch)) c) if !(Character.isLetter(ch) && Character.isWhiteSpace(ch)) d) if (!Character.isLetter(ch) || Character.isWhiteSpace(ch))

b) if !(Character.isLetter(ch) || Character.isWhiteSpace(ch))

Which of the following options checks that city is neither Chicago nor Dallas? a) if (city != "Chicago" || city != "Dallas") b) if !(city == "Chicago" || city == "Dallas") c) if !(city == "Chicago" && city == "Dallas") d) if (city != "Chicago" || city == "Dallas")

b) if !(city == "Chicago" || city == "Dallas")

Which of the following options checks that the string country is neither China nor Denmark? a) if (!country.equals("China") || !country.equals("Denmark") b) if !(country.equals("China") || country.equals("Denmark")) c) if !(country.equals("China") && country.equals("Denmark")) d) if (country.equals("China") || country.equals("Denmark"))

b) if !(country.equals("China") || country.equals("Denmark"))

What is the conditional required to check whether the length of a string s1 is odd? a) if ((s1.length() % 2) == 0) b) if ((s1.length() % 2) != 0) c) if ((s1.length() / 2)) d) if ((s1.length() * 2))

b) if ((s1.length() % 2) != 0)

boolean attendance = true; boolean failed = false; Which of the following if statement s includes a condition that evaluates to true? a) if (attendance == "true") { . . . } b) if (attendance) { . . . } c) if (failed) { . . . } d) if (attendance == failed) { . . . }

b) if (attendance) { . . . }

Which of the following conditions tests whether the user enters an integer value that will then be assigned to the floor variable? int floor = 0; Scanner in = new Scanner(System.in); System.out.print("Floor: "); ( . . . ) floor = in.nextInt(); a) if (in.nextInt(floor)) b) if (in.hasNextInt()) c) if (in.fail) d) if (in.nextInt())

b) if (in.hasNextInt())

Consider the following code snippet: boolean married = true; boolean engaged = false; Which of the following if statements includes a condition that evaluates to true? a) if (married == "true") { . . . } b) if (married) { . . . } c) if (engaged) { . . . } d) if (married == engaged) { . . . }

b) if (married) { . . . }

Which of the following expressions represents a legal way of checking whether a value assigned to the num variable falls in the range 100 to 200? a) if (num >= 200 && num <= 100) b) if (num >= 100 && num <= 200) c) if (num >= 100 || num <= 200) d) if (num >= 200 || num <= 100)

b) if (num >= 100 && num <= 200)

Which of the following expressions represents a legal way of checking whether a value assigned to the number variable falls between 50 and 100 inclusive? a) if (number >= 100 && number <= 50) b) if (number >= 50 && number <= 100) c) if (number >= 50 || number <= 100) d) if (number >= 100 || number <= 50)

b) if (number >= 50 && number <= 100)

2) Identify the correct statement for defining an integer array named numarray of ten elements. a) int[] numarray = new int[9]; b) int[] numarray = new int[10]; c) int[10] numarray; d) int numarray[10];

b) int[] numarray = new int[10];

11) When an array myArray is only partially filled, how can the programmer keep track of the current number of elements? a) access myArray.length() b) maintain a companion variable that stores the current number of elements c) access myArray.currentElements() d) access myArray.length() - 1

b) maintain a companion variable that stores the current number of elements

An if statement inside another if statement is called a a) switch statement b) nested if statement c) break statement d) syntax error, since that is not permitted in Java

b) nested if statement

26.) Suppose you wish to process an array of values and eliminate any potential duplicate values stored in the array. Which array algorithms might be adapted for this? a) find the minimum b) remove an element c) add an element d) calculate the sum of the elements

b) remove an element

36) Babbage's machine for automatically producing printed tables was called a) the slide rule. b) the difference engine. c) the mechanical calculator. d) the cog wheel.

b) the difference engine.

23) The binary search is faster than the linear search, providing a) the size of the array is a power of two. b) the elements of the array are ordered. c) the elements of the array are unordered. d) the element being searched for is actually in the array.

b) the elements of the array are ordered.

92) When an integer literal is added to an array list declared as ArrayList<Integer>, Java performs: a) casting b) trimming c) auto-boxing d) auto-fixing

b) trimming

Assuming that the user provides 49 as input, what is the output of the following code snippet? int x = 0; int y = 0; System.out.print("Please enter y: "); Scanner in = new Scanner(System.in); y = in.nextInt(); if (y > 50); { x = y; } System.out.println("x: " + x); a) x: 0 b) x: 49 c) x: 50 d) There is no output due to compilation errors.

b) x: 49

97) Which one of the following is the correct code snippet for calculating the largest value in an integer array list aList? a) int max = 0; for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } } b) int max = aList.get(0); for (int count = 1; count < aList.size(); count++) { if (aList.get(count) > max) { max = aList.get(count); } } c) int max = aList[1]; for (int count = 1; count < aList.size();count++) { if (aList.get(count) > max) { max = aList.get(count); } } d) int max = aList.get(0); for (int count = 1; count > aList.size();count++) { if (aList.get(count) >= max) { max = aList.get(count); } }

b.

32) Consider the telephone book as a physical object that can help understand algorithms. What kind of algorithm might be visualized using it? a) Sorting Searchingb) c) Finding the maximum d) Monte Carlo methods

b.) Searching

27) Which code snippet finds the largest value in an array that is only partially full? a) double largest = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] > largest) { largest = values[i]; } } b) double largest = values[0]; for (int i = 1; i < values.length; i++) { if (values[i] < largest) { largest = values[i]; } } c) double largest = values[0]; for (int i = 1; i < currSize; i++) { if (values[i] > largest) { largest = values[i]; } } d) double largest = values[0]; for (int i = 1; i < currSize; i++) { if (values[i] < largest) { largest = values[i]; } }

c) double largest = values[0]; for (int i = 1; i < currSize; i++) { if (values[i] > largest) { largest = values[i]; } }

13) Which code snippet prints out the elements in a partially filled array of integers? a) for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[i]); } b) for (int i = 0; i < myArray.currLength(); i++) { System.out.print(myArray[i]); } c) for (int i = 0; i < currLength; i++) { System.out.print(myArray[i]); } d) for (int i = 0; i < myArray.length(); i++) { System.out.print(myArray[currLength]); }

c) for (int i = 0; i < currLength; i++) {

44) Consider the following code snippet in Java 6 or later: String[] data = { "abc", "def", "ghi", "jkl" }; String[] data2 = Arrays.copyOf(data, data.length - 1); What does the last element of data2 contain? a) "abc" b) "def" c) "ghi" d) "jkl"

c) "ghi"

69) Which of the following operators compare using short-circuit evaluation? a) ++ b) - c) && d) ==

c) &&

What is the value of num after you run the following code snippet? int num = 100; if (num <= 100) { num++; } if (num <= 200) { num--; } if (num <= 300) { num++; } if (num <= 400) { num--; } if (num <= 500) { num++; } a) 99 b) 100 c) 101 d) 102

c) 101

What is the value of the cost variable after the following code snippet is executed? int cost = 82; if (cost < 100) { cost = cost + 10; } if (cost > 50) { cost = cost * 2; } if (cost < 100) { cost = cost - 20; } a) 82 b) 92 c) 184 d) 164

c) 184

100) Consider the following code snippet. What does the array contain at the end of the program? public static fillWithRandomNumbers(double[] values) { double[] numbers = new double[values.length]; for (int i = 0; i < numbers.length; i++) { numbers[i] = Math.random(); } values = numbers; } public static void main(String[] args) { double[] num = new double[20]; fillWithRandomNumbers(num); } a) 20 random numbers b) Undefined data due to compilation error c) 20 zeros because array num is not changed by method d) Array index bound error

c) 20 zeros because array num is not changed by method

) Assuming that a user enters 25 as the value for x, what is the output of the following code snippet? int x = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); x = in.nextInt(); if (x < 100) { x = x + 5; } if (x < 500) { x = x - 2; } if (x > 10) { x++; } else { x--; } System.out.println(x); a) 27 b) 28 c) 29 d) 30

c) 29

What is the output of the following code snippet? int x = 25; if (x < 100) { x = x + 5; } if (x < 500) { x = x - 2; } if (x > 10) { x++; } else { x--; } System.out.println(x); a) 27 b) 28 c) 29 d) 30

c) 29

58) Consider the following code snippet: int[][] numarray = { { 3, 2, 3 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); What is the output of the given code snippet? a) 00 b) 31 c) 30 d) 03

c) 30

4) What is the output of the following code snippet? int[] myarray = { 10, 20, 30, 40, 50 }; System.out.print(myarray[2]); System.out.print(myarray[3]); a) 1050 b) 2030 c) 3040 d) 4050

c) 3040

83) What is the output of the code snippet below? int[][] numarray = { { 8, 7, 6 }, { 0, 0, 0 } }; System.out.print(numarray[0][0]); System.out.print(numarray[1][0]); a) 00 b) 87 c) 80 d) 08

c) 80

63) Consider the following code snippet: int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 } }; int val = arr[0][2] + arr[1][2]; System.out.println(val); What is the output of the given code snippet on execution? a) 5 b) 7 c) 9 d) 10

c) 9

What is the output after running the following code snippet? int number = 600; if (number < 200) { System.out.println("Low spender"); } else if (number < 500) { System.out.println("Spending in moderation"); } else if (number < 1000) { System.out.println("Above average!"); } else { System.out.println("High Roller!"); } a) Low spender b) Spending in moderation c) Above average! d) High Roller!

c) Above average!

93) What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(1, "Tony"); for (String s : names) { System.out.print(s + ", "); } a) Cal, Bob, Ann b) Ann, Bob c) Ann, Tony d) Cal, Bob, Tony

c) Ann, Tony

Which of the following variables is used to store a condition that can be either true or false? a) Algebraic b) Logical c) Boolean d) Conditional

c) Boolean

Which of the following coding techniques can hand-tracing be applied to? a) Pseudocode b) Java code c) Both pseudocode and Java code d) Neither pseudocode nor Java code

c) Both pseudocode and Java code

In a switch statement, if a break statement is missing a) The break happens at the end of each branch by default b) The statement will not compile c) Execution falls through the next branch until a break statement is reached d) The default case is automatically executed

c) Execution falls through the next branch until a break statement is reached

Assuming that the user enters 60 as the input, what is the output after running the following code snippet? int num = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num = in.nextInt(); if (num < 10) { System.out.println("Too small!"); } else if (num < 50) { System.out.println("Intermediate!"); } else if (num < 100) { System.out.println("High!"); } else { System.out.println("Too high!"); } a) Too small! b) Intermediate! c) High! d) Too high!

c) High!

18) Which statements about the enhanced for loop are true? I. It is suitable for all array algorithms II. It does not allow the contents of the array to be modified III. It does not require the use of an index variable a) I, II b) I, III c) II, III d) I, II, III

c) II, III

28) When a Java program terminates and reports an exception, the error message contains which pieces of useful information? I. The compile and revision control history of the source code changes that caused the error II. The name of the exception that occurred III. The stack trace a) I, II b) I, III c) II, III d) I, II, III

c) II, III

34) Which statement(s) about the size of a Java array, array list, and string are true? I. The syntax for determining the size of an array, an array list, and a string in Java is consistent among the three II. The string uses s.size(), while the array list uses a.length() III. The array uses a.length, which is not a method call a) I b) II c) III d) II, III

c) III

Which of the following statements is (are) true about an if statement? I. It guarantees that several statements are always executed in a specified order. II. It repeats a set of statements as long as the condition is true. III. It allows the program to carry out different actions depending on the value of a condition. a) I b) II c) III d) I, II, III

c) III

Which of the following statements is true about the "nested if" structure? a) It cannot have any else branches at all. b) It allows multiple else branches in a single if statement. c) It allows one if statement within another if statement. d) It does not allow multiple else branches inside a nested if statement. Answer: c

c) It allows one if statement within another if statemen

The following code snippet contains an error. What is the error? if (cost > 100); { cost = cost - 10; } System.out.println("Discount cost: " + cost); a) Syntax error (won't compile) b) Logical error: use of an uninitialized variable c) Logical error: if statement has do-nothing statement after if condition d) Logical error: assignment statement does not show equality

c) Logical error: if statement has do-nothing statement after if condition

What is the output of the following code snippet? int num = 100; if (num != 100) { System.out.println("100"); } else { System.out.println("Not 100"); } a) There is no output due to compilation errors. b) 100 c) Not 100 d) 100 Not 100

c) Not 100

What is the output of the following code snippet? int digit = 500; if (digit != 500) { System.out.println("500"); } else { System.out.println("Not 500"); } a) There is no output due to compilation errors. b) 500 c) Not 500 d) 500 Not 500

c) Not 500

What is the output of the following code snippet? double salary = 55000; double cutOff = 65000; double minSalary = 40000; if (minSalary > salary) { System.out.println("Minimum salary requirement is not met."); } if (cutOff < salary) { System.out.println("Maximum salary limit is exceeded."); } else { System.out.println("Salary requirement is met."); } a) Minimum salary requirement is not met. b) Maximum salary limit is exceeded. c) Salary requirement is met. d) There is no output

c) Salary requirement is met.

52) Consider the following code snippet. Which statement should be used to fill in the empty line so that provides the output will be [32, 54, 67.5, 29, 35]? public static void main(String[] args) { double data[] = {32, 54, 67.5, 29, 35}; ______________ System.out.println(str); } a) String str = Arrays.format(data); b) String str = data.toString(); c) String str = Arrays.toString(data); d) String str = str + ", " + data[i];

c) String str = Arrays.toString(data);

10) What is displayed after executing the given code snippet? int[] mymarks = new int[10]; int total = 0; Scanner in = new Scanner(System.in); for (int cnt = 1; cnt <= 10; cnt++) { System.out.print("Enter the marks: "); mymarks[cnt] = in.nextInt(); total = total + mymarks[cnt]; } System.out.println(total); a) The code snippet displays the total marks of all ten subjects. b) The for loop causes a run-time time error on the first iteration. c) The code snippet causes a bounds error. d) The code snippet displays zero.

c) The code snippet causes a bounds error.

5) What is the result of executing this code snippet? int[] marks = { 90, 45, 67 }; for (int i = 0; i <= 3; i++) { System.out.println(marks[i]); } a) The code snippet does not give any output. b) The code snippet displays all the marks stored in the array without any redundancy. c) The code snippet causes a bounds error. d) The code snippet executes an infinite loop.

c) The code snippet causes a bounds error.

98) Consider the following code snippet, where the array lists contain elements that are stored in ascending order: ArrayList<Integer> list1 = new ArrayList<Integer>(); ArrayList<Integer> list2 = new ArrayList<Integer>(); . . . int count = 0; for (int i = 0; i < list1.size() && i < list2.size(); i++) { if (list1.get(i) == list2.get(i)) { count++; } } Which one of the following descriptions is correct for the given code snippet? a) The code snippet finds the highest value out of the two array lists. b) The code snippet finds the lowest value out of the two array lists. c) The code snippet compares the values of two array lists and stores the count of total matches found. d) The code snippet adds the values of the two array lists.

c) The code snippet compares the values of two array lists and stores the count of total matches found.

Which of the following statements is true about the if statement? a) The if statement can have only one condition that evaluates to an integer value. b) The if block is optional. c) The else block is optional. d) The if and else blocks should always be included within curly braces.

c) The else block is optional.

87) Which statement is true about the code snippet below? ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); ArrayList<String> friends = names; friends.add("Harry"); a) The final size of names is 2; the final size of friends is 3 b) The final size of names is 3; the final size of friends is 2 c) The final size of names is 3; the final size of friends is 3 d) Compilation error

c) The final size of names is 3; the final size of friends is 3

47) Consider the following method: public static int mystery(int length, int n) { int[] result = new int[length]; for (int i = 0; i < result.length; i++) { result[i] = (int) (n * Math.random()); } return result; } Which statement is correct about the code? a) The method works perfectly b) The method returns a random number c) The method return type should be changed to int[] d) The method has a bounds error when the array size is too large Answer: c

c) The method return type should be changed to int[]

Assuming that a user enters 15 as input, what is the output of the following code snippet? Scanner in = new Scanner(System.in); System.out.print("Please enter a number: "); int number = in.nextInt(); if (number > 20) { System.out.println("The number is LARGE!"); } else { System.out.println("The number is SMALL!"); } a) There is no output due to compilation errors. b) The number is LARGE! c) The number is SMALL! d) The number is LARGE! The number is SMALL!

c) The number is SMALL!

62) Consider the following code snippet: int val = arr[0][2]; Which value of arr is stored in the val variable? a) The value in the first row and the second column b) The value in the first row and the first column c) The value in the first row and the third column d) The value in the third row and the second column

c) The value in the first row and the third column

Which of the following options refers to the technique of simulating program execution on a sheet of paper? a) Compiling b) Prototyping c) Tracing d) Debugging

c) Tracing

What is the output of the following code snippet? int age = 25; if (age > 30) { System.out.println("You are wise!"); } else { System.out.println("You have much to learn!"); } a) There is no output due to compilation errors. b) You are wise! c) You have much to learn! d) You are wise! You have much to learn!

c) You have much to learn!

25) Suppose you wish to use an array to solve a new problem. What is the first step to take in finding a solution? a) structure a program using methods b) adapt a built-in array algorithm c) decompose the problem into steps d) assemble a test program and run it

c) decompose the problem into steps

37) Java 7 introduced enhanced syntax for declaring array lists, which is termed a) angle brackets. b) method lists. c) diamond syntax. d) symmetric slants.

c) diamond syntax.

Consider a situation where multiple if statements are combined into a chain to evaluate a complex condition. Which of the following reserved words is used to define the branch to be executed when none of the conditions are true? a) if b) else if c) else d) All of the above items

c) else

Which of the following expressions represents a legal way of checking whether a value for the num variable is either less than 100 or more than 200? a) if (num <= 100 && num >= 200) b) if (num < 100 && num > 200) c) if (num < 100 || num > 200) d) if (num <= 100 || num >= 200)

c) if (num < 100 || num > 200)

which of the following conditions tests for the user to enter the string "Hello"? String s; Scanner in = new Scanner(System.in); System.out.print("Enter a word: "); s = in.next(); a) if (s == "Hello") b) if (s.substring(0,5) == "Hello") c) if (s.equals("Hello")) d) if (s = "Hello")

c) if (s.equals("Hello"))

Which code snippet will output "Yes!" when two strings s1 and s2 are equal? a) if (s1 = s2) { System.out.println("Yes!"); } b) if (s1 == s2) { System.out.println("Yes!"); } c) if (s1.equals(s2)) { System.out.println("Yes!"); } d) if (s1.compareTo(s2) == 1) { System.out.println("Yes!");

c) if (s1.equals(s2)) { System.out.println("Yes!"); }

Which of the following is the correct syntax for an if statement? a) if (x < 10) { size = "Small"; } else (x < 20) { size = "Medium"; } b) if (x < 10); { size = "Small"; } else (x < 20) { size = "Medium"; } c) if (x < 10) { size = "Small"; } else { size = "Medium"; } d) if { size = "Small"; } else (x < 20) { size = "Medium"; }

c) if (x < 10) { size = "Small"; } else { size = "Medium"; }

31) Consider using a deck of cards as a way to visualize a shuffle algorithm. When two cards shuffle their position, what has to happen to the size of the array holding them? a) it increases by one b) it decreases by one c) it stays the same d) it increases by two

c) it stays the same

) Consider the following code snippet. Assuming that the user enters first 20 and then 12 as the two input values, what is the output of the code snippet? int num1 = 0; int num2 = 0; int num3 = 0; int num4 = 0; int num5 = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num1 = in.nextInt(); System.out.print("Enter a number: "); num2 = in.nextInt(); if (num1 < num2) { num3 = num1; } else { num3 = num2; } if (num1 < num2 + 10) { num4 = num1; } else if (num1 < num2 + 20) { num5 = num1; } System.out.println("num1 = " + num1 + " num2 = " + num2 + " num3 = " + num3 + " num4 = " + num4 + " num5 = " + num5); a) num1 = 20 num2 = 12 num3 = 20 num4 = 20 num5 = 0 b) num1 = 20 num2 = 12 num3 = 12 num4 = 0 num5 = 20 c) num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0 d) num1 = 20 num2 = 12 num3 = 20 num4 = 0 num5 = 20

c) num1 = 20 num2 = 12 num3 = 12 num4 = 20 num5 = 0

12) Suppose you wish to write a method that returns the sum of the elements in the partially filled array myArray. Which is a reasonable method header? a) public static int sum(int[] values) b) public static int sum() c) public static int sum(int[] values, int currSize) d) public static int sum(int[] values, int size, int currSize)

c) public static int sum(int[] values, int currSize)

80) Which one of the following is a correct declaration for a method named passAList that has as arguments an array list myList of size 10 and an integer array intArr of size 20, and that modifies the contents of myList and intArr? a) public static void passAList(ArrayList<Integer> myList(10), int[] intArr) b) public static void passAList(ArrayList<Integer> myList, int[20] intArr) c) public static void passAList(ArrayList<Integer> myList, int[] intArr) d) public static void passAList(ArryaList<Integer> myList, int intArr)

c) public static void passAList(ArrayList<Integer> myList, int[] intArr)

67) Which one of the following is a correct declaration for a method named passAList with the array list num of size 5 as a parameter? a) public static void passAList(ArrayList<Integer> num[5]) b) public static void passAList(ArrayList<Integer> num, 5) c) public static void passAList(ArrayList<Integer> num) d) public static void passAList(num)

c) public static void passAList(ArrayList<Integer> num)

65) Consider the following code snippet: ArrayList<Integer> num1 = new ArrayList<Integer>(); int data; Scanner in = new Scanner(System.in); for (int i = 0; i < 5; i++) { data = in.nextInt(); num1.add(data); if (data == 0 && num1.size() > 3) { num1.remove(num1.size() - 1); } } System.out.println("size is : " + num1.size()); What is the output of the given code snippet if the user enters 1,2,0,0,1 as the input? a) size is : 1 b) size is : 2 c) size is : 4 d) size is : 0

c) size is : 4

Assuming that the user provides 303 as input, what is the output of the following code snippet? int x; int y; x = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter a number: "); y = in.nextInt(); if (y > 300) { x = y; } else { x = 0; } System.out.println("x: " + x); a) x: 0 b) x: 300 c) x: 303 d) There is no output due to compilation errors.

c) x: 303

Assuming that the user provides 3 as input, what is the output of the following code snippet? int x; int y; x = 0; System.out.print("Please enter y: "); Scanner in = new Scanner(System.in); y = in.nextInt(); if (y > 0) { x = 2 * y; } else { x = 2 + x; } System.out.println("x: " + x); a) x: 2 b) x: 4 c) x: 6 d) There is no output due to compilation errors

c) x: 6

101) What is true about the following code snippet? public static double[] fillWithRandomNumbers(double[] values) { double[] numbers = new double[values.length]; for (int i = 0; i < numbers.length; i++) { numbers[i] = Math.random(); } return Arrays.copyOf(numbers, numbers.length); } public static void main(String[] args) { double[] num = new double[20]; num = fillWithRandomNumbers(num); } a) The code has a run-time error b) The code has a compilation error c) The code has a logic error d) The code is inefficient

d

30) Which code snippet calculates the sum of all the elements in even positions in an array? a) int sum = 0; for (int i = 1; i < values.length; i+=2) { sum++; } b) int sum = 0; for (int i = 0; i < values.length; i++) { sum++; } c) int sum = 0; for (int i = 0; i < values.length; i++) { sum += values[i]; } d) int sum = 0; for (int i = 0; i < values.length; i + 2) { sum += values[i]; }

d) int sum = 0; for (int i = 0; i < values.length; i + 2) { sum += values[i]; }

Which of the following operators is used to combine two Boolean conditions? a) ## b) $$ c) %% d) &&

d) &&

Which condition, when supplied in the if statement below in place of (. . .), will correctly protect against division by zero? if (. . .) { result = grade / num; System.out.println("Just avoided division by zero!"); } a) (grade == 0) b) ((grade / num) == 0) c) (num == 0) d) (num != 0)

d) (num != 0)

6) What is the valid range of index values for an array of size 10? a) 0 to 10 b) 1 to 9 c) 1 to 10 d) 0 to 9

d) 0 to 9

Assuming that a user enters 5 as the value for num, what is the output of the following code snippet? int num = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num = in.nextInt(); if (num < 50) { num = num + 5; } if (num < 10) { num = num - 2; } if (num > 5) { num++; } else { num--; } System.out.println(num); a) 0 b) 9 c) 5 d) 11

d) 11

What is the output of the following code snippet? int num = 100; if (num < 100) { if (num < 50) { num = num - 5; } else { num = num - 10; } } else { if (num > 150) { num = num + 5; } else { num = num + 10; } } System.out.println(num); a) 95 b) 100 c) 105 d) 110

d) 110

What is the output of the following code snippet? int s1 = 20; if (s1 <= 20) { System.out.print("1"); } if (s1 <= 40) { System.out.print("2"); } if (s1 <= 20) { System.out.print("3"); } a) 1 b) 2 c) 3 d) 123

d) 123

82) How many elements can be stored in a two-dimensional 5 by 6 array? a) 5 b) 6 c) 11 d) 30

d) 30

Assuming that a user enters 10, 20, and 30 as input values one after another, separated by spaces, what is the output of the following code snippet? int num1 = 0; int num2 = 0; int num3 = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); num1 = in.nextInt(); System.out.print("Enter a number: "); num2 = in.nextInt(); System.out.print("Enter a number: "); num3 = in.nextInt(); if (num1 > num2) { if (num1 > num3) { System.out.println(num1); } else { System.out.println(num3); } } else { if (num2 > num3) { System.out.println(num2); } else { System.out.println(num3); } } a) 0 b) 10 c) 20 d) 30

d) 30

56) How many elements can be stored in an array of dimension 2 by 3? a) 2 b) 3 c) 5 d) 6

d) 6

70) What is the output of the following code snippet? public static int check(ArrayList<Integer> listData) { int sum = 0; for (int i = 0; i < listData.size(); i++) { sum = sum + listData.get(i); } return sum; } public static void main(String[] args) { ArrayList<Integer> vdata = new ArrayList<Integer>(); int rsum; for (int cnt = 0; cnt < 3; cnt++) { vdata.add(cnt + 1); } rsum = check(vdata); System.out.println(rsum); } a) 4 b) 2 c) 3 d) 6

d) 6

95) What is the output of the following statements? ArrayList<String> cityList = new ArrayList<String>(); cityList.add("London"); cityList.add("New York"); cityList.add("Paris"); cityList.add("Toronto"); cityList.add("Hong Kong"); cityList.add("Singapore"); System.out.print(cityList.size()); System.out.print(" " + cityList.contains("Toronto")); System.out.print(" " + cityList.indexOf("New York")); System.out.println(" " + cityList.isEmpty()); a) 5 true 1 false b) 6 true 2 false c) 5 false 1 false d) 6 true 1 false

d) 6 true 1 false

What is the value of the price variable after the following code snippet is executed? int price = 42; if (price < 40) { price = price + 10; } if (price > 30) { price = price * 2; } if (price < 100) { price = price - 20; } a) 42 b) 52 c) 84 d) 64

d) 64

Assuming that a user enters 50, 70, and 60 as input values one after another, separated by spaces, what is the output of the following code snippet? int number1 = 0; int number2 = 0; int number3 = 0; Scanner in = new Scanner(System.in); System.out.print("Enter a number: "); number1 = in.nextInt(); System.out.print("Enter a number: "); number2 = in.nextInt(); System.out.print("Enter a number: "); number3 = in.nextInt(); if (number1 > number2) { if (number1 > number3) { System.out.println(number1); } else { System.out.println(number3); } } else { if (number2 > number3) { System.out.println(number2); } else { System.out.println(number3); } } a) 0 b) 50 c) 60 d) 70

d) 70

Assuming that a user enters 68 as the score, what is the output of the following code snippet? int score = 68; if (score < 50) { System.out.println("F"); } else if (score >= 50 || score < 55) { System.out.println("D"); } else if (score >= 55 || score < 65) { System.out.println("C"); } else if (score >= 65 || score < 75) { System.out.println("B"); } else if (score >= 75 || score < 80) { System.out.println("B+"); } else { System.out.println("A"); } a) D b) C c) B d) A

d) A

) Assuming that a user enters 64 as his score, what is the output of the following code snippet? int score = 0; Scanner in = new Scanner(System.in); System.out.print("Enter your score: "); score = in.nextInt(); if (score < 40) { System.out.println("F"); } else if (score >= 40 || score < 50) { System.out.println("D"); } else if (score >= 50 || score < 60) { System.out.println("C"); } else if (score >= 60 || score < 70) { System.out.println("B"); } else if (score >= 70 || score < 80) { System.out.println("B+"); } else { System.out.println("A"); } a) D b) C c) B d) A

d) A.

Consider the following code snippet. What is the output? int score = 68; if (score < 50) { System.out.print("You need to practice!"); } if (score < 100) { System.out.print("Almost respectable!"); } if (score < 150) { System.out.print("You hit triple digits!"); } if (score < 250) { System.out.print("Impressive!"); } a) You need to practice! b) Almost respectable !You hit triple digits! c) You hit triple digits !Impressive! d) Almost respectable !You hit triple digits !Impressive!

d) Almost respectable !You hit triple digits !Impressive!

66) Which one of the following statements about declaring an array list as a method parameter is true? a) An array list declared as a method parameter should be accompanied with its size. b) An array list declared as a method parameter is a constant value by default. c) An array list value cannot be modified in a method when the array list is declared as a parameter. d) An array list value can be modified inside the method.

d) An array list value can be modified inside the method.

94) What is the output of the following statements? ArrayList<String> names = new ArrayList<String>(); names.add("Bob"); names.add(0, "Ann"); names.remove(1); names.add("Cal"); names.set(2, "Tony"); for (String s : names) { System.out.print(s + ", "); } a) Cal, Bob, Ann b) Ann, Bob c) Ann, Cal, Tony d) Array list bound error

d) Array list bound error

89) Consider the following code snippet: public static void main(String[] args) { ArrayList<String> names = new ArrayList<String>(); names.add("John"); names.add("Jerry"); names.add("Janet"); ArrayList<String> names = reverse(names); } public static ArrayList<String> reverse(ArrayList<String> names) { ArrayList<String> result = new ArrayList<String>(); for (int i = names.size() - 1; i >= 0; i--) { result.add(names.get(i)); } return <String>result; } Which statement is true after the main method is executed? a) names contains "Janet", "Jerry", "John" in this order b) names contains "John", "Jerry", "Janet" in this order c) reverse method has a bound error d) Compilation error due to the return statement in reverse method

d) Compilation error due to the return statement in reverse method

73) What is the output of the following code snippet? ArrayList<Integer> num; num.add(4); System.out.println(num.size()); a) 1 b) 0 c) 4 d) Error because num is not initialized

d) Error because num is not initialized

What is the output of the following code snippet? int num1 = 40; if (num1 <= 40) { System.out.print("F"); } if (num1 <= 75) { System.out.print("C"); } if (num1 <= 90) { System.out.print("B"); } a) F b) C c) B d) FCB

d) FCB

Assuming that a user enters 5 as the age, what is the output of the following code snippet? int age = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter your age: "); age = in.nextInt(); if (age < 10) { System.out.println("Kid"); } if (age < 30) { System.out.print("Young"); } if (age < 70) { System.out.print("Aged"); } if (age < 100) { System.out.print("Old"); } a) Kid b) Kid Young c) Kid YoungAged d) Kid YoungAgedOld

d) Kid YoungAgedOld

33) Why is the use of physical objects helpful in algorithm design? a) It simulates the way the computer actually implements the algorithm b) It is more abstract than using a pencil and paper c) Because the constraints on physical things are the same as the constraints on bits and bytes d) Many people feel it is less intimidating than drawing diagrams

d) Many people feel it is less intimidating than drawing diagrams

What is the output of the following code snippet? boolean attendance = false; String str = "Unknown"; attendance = !(attendance); if (!attendance) { str = "False"; } if (attendance) { attendance = false; } if (attendance) { str = "True"; } else { str = "Maybe"; } System.out.println(str); a) False b) True c) Unknown d) Maybe

d) Maybe

What is the output of the following code snippet? int shoeSize = 8; if (shoeSize < 6) { System.out.println("Petite"); } if (shoeSize < 8) { System.out.println("Small"); } if (shoeSize < 10) { System.out.println("Medium"); } if (shoeSize < 14) { System.out.println("Large"); } a) Petite b) Petite Small c) Small Medium d) Medium Large

d) Medium Large

75) Consider the following code snippet: ArrayList<Integer> arrList = new ArrayList<Integer>(); for (int i = 0; i < arrList.size(); i++) { arrList.add(i + 3); } What value is stored in the element of the array list at index 0? a) 0 b) 3 c) 6 d) None

d) None

Assuming that a user enters 56 for age, what is the output of the following code snippet? int age = 0; Scanner in = new Scanner(System.in); System.out.print("Please enter your age: "); age = in.nextInt(); if (age < 13) { System.out.println("Kid!"); } if (age >= 13 && age < 19) { System.out.println("Teen!"); } if (age >= 19 && age < 30) { System.out.println("Young!"); } if (age >= 30 && age < 50) { System.out.println("Adult!"); } if (age >= 50) { System.out.println("Old!"); } a) Teen! b) Young! c) Adult! d) Old!

d) Old!

What kind of operator is the <= operator? a) Ternary b) Arithmetic c) Inequality d) Relational

d) Relational

17) What is the result of the following code? for (double element : values) { element = 0; } a) The elements of the array values are initialized to zero. b) The elements of the array element are initialized to zero. c) The first element of the array values is initialized to zero. d) The array values is not modified.

d) The array values is not modified.

90) Which statement is true about the code snippet below? public static void main(String[] args) { Scanner in = new Scanner(System.in); ArrayList<Double> inputs = new ArrayList<Double>(); int ind = 0; while (in.hasNextDouble()) { inputs.set(ind, in.nextDouble()); ind++; } } a) The code adds all input numbers to the array list b) The code has compile-time error c) The array list is full after 100 numbers are entered d) The code has a run-time error

d) The code has a run-time error

When an if statement is nested inside another if statement, it creates the possibility of a) an infinite loop b) the misuse of the break statement c) type mismatch d) The dangling else

d) The dangling else

71) What is the result of the following definition of an array list? ArrayList<Double> points; a) The statement causes a compile time error as the size of the array list is not defined. b) The statement creates an array list of size 0. c) The statement creates an array list with unlimited size. d) The statement defines an array list reference but leaves its value as null.

d) The statement defines an array list reference but leaves its value as null.

42) Is there any thing wrong with the following code snippet? String[] data = { "abc", "def", "ghi", "jkl" }; String searchedValue = "ghi"; int pos = 0; boolean found = false; while (pos < data.length) { if (data[pos].equals(searchedValue)) { found = true; } else { found = false; } pos++; } if (found) { System.out.println("Found at position: " + pos); } else { System.out.println("Not found"); } a) There is nothing wrong. b) There is compile-time error. c) There is a bounds error. d) There is a logic error.

d) There is a logic error.

53) Select the statement that reveals the logic error in the following method. public static double minimum(double[] data) { double smallest = 0.0; for (int i = 0; i < data.length; i++) { if (data[i] < smallest) { smallest = data[i]; } } return smallest; } a) double m = minimum(new double[] { 1.2, -3.5, 6.6, 2.4 }); b) double m = minimum(new double[] { 1.2, 23.5, 16.6, -23.4 }); c) double m = minimum(new double[] { -10.2, 3.5, 62.6, 21.4 }); d) double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

d) double m = minimum(new double[] { 12.2, 31.5, 6.6, 2.4 });

Suppose one needs an if statement to check whether an integer variable pitch is equal to 440 (which is the frequency of the note "A" to which strings and orchestras tune). Which condition is correct? a) if (pitch - 440 = 0) b) if ((pitch !< 440) && (pitch !> 440)) c) if (pitch = 440) d) if (pitch == 440)

d) if (pitch == 440)

21) When an array reading and storing input runs out of space a) the program must be recompiled with a bigger size for the array. b) the array must be "grown" using the growArray method. c) it automatically resizes to accommodate new elements. d) the array must be "grown" using the new command and the copyOf method.

d) the array must be "grown" using the new command and the copyOf method.


Related study sets

How to Read Literature Like a Professor 1-11

View Set

BiF - vzorečky (pracuje se na tom)

View Set

Neural networks and Deep learning

View Set

AnaPhy Ch. 17 The Special Senses

View Set

Lifespan Development Quiz Questions

View Set

Macro Exam 2 (HW 7), Econ Test 2, MACRO MIDTERM, Macroeconomics Exam 1, Quiz 7, Economics Chapter 16, Chapter 5, 14,15,16 (Mid-Term), Chapter 12 Macro Review Questions, Economics Homework, Macro Test 2, ch 17, Chapter 17 Macroeconomics - Long / Short...

View Set

Maternal Child 5-6 High Risk Pregnancy

View Set