Multiple Choice Identify the
choice that best completes the statement or answers the question.
|
|
1.
|
What is the output of the following code segment?
for ( int j = 10; j > 5; j-- ) { System.out.print( j +
" " ); } System.out.println();
a. | 10 11 12 13 14
15 | b. | 9 8 7 6 5 4 3 2 1
0 | c. | 10 9 8 7 6
5 | d. | 10 9 8 7
6 |
|
|
2.
|
What is the number of times that the loop below will display
“HELLO”assuming that b>a?
for(x=a; x<=b;
x++) System.out.println(“HELLO”);
|
|
3.
|
Which of the following represents the proper structure of a for loop?
a. | for (initialize counter; update counter) {
statement; statement; . .
.; } | b. | for (initialize counter; test counter; update counter)
{ statement; statement; . .
.; } | c. | for (update counter; initialize counter; test counter )
{ statement; statement; . .
.; } | d. | for (test counter; initialize counter; update counter)
{ statement; statement; . .
.; } |
|
|
4.
|
What will be the output of the following while statement?
int x=10; while(x>0) {
System.out.println(x + “ “); x-=3; }
a. | 10 7 4 1 | c. | 10 8 6 4
2 0 | b. | 10 9 8 7 6 5 4 3 2
1 | d. | 10 8 6
4 2 |
|
|
5.
|
Consider the following two code segments: Segment 1 | Segment 2 | int x = 0; while(x<10) {
x++; System.out.println(x);
} | for(int y=0; y<10;
y++) {
System.out.println(y);
} | | |
Which of
the following statements is true?
a. | Both segments produce the same output | b. | Both segments do NOT produce the same
output | c. | The segments would produce the same output if the statements inside the while loop
were reversed | d. | Both b and c are correct |
|
|
6.
|
Which of the following code snippets would display the square roots of 25, 20,
15, and 10?
a. | int number = 25; while (number >
10){ System.out.println ("The square root of " +
number + " is " + Math.sqrt
(number)); number -= 5; } | b. | int number = 25; while (number <= 10){
System.out.println ("The square root of " +
number + " is " + Math.sqrt
(number)); number--; } | c. | int number = 25; while (number >= 10){
System.out.println ("The square root of " +
number + " is " + Math.sqrt
(number)); number -= 5; } | d. | int number = 25; while (number >= 10){
System.out.println ("The square root of " +
number + " is " + Math.sqrt
(number)); number--; } |
|
|
7.
|
Consider the following code segment: int x = 2, y =
0; while(x>y) {
x--; y++;
} System.out.println(x -
y);
What is the output of the System.out.println
statement?
|
|
8.
|
Consider the following code segment, which will be used to determine the number
of years required for a $10,000 investment to grow to $25,000 at a simple interest rate of
3%.. int years = 0; double balance = 10000,
interest; while(balance<25000) {
years++; interest = balance * .03; <BLANK>
} System.out.println(“Years = “ + years);
What is the
following statements should be placed where <BLANK> is so that the code works as
intended?
a. | balance = interest; | b. | balance++; | c. | balance +=
interest; | d. | balance *= interest; |
|
|
9.
|
Consider the following code segment. for(int k=0; k<10; k++) {
for(int j=0; j<5;
j++)
System.out.print(“*”);
}How many asterisks (stars) will be
displayed when this code segment is executed?
|
|
10.
|
Which code segment will correctly print the following?
x xx xxx xxxx
a. | for(int x=1; x<=4; x++) { for(int y=1; y<=4; y++) System.out.print(x); System.out.println(); } | b. | for(int x=1; x<=4; x++) { for(int y=1; y<=x;
y++) System.out.print(x); System.out.println(); } | c. | for(int x=1; x<=4; x++) { for(int y=1; y<=4;
y++) System.out.print(“x”); System.out.println(); } | d. | for(int x=1; x<=4; x++) { for(int y=1; y<=x;
y++) System.out.print(“x”); System.out.println(); } |
|