MIS Exam 2 - LOOPS

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

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

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

b

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

b

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.

b

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

b

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

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

c

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

c

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

c

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.

c

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

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

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

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

a

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

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

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

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.

b

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

b

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

b

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

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

c

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

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

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

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

d

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)

d

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

d

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

d

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

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

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

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)

d

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

d

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


संबंधित स्टडी सेट्स

Old Testament Final Exam (quiz questions)

View Set

Pharmacology assessment (B) 2019

View Set

Salesforce ADM 201 Study Questions (Ravi Benedetti Guide via Adam Frank)

View Set

Ch 1 Introducing Environmental Science and Sustainability objectives

View Set

Dynamic Study Modules: Fluid, Electrolyte, and Acid-Base Balance

View Set

Econ 201 Online Midterm Exam (Questions Only)

View Set