JAVA CHAPTER 2 REVIEW EXAM
QUESTIONS WITH COMPLETE
SOLUTIONS
What will be displayed as a result of executing the following code?
int x = 6;
String msg = "I am enjoying this class.";
String msg1 = msg.toUpperCase();
String msg2 = msg.toLowerCase();
char ltr = msg.charAt(x);
int strSize = msg.length();
System.out.println(msg);
System.out.println(msg1);
System.out.println(msg2);
System.out.println("Character at index x = " +
ltr);
System.out.println("msg has " + strSize +
"characters."); - Answer- I am enjoying this class.
I AM ENJOYING THIS CLASS.
i am enjoying this class.
Character at index x = n
msg has 25characters.
What will be displayed as a result of executing the following code?
public class test
{
public static void main(String[] args)
{
int value1 = 9;
System.out.println(value1);
int value2 = 45;
System.out.println(value2);
System.out.println(value3);
value = 16;
}
} - Answer- Nothing, there is an error
Which of the following is NOT a valid comment statement?
A) // comment 1
B) /* comment 2 */
, C) */ comment 3 /*
D) /** comment 4 */ - Answer- C) */ comment 3 /*
When saving a Java source file, save it with an extension of:
A) .javac
B) .class
C) .src
D) .java - Answer- D) .java
Every Java application program must have:
A) a class named MAIN
B) a method named main
C) comments
D) integer variables - Answer- B) a method named main
To print "Hello, world" on the monitor, use the following Java statement:
A) SystemOutPrintln("Hello, world");
B) System.out.println{"Hello, world"}
C) System.out.println("Hello, world");
D) Print "Hello, world"; - Answer- c) System.out.println("Hello, world");
To display the output on the next line, you can use the println method or use this escape
sequence
in the print method.
A) \n
B) \r
C) \t
D) \b - Answer- A) \n
This is a named storage location in the computer's memory.
A) Literal
B) Constant
C) Variable
D) Operator - Answer- C) variable
What would be displayed as a result of the following code?
int x = 578;
System.out.print("There are " +
x + 5 + "\n" +
"hens in the hen house."); - Answer- "There are 5785
hens in the hen house."
Variables are classified according to their:
A) value
B) data type
C) names
QUESTIONS WITH COMPLETE
SOLUTIONS
What will be displayed as a result of executing the following code?
int x = 6;
String msg = "I am enjoying this class.";
String msg1 = msg.toUpperCase();
String msg2 = msg.toLowerCase();
char ltr = msg.charAt(x);
int strSize = msg.length();
System.out.println(msg);
System.out.println(msg1);
System.out.println(msg2);
System.out.println("Character at index x = " +
ltr);
System.out.println("msg has " + strSize +
"characters."); - Answer- I am enjoying this class.
I AM ENJOYING THIS CLASS.
i am enjoying this class.
Character at index x = n
msg has 25characters.
What will be displayed as a result of executing the following code?
public class test
{
public static void main(String[] args)
{
int value1 = 9;
System.out.println(value1);
int value2 = 45;
System.out.println(value2);
System.out.println(value3);
value = 16;
}
} - Answer- Nothing, there is an error
Which of the following is NOT a valid comment statement?
A) // comment 1
B) /* comment 2 */
, C) */ comment 3 /*
D) /** comment 4 */ - Answer- C) */ comment 3 /*
When saving a Java source file, save it with an extension of:
A) .javac
B) .class
C) .src
D) .java - Answer- D) .java
Every Java application program must have:
A) a class named MAIN
B) a method named main
C) comments
D) integer variables - Answer- B) a method named main
To print "Hello, world" on the monitor, use the following Java statement:
A) SystemOutPrintln("Hello, world");
B) System.out.println{"Hello, world"}
C) System.out.println("Hello, world");
D) Print "Hello, world"; - Answer- c) System.out.println("Hello, world");
To display the output on the next line, you can use the println method or use this escape
sequence
in the print method.
A) \n
B) \r
C) \t
D) \b - Answer- A) \n
This is a named storage location in the computer's memory.
A) Literal
B) Constant
C) Variable
D) Operator - Answer- C) variable
What would be displayed as a result of the following code?
int x = 578;
System.out.print("There are " +
x + 5 + "\n" +
"hens in the hen house."); - Answer- "There are 5785
hens in the hen house."
Variables are classified according to their:
A) value
B) data type
C) names