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).
True or False
A left brace in a Java program is always followed by a right brace later in the program.
TRUE
Explanation: Every opening { must have a corresponding closing } to maintain proper syntax.
True or False
A variable must be declared before it can be used.
TRUE
Explanation: Java enforces variable declaration before use to ensure type safety.
True or False
Variable names may begin with a number.
FALSE
Explanation: Variable names must begin with a letter, _, or $.
True or False
You cannot change the value of a variable whose declaration uses the final keyword.
TRUE
Explanation: The final keyword makes a variable constant, so its value cannot be modified.
True or False
Comments that begin with // can be processed by javadoc.
FALSE
Explanation: Only comments starting with /** are processed by javadoc.
True or False
If one of an operator’s operands is a double, and the other operand is an int, Java will automatically convert the value of the double to an int.
FALSE
Explanation: Java promotes the int to a double to prevent precision loss.
Find the errors:
/ What’s wrong with this program? /
public MyProgram
{
public static void main(String[] args);
}
int a, b, c \ Three integers
a = 3
b = 4
c = a + b
System.out.println(‘The value of c is’ + C);
{
Corrected Program:
// What’s wrong with this program?
public class MyProgram {
public static void main(String[] args) {
int a, b, c; // Three integers
a = 3;
b = 4;
c = a + b;
System.out.println(“The value of c is “ + c);
}
}
Explanation of Fixes:
Comments: Ensured valid syntax for single-line comments.
Class Declaration: Added the missing class keyword.
Main Method: Provided a valid method signature and body.
Variable Declarations: Moved them into the method and fixed the comment syntax.
Assignments: Added missing semicolons to terminate statements.
Output Statement: Fixed string formatting and corrected variable case.
Extra Braces: Removed redundant opening brace.
Now the program is valid and will correctly compile and run, displaying:
The value of c is 7
Modify the following program so it prints two blank lines
between each line of text.
6
12
−3
public class
{
public static void main(String[] args)
{
System.out.print(“Hearing in the
distance”);
System.out.print(“Two mandolins like
creatures in the”);
System.out.print(“dark”);
System.out.print(“Creating the agony of
ecstasy.”);
System.out.println(“ –
George Barker”);
}
}
Here’s the corrected program:
public class MyProgram {
public static void main(String[] args) {
System.out.print(“Hearing in the distance”);
System.out.println(); // Blank line
System.out.println(); // Blank line
System.out.print("Two mandolins like creatures in the"); System.out.println(); // Blank line System.out.println(); // Blank line System.out.print("dark"); System.out.println(); // Blank line System.out.println(); // Blank line System.out.print("Creating the agony of ecstasy."); System.out.println(); // Blank line System.out.println(); // Blank line System.out.println(" – George Barker"); } } Explanation of Changes: System.out.println();: These statements are added after each System.out.print() statement to print a blank line. Two blank lines are printed between each line of text as requested. Class Name: I assumed the class name should be MyProgram, but you can change it to whatever you prefer.
What will the following code output?
int apples = 0, bananas = 2, pears = 10;
apples += 10;
bananas *= 10;
pears /= 10;
System.out.println(apples + “ “ +
bananas + “ “ +
pears);
10 20 1
What will the following code output?
double d = 12.9;
int i = (int)d;
System.out.println(i);
12
What will the following code output?
String message = “Have a great day!”;
System.out.println(message.charAt(5));
the code will output a space.
What will the following code output?
String message = “Have a great day!”;
System.out.println(message.toUpperCase());
System.out.println(message);
HAVE A GREAT DAY!
Have a great day!