CSA - Unit 6

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

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

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

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

A) 100

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.

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

What does the following code snippet display? char ch1 = '\u0000'; char ch2 = '\uffff'; Random generator = new Random(); for (inti = 0; i< 1000; i++) { if (i % 50 == 0) { System.out.println(); } System.out.print((char)(ch1 + generator.nextDouble() * (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'.

A) It displays random Unicode characters.

Which for loop prints data across each row in the following code snippet? inti; 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

A) The inner for loop

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

A) The value stored there has changed to something new

In the following code snippet, when does the execution of the program switch from the inner loop to the outer loop? inti; 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

A) When the value of j becomes 5

What is the data type of the number generated by the Random.nextDouble() method? A) double B) float C) int D) String

A) double

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

A) for(i = 1; i != n; i++)

Suppose that the chance to hit the jackpot in a lottery is one in one million. Which of the expressions simulates that random number? (Assume Random generator = new Random();) A) generator.nextDouble() * 1000000 B) generator.nextDouble() * 100000 C) generator.nextDouble() * 9999990 + 1 D) generator.nextDouble() * 1000000 + 1

A) generator.nextDouble() * 1000000

What are the values of i and j after the following code fragment runs? inti = 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

A) i = 1951, j = 0

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? (Assume Random generator = new Random();) A) int sum = generator.nextInt(6) + generator.nextInt(6) + 2 B) int sum = generator.nextInt(12) + 1 C) int sum = generator.nextInt(6 + 1) + generator.nextInt(6 + 1) D) int sum = generator.nextInt(11) + 2

A) int sum = generator.nextInt(6) + generator.nextInt(6) + 2

What does the following code do? int sum = 0; final double count = 1000; Random generator = new Random(); for (inti = 1; i<= count; i++) { sum = sum + generator.nextInt(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.

B) It calculates the average of 1000 random numbers between 0 and 100.

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

B) for (int i = 0) { }

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

B) years--;

What is the output of the code below? for (int val = 0; val < 4; val ++) { int sum = val; for (intnum = 0; num<val; num++) { sum = sum + num; } "System.out.print (sum + "" "");" } A) 0 1 3 6 B) 0 1 4 10 C) 0 2 5 9 D) 0 2 7 16

C) 0 2 5 9

Assume the following variable has been declared and given a value as shown: Random rand = new Random(); int number = rand.nextInt (27) * 2 + 3; What are the smallest and largest values number may be assigned? A) 3, 57 B) 0, 27 C) 3, 55 D) 0, 26

C) 3, 55

How many times does the following loop execute? intupperCaseLetters = 0; "String str = ""abcdEfghI"";" boolean found = false; for (inti = 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

C) 5 times

Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedalsof 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.

C) A storyboard can be used.

How many times does the following loop execute? double d; Random generator = new Random(); double x = generator.nextDouble() * 100; do { d = Math.sqrt(x) * Math.sqrt(x) - x; System.out.println(D); x = generator.nextDouble() * 10001; } while (d != 0); A) Exactly once B) Exactly twice C) Can't be determined D) Always infinite loop

C) Can't be determined

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

C) I only

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

C) III only

Which of the following loops executes the statements inside the loop before checking the condition? A) for B) while C) do D) do-for

C) do

What are the values of i and j after the following code snippet executes? inti = 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

C) i = 1951, j = 0

What are the values of i and j after the following code snippet is run? inti = 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

C) i = 351, j = 2

What is the output of this code snippet if the user enters the numbers 1 2 3 4 -1 5? double total = 0; booleanhasValidNumber = 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

D) 10.0

What is the output of the code snippet given below? "String s = ""12345"";" inti = 1; while (i< 5) { System.out.print(s.substring(i, i + 1)); i++; } A) No output B) 1234 C) 12345 D) 2345

D) 2345

What will be printed by the statements below? intval = 1; int sum = 0; while (val< 5) { sum = 0; sum = sum + val; val++; } System.out.print (sum); A) 15 B) 10 C) 5 D) 4

D) 4

What will be the output of the following code snippet? inti; int j; for (i = 0; i<7; 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) A 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.

D) A 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.

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.

D) A sentinel is a value that indicates the end of an input sequence.

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

D) An alphabetic character

What will be the range of the random numbers generated by the following code snippet? Random generator = new Random(); int r1 = generator.nextInt(50) + 1; A) Between 1 and 49 B) Between 0 and 50 C) Between 0 and 49 D) Between 1 and 50

D) Between 1 and 50

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

D) I, II, and III

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? (Assume Random generator = new Random();) A) System.out.println(generator.nextInt(1)+ " " + generator.nextInt(2)); B) System.out.println((generator.nextDouble()+ 2) * 2); C) System.out.println(generator.nextInt(1)+ " " + generator.nextInt(1)); D) System.out.println(generator.nextInt(2) + " " + generator.nextInt(2));

D) System.out.println(generator.nextInt(2) + " " + generator.nextInt(2));

Which of the following code snippets will generate a random number between 0 (inclusive) and 79 (inclusive)? (Assume Random generator = new Random();) A) intval = generator.nextInt(79) + 1; B) intval = generator.nextInt(80) - 1; C) intval = generator.nextInt(79); D) intval = generator.nextInt(80);

D) intval = generator.nextInt(80);

Which of the following loops will print the odd numbers between 0 and 20? A) while (num< 10) { int value = num * 2 - 1; "System.out.print (value + "" "");" num ++; } B) while (num< 20) { int value = num * 2 - 1; "System.out.print (value + "" "");" num ++; } C) while (num< 10) { "System.out.print (num + "" "");" num += 2; } D) while (num< 20) { num += 2; "System.out.print (num + "" "");" }

A) while (num< 10) { int value = num * 2 - 1; "System.out.print (value + "" "");" num ++; }

What will be the output of the following code snippet? boolean token1 = true; while (token1) { for (inti = 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.

B) Hello will be displayed 10 times.

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

B) I and II only

What is the output of the code snippet given below? "String s = ""12345"";" inti = 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

B) No output (infinite loop)

What is the output of the code snippet given below? "String s = ""abcde"";" inti = 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

B) No output (infinite loop)

What is the output of this code snippet? "String str = ""ABCabc"";" char ch; inti = 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 3333 ... (infinite loop) D) 0 1 2

C) 3 3333 ... (infinite loop)

How many times does the following loop run? inti = 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

C) 3 times

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

C) 30

What output does this while loop generate? j = 6; while (j > 0) { "System.out.print(j + "", "");" j--; } A) No output is generated. B) 6, 5, 4, 3, 2, 1 C) 6, 5, 4, 3, 2, 1, D) The output is infinite.

C) 6, 5, 4, 3, 2, 1,

What is the output of the code fragment given below? inti = 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.

C) 63

How many times does the code snippet given below display ""Loop Execution""?" inti = 1; while (i != 10) { "System.out.println (""Loop Execution"");" i++; } A) Infinite times B) 8 times C) 9 times D) 10 times

C) 9 times

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

C) III only

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.

C) No output after successful compilation.

Is the code snippet written below legal? "String s = ""1234"";" for (inti = 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 = 4, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error. D) No; line 4 should have no semicolon at the end.

C) No; for i = 4, s.substring(i, i + 1) will result in an StringIndexOutOfBounds error.

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

C) Run-time error

What does the following loop compute? Scanner in = new Scanner (System.in); int sum = 0; int count = 0; while (in.hasNextInt()) { int value = in.nextInt(); if (value > 0) { sum += value; count++; } } double result = (sum * 1.0)/count; A) The average of all the integers in the input B) The sum of all the positive integers in the input divided by the number of integers in the input C) The average of all the positive integers in the input D) The second smallest value in the input

C) The average of all the positive integers in the input

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

C) The information needed to solve the problem, and how to present that information

Which statement about this code snippet is accurate? int years = 50; double balance = 10000; double targetBalance = 20000; double rate = 3; for (inti = 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.

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

What is the output of the following code fragment? inti = 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

C) The value of sum is 120

What is the output of the code snippet given below? "String s = ""abcdefghijkl"";" inti = 1; do { if (i> 2) { System.out.print(s.substring (1, i)); } i++; } while (i< 5); A) abcd B) bcde C) bcbcd D) cdef

C) bcbcd

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

C) c <= 'Z'

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

C) for

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

C) while (in.hasNextDouble());

What is the output of the code below? for (int val = 0; val < 4; val ++) { "System.out.print (""+"");" for (intnum = 0; num<val; num++) { "System.out.print (""0"");" } } A)+0+00+000+0000 B)+000+000+000+000 C)++0+00+000 D)++++000000

C)++0+00+000

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.

D) "Hello" will be displayed only once.

What is the output of the following code snippet? inti = 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.

D) "Inside the while loop" will be displayed only once.

What is the sentinel value in the following code snippet? public static void main(String[] args) { int age = 0; intsumOfAges = 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

D) -1

What is the output of the code snippet given below? inti = 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)

D) 0 2 4 6 8 10 12 14 .... (infinite loop)

What is the output of the code snippet given below? inti = 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)

D) 0 2 4 6 8 10 12 14 .... (infinite loop)

What is the output of the code snippet given below? inti = 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)

D) 0 3 6 9 .... (infinite loop)

What is the last output line of the code snippet given below? inti = 5; while (i>= 1) { intnum = 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**

D) 1**

How many times will the following loop run? inti = 0; while (i< 10) { System.out.println(i); i++; } A) 0 B) 8 C) 9 D) 10

D) 10

What will be printed by the statements below? int a = 10; while (a > 5) { a = a - 2; "System.out.print (a + "" "");" } A) 10 8 6 B) 10 8 6 4 C) 8 6 D) 8 6 4

D) 8 6 4

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

D) An infinite number of times

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

D) I, II, and III

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

D) II or III

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.

D) It gives valuable insight that you do not get by running the code.

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.

D) Only one time.

Which statement is correct about the execution of the loop in the following code fragment? double num; intincr = 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.

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

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.

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

What is the output of the code snippet given below? "String s = ""aeiou"";" inti = 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

D) aei

What is the output of the code snippet given below? "String s = ""abcde"";" inti = 1; while (i< 5) { System.out.print(s.substring(i, i + 1)); i++; } A) No output B) abcd C) abcde D) bcde

D) bcde

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;

D) done = true;

Which of the following loops executes exactly 10 times? A) for (inti = 0; i<= 10; i++) { } B) inti = 0; boolean found = false; do { i++; if (i % 10 == 0) { found = true; } } while (i< 10 && !founD); C) inti = 0; while (i<= 10) { i++; } D) for (int i = 1; i <= 10; i++)

D) for (int i = 1; i <= 10; i++)

What is the output of the code snippet given below? inti; 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

D) i =5, j =5

What changes do you need to make in the following code snippet to display "Let us learn Java" exactly 10 times? inti = 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) inti = 1;

D) inti = 1;

What is the output of the code fragment given below? inti = 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

D) j=14

What is the output of the following code snippet? double a = 2; int n = 16; double r = 1; double b = a; inti = 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) r = 16.0 B) r = 128.0 C) r = 4096.0 D) r = 65536.0

D) r = 65536.0

Assume the following variable has been declared and given a value as shown: Random rand = new Random(); Which of the following will generate a random integer in the range - 20 to 20, inclusive, where each value has an equal chance of being generated? A) rand.nextInt (-20, 20) B) rand.nextInt(20) - 41 C) rand.nextInt (-20) + 40 D) rand.nextInt(41) - 20

D) rand.nextInt(41) - 20

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

A) 2 4 7 11 16 22

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

A) 211

Assume the following variable has been declared and given a value as shown: Random rand = new Random(); double number = rand.nextDouble () * 2 + 3; What are the smallest and largest values number may be assigned? A) 3.0, 5.0 B) 0.0, 6.0 C) -3.0, 3.0 D) 0.0, 3.0

A) 3.0, 5.0

What does the following code snippet display? char ch1 = '\u0000'; char ch2 = '\uffff'; for (inti = 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'.

A) It displays random Unicode characters.

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.

A) Make it easy to distinguish between user input and program output.

What does the method below return? intfindSomething (String str) { int position = 0; while (position <str.length() && (str.charAt (position) != 'e')) { position++; } return position; } A) The position of the first 'e' in the string or the length of the string if there is no 'e' B) The position of the last 'e' in the string or the length of the string if there is no 'e' C) The position of the first character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e' D) The position of the last character that is not an 'e' in the string or the length of the string if there is no character that is not an 'e'

A) The position of the first 'e' in the string or the length of the string if there is no 'e'

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

A) for (i = 1; i != n; i++)

What for loop can be used in the indicated area so the code will print: **** *** ** * for (int val = 0; val < 4; val ++) { "System.out.print (""+"");" // Put for loop here { "System.out.print (""*"");" } System.out.println (); } A) for (intnum = 0; num< 4 - val; num++) B) for (intnum = 0; num<val; num++) C) for (intnum = 4; num<val; num++) D) for (intnum = 4; num> 0; num--)

A) for (intnum = 0; num< 4 - val; num++)

Which of the following loops will print the odd numbers between 0 and 20? A) int num = 1; while (num< 20) { "System.out.print (num + "" "");" num += 2; } B) int num = 1; while (num< 20) { "System.out.print (num + "" "");" num ++; } C) int num = 0; while (num< 20) { "System.out.print (num + "" "");" num += 2; } D) int num = 1; while (num< 20) { num += 2; "System.out.print (num + "" "");" }

A) int num = 1; while (num< 20) { "System.out.print (num + "" "");" num += 2; }

Which of the following code snippets displays the output exactly 10 times? A) inti = 0; while (i< 10); { "System.out.println(""This is example 1."");" i++; } B) inti = 0; while (i< 10) { "System.out.println(""This is example 2."");" i++; } C) inti = 0; while (i< 10) { "System.out.println(""This is example 3."");" } D) inti = 1; while (i< 10) { "System.out.println(""This is example 4."");" i++;

B) inti = 0; while (i< 10) { "System.out.println(""This is example 2."");" i++; }

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.

B) ""Hello"" will be displayed only once.

What is the output of the following code snippet? int f1 = 0; int f2 = 1; intfRes; "System.out.print(f1 + "" "");" "System.out.print(f2 + "" "");" for (inti = 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 3455 C) 0 1 4 6 8 10 12 14 16 18 D) 0 1 6 7 9 12 14 17 19 21

B) 0 1 1 2 3 5 8 13 21 3455

What will be printed by the statements below? for (intctr = 0; ctr< 10; ctr++) { "System.out.print (ctr + "" "");" } A) 0 1 2 3 4 5 6 7 8 9 10 B) 0 1 2 3 4 5 6 7 8 9 C) 0 2 4 6 8 D) 0 1 3 5 7 9

B) 0 1 2 3 4 5 6 7 8 9

What is the output of the following code snippet? inti = 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

B) 1 3 5 7 9 11 13 19

What is the output of the following code snippet? inti = 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

B) 1 3 9

What will be printed by the statements below? for (intctr = 10; ctr> 5; ctr--) { "System.out.print (ctr + "" "");" } A) 10 9 8 7 6 5 B) 10 9 8 7 6 C) 5 6 7 8 9 10 D) 6 7 8 9 10

B) 10 9 8 7 6

What values does counter variable i assume when this loop executes? for (inti = 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

B) 20, 14, 8, 2, -4

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.

B) The Average Salary: 30

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.

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

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) // better is while (j * j <= number) { 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. // incorrect if number is 1 D) The code snippet will loop forever.

B) The code snippet will display the desired result.

What is the output of the following code fragment? inti = 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

B) The value of sum is 66

When hand-tracing the loop in the code snippet below, which variables are important to evaluate? inti=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

B) The variables i and sum

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.

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

Which of the following conditions can be added to the code below so it will loop until the value of sum is greater than 100? Scanner in = new Scanner (System.in); int sum = 0; do { sum += in.nextInt(); } while (/* put condition here */) A) sum != 0 B) sum <= 100 C) sum > 100 D) sum == 100

B) sum <= 100

What is the output of the following code snippet? inti = 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)

C) 1 2 3 4 5 6 7 8 End

What will be printed by the statements below? intval = 1; int sum = 0; while (val< 5) { sum = sum + val; val++; } System.out.print (sum); A) 4 B) 5 C) 10 D) 15

C) 10

What will be printed by the statements below? int a = 10; while (a > 5) { "System.out.print (a + "" "");" a = a - 2; } A) 10 9 8 7 6 5 B) 10 8 6 4 C) 10 8 6 D) 10 8

C) 10 8 6

How many times does the following code fragment display ""Hi""?" inti = 10; while (i>= 0) { "System.out.println(""Hi"");" i--; } A) 9 times B) 10 times C) 11 times D) 12 times

C) 11 times

How many times does the loop execute in the following code fragment? inti; for (i = 0; i< 50; i = i + 4) { System.out.println(i); } A) 11 B) 12 C) 13 D) 14

C) 13

How many times does the code snippet below display ""Hello""?" inti = 0; while (i != 15) { "System.out.println(""Hello"");" i++; } A) Infinite times B) 14 times C) 15 times D) 16 times

C) 15 times

How many times does the following loop run? inti = 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

C) 2 times

How many times is the text "Let's have fun with Java." printed when this code snippet is run? inti = 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

C) 3

What is the result when the following code is run? double x = 1; double y = 1; inti = 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

C) 3


Ensembles d'études connexes

Earth- An Introduction to Physical Geology #4

View Set

Chapter 8: Business Markets and Buying Behavior

View Set

REVIEW PHRM Chapter 18 Drugs Used for Seizure Disorders

View Set

Exammaster PANCE Practice V3 Block 1

View Set

QUIZ 9b. The Water-Soluble Vitamins: B Vitamins and Vitamin C

View Set

MGT 3332 Operations Management Chapter 1

View Set