Exam 6: Loops

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

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;

Free
(Multiple Choice)
4.9/5
(29)
Correct Answer:
Verified

C

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;

Free
(Multiple Choice)
4.9/5
(35)
Correct Answer:
Verified

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

Free
(Multiple Choice)
4.8/5
(36)
Correct Answer:
Verified

D

Which of the following is considered a loop with a bound that could be problematic?

(Multiple Choice)
4.9/5
(34)

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

(Multiple Choice)
4.8/5
(36)

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

(Multiple Choice)
4.8/5
(40)

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

(Multiple Choice)
4.9/5
(34)

Is the following code snippet legal? boolean b = false; Do { System.out.println("Do you think in Java?"); } While (b !=B);

(Multiple Choice)
4.8/5
(41)

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

(Multiple Choice)
4.8/5
(39)

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

(Multiple Choice)
4.9/5
(34)

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

(Multiple Choice)
4.8/5
(34)

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

(Multiple Choice)
4.9/5
(40)

What is the output of the code snippet given below? String s = "12345"; Int i = 1; Do { If (i > 1) { System.out.print); } } While (i < 5);

(Multiple Choice)
4.8/5
(44)

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

(Multiple Choice)
4.8/5
(33)

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

(Multiple Choice)
4.9/5
(35)

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

(Multiple Choice)
4.8/5
(43)

What will be printed by the statements below? Int a = 10; While (a > 5) { System.out.print (a + " "); A = a - 2; }

(Multiple Choice)
4.9/5
(38)

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

(Multiple Choice)
4.8/5
(40)

The process of hand-tracing code is valuable because

(Multiple Choice)
4.8/5
(32)

What will be printed by the statements below? Int val = 1; Int sum = 0; While (val < 5) { Sum = sum + val; Val++; } System.out.print (sum);

(Multiple Choice)
4.9/5
(36)
Showing 1 - 20 of 128
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)