Chapter 5: Loops Self Test Questions Flashcards

1
Q

How many times will the following code print “Welcome to Java”?

int count = 0;
while (count < 10) {
System.out.println(“Welcome to Java”);
count++;
}

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Analyze the following code. Please select all that apply.

int count = 0;
while (count < 100) {
// Point A
System.out.println(“Welcome to Java!”);
count++;
// Point B
}

// Point C
A. count < 100 is always true at Point A
B. count < 100 is always true at Point B
C. count < 100 is always false at Point B
D. count < 100 is always true at Point C
E. count < 100 is always false at Point C

A

A & E

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What will be displayed when the following code is executed?

int number = 6;
while (number > 0) {
number -= 3;
System.out.print(number + “ “);
}

A

3 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How many times will the following code print “Welcome to Java”?

int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (count++ < 10);

A

11

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many times will the following code print “Welcome to Java”?

int count = 0;
do {
System.out.println(“Welcome to Java”);
} while (++count < 10);

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

10

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Analyze the following statement:

double sum = 0;
for (double d = 0; d < 10;) {
d += 0.1;
sum += sum + d;
}

A. The program has a compile error because the adjustment is missing in the for loop.
B. The program has a compile error because the control variable in the for loop cannot be of the double type.
C. The program runs in an infinite loop because d < 10 would always be true.
D. The program compiles and runs fine.

A

The program compiles and runs fine.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + … + 99/100?

A:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum = i / (i + 1);
}
System.out.println(“Sum is “ + sum);

B:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1);
}
System.out.println(“Sum is “ + sum);

C:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += 1.0 * i / (i + 1);
}
System.out.println(“Sum is “ + sum);

D:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);

E:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);

A

c & d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which of the following loops correctly computes 1/2 + 2/3 + 3/4 + … + 99/100?

A:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum = i / (i + 1);
}
System.out.println(“Sum is “ + sum);

B:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1);
}
System.out.println(“Sum is “ + sum);

C:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += 1.0 * i / (i + 1);
}
System.out.println(“Sum is “ + sum);

D:
double sum = 0;
for (int i = 1; i <= 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);

E:
double sum = 0;
for (int i = 1; i < 99; i++) {
sum += i / (i + 1.0);
}
System.out.println(“Sum is “ + sum);

A

c & d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The following loop displays _______________.

for (int i = 1; i <= 10; i++) {
System.out.print(i + “ “);
i++;
}

A

1 3 5 7 9

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the output for y?

int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}
System.out.println(y);

A

45

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is i after the following for loop?

int y = 0;
for (int i = 0; i < 10; ++i) {
y += i;
}

A

undefined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Is the following loop correct?

for ( ; ; );

A

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Analyze the following fragment:

double sum = 0;
double d = 0;
while (d != 10.0) {
d += 0.1;
sum += sum + d;
}
A. The program does not compile because sum and d are declared double, but assigned with integer value 0.
B. The program never stops because d is always 0.1 inside the loop.
C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.
D. After the loop, sum is 0 + 0.1 + 0.2 + 0.3 + … + 1.9

A

C. The program may not stop because of the phenomenon referred to as numerical inaccuracy for operating with floating-point numbers.

16
Q

Analyze the following code. Please select all that apply.

public class Test {
public static void main (String[] args) {
int i = 0;
for (i = 0; i < 10; i++);
System.out.println(i + 4);
}
}

A

C. The program compiles despite the semicolon (;) on the for loop line, and displays 14.

D. The for loop in this program is same as for (i = 0; i < 10; i++) { }; System.out.println(i + 4);

17
Q

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)

A

45

18
Q

A. B. C. D.
1 1 2 3 4 5 6 1 1 2 3 4 5 6
1 2 1 2 3 4 5 2 1 1 2 3 4 5
1 2 3 1 2 3 4 3 2 1 1 2 3 4
1 2 3 4 1 2 3 4 3 2 1 1 2 3
1 2 3 4 5 1 2 5 4 3 2 1 1 2
1 2 3 4 5 6 1 6 5 4 3 2 1 1

Which pattern is produced by the following code?

for (int i = 1; i <= 6; i++) {
for (int j = 6; j >= 1; j–)
System.out.print(j <= i ? j + “ “ : “ “ + “ “);
System.out.println();
}

A

Pattern C

19
Q

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

A

100

20
Q

To add 0.01 + 0.02 + … + 1.00, what order should you use to add the numbers to get better accuracy?
A. add 0.01, 0.02, …, 1.00 in this order to a sum variable whose initial value is 0.
B. add 1.00, 0.99, 0.98, …, 0.02, 0.01 in this order to a sum variable whose initial value is 0.

A

A. add 0.01, 0.02, …, 1.00 in this order to a sum variable whose initial value is 0.

21
Q

Analyze the following code.

double sum = 0;
for (double d = 0; d < 10; sum += sum + d) {
d += 0.1;
}
A. The program has a syntax error because the adjustment statement is incorrect in the for loop.
B. The program has a syntax error because the control variable in the for loop cannot be of the double type.
C. The program compiles but does not stop because d would always be less than 10.
D. The program compiles and runs fine.

A

D

22
Q

Will the following program terminate?

int balance = 10;

while (true) {
if (balance < 9)
break;
balance = balance - 9;
}
A. Yes
B. No

A

yes

23
Q

What is sum after the following loop terminates?

int sum = 0;
int item = 0;
do {
item++;
sum += item;
if (sum > 4)
break;
}
while (item < 5);

A

6

24
Q

What is the output after the following loop terminates?

int number = 25;
int i;

boolean isPrime = true;
for (i = 2; i < number && isPrime; i++) {
if (number % i == 0) {
isPrime = false;
}
}

System.out.println(“i is “ + i + “ isPrime is “ + isPrime);
A. i is 5 isPrime is true
B. i is 5 isPrime is false
C. i is 6 isPrime is true
D. i is 6 isPrime is false

A

D. i is 6 isPrime is false

25
Q

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);
A. i is 5 isPrime is true
B. i is 5 isPrime is false
C. i is 6 isPrime is true
D. i is 6 isPrime is false

A

B. i is 5 isPrime is false

26
Q

What is sum after the following loop terminates?

int sum = 0;
int item = 0;
do {
item++;
if (sum >= 4)
continue;
sum += item;
}
while (item < 5);

A

6

27
Q

Will the following program terminate?

int balance = 10;

while (true) {
if (balance < 9)
continue;
balance = balance - 9;
}
A. Yes
B. No

A

no

28
Q

What balance after the following code is executed?

int balance = 10;

while (balance >= 1) {
if (balance < 9)
continue;
balance = balance - 9;
}
A. -1
B. 0
C. 1
D. 2
E. The loop does not end

A

E

29
Q

What is the number of iterations in the following loop?

for (int i = 1; i < n; i++) {
// iteration
}

A

n-1

30
Q

What is the number of iterations in the following loop?

for (int i = 1; i <= n; i++) {
// iteration
}

A

n

31
Q

Suppose the input for number is 9. What is the output from running the following program?

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print(“Enter an integer: “);
int number = input.nextInt();

int i;

boolean isPrime = true;
for (i = 2; i < number && isPrime; i++) {
  if (number % i == 0) {
    isPrime = false;
  }
}

System.out.println("i is " + i);

if (isPrime)
  System.out.println(number + " is prime");
else
  System.out.println(number + " is not prime");   } }  A. i is 3 followed by 9 is prime  B. i is 3 followed by 9 is not prime  C. i is 4 followed by 9 is prime  D. i is 4 followed by 9 is not prime
A

D. i is 4 followed by 9 is not prime

32
Q

Analyze the following code:

import java.util.Scanner;

public class Test {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 100000; i++) {
Scanner input = new Scanner(System.in);
sum += input.nextInt();
}
}
}
A. The program does not compile because the Scanner input = new Scanner(System.in); statement is inside the loop.
B. The program compiles, but does not run because the Scanner input = new Scanner(System.in); statement is inside the loop.
C. The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop.
D. The program compiles, but does not run because there is not prompting message for entering the input.

A

C. The program compiles and runs, but it is not efficient and unnecessary to execute the Scanner input = new Scanner(System.in); statement inside the loop. You should move the statement before the loop.