Chapter 3 Practice Questions Flashcards
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.
b
Explanation: The if statement evaluates a boolean condition and decides whether to execute the following statement or block of code.
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.
c
Explanation: Without curly braces, only the first statement after the if condition is executed conditionally.
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
Explanation: The condition average > 95 evaluates to true, so highScore is set to true.
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
Explanation: Characters are ordinal in Java and can be compared using relational operators. ‘A’ is less than ‘Z’ in Unicode order.
True or False: The logical OR (||) operator evaluates both operands even if the first one is true
False
Explanation: The || operator performs short-circuit evaluation. It stops evaluating as soon as one operand is true.
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
b
Explanation: The condition x > y is false, so the value after the colon (30) is assigned to result.
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
b
Explanation: The correct syntax is:
BooleanExpression ? Value1 : Value2
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
Explanation: The equalsIgnoreCase method compares strings while ignoring case differences.
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
b
Explanation: The condition !(temperature > 100) evaluates to false, so the else block executes.
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.
b
Explanation: The if-else-if structure simplifies nested decision logic. Only the first true condition executes.
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
b
Explanation: The == operator compares object references, not content. Since s1 and s2 reference different objects, the result is false.
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.
b
Explanation: The scope of a local variable starts at its declaration and ends at the end of the method.
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)
c
Explanation: The equalsIgnoreCase method is specifically designed for comparing strings without considering case sensitivity.
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
C
Explanation: The if-else statement allows branching in the program based on whether a condition evaluates to true or false.
Which of the following is NOT a valid type for a switch expression?
A. byte
B. String
C. double
D. char
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.