2.1 Fill in the blanks in each of the following statements:
a) An opening brace begins the body of every method, and a closing brace ends the body of every method.
b) The IF statement is used to make decisions.
c) begins \\ an end-of-line comment.
d) Space characters, Newlines and Tabs are called white space.
e) Keywords are reserved for use by Java.
f) Java applications begin execution at Main method.
g) Methods, System.out.print, System.out.println and System.out.printf display information in a command window.
2.2 State whether each of the following is true or false. If false, explain why.
a) Comments cause the computer to print the text after the // on the screen when the program executes. False
b) All variables must be given a type when they are declared. True
c) Java considers the variables number and NuMbEr to be identical. False
d) The remainder operator (%) can be used only with integer operands. False
e) The arithmetic operators *, /, %, + and - all have the same level of precedence. False
2.3 Write statements to accomplish each of the following tasks:
a) Declare variables c, thisIsAVariable, q76354 and number to be of type int. int c; int thisIsAVariable; int q76354; int number;
b) Prompt the user to enter an integer.
System.out.print (“enter an interger:”);
c) 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.
d) Print "This is a Java program" on one line in the command window. Use method
System.out.printLn (“this is a java program”);
e) 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.
System.out.printLn (“this is a Java”);
OR System.out.printLn (“this is a java \nprogram”);
f) 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.
System.out.printf (“%s\n%s\n”, “this is a java”, “program”);
g) If the variable number is not equal to 7, display "The variable number is not equal to 7". if ( number != 7 )
System.out.println( "The variable number is not equal to 7" );
2.4 Identify and correct the errors in each of the following statements:
a) if ( c < 7 );
System.out.println( "c is less than 7" );
Error: Semicolon after the right parenthesis of the condition ( c < 7 ) in the if.
Correction: Remove the semicolon after the right parenthesis.