# Help with this pracitce test?

Hey everyone. This may seem a bit nutty but I have some questions off of my practice test (29 to be exact) that I want to post here (answers included) in hopes that someone can kind of explain some of the question to me and why the each answer is correct. I'm not asking you to help with everyone. Just pick and choose if you wish and of course, feel free to skip this all together.
OK Here goes:

1) What is i after the following for loop?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}

A) 10 9 C) 11 D) undefined
___________________________________________________________
2) What is the number of iterations in the following loop:
for (int i = 1; i <= n; i++) {
// iteration
}

A) n n + 1 C) n - 1 D) 2*n
________________________________________________________
3) After the continue outer statement is executed in the following loop, which statement is executed?
outer:
for (int i = 1; i < 10; i++) {
inner:
for (int j = 1; j < 10; j++) {
if (i * j > 50)
continue outer;
System.out.println(i * j);
}
}
next:

A) The statement labeled next.
The control is in the inner loop, and the next iteration of the inner loop is executed.
C) The control is in the outer loop, and the next iteration of the outer loop is executed.
D) The program terminates.
_______________________________________________________________
4) 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) 0 9 C) 10 D) 11 E) 8

5) What is the output for y?
int y = 0;
for (int i = 0; i<10; ++i) {
y += i;
}
System.out.println(y);

A) 11 10 C) 12 D) 45 E) 13

___________________________________________________________
6) You can always convert a for loop to a while loop.
A) true false
________________________________________________________
7) What is 1.0 + 1.0 + 1.0 == 3.0?
A) true
false
C) There is no guarantee that 1.0 + 1.0 + 1.0 == 3.0 is true.
__________________________________________________________
8) Analyze the following code.
int x = 1;
while (0 < x) && (x < 100)
System.out.println(x++);

A) The loop runs for ever.
The code does not compile because (0 < x) && (x < 100) is not enclosed in a pair of
parentheses.
C) The numbers 2 to 100 are displayed.
D) The code does not compile because the loop body is not in the braces.
E) The numbers 1 to 99 are displayed.
______________________________________________________________
9) You can always convert a while loop to a for loop.
A) true false
29) The while loop and the do loop are equivalent in their expressive power; in other words, you can
rewrite a while loop using a do loop, and vice versa.
________________________________________________________________
10)
A) true false
___________________________________________

11) What balance after the following code is executed?
int balance = 10;
while (balance >= 1) {
if (balance < 9) continue;
balance = balance - 9;
}

A) The loop does not end
-1
C) 2
D) 1
E) 0

_____________________________________________________

12) What the output of the following code:
for ( ; ; )
System.out.println("Welcome to Java");

A) prints out Welcome to Java one time. prints out Welcome to Java forever.
C) prints out Welcome to Java two times. D) does not print anything.
__________________________________________________________

13) Assume x is 0. What is the output of the following statement?
if (x > 0)
printf("x is greater than 0");
else if (x < 0)
printf("x is less than 0");
else
printf("x equals 0");

A) x equals 0 x is greater than 0
C) x is less than 0 D) None
____________________________________________________________

14) What is y after the following for loop statement is executed?
int y = 0;
for (int i = 0; i < 10; ++i) {
y += 1;
}

A) 12 10 C) 11 D) 9
___________________________________________________

15) The elements inside the for loop control are separated using semicolons instead of commas. 34)
A) true false
_______________________________________________________

16) What the output of the following code:
for ( ; false ; )
System.out.println("Welcome to Java");

A) prints out Welcome to Java one time. prints out Welcome to Java forever.
C) does not print anything. D) prints out Welcome to Java two times.
_______________________________________________________

17) A variable declared in the for loop control can be used after the loop exits. 36)
A) true false
________________________________________

18) A continue statement can be used only in a loop.
A) true false
_______________________________________________________
19) What is the output of the following fragment?
int i = 1;
int j = 1;
while (i < 5) {
i++;
j = j * 2;
}
System.out.println(j);

A) 16 64 C) 32 D) 4 E) 8
8

20) You can always write a program without using break or continue in a loop.
A) true false
___________________________________________________________
21) 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;
}

A) 1 2 C) -1 D) 0
__________________________________

22) 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 control variable in the for loop cannot be of the
double type.
The program compiles and runs fine.
C) The program compiles but does not stop because d would always be less than 10.
D) The program has a syntax error because the adjustment statement is incorrect in the for loop.
______________________________________

23) Which of the following expression yields an integer between 0 and 100, inclusive? 42)
A) (int)(Math.random() * 100) (int)(Math.random() * 101)
C) (int)(Math.random() * 100 + 1) D) (int)(Math.random() * 100) + 1
_____________________________________________________

24) In a for statement, if the continuation condition is blank, the condition is assumed to be ________.
A) true false
__________________________________________________________
25) What is the output of the following fragment?
for (int i = 0; i < 15; i++) {
if (i % 4 == 1)
System.out.print(i + " ");
}

A) 1 3 5 7 9 11 13
1 3 5 7 9 11 13 15
C) 1 5 9 13 16
D) 1 5 9 13
E) 1 4 8 12

____________________________________________________
26) A break statement can be used only in a loop. 4
A) true false
________________________________________________--
27) Suppose cond1 is a Boolean expression. When will this while condition be true?
while (cond1) ...

A) in case cond1 is true always false
C) in case cond1 is false D) always true
___________________________________________________________
28) Which of the following loops produces the following output? (choose all that apply)
1 2 3 4 1 2 3 1 2 1
(I)
for (int i = 5; i > 0; i--) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
}
(II)
for (int i = 1; i < 5; i++) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
}
(III)
int i = 0;
while (i < 5) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
i++;
}
(IV)
int i = 5;
while (i > 0) {
for (int j = 1; j < i; j++)
System.out.print(j + " ");
System.out.println();
i--;
}

A) (II) (IV) C) (I) D) (III)
ANSWER IS B & C

29) Which of the loop statements always have their body executed at least once?
A) The while loop The for loop C) The do-while loop