Complete the following questions. Use of the textbook is permitted. Submit your
ID: 2249271 • Letter: C
Question
Complete the following questions. Use of the textbook is permitted. Submit your work in an
electronic format using the template at the end of this document.
1. What is the value of result after the following Java statements execute?
int a, b, c, d, result;
a = 4;
b = 12;
c = 37;
d = 51;
result = d % a * c + a % b + a;
a. 119
b. 51
c. 127
d. 59
2. What will be output after the following Java statements have been executed?
int a, b, c, d;
a = 4;
b = 12;
c = 37;
d = 51;
if ( a < b )
System.out.println( "a < b" );
if ( a > b )
System.out.println( "a > b" );
if ( d <= c )
System.out.println( "d <= c" );
if ( c != d )
System.out.println( "c != d" );
a. a < b
c != d
b. a < b
d <= c
c != d
c. a > b
c != d
d. a < b
c < d
a != b
3. Which of the following is not a compilation error?
a. Neglecting to initialize a local variable in a method before it is used.
b. Placing a semicolon at the end of the first line of an if statement.
c. Omitting the left and right parenthesis for the condition of an if statement.
d. All are compilation errors.
4. Consider the following two Java code segments:
Segment 1 Segment 2
int i = 0;
for ( int i = 0; i <= 20; i++ )
while ( i < 20 ) {
{ System.out.println( i );
i++; }
System.out.println( i );
}
Which of the following statements are true?
a. The output from these segments is not the same.
b. The scope of the control variable i is different for the two segments.
c. Both (a) and (b) are true.
d. Neither (a) nor (b) is true.
5. Consider the classes below:
public class TestA
{
public static void main( String args[] )
{
int x = 2, y = 20, counter = 0;
for ( int j = y % x; j < 100; j += ( y / x ) )
counter++;
} // end main
} // end class TestA
public class TestB
{
public static void main(String args[])
{
int counter = 0;
for ( int j = 10; j > 0; --j )
++counter;
} // end main
} // end class TestB
Which of the following statements is true?
a. The value of counter will be different at the end of each for loop for each class.
b. The value of j will be the same for each loop for all iterations
c. Both (a) and (b) are true.
d. Neither (a) nor (b) is true.
6. Which of the following for-loop control headers results in equivalent numbers of iterations:
A. for ( int q = 1; q <= 100; q++ )
B. for ( int q = 100; q >= 0; q-- )
C. for ( int q = 99; q > 0; q -= 9 )
D. for ( int q = 990; q > 0; q -= 90 )
a. A and B.
b. C and D.
c. A and B have equivalent iterations and C and D have equivalent iterations.
d. None of the loops have equivalent iterations.
7. Which of the following will count down from 10 to 1 correctly?
a. for ( int j = 10; j <= 1; j++ )
b. for ( int j = 1; j <= 10; j++ )
c. for ( int j = 10; j > 1; j-- )
d. for ( int j = 10; j >= 1; j-- )
8. Which of the following statements about a do…while repetition statement is true?
a. The body of a do…while loop is executed only if the terminating condition is true.
b. The body of a do…while loop is executed only once.
c. The body of a do…while loop is always executed at least once.
d. None of the above
9. Which of the following will not help prevent infinite loops?
a. Include braces around the statements in a do…while statement.
b. Ensure that the header of a for or while statement is not followed by a semicolon.
c. If the loop is counter-controlled, the body of the loop should increment or decrement the
counter as needed.
d. If the loop is sentinel-controlled, ensure that the sentinel value is input eventually
10. Which of the following statements about the break statement is false?
a. The break statement is used to exit a repetition structure early and continue execution after the
loop.
b. A break statement can only break out of an immediately enclosing while, for, do…while or
switch statement.
c. The break statement, when executed in a while, for or do…while, skips the remaining
statements in the loop body and proceeds with the next iteration of the loop.
d. Common uses of the break statement are to escape early from a loop or to skip the remainder
of a switch.
Explanation / Answer
41. result = d % a * c + a % b + a;
d%a=3
3*37=111
a%b=4
Hence 111+4+4=119
(a)119
2. (a) a < b
c != d
Because othere two conditions are not true and statemetns under those two conditions won't beexecuted.
3. (b) Placing a semicolon at the end of the first line of an if statement.
4 (c) Both (a) and (b) are true.
Both of them produces different output. In while loop, the output is from 1 to 20 where as in for loop the output will be from 0 to 20. Also in while, scope of variable i is outside the while loop also where as the in for section, scope of variable i is in the for loop only.