Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Self-Review Exercises

2.1

Fill in the blanks in each of the following statements:

  1. A(n) _______ begins the body of every method, and a(n) _______ ends the body of every method.
  2. The _______ statement is used to make decisions.
  3. _______ begins an end-of-line comment.
  4. _______, _______ and _______ are called white space.
  5. _______ are reserved for use by Java.
  6. Java applications begin execution at method _______.
  7. Methods _______, _______ and _______ display information in a command window.

2.2

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

  1. Comments cause the computer to print the text after the // on the screen when the program executes.
  2. All variables must be given a type when they're declared.
  3. Java considers the variables number and NuMbEr to be identical.
  4. The remainder operator (%) can be used only with integer operands.
  5. The arithmetic operators *, /, %, + and - all have the same level of precedence.

2.3

Write statements to accomplish each of the following tasks:

  1. Declare variables c, thisIsAVariable, q76354 and number to be of type int.
  2. Prompt the user to enter an integer.
  3. Input an integer and assign the result to int variable value. Assume Scanner variable input can be used to read a value from the keyboard.
  4. Print "This is a Java program" on one line in the command window. Use method System.out.println.
  5. Print "This is a Java program" on two lines in the command window. The first line should end with Java. Use method System.out.println.
  6. Print "This is a Java program" on two lines in the command window. The first line should end with Java. Use method System.out.printf and two %s format specifiers.
  7. If the variable number is not equal to 7, display "The variable number is not equal to 7".

2.4

Identify and correct the errors in each of the following statements:

  1. if ( c < 7 );
       System.out.println( "c is less than 7" );
  2. if ( c => 7 )
       System.out.println( "c is equal to or greater than 7" );

2.5

Write declarations, statements or comments that accomplish each of the following tasks:

  1. State that a program will calculate the product of three integers.
  2. Create a Scanner called input that reads values from the standard input.
  3. Declare the variables x, y, z and result to be of type int.
  4. Prompt the user to enter the first integer.
  5. Read the first integer from the user and store it in the variable x.
  6. Prompt the user to enter the second integer.
  7. Read the second integer from the user and store it in the variable y.
  8. Prompt the user to enter the third integer.
  9. Read the third integer from the user and store it in the variable z.
  10. Compute the product of the three integers contained in variables x, y and z, and assign the result to the variable result.
  11. Display the message "Product is" followed by the value of the variable result.

2.6

Using the statements you wrote in Exercise 2.5, write a complete program that calculates and prints the product of three integers.

  • + Share This
  • 🔖 Save To Your Account