Chapter 3 Flashcards
What does an if statement do?
It decides whether a section of code executes based on a condition.
What is the syntax for an if statement?
if (boolean expression)
statement;
What happens if an if statement doesn’t use curly braces?
Only the first statement after the if condition is executed conditionally.
How do you group multiple statements in an if block?
Use curly braces {} to enclose the statements.
Example:
if (x > 0) {
System.out.println(“Positive”);
System.out.println(“Greater than zero”);
}
What is a flag?
A boolean variable that monitors a condition in a program.
How can characters be compared in Java?
Characters can be compared using relational operators because they are stored as Unicode values.
Example:
if (‘A’ < ‘Z’)
System.out.println(“A comes before Z”);
What does an if-else statement do?
Executes one block of code if the condition is true and another block if the condition is false.
Syntax:
if (condition)
statement1;
else
statement2;
What is a nested if statement?
An if statement inside another if statement.
How does an if-else-if statement work?
It simplifies multiple decision branches.
Syntax:
if (condition1)
statement1;
else if (condition2)
statement2;
else
statement3;
What are the three logical operators in Java?
Logical AND (&&)
Logical OR (||)
Logical NOT (!)
When does && evaluate to true?
When both operands are true.
When does || evaluate to true?
When at least one operand is true.
What does the ! operator do?
It negates the truth value of a boolean expression.
Example:
if (!(temperature > 100))
System.out.println(“Below max temperature”);
What is short-circuit evaluation?
&& stops evaluating as soon as one operand is false.
|| stops evaluating as soon as one operand is true.
Can relational operators compare two String objects?
No, use methods like equals or compareTo.
What is the syntax of the conditional (ternary) operator?
BooleanExpression ? Value1 : Value2;
Example:
int number = x > y ? 10 : 5;
What is the scope of a local variable?
From the point of its declaration to the end of the method in which it is declared.
How does the equalsIgnoreCase method work with strings?
It compares two String objects while ignoring case differences.
What is the purpose of the if-else statement in Java?
It is used for branching based on a true/false condition.
What data types can the SwitchExpression in a switch statement evaluate?
byte, short, int, long, char, or String (Java 7+
True or False: Each CaseExpression in a switch statement must be unique.
Each case must have a unique value.
What happens if a break statement is omitted in a switch case?
Execution will “fall through” to the next case.
How do you define the default section in a switch statement?
Use the default keyword to specify the code that executes when no case matches:
default:
// statements
Write an example of using a switch with String values.
switch (color) {
case “red”:
System.out.println(“Red selected”);
break;
case “blue”:
System.out.println(“Blue selected”);
break;
default:
System.out.println(“No match”);
}
What is the syntax of the System.out.printf method?
System.out.printf(FormatString, ArgList);
What does the String.format method do differently than System.out.printf?
It returns the formatted string instead of displaying it on the console.
Write an example of using String.format to store a formatted string.
String message = String.format(“Name: %s Age: %d”, “Alice”, 25);
System.out.println(message);
What is the purpose of the default case in a switch statement?
The default case executes when no CaseExpression matches the SwitchExpression.
What will this code output if choice = 1?
switch (choice) {
case 1:
System.out.println(“Choice 1”);
case 2:
System.out.println(“Choice 2”);
default:
System.out.println(“Default choice”);
}
Output:
Choice 1
Choice 2
Default choice
The break statements are missing, so the cases fall through.
True or False: A switch statement can evaluate floating-point numbers like float or double.
switch cannot evaluate floating-point numbers. It only works with ordinal types like int, char, and String.
What is the purpose of formatted output in Java?
To control the appearance of the output, such as alignment, decimal precision, or padding.