1
Consider the following output.
10 9 8 7 6 5 4 3 2 1
Which of the following loops will produce this output?
A. for (int i = 0; i < 10; i--)
System.out.print (i + " ");
B. for (int i = 10; i >= 0; i--)
System.out.print (i + " ");
C. for (int i = 0; i <= 10; i++)
System.out.print ((10 - i) + " ");
D. for (int i = 0; i < 10; i++)
System.out.print ((10 - i) + " ");
E. for (int i = 10; i > 0; i--)
System.out.print ((10 - i) + " ");
Correct Answer: D
Explanation:
2
A program has been written to process the scores of soccer games. Consider the following
code segment, which is intended to assign an appropriate string to outcome based
on the number of points scored by each of two teams.
if (team1Points == team2Points)
outcome = "Tie Game";
if (team1Points > team2Points)
outcome = "Team 1 Wins";
else
outcome = "Team 2 Wins";
The code doesn't work properly. For which of the following cases will the code assign the wrong string to outcome?
I. both teams score the same number of points
II. Team 1 scores more points than Team 2
III. Team 2 scores more points than Team 1
A. I only B. II only C. III only D. I and III only E. II and III only
Correct Answer: A
Explanation:
3
Consider the following code segment.
for (int i = 1; i < 5; i++)
for (int k = i; k > 2; k--)
System.out.print (k + " ");
What is printed as a result of executing the code segment?
A. 3 4 3
B. 3 4 4
C. 1 2 3 4 3
D. 2 3 2 4 3 2
E. Many digits are printed due to an infinite loop
Correct Answer: A
Explanation:
4
Consider the following code segment.
for (int i = 1; i < 25; i = i + 5)
if (i % 5 == 0)
System.out.print (i + " ");
What is printed as a result of executing the code segment?
A. 5 10 15 20
B. 5 10 15 20 25
C. 5 15
D. 6 11 16 23
E. Nothing is printed
Correct Answer: E
Explanation:
5
Consider the following while loop.
int k = 8;
while (k > 0)
{
k = k - 2;
System.out.println (k);
}
Which of the following for loops produces the same output as the while loop?
A. for (int k = 8; k >= 0; k = k - 2)
System.out.println (k);
B. for (int k = 8; k > 0; k = k - 2)
System.out.println (k);
C. for (int k = 8; k > 2; k = k - 2)
System.out.println (k);
D. for (int k = 6; k >= 0; k = k - 2)
System.out.println (k);
E. for (int k = 6; k > 0; k = k - 2)
System.out.println (k);
Correct Answer: D
Explanation:
Consider the following output.
10 9 8 7 6 5 4 3 2 1
Which of the following loops will produce this output?
A. for (int i = 0; i < 10; i--)
System.out.print (i + " ");
B. for (int i = 10; i >= 0; i--)
System.out.print (i + " ");
C. for (int i = 0; i <= 10; i++)
System.out.print ((10 - i) + " ");
D. for (int i = 0; i < 10; i++)
System.out.print ((10 - i) + " ");
E. for (int i = 10; i > 0; i--)
System.out.print ((10 - i) + " ");
Correct Answer: D
Explanation:
2
A program has been written to process the scores of soccer games. Consider the following
code segment, which is intended to assign an appropriate string to outcome based
on the number of points scored by each of two teams.
if (team1Points == team2Points)
outcome = "Tie Game";
if (team1Points > team2Points)
outcome = "Team 1 Wins";
else
outcome = "Team 2 Wins";
The code doesn't work properly. For which of the following cases will the code assign the wrong string to outcome?
I. both teams score the same number of points
II. Team 1 scores more points than Team 2
III. Team 2 scores more points than Team 1
A. I only B. II only C. III only D. I and III only E. II and III only
Correct Answer: A
Explanation:
3
Consider the following code segment.
for (int i = 1; i < 5; i++)
for (int k = i; k > 2; k--)
System.out.print (k + " ");
What is printed as a result of executing the code segment?
A. 3 4 3
B. 3 4 4
C. 1 2 3 4 3
D. 2 3 2 4 3 2
E. Many digits are printed due to an infinite loop
Correct Answer: A
Explanation:
4
Consider the following code segment.
for (int i = 1; i < 25; i = i + 5)
if (i % 5 == 0)
System.out.print (i + " ");
What is printed as a result of executing the code segment?
A. 5 10 15 20
B. 5 10 15 20 25
C. 5 15
D. 6 11 16 23
E. Nothing is printed
Correct Answer: E
Explanation:
5
Consider the following while loop.
int k = 8;
while (k > 0)
{
k = k - 2;
System.out.println (k);
}
Which of the following for loops produces the same output as the while loop?
A. for (int k = 8; k >= 0; k = k - 2)
System.out.println (k);
B. for (int k = 8; k > 0; k = k - 2)
System.out.println (k);
C. for (int k = 8; k > 2; k = k - 2)
System.out.println (k);
D. for (int k = 6; k >= 0; k = k - 2)
System.out.println (k);
E. for (int k = 6; k > 0; k = k - 2)
System.out.println (k);
Correct Answer: D
Explanation:
(0) Comments
Post a Comment