Chapter 3 Flashcards

1
Q

What does an if statement do?

A

It decides whether a section of code executes based on a condition.

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

What is the syntax for an if statement?

A

if (boolean expression)
statement;

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

What happens if an if statement doesn’t use curly braces?

A

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
4
Q

How do you group multiple statements in an if block?

A

Use curly braces {} to enclose the statements.
Example:

if (x > 0) {
System.out.println(“Positive”);
System.out.println(“Greater than zero”);
}

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

What is a flag?

A

A boolean variable that monitors a condition in a program.

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

How can characters be compared in Java?

A

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

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

What does an if-else statement do?

A

Executes one block of code if the condition is true and another block if the condition is false.
Syntax:

if (condition)
statement1;
else
statement2;

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

What is a nested if statement?

A

An if statement inside another if statement.

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

How does an if-else-if statement work?

A

It simplifies multiple decision branches.
Syntax:

if (condition1)
statement1;
else if (condition2)
statement2;
else
statement3;

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

What are the three logical operators in Java?

A

Logical AND (&&)
Logical OR (||)
Logical NOT (!)

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

When does && evaluate to true?

A

When both operands are true.

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

When does || evaluate to true?

A

When at least one operand is true.

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

What does the ! operator do?

A

It negates the truth value of a boolean expression.
Example:

if (!(temperature > 100))
System.out.println(“Below max temperature”);

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

What is short-circuit evaluation?

A

&& stops evaluating as soon as one operand is false.
|| stops evaluating as soon as one operand is true.

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

Can relational operators compare two String objects?

A

No, use methods like equals or compareTo.

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

What is the syntax of the conditional (ternary) operator?

A

BooleanExpression ? Value1 : Value2;
Example:

int number = x > y ? 10 : 5;

17
Q

What is the scope of a local variable?

A

From the point of its declaration to the end of the method in which it is declared.

18
Q

How does the equalsIgnoreCase method work with strings?

A

It compares two String objects while ignoring case differences.

19
Q

What is the purpose of the if-else statement in Java?

A

It is used for branching based on a true/false condition.

20
Q

What data types can the SwitchExpression in a switch statement evaluate?

A

byte, short, int, long, char, or String (Java 7+

21
Q

True or False: Each CaseExpression in a switch statement must be unique.

A

Each case must have a unique value.

22
Q

What happens if a break statement is omitted in a switch case?

A

Execution will “fall through” to the next case.

23
Q

How do you define the default section in a switch statement?

A

Use the default keyword to specify the code that executes when no case matches:

default:
// statements

24
Q

Write an example of using a switch with String values.

A

switch (color) {
case “red”:
System.out.println(“Red selected”);
break;
case “blue”:
System.out.println(“Blue selected”);
break;
default:
System.out.println(“No match”);
}

25
What is the syntax of the System.out.printf method?
System.out.printf(FormatString, ArgList);
26
What does the String.format method do differently than System.out.printf?
It returns the formatted string instead of displaying it on the console.
27
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);
28
What is the purpose of the default case in a switch statement?
The default case executes when no CaseExpression matches the SwitchExpression.
29
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.
30
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.
31
What is the purpose of formatted output in Java?
To control the appearance of the output, such as alignment, decimal precision, or padding.