Self-Review Exercises
2.1 Fill in theblanks in each of the following statements:
a) A(n) { beginsthe body of every method, and a(n) } ends the body of every method.
b) You can use the ifstatement to make decisions.
c) // begins anend-of-line comment.
d) Space characters, newlines and tabs are called whitespace.
e) Keywords arereserved for use by Java.
f) Java applicationsbegin execution at main method .
g) Methods System.out.print, System.out.println and System.out.printf displayinformation in a command window.
2.2 State whethereach of the following is true or false. If false, explain why.
a) Comments cause thecomputer to print the text after the // on the screenwhen the program executes. (False)
b) All variables must begiven a type when they’re declared. (True)
c) Java considers thevariables number and NuMbEr to be identical. (False)
d) The remainder operator (%) can be used only withinteger operands. (False)
e) The arithmeticoperators *, /, %, + and - all have the same level of precedence. (False)
2.3 Writestatements to accomplish each of the following tasks:
a) Declare variables c, thisIsAVariable, q76354 and number to be of typeint.
b) Prompt the user toenter an integer.
c) Input an integer andassign the result to int variable value. Assume Scanner variable input can be used to read a value from the keyboard.
d) Print "Thisis a Java program" on one line in the command window. Use method System.out.println.
e) Print "Thisis a Java program" on two lines in the command window. The first line should endwith Java. Use method System.out.printf and two %s formatspecifiers.
f) If the variable number is not equalto 7, display "The variable number is not equal to 7".
2.4 Identify andcorrect the errors in each of the following statements:
a) if (c <7);
System.out.println("c isless than 7");
b) if (c=> 7)
System.out.println("c isequal to or greater than 7");
2.5 Writedeclarations, statements or comments that accomplish each of the followingtasks:
a) State that a programwill calculate the product of three integers.
b) Create a Scanner called input that readsvalues from the standard input.
c) Declare the variables x, y, z and result to be of typeint.
d) Prompt the user toenter the first integer.
e) Read the first integerfrom the user and store it in the variable x.
f) Prompt the user toenter the second integer.
g) Read the secondinteger from the user and store it in the variable y.
h) Prompt the user toenter the third integer.
i) Read the third integerfrom the user and store it in the variable z.
j) Compute the product ofthe three integers contained in variables x, y and z, and assign theresult to the variable result.
k) Use System.out.printfto display the message "Product is"followed by the value of the variable result.
2.6 Using thestatements you wrote in Exercise 2.5, write a complete program that calculates andprints the product of three integers.