Chapter 3 Practice Questions Flashcards

1
Q

Which of the following is true about the if statement in Java?
a) It can only evaluate numerical expressions.
b) It decides whether a block of code should execute based on a boolean condition.
c) It can execute multiple blocks of code without curly braces.
d) It cannot span more than one line.

A

b
Explanation: The if statement evaluates a boolean condition and decides whether to execute the following statement or block of code.

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

What will happen if curly braces are omitted in an if statement with multiple lines?
a) The code will not compile.
b) All statements will execute conditionally.
c) Only the first statement will execute conditionally.
d) The last statement will execute conditionally.

A

c
Explanation: Without curly braces, only the first statement after the if condition is executed conditionally.

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

What is the value of highScore after the following code executes?

boolean highScore = false;
int average = 96;
if (average > 95)
highScore = true;
a) true
b) false
c) Compilation error
d) Runtime error

A

a
Explanation: The condition average > 95 evaluates to true, so highScore is set to true.

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

What does the following code do?

char c = ‘A’;
if (c < ‘Z’)
System.out.println(“A is less than Z”);
a) Prints “A is less than Z”.
b) Throws a runtime exception.
c) Fails to compile because characters cannot be compared.
d) Does nothing.

A

a
Explanation: Characters are ordinal in Java and can be compared using relational operators. ‘A’ is less than ‘Z’ in Unicode order.

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

True or False: The logical OR (||) operator evaluates both operands even if the first one is true

A

False
Explanation: The || operator performs short-circuit evaluation. It stops evaluating as soon as one operand is true.

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

What will the following code print?

int x = 5, y = 10;
int result = x > y ? 20 : 30;
System.out.println(result);
a) 20
b) 30
c) 5
d) Compilation error

A

b
Explanation: The condition x > y is false, so the value after the colon (30) is assigned to result.

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

What is the correct syntax to use the conditional operator?
a) Value1 ? BooleanExpression : Value2
b) BooleanExpression ? Value1 : Value2
c) BooleanExpression ? : Value1 Value2
d) BooleanExpression : Value1 ? Value2

A

b
Explanation: The correct syntax is:

BooleanExpression ? Value1 : Value2

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

What will the following code output?

String str1 = “Java”;
String str2 = “java”;
if (str1.equalsIgnoreCase(str2)) {
System.out.println(“Strings are equal”);
} else {
System.out.println(“Strings are not equal”);
}
a) Strings are equal
b) Strings are not equal
c) Compilation error
d) Runtime exception

A

a
Explanation: The equalsIgnoreCase method compares strings while ignoring case differences.

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

What will the following code output?

int temperature = 105;
if (!(temperature > 100))
System.out.println(“Below the maximum temperature.”);
else
System.out.println(“Above the maximum temperature.”);
a) Below the maximum temperature.
b) Above the maximum temperature.
c) Compilation error
d) No output

A

b
Explanation: The condition !(temperature > 100) evaluates to false, so the else block executes.

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

Which of the following is true about if-else-if statements?
a) All conditions are evaluated regardless of previous outcomes.
b) They are useful for replacing complex nested if statements.
c) Every if-else-if statement must end with an else.
d) They require curly braces for each condition.

A

b
Explanation: The if-else-if structure simplifies nested decision logic. Only the first true condition executes.

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

hat is the result of the following code?

String s1 = “Hello”;
String s2 = new String(“Hello”);
if (s1 == s2)
System.out.println(“References are equal”);
else
System.out.println(“References are not equal”);
a) References are equal
b) References are not equal
c) Compilation error
d) Runtime exception

A

b
Explanation: The == operator compares object references, not content. Since s1 and s2 reference different objects, the result is false.

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

What is the scope of a local variable in Java?
a) From the start of the program to the end of the method.
b) From its declaration to the end of the method.
c) From its declaration to the end of the class.
d) From its declaration to the start of the method.

A

b
Explanation: The scope of a local variable starts at its declaration and ends at the end of the method.

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

Which of the following is a valid comparison for strings ignoring case?
a) str1 == str2
b) str1.equals(str2)
c) str1.equalsIgnoreCase(str2)
d) str1.compareTo(str2)

A

c
Explanation: The equalsIgnoreCase method is specifically designed for comparing strings without considering case sensitivity.

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

What is the purpose of the if-else statement in Java?
A. To iterate over elements in a collection
B. To create new objects
C. To branch the flow of a program based on a true/false condition
D. To terminate a program

A

C
Explanation: The if-else statement allows branching in the program based on whether a condition evaluates to true or false.

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

Which of the following is NOT a valid type for a switch expression?
A. byte
B. String
C. double
D. char

A

C
Explanation: switch expressions only work with ordinal types (byte, short, int, long, char) and String in Java 7+ but not floating-point types like double.

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

What will the following code output if choice = 2?
switch (choice) {
case 1:
System.out.println(“Case 1”);
case 2:
System.out.println(“Case 2”);
default:
System.out.println(“Default case”);
}
A. Case 2
B. Case 2, Default case
C. Case 2, Case 1
D. Case 2, Case 1, Default case

A

B
Explanation: Without break statements, execution falls through from one case to the next. Only Case 2 and Default case will execute.

17
Q

True or False: The default section of a switch statement is mandatory.

A

False
Explanation: The default section is optional and only executes if no case matches the switch expression.

18
Q

What does this code output?
int number = 5;
System.out.printf(“The number is: %03d”, number);
A. The number is: 5
B. The number is: 05
C. The number is: 005
D. The number is: 0005

A

C
Explanation: The format specifier %03d pads the number with zeros to ensure it is at least 3 digits wide.

19
Q

What will happen if the following code is executed?
switch (10) {
case 5:
System.out.println(“Case 5”);
break;
case 10:
System.out.println(“Case 10”);
default:
System.out.println(“Default case”);
}
A. Compilation error due to missing break
B. Only “Case 10” is printed
C. “Case 10” and “Default case” are printed
D. The program will not run

A

B
Explanation: The case for 10 matches the switch expression, and because it contains a break, only “Case 10” is printed.

20
Q

How can formatted output be stored in a string?
A. By using System.out.printf
B. By using String.format
C. By using System.console()
D. By using System.in

A

B
Explanation: The String.format method returns a formatted string, which can be assigned to a variable.

21
Q

True or False: System.out.printf and String.format have identical formatting capabilities.

A

True
Explanation: Both methods use the same syntax for formatting and support the same format specifiers.

22
Q

What does this code output?
switch (‘B’) {
case ‘A’:
System.out.println(“Case A”);
case ‘B’:
System.out.println(“Case B”);
case ‘C’:
System.out.println(“Case C”);
break;
default:
System.out.println(“Default case”);
}
A. Case B
B. Case B, Case C
C. Case B, Case C, Default case
D. Compilation error

A

B
Explanation: Without a break in case ‘B’, execution falls through to case ‘C’.

23
Q

Which of the following statements is true about the switch statement?
A. It can evaluate boolean expressions.
B. It can only evaluate integer expressions.
C. It supports fall-through behavior by default.
D. The default case is required.

A

C
Explanation: By default, cases in a switch fall through unless a break statement is included.