Java Chapter 6 - Loops

¡Supera tus tareas y exámenes ahora con Quizwiz!

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

0 2 4 6 8 10 12 14 .... (infinite loop)

How many times does the following code fragment display "Hi"? int i = 10; while (i >= 0) { System.out.println("Hi"); i--; }

11 times

How many times does the loop execute in the following code fragment? int i; for (i = 0; i < 50; i = i + 4) { System.out.println(i); }

13

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

15 times

What values does counter variable i assume when this loop executes? for (int i = 20; i >= 2; i = i - 6) { System.out.print(i + ","); }

20, 14, 8, 2, -4

What will be printed by the statements below? int val = 1; int sum = 0; while (val < 5) { sum = 0; sum = sum + val; val++; } System.out.print (sum);

4

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

6, 5, 4, 3, 2, 1,

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

63

A loop inside another loop is called:

A nested loop

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

Run-time error

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

Yes, it is legal and prints "Do you think in Java?" once.

Which loop executes the statements inside the loop before checking the condition?

do

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

int i = 1;

Write a loop that will print the odd numbers between 0 and 20?

int num = 1; while (num < 20) { System.out.print (num + " "); num += 2; }

What condition can be added to the code below so it will loop until the value of sum is greater than 100? Scanner in = new Scanner (System.in); int sum = 0; do { sum += in.nextInt(); } while (/* put condition here */)

sum <= 100

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

-1

What is the output of the code snippet given below? int i = 0; while (i != 11) { System.out.print(" " + i); i = i + 2; }

0 2 4 6 8 10 12 14 .... (infinite loop)

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

2 times

How many times is the text "Let's have fun with Java." printed when this code snippet is run? int i = 0; do { System.out.println("Let's have fun with Java."); i++; if (i % 2 == 0) { i = 10; } } while (i <= 10);

3

What will be the output of the following code snippet? boolean token1 = true; while (token1) { for (int i = 0; i < 10; i++) { System.out.println("Hello"); } token1 = false; }

Hello will be displayed 10 times.

What does the following loop compute? Scanner in = new Scanner (System.in); int sum = 0; int count = 0; while (in.hasNextInt()) { int value = in.nextInt(); if (value > 0) { sum += value; count++; } } double result = (sum * 1.0)/count;

The average of all the positive integers in the input

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

The loop will execute at least once even if the user has entered the sentinel value.

Which 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?

do

What statement will correctly complete the loop in this code snippet. int years = 20; double balance = 10000; while (years > 0) { __________ double interest = balance * rate / 100; balance = balance + interest; }

years--;

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

"Hello" will be displayed only once.

Assume the following variable has been declared and given a value as shown: Random rand = new Random(); int number = rand.nextInt (27) * 2 + 3; What are the smallest and largest values number may be assigned?

3, 55

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

9 times

Which of the following statements is correct about a sentinel?

A sentinel is a value that indicates the end of an input sequence.

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 suggested value should the code use as the sentinel?

An alphabetic character

How many times does the following loop execute? for (double d = 1; d != 10; d++) { d = d / 3; System.out.print(d + " "); }

An infinite number of times

What is the output of the code snippet given below? String s = "abcde"; int i = 1; do { if (i > 1) { System.out.print(s.substring (i, i + 1)); } } while (i < 5);

No output (infinite loop)

In the __________ loop header, you can include multiple update expressions, separated by commas, but it is not recommended.

for

Which of the following for loops is illegal?

for (int i = 0) { }

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

0 1 1 2 3 5 8 13 21 34 55

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

1 3 5 7 9 11 13 19

How many times will the following loop run? int i = 0; while (i < 10) { System.out.println(i); i++; }

10

What will be printed by the statements below? int val = 1; int sum = 0; while (val < 5) { sum = sum + val; val++; } System.out.print (sum);

10

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

10 8 6

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

10 9 8 7 6

What does the method below return? int findSomething (String str) { int position = 0; while (position < str.length() && (str.charAt (position) != 'e')) { position++; } return position; }

The position of the first 'e' in the string or the length of the string if there is no 'e'

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

bcde


Conjuntos de estudio relacionados

Inflammatory Bowel Disease: Crohn's Disease/UC

View Set

Biopsychology Intro, Chap 1 and Chap 2.1

View Set

Vocabulary Workshop Level F Unit 7, 8, 9

View Set

Chapter 1: Organizational Behavior

View Set

Chapter 14 The Digestive System and Body Metabolism

View Set