Java Chapter 04 SD1420
The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
Analyze the following fragment: double sum = 0; double d = 0; while (d != 10.0) { d += 0.1; sum += sum + d; }
100
How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) System.out.println(i * j);
45
How many times is the println statement executed? for (int i = 0; i < 10; i++) for (int j = 0; j < i; j++) System.out.println(i * j);
10
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); count++; } while (count < 10);
11
How many times will the following code print "Welcome to Java"? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 10);
10
How many times will the following code print "Welcome to Java"? int count = 0; while (count < 10) { System.out.println("Welcome to Java"); count++; }
10
How many times will the following code print "Welcome to Java"? int count = 0; while (count++ < 10) { System.out.println("Welcome to Java"); }
Yes
Is the following loop correct? for (; ; );
1 3 5 7 9
The following loop displays _______________. for (int i = 1; i <= 10; i++) { System.out.print(i + " "); i++; }
undefined
What is i after the following for loop? int y = 0; for (int i = 0; i<10; ++i) { y += i; }
15
What is sum after the following loop terminates? int sum = 0; int item = 0; do { item++; sum += item; if (sum >= 4) continue; } while (item < 5);
n
What is the number of iterations in the following loop: for (int i = 1; i <= n; i++) { // iteration }
Explanation: y should be 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
What is the output for y? int y = 0; for (int i = 0; i<10; ++i) { y += i; } System.out.println(y);
x is 4
What is the output of the following code? int x = 0; while (x < 4) { x = x + 1; } System.out.println("x is " + x);
10
What is the value in count after the following loop is executed? int count = 0; do { System.out.println("Welcome to Java"); } while (count++ < 9); System.out.println(count);
3 0
What will be displayed when the following code is executed? int number = 6; while (number > 0) { number -= 3; System.out.print(number + " "); }
yes
Will the following program terminate? int balance = 10; while (true) { if (balance < 9) break; balance = balance - 9; }
The program compiles and runs fine.
double sum = 0; for (double d = 0; d < 10;) { d += 0.1; sum += sum + d; }
6
int sum = 0; int item = 0; do { item++; sum += item; if (sum > 4) break; } while (item < 5);