How many times will the following code print “Welcome to Java”?
int count = 0;
while (count++ < 10) {
System.out.println(“Welcome to Java”);
}
10
To add 0.01 + 0.02 + … + 1.00, what order should you use to add the numbers to get better accuracy?
add 0.01, 0.02, …, 1.00 in this order to a sum variable whose initial value is 0.
Which of the following loops prints “Welcome to Java” 10 times?
A:
for (int count = 1; count <= 10; count++) {
System.out.println(“Welcome to Java”);
}
B:
for (int count = 0; count < 10; count++) {
System.out.println(“Welcome to Java”);
}
C:
for (int count = 1; count < 10; count++) {
System.out.println(“Welcome to Java”);
}
D:
for (int count = 0; count <= 10; count++) {
System.out.println(“Welcome to Java”);
}
AB
Do the following two statements in (I) and (II) result in the same value in sum?
(I):
for (int i = 0; i < 10; ++i) {
sum += i;
}(II):
for (int i = 0; i < 10; i++) {
sum += i;
}
Yes
Is the following loop correct?
for ( ; ; );
Yes
What is the output for y?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
System.out.println(y);45
How many times will the following code print “Welcome to Java”?
int count = 0;
do {
System.out.println(“Welcome to Java”);
} 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”);
count++;
} while (count < 10);
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);
10
The following loop displays _______________.
for (int i = 1; i <= 10; i++) {
System.out.print(i + “ “);
i++;
}
1 3 5 7 9
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)
45
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
What is the number of iterations in the following loop?
for (int i = 1; i < n; i++) {
// iteration
}n - 1
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
What is the output of the following code?
int x = 0;
while (x < 4) {
x = x + 1;
}
System.out.println("x is " + x);x is 4
What will be displayed when the following code is executed?
int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + " ");
}3 0
What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}10
What is the output after the following loop terminates?
int number = 25;
int i;
boolean isPrime = true;
for (i = 2; i < number; i++) {
if (number % i == 0) {
isPrime = false;
break;
}
}
System.out.println("i is " + i + " isPrime is " + isPrime)i is 5 isPrime is false
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);
100
What is the value of balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9)
break;
balance = balance - 9;
}1