Exam 6: Loops

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

Which for loop prints data across each row in the following code snippet? Int i; Int j; For (i = 1; i <= 3; i++) { For (j = 1; j <= 3; j++) { System.out.print("X"); } System.out.println(""); }

(Multiple Choice)
4.8/5
(36)

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

What output does this while loop generate? j = 6; While (j > 0) { System.out.print(j + ", "); J--; }

(Multiple Choice)
4.9/5
(38)

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

(Multiple Choice)
4.8/5
(35)

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

(Multiple Choice)
4.9/5
(49)

What will be printed by the statements below? for (int ctr = 10; ctr > 5; ctr--) { System.out.print (ctr + " "); }

(Multiple Choice)
4.8/5
(42)

Which code snippet produces the sum of the first n even numbers?

(Multiple Choice)
4.8/5
(41)

What does the following code snippet display? char ch1 = '\u0000'; Char ch2 = '\uffff'; Random generator = new Random(); For (int i = 0; i < 1000; i++) { If (i % 50 == 0) { System.out.println(); } System.out.print((char)(ch1 + generator.nextDouble() * (ch2 - ch1 + 1))); }

(Multiple Choice)
4.8/5
(32)

Suppose you must design a program to calculate the roll-out (number of inches traveled in one revolution of the pedals of a bicycle based on its gear combinations). The user must provide the gear sizes, which must be converted into roll-out for all different gear combinations. How can the flow of user interaction for this problem be designed?

(Multiple Choice)
4.9/5
(32)

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)
4.9/5
(31)

When hand-tracing a portion of code, which statement about Boolean conditions is true?

(Multiple Choice)
4.8/5
(40)

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

(Multiple Choice)
4.9/5
(41)

Which of the following statements is correct about a sentinel?

(Multiple Choice)
5.0/5
(36)

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

What are the values of i and j after the following code snippet executes? 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); System.out.println(j);

(Multiple Choice)
4.8/5
(45)

A loop inside another loop is called:

(Multiple Choice)
4.9/5
(36)

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

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) // better is while (j * j <= number) { 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.8/5
(35)

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

(Multiple Choice)
4.9/5
(43)

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)
5.0/5
(46)
Showing 41 - 60 of 128
close modal

Filters

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