hts.StevenWood.com

[ Last Page ]

Review Questions

  1. Fill in the blanks in each of the following. Solutions
    1. The __________ begins the body of every method and the __________ ends the body of every method.
    2. Every statement ends with a(n) __________.
    3. The __________ structure is used to make decisions.
    4. __________ begins a single line comment.
    5. __________ are reserved for use by Java and can't be variable names.
    6. Java applications begin execution at method __________.
    7. The __________ selection structure is used to execute one action when a condition is true another action when that condition is false.
    8. Repetition of a set of instructions a specific number of times is called __________ repetition.
    9. 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.
  2. State whether each of the following is true or false. If false, explain why.  Solutions
    1. Comments cause the computer to print the text after the // on the screen when the program is executed.
    2. All variables must be given a type when they are declared.
    3. Java considers the variables number and numBER to be identical.
    4. Method Integer.parseInt converts an integer to a String.
    5. The expression ( x > y && a < b ) is true if either x > y is true or a < b is true.
    6. The expression containing the || operator is true if either or both of its operands is true.
  3. Write Java statements to accomplish each of the following.   Solutions
    1. Declare variables h, variableName, w342 and number to of type int.
    2. convert a String to an integer and store the converted value in integer variable age. Assume that the String is stored in tempValue.
    3. If the variable age is not equal to 7, display "The variable age is not equal to 7".
    4. Print the message "This is a Java test" on one line.
    5. Print the message "This is a Java test" on two lines where the first line ends with a. Use only one statement.
    6. 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?
    7. Test if the value of the variable number is greater than 20. If it is display "Number is greater than 20".
    8. Decrement the variable x by 1, then subtract it from the variable total.  Do this in one statement.
    9. Calculate the remainder after q is divided by divisor and assign the result to q. Write this statement two different ways.
    10.  
  4. Identify and correct the errors in each of the following statements. Solutions
    1. (display numbers from 1 to 10)
  5. Write a statement (or comment) to accomplish each of the following
    1. Give the comments that are required for all assignments.
    2. Declare the variables varA, varB, varC, and varResult to be of type int.
    3. Declare the variables aVal, bVal, c to be of type String.
    4. Prompt the user to enter the first value, read the value from the user and store it in the variable aVal.
    5. Prompt the user to enter the first value, read the value from the user and store it in the variable bVal.
    6. Prompt the user to enter the first value, read the value from the user and store it in the variable bVal.
    7. Convert aVal to an int and store the results in the variable varA.
    8. Convert bVal to an int and store the results in the variable varB.
    9. Convert cVal to an int and store the results in the variable varC.
    10. Compute the product of the three integers contained in variables varA, varB, varC, and assign the result to the variable varResult.
    11. Display a dialogue "The product is " followed by the value of the variable varResult.
  6. Write four different Java statements that each add 1 to integer variable y.
  7. Write a Java statement to accomplish each of the following tasks.
    1. Declare variables sum and x to be of type int.
    2. Assign 10 to x.
    3. Assign 0 to variable sum.
    4. Add variable x to variable sum and assign the result to variable sum.
    5. Print "The sum is: " followed by the value of variable sum.
  8. 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.
  9. 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.
    1. product *= x++;
    2. quotient /= --x;
  10. Write a Java statement or a set of Java statements to accomplish each of the following.
    1. Sum the odd integers between 1 and 100 using a for structure. Assume the integer variables sum and count have been declared.
    2. Calculate the value of 2.5 raised to the power of 3 using the pow method.
    3. 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.
    4. Repeat part (c) using a for structure.
  11. Answer the following questions:
    1. What is a syntax error? logic error? run-time error?
    2. What is the import statement used for?
    3. Why do we need to convert a String into a number variable?
    4. What other ways can we write the statement:
      weight = weight + 1;
    5. What program code could we use to find the value of the largest integer value?
    6. What is an IPO chart and how is it used?
    7. What are the flowchart symbols?
  12. 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"
  13. 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
  14. 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. 

 
[Last Page]
All material on this site is copyright © 1997- by Steven Wood or as credited. All right reserved. Use of this site indicates you agreement with the  terms of use.
Send comments or questions to about this page.
Every attempt has been made to credit work under copyright. If there are any claims that copyright has been missed please contact the .
+SDG+