|
| |
Review Questions
- Fill in the blanks in each of the following. Solutions
- The __________ begins the body of every method and the __________ ends
the body of every method.
- Every statement ends with a(n) __________.
- The __________ structure is used to make decisions.
- __________ begins a single line comment.
- __________ are reserved for use by Java and can't be variable names.
- Java applications begin execution at method __________.
- The __________ selection structure is used to execute one action when
a condition is true another action when that condition is false.
- Repetition of a set of instructions a specific number of times is
called __________ repetition.
- When it is not known in advance how many times a set of statements
will be repeated, a __________ value can be used to terminate the
repetition.
- State whether each of the following is true or false. If false, explain
why. Solutions
- Comments cause the computer to print the text after the // on the
screen when the program is executed.
- All variables must be given a type when they are declared.
- Java considers the variables number and numBER to be
identical.
- Method Integer.parseInt converts an integer to a String.
- The expression ( x > y && a < b ) is true if
either x > y is true or a < b is true.
- The expression containing the || operator is true if either or both of
its operands is true.
- Write Java statements to accomplish each of the following. Solutions
- Declare variables h, variableName, w342 and number
to of type int.
- convert a String to an integer and store the converted value in
integer variable age. Assume that the String is stored in tempValue.
- If the variable age is not equal to 7, display "The
variable age is not equal to 7".
- Print the message "This is a Java test" on one line.
- Print the message "This is a Java test" on two lines
where the first line ends with a. Use only one statement.
- Assign the sum of a and b to z and increment b
by 1 after the calculation. Do this in two statements. How would you do
it in one statement?
- Test if the value of the variable number is greater than 20. If
it is display "Number is greater than 20".
- Decrement the variable x by 1, then subtract it from the
variable total. Do this in one statement.
- Calculate the remainder after q is divided by divisor
and assign the result to q. Write this statement two different
ways.
-
- Identify and correct the errors in each of the following statements. Solutions







(display
numbers from 1 to 10)
- Write a statement (or comment) to accomplish each of the following
- Give the comments that are required for all assignments.
- Declare the variables varA, varB, varC, and varResult
to be of type int.
- Declare the variables aVal, bVal, c to
be of type String.
- Prompt the user to enter the first value, read the value from the user
and store it in the variable aVal.
- Prompt the user to enter the first value, read the value from the user
and store it in the variable bVal.
- Prompt the user to enter the first value, read the value from the user
and store it in the variable bVal.
- Convert aVal to an int and store the results in the
variable varA.
- Convert bVal to an int and store the results in the
variable varB.
- Convert cVal to an int and store the results in the
variable varC.
- Compute the product of the three integers contained in variables varA,
varB, varC, and assign the result to the variable varResult.
- Display a dialogue "The product is " followed by the
value of the variable varResult.
- Write four different Java statements that each add 1 to integer variable y.
- Write a Java statement to accomplish each of the following tasks.
- Declare variables sum and x to be of type int.
- Assign 10 to x.
- Assign 0 to variable sum.
- Add variable x to variable sum and assign the
result to variable sum.
- Print "The sum is: " followed by the value
of variable sum.
- Combine the statement that you wrote in the previous question into a Java
application that calculates and prints the sum of the integers from 10 to
20. Use the while structure to loop through the calculations and increment
statements.
- Determine the values of each variable after the calculation is performed.
Assume that when each statement begins executing, all variables have the
integer value 5.
- product *= x++;
- quotient /= --x;
- Write a Java statement or a set of Java statements to accomplish each of
the following.
- Sum the odd integers between 1 and 100 using a for
structure. Assume the integer variables sum and count have
been declared.
- Calculate the value of 2.5 raised to the power of 3
using the pow method.
- Print the integers from 1 to 20 using a while loop and
the counter variable x. Assume that the variable x has been
declared but not initialized.
- Repeat part (c) using a for structure.
- Answer the following questions:
- What is a syntax error? logic error? run-time error?
- What is the import statement used for?
- Why do we need to convert a String into a number variable?
- What other ways can we write the statement:
weight = weight + 1;
- What program code could we use to find the value of the largest
integer value?
- What is an IPO chart and how is it used?
- What are the flowchart symbols?
- Develop an IPO chart and flowchart for the following problem:
Enter in 2 numbers and find the total. If the total is over 10 display the
word "Big" otherwise display the word "Small"
- Print the integers from 1 to 20 using a while loop and the counter
variable x. Assume that the variable x has been declared but not
initialized. Solutions
- Print the integers from 1 to 20 using a for loop and the counter variable
x. Assume that the variable x has been declared but not initialized. Solutions
Source: Deitel, H.M. and Deitel P.J. Java: How to Program. 3rd ed. Upper Saddle River, New
Jersey. Prentice Hall.1999.
ISBN 0-13-012507-5, p 68-69, 149,195-196.
|