Exam 4: Loops

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

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?

(Multiple Choice)
4.9/5
(40)

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

(Multiple Choice)
4.8/5
(29)

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

(Multiple Choice)
4.9/5
(33)

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

(Multiple Choice)
4.7/5
(37)

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

(Multiple Choice)
4.8/5
(34)

Is the code snippet written below legal? String s = "1234"; For (int i = 0; i <= 5; i++) { System.out.print); }

(Multiple Choice)
4.7/5
(34)

Which of the following is considered a loop with a problematic condition?

(Multiple Choice)
4.8/5
(37)

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

(Multiple Choice)
4.7/5
(31)

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?

(Multiple Choice)
4.9/5
(44)

A loop inside another loop is called:

(Multiple Choice)
4.8/5
(38)

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?

(Multiple Choice)
4.8/5
(37)

Which of the following code snippets will generate a random number between 0 and 79?

(Multiple Choice)
4.8/5
(31)

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

(Multiple Choice)
4.9/5
(38)

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

Which error type does the "off-by-one" error belong to?

(Multiple Choice)
4.7/5
(37)

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

(Multiple Choice)
4.8/5
(43)

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?

(Multiple Choice)
4.9/5
(48)

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

(Multiple Choice)
4.9/5
(35)

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

(Multiple Choice)
4.9/5
(37)

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

(Multiple Choice)
4.8/5
(45)
Showing 21 - 40 of 100
close modal

Filters

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