Exam 4: Loops

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

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

(Multiple Choice)
4.9/5
(36)

What is the last output line of the code snippet given below? Int i = 5; While (i >= 1) { Int num = 1; For (int j = 1; j <= i; j++) { System.out.print(num + "**"); Num = num * 2; } System.out.println(); I = i - 1; }

(Multiple Choice)
4.9/5
(48)

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

Select the statement that correctly completes the loop in this code snippet. Int years = 20; Double balance = 10000; While (years > 0) { __________ Double interest = balance * rate / 100; Balance = balance + interest; }

(Multiple Choice)
4.9/5
(38)

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)

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

Which statement about this code snippet is accurate? Int years = 50; Double balance = 10000; Double targetBalance = 20000; Double rate = 3; For (int i = 1; i <= years; i++) { If (balance >= targetBalance) { I = years + 1; } Else { Double interest = balance * rate / 100; Balance = balance + interest; } }

(Multiple Choice)
4.7/5
(36)

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

Which of the following statements is correct about a sentinel?

(Multiple Choice)
5.0/5
(36)

How many times does the following loop execute? double d; double x = Math.random() * 100; do { d = Math.sqrt(x) * Math.sqrt(x) - x; System.out.println(d); x = Math.random() * 10001; } while (d != 0);

(Multiple Choice)
4.9/5
(42)

What will be the range of the random numbers generated by the following code snippet? Int r1 = (int) (Math.random() * 50) + 1;

(Multiple Choice)
4.8/5
(39)

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

(Multiple Choice)
4.8/5
(40)

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

(Multiple Choice)
4.9/5
(48)

What will be the output of the following code snippet? Int i; Int j; For (i = 0; i < 6; i++) { For (j = 7; j > i; j--) { System.out.print("*"); } System.out.println(""); }

(Multiple Choice)
4.8/5
(30)

What is the sentinel value in the following code snippet? Public static void main(String[] args) { Int age = 0; Int sumOfAges = 0; Int stop = 1; Scanner reader = new Scanner(System.in); System.out.println("Enter an age (-1 to stop): "); Age = reader.nextInt(); While (age != -1) { SumOfAges = sumOfAges + age; System.out.println("Enter an age (-1 to stop): "); Age = reader.nextInt(); } System.out.println("Sum of ages " + sumOfAges); Return 0; }

(Multiple Choice)
4.7/5
(30)

Which of the following loop(s) could possibly not enter the loop body at all? I. for loop II. while loop III. do loop

(Multiple Choice)
4.8/5
(34)

How many times does the code snippet given below display "Loop Execution"? Int i = 1; While (i != 10) { System.out.println ("Loop Execution"); I++; }

(Multiple Choice)
4.8/5
(40)

What is the output of the following code snippet? Int f1 = 0; Int f2 = 1; Int fRes; System.out.print(f1 + " "); System.out.print(f2 + " "); For (int i = 1; i < 10; i++) { FRes = f1 + f2; System.out.print(fRes + " "); F1 = f2; F2 = fRes; } System.out.println();

(Multiple Choice)
4.7/5
(40)

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

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.9/5
(39)
Showing 41 - 60 of 100
close modal

Filters

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