Chapter 2 Book Quiz Flashcards
Every complete statement ends with a .
a. period
b. parenthesis
c. semicolon
d. ending brace
c. semicolon
Explanation: In Java, every statement must end with a semicolon (;) to indicate the end of that instruction.
The following data are all examples of .
72, ‘A’, “Hello World”, 2.8712
a. variables
b. literals
c. strings
d. none of these
b. literals
Explanation: Literals represent constant values written directly in the program, such as integers (72), characters (‘A’), strings (“Hello World”), and floating-point numbers (2.8712).
A group of statements, such as the contents of a class or a method, are enclosed in .
a. braces {}
b. parentheses ()
c. brackets []
d. any of these will do
a. braces {}
Explanation: Braces {} are used in Java to group statements together, such as in classes, methods, loops, and conditional blocks.
Which of the following are not valid assignment statements?
a. total = 9;
b. 72 = amount;
c. profit = 129
d. letter = ‘W’;
b. 72 = amount; and c. profit = 129
Explanation:
72 = amount; is invalid because literals cannot appear on the left side of an assignment.
profit = 129 is invalid because it is missing a semicolon at the end.
The other options are valid.
Which of the following are not valid println statements?
a. System.out.println + “Hello World”;
b. System.out.println(“Have a nice day”);
c. out.System.println(value);
d. println.out(Programming is great fun);
a, c, and d.
Explanation:
System.out.println + “Hello World”; is invalid because + cannot replace parentheses.
out.System.println(value); has the order of System.out reversed.
println.out(Programming is great fun); is completely invalid syntax.
Only option b is valid.
The negation operator is .
a. unary
b. binary
c. ternary
d. none of these
a. unary
Explanation: The negation operator (!) operates on a single boolean operand, making it unary.
This keyword is used to declare a named constant.
a. constant
b. namedConstant
c. final
d. concrete
c. final
Explanation: The final keyword declares a variable as constant, meaning its value cannot be changed after initialization.
These characters mark the beginning of a multi-line comment.
a. //
b. /*
c. */
d. /**
b. /*
Explanation: Multi-line comments in Java begin with /* and end with */.
These characters mark the beginning of a single-line comment.
a. //
b. /*
c. */
d. /**
a. //
Explanation: Single-line comments start with // and continue until the end of the line.
These characters mark the beginning of a documentation comment.
a. //
b. /*
c. */
d. /**
d. /**
Explanation: Documentation comments for use with javadoc start with /** and end with */.
Which Scanner class method would you use to read a string as input?
a. nextString
b. nextLine
c. readString
d. getLine
b. nextLine
Explanation: The nextLine() method of the Scanner class reads a string from the input until the end of the line.
Which Scanner class method would you use to read a double as input?
a. nextDouble
b. getDouble
c. readDouble
d. None of these
a. nextDouble
Explanation: The nextDouble() method reads a floating-point number of type double.
You can use this class to display dialog boxes.
a. JOptionPane
b. BufferedReader
c. InputStreamReader
d. DialogBox
a. JOptionPane
Explanation: The JOptionPane class provides methods to display dialog boxes such as input dialogs and message dialogs.
When Java converts a lower-ranked value to a higher-ranked type, it is called a(n) .
a. 4-bit conversion
b. escalating conversion
c. widening conversion
d. narrowing conversion
c. widening conversion
Explanation: Widening conversion automatically converts a smaller data type to a larger data type, such as int to double.
This type of operator lets you manually convert a value, even if it means that a narrowing conversion will take place.
a. cast
b. binary
c. uploading
d. dot
a. cast
Explanation: A cast operator explicitly converts one data type to another, even if precision loss occurs (narrowing).