Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help with this pracitce test?

hallyhally Posts: 2Member
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. :p
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 B) 9 C) 11 D) undefined
ANSWER IS D
___________________________________________________________
2) What is the number of iterations in the following loop:
for (int i = 1; i <= n; i++) {
// iteration
}

A) n B) n + 1 C) n - 1 D) 2*n
ANSWER IS A
________________________________________________________
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.
B) 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.
ANSWER IS C
_______________________________________________________________
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 B) 9 C) 10 D) 11 E) 8

ANSWER IS D
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 B) 10 C) 12 D) 45 E) 13

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

A) The loop runs for ever.
B) 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.
ANSWER IS B
______________________________________________________________
9) You can always convert a while loop to a for loop.
A) true B) 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.
ANSWER IS A
________________________________________________________________
10)
A) true B) false
ANSWER IS A
___________________________________________

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
B) -1
C) 2
D) 1
E) 0
ANSWER IS A

_____________________________________________________

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

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

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 B) x is greater than 0
C) x is less than 0 D) None
ANSWER IS B
____________________________________________________________

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 B) 10 C) 11 D) 9
___________________________________________________

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

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

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

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

18) A continue statement can be used only in a loop.
A) true B) false
ANSWER IS A
_______________________________________________________
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 B) 64 C) 32 D) 4 E) 8
8
ANSWER IS A

20) You can always write a program without using break or continue in a loop.
A) true B) false
ANSWER IS A
___________________________________________________________
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 B) 2 C) -1 D) 0
ANSWER IS A
__________________________________

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.
B) 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.
ANSWER IS B
______________________________________

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

24) In a for statement, if the continuation condition is blank, the condition is assumed to be ________.
A) true B) 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
B) 1 3 5 7 9 11 13 15
C) 1 5 9 13 16
D) 1 5 9 13
E) 1 4 8 12

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

A) in case cond1 is true B) always false
C) in case cond1 is false D) always true
ANSWER IS C
___________________________________________________________
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) B) (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 B) The for loop C) The do-while loop
ANSWER IS C

Comments

  • jeanfrgjeanfrg Posts: 6Member
    What do you need exactly because the answers are already done.
  • hallyhally Posts: 2Member
    Right the answers are done and so is the test at this point. :) But I guess I'm just asking for things to be explained. Like WHY they are the answers. I am just trying to be taught in different ways. Asking to have each individual questions explained is one of those ways and since I will be taking the retake test which our professor was nice enough to allow, I'd still welcome any input.
Sign In or Register to comment.