Chapter 2 Book Quiz Flashcards

1
Q

Every complete statement ends with a .

a. period
b. parenthesis
c. semicolon
d. ending brace

A

c. semicolon
Explanation: In Java, every statement must end with a semicolon (;) to indicate the end of that instruction.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The following data are all examples of .
72, ‘A’, “Hello World”, 2.8712

a. variables
b. literals
c. strings
d. none of these

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

a. braces {}
Explanation: Braces {} are used in Java to group statements together, such as in classes, methods, loops, and conditional blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which of the following are not valid assignment statements?

a. total = 9;
b. 72 = amount;
c. profit = 129
d. letter = ‘W’;

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The negation operator is .

a. unary
b. binary
c. ternary
d. none of these

A

a. unary
Explanation: The negation operator (!) operates on a single boolean operand, making it unary.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

This keyword is used to declare a named constant.

a. constant
b. namedConstant
c. final
d. concrete

A

c. final
Explanation: The final keyword declares a variable as constant, meaning its value cannot be changed after initialization.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

These characters mark the beginning of a multi-line comment.

a. //
b. /*
c. */
d. /**

A

b. /*
Explanation: Multi-line comments in Java begin with /* and end with */.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

These characters mark the beginning of a single-line comment.

a. //
b. /*
c. */
d. /**

A

a. //
Explanation: Single-line comments start with // and continue until the end of the line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

These characters mark the beginning of a documentation comment.

a. //
b. /*
c. */
d. /**

A

d. /**
Explanation: Documentation comments for use with javadoc start with /** and end with */.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which Scanner class method would you use to read a string as input?

a. nextString
b. nextLine
c. readString
d. getLine

A

b. nextLine
Explanation: The nextLine() method of the Scanner class reads a string from the input until the end of the line.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which Scanner class method would you use to read a double as input?

a. nextDouble
b. getDouble
c. readDouble
d. None of these

A

a. nextDouble
Explanation: The nextDouble() method reads a floating-point number of type double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

You can use this class to display dialog boxes.

a. JOptionPane
b. BufferedReader
c. InputStreamReader
d. DialogBox

A

a. JOptionPane
Explanation: The JOptionPane class provides methods to display dialog boxes such as input dialogs and message dialogs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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

A

c. widening conversion
Explanation: Widening conversion automatically converts a smaller data type to a larger data type, such as int to double.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

a. cast
Explanation: A cast operator explicitly converts one data type to another, even if precision loss occurs (narrowing).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

True or False
A left brace in a Java program is always followed by a right brace later in the program.

A

TRUE
Explanation: Every opening { must have a corresponding closing } to maintain proper syntax.

17
Q

True or False
A variable must be declared before it can be used.

A

TRUE
Explanation: Java enforces variable declaration before use to ensure type safety.

18
Q

True or False
Variable names may begin with a number.

A

FALSE
Explanation: Variable names must begin with a letter, _, or $.

19
Q

True or False
You cannot change the value of a variable whose declaration uses the final keyword.

A

TRUE
Explanation: The final keyword makes a variable constant, so its value cannot be modified.

20
Q

True or False
Comments that begin with // can be processed by javadoc.

A

FALSE
Explanation: Only comments starting with /** are processed by javadoc.

21
Q

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.

A

FALSE
Explanation: Java promotes the int to a double to prevent precision loss.

22
Q

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);
{

A

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

23
Q

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”);
}
}

A

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.
24
Q

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);

A

10 20 1

25
Q

What will the following code output?
double d = 12.9;
int i = (int)d;
System.out.println(i);

A

12

26
Q

What will the following code output?
String message = “Have a great day!”;
System.out.println(message.charAt(5));

A

the code will output a space.

27
Q

What will the following code output?
String message = “Have a great day!”;
System.out.println(message.toUpperCase());
System.out.println(message);

A

HAVE A GREAT DAY!
Have a great day!