Chapter 3: Making decisions Flashcards
Control flow statements allow applications to execute specific code segments selectively.
True
A block of code in Java consists of zero or more statements enclosed in balanced braces ({}).
True
The target of a decision-making statement can be a single statement or a block of statements.
True
The if statement requires curly braces only when it has multiple statements.
True
The else statement in Java is optional.
True
Java 16 introduced pattern matching with if statements and the instanceof operator.
True
Pattern matching increases boilerplate code in Java applications.
If false, why?
Pattern matching reduces redundant code, making it more concise.
(Answer: False)
The pattern variable in pattern matching must be explicitly cast before use.
False: The instanceof check implicitly casts the variable when the condition is met.
A pattern variable can be of the same type as the left-hand side of an instanceof expression. If false, why?
The instanceof check implicitly casts the variable when the condition is met.
Pattern matching allows an instanceof expression to declare variables with a type different from the checked type.
If false, why?
The pattern variable must match a subtype of the checked type.
Given Integer value = 123;, both if(value instanceof Integer) {} and if(value instanceof Integer data) {} compile successfully.
If false, why?
The second statement does not compile because pattern matching requires the pattern variable type to be a strict subtype of Integer.
The traditional instanceof operator enforces the same subtype restrictions as pattern matching.
If false, why?
Traditional instanceof does not require the type to be a strict subtype.
The compiler applies flow scoping when working with pattern matching.
True
Flow scoping means the variable is only in scope when the compiler can definitively determine its type.
True
Flow scoping is not strictly hierarchical like instance, class, or local scoping.
True
A switch statement allows case values to be combined using commas.
True
A switch statement does not require any case statements.
True
The break statement is optional inside switch cases.
True
A switch statement does not require parentheses. If false, why?
Parentheses are required in a switch statement.
A switch statement does not require a beginning curly brace.
A beginning curly brace is mandatory.
The default case is required in a switch statement.
The default case is optional.
A break statement always terminates the switch statement.
If break is omitted, execution continues to the next case.
Flow scoping works exactly like local variable scoping.
Flow scoping is based on program flow, not hierarchical rules.
In pattern matching, a variable declared inside an if statement always remains in scope outside it.
The variable remains in scope only if the compiler guarantees its type.