Self-Review Exercises (Sections C.1C.13)

C.1 Fill in the blanks in each of the following statements:

a) All programs can be written in terms of three types of control structures: _____________, and _____________.

b) The _____________statement is used to execute one action when a condition is true and another when that condition is false.

c) When it’s not known in advance how many times a set of statements will be repeated, a(n) _____________value can be used to terminate the repetition.

d) Java is a(n) _____________language; it requires all variables to have a type.

e) If the increment operator is _____________to a variable, first the variable is incremented by 1, then its new value is used in the expression.

C.2 State whether each of the following is true or false. If false, explain why.

a) A set of statements contained within a pair of parentheses is called a block.

b) A selection statement specifies that an action is to be repeated while some condition remains true.

c) A nested control statement appears in the body of another control statement.

d) Specifying the order in which statements execute in a program is called program control.

e) Instance variables of type boolean are given the value true by default.

C.3 Write Java statements to accomplish each of the following tasks:

a) Use one statement to assign the sum of x and y to z, then increment x by 1.

b) Test whether variable count is greater than 10. If it is, print "Count is greater than 10".

c) Use one statement to decrement the variable x by 1, then subtract it from variable total and store the result in variable total.

d) Calculate the remainder after q is divided by divisor, and assign the result to q. Write this statement in two different ways.

C.4 Write a Java statement to accomplish each of the following tasks:

a) Declare variables sum and x to be of type int.

b) Assign 1 to variable x.

c) Assign 0 to variable sum.

d) Add variable x to variable sum, and assign the result to variable sum.

e) Print "The sum is: ", followed by the value of variable sum.

C.5 Determine the value of the variables in the statement product *= x++; after the calculation is performed. Assume that all variables are type int and initially have the value 5.

C.6 Identify and correct the errors in each of the following sets of code:

a)

while ( c <= 5 )
{
    product *= c;
    ++c;

b)

if ( gender == 1 )
   System.out.println( "Woman" );
else;
   System.out.println( "Man" );

C.7 What is wrong with the following while statement?

while ( z >= 0 )
   sum += z;

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset