Exam 6: Loops

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

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

(Multiple Choice)
4.9/5
(30)

What is the output of the code snippet given below? Int i = 0; While (i != 9) { System.out.println("" + i); I = i + 2; }

(Multiple Choice)
4.7/5
(34)

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

(Multiple Choice)
4.9/5
(53)

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

(Multiple Choice)
4.8/5
(36)

Which of the following loops will print the odd numbers between 0 and 20?

(Multiple Choice)
4.8/5
(38)

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

(Multiple Choice)
4.8/5
(40)

Which statement about storyboards is true?

(Multiple Choice)
5.0/5
(41)

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

(Multiple Choice)
4.8/5
(45)

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

(Multiple Choice)
4.7/5
(43)

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

(Multiple Choice)
4.8/5
(36)

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?

(Multiple Choice)
4.7/5
(31)

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

(Multiple Choice)
4.8/5
(45)

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

(Multiple Choice)
4.7/5
(45)

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

(Multiple Choice)
4.8/5
(39)

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

(Multiple Choice)
4.9/5
(39)

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

(Multiple Choice)
4.9/5
(36)

Which of the following conditions can be added to the code below so it will loop until the user enters "no" or "NO"? Scanner in = new Scanner (System.in); System.out.print ("Continue? "); String response = in.next(); While (/* put condition here */) { System.out.println ("Hello beautiful!"); System.out.print ("Continue? "); Response = in.next(); }

(Multiple Choice)
4.9/5
(30)

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

(Multiple Choice)
4.9/5
(41)

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

(Multiple Choice)
4.7/5
(38)

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
(35)
Showing 101 - 120 of 128
close modal

Filters

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