Exam 4: Loops

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

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

(Multiple Choice)
5.0/5
(37)

Which of the following loops executes the statements inside the loop before checking the condition?

(Multiple Choice)
4.7/5
(40)

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

(Multiple Choice)
4.7/5
(34)

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.7/5
(38)

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

(Multiple Choice)
4.8/5
(40)

What will be the output of the following code snippet? Boolean token = false; Do { System.out.println("Hello"); } While (token);

(Multiple Choice)
4.8/5
(24)

What will be the output of the following code snippet? Boolean token = false; While (token) { System.out.println("Hello"); }

(Multiple Choice)
4.9/5
(35)

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

(Multiple Choice)
4.8/5
(44)

Which of the following code snippets displays the output exactly 10 times?

(Multiple Choice)
4.9/5
(35)

What is the output of the following code fragment? Int i = 1; Int sum = 0; While (i <= 11) { Sum = sum + i; I++; } System.out.println("The value of sum is " + sum);

(Multiple Choice)
4.9/5
(27)

What is the data type of the number generated by the Math.random() function?

(Multiple Choice)
4.9/5
(41)

Which statement is correct about the execution of the loop in the following code fragment? Double num; Int incr = 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);

(Multiple Choice)
4.8/5
(44)

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.7/5
(37)

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

(Multiple Choice)
4.7/5
(42)

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)
5.0/5
(36)

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)
5.0/5
(41)

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.8/5
(39)

How many times does the code snippet below display "Hello"? Int i = 0; While (i != 15) { System.out.println("Hello"); I++; }

(Multiple Choice)
4.8/5
(35)

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

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

(Multiple Choice)
4.9/5
(30)
Showing 61 - 80 of 100
close modal

Filters

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