unit 3-4 Flashcards

(50 cards)

1
Q

What is a boolean expression?

A

An expression that evaluates to true or false.

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

What are the boolean literals in Java?

A

true and false

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

What operator is used for equality comparison?

A

==

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

What does != mean?

A

Not equal to

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

What does > mean in Java?

A

Greater than

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

What does >= mean?

A

Greater than or equal to

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

What does < mean in Java?

A

Less than

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

What does <= mean?

A

Less than or equal to

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

What operator is used for logical AND?

A

&&

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

What operator is used for logical OR?

A

||

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

What operator is used for logical NOT?

A

!

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

What is the result of true && false?

A

false

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

What is the result of true || false?

A

true

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

What is the result of !false?

A

true

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

What is the correct way to write an if statement in Java?

A

if (condition) {
// code
}

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

How do you write an if-else statement?

A

if (condition) {
// true block
} else {
// false block
}

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

What is an else if?

A

An additional condition checked only if previous ones are false.

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

When is an else block executed?

A

Only when all previous if/else if conditions are false.

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

What does this return? “hello”.equals(“Hello”)

A

false — case-sensitive comparison.

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

What method is used to compare strings for equality?

A

.equals()

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

Can you use == to compare strings reliably?

A

No. It compares memory addresses, not content.

22
Q

What is a nested if?

A

An if inside another if.

23
Q

What is short-circuit evaluation?

A

Java stops evaluating a boolean expression as soon as the result is known.

24
Q

What is DeMorgan’s Law?

A

Rules for distributing ! across && and ||:
!(A && B) → !A || !B
!(A || B) → !A && !B

25
What is the output of this code? int x = 5; if (x > 3 && x < 10) { System.out.print("Yes"); }
Yes
26
What is a loop?
A control structure that repeats code.
27
What are the 3 loop types in Java?
while, for, and enhanced for loops.
28
What does a while loop do?
Repeats as long as the condition is true.
29
Give a simple while loop example.
int i = 0; while (i < 5) { System.out.print(i); i++; }
30
What must you avoid in a while loop?
Infinite loops — make sure the condition becomes false eventually.
31
What is the format of a for loop?
for (int i = 0; i < 5; i++) { // code }
32
When should you use a for loop over while?
When the number of iterations is known.
33
What is an enhanced for loop?
A loop used to iterate over arrays or collections. Example: for (int x : arr) { System.out.println(x); }
34
Can you modify an array with an enhanced for loop?
Not directly — use index-based loop to modify.
35
How do you exit a loop early?
Use break;
36
How do you skip to the next iteration of a loop?
Use continue;
37
What is nested iteration?
A loop inside another loop.
38
How many times does this print? for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { System.out.print("*"); } }
6 times (2 * 3)
39
What is loop tracing?
Following variable values step by step during loop execution.
40
What is the output of: for (int i = 5; i > 0; i--) { System.out.print(i); }
54321
41
What type of loop is this? while (true) { // endless }
Infinite loop
42
Why avoid modifying a list during iteration?
It may cause errors or skip elements.
43
Can a for loop condition use &&?
Yes. Example: for (int i = 0; i < 10 && arr[i] != 0; i++) {}
44
What loop is best for traversing an array?
Use for or enhanced for.
45
How do you count specific items in a loop?
Use an if inside the loop and a counter variable.
46
What happens if you forget to update the loop variable?
Infinite loop.
47
How can loops help with String processing?
You can use .charAt(i) in a loop to check each character.
48
What’s a common use of nested loops?
Working with 2D arrays.
49
When does this loop stop? int x = 10; while (x > 0) { x--; }
After 10 iterations.
50
How would you reverse a string using a loop?
Loop from last index to 0, printing each character.