Java Chapter 3 Flashcards
(227 cards)
a ChWhat is the boolean expression in an if statement?
The expression inside the if statement’s parentheses.
What does the following code print if hourOfDay is 10?
```java
if (hourOfDay < 11) {
System.out.println(“Good Morning”);
}
~~~
“Good Morning”
What does the following code print if hourOfDay is 12?
```java
if (hourOfDay >= 11) {
System.out.println(“Good Afternoon”);
}
~~~
“Good Afternoon”
Why is checking two separate if statements inefficient?
Because the computer checks both conditions even if the first one is already true.
What is the purpose of an else statement?
It provides an alternative action that only runs if the first condition was false (“otherwise, do this instead”).
Q: How can you rewrite the given code to be more efficient using else?A:
```java
if (hourOfDay < 11) {
System.out.println(“Good Morning”);
} else {
System.out.println(“Good Afternoon”);
}
~~~
Would you like any modifications or additional questions?
What is wrong with this code?
```java
if (age >= 13) {…}
else if (age >= 18) {…}
~~~
The second condition (age >= 18) will never run because it’s covered by the first condition (age >= 13).
How should you structure conditions to avoid overlapping issues? (In terms of if else)
Check the more restrictive condition first:
```java
if (age >= 18) {…}
else if (age >= 13) {…}
~~~
What is the mistake in this code?
```java
if (ready = true) {…}
~~~
It assigns true to ready instead of comparing it (should use ==).
What is the correct way to check if ready is true?
hourOfDay is an integer, but if requires a boolean expression (true/false).
How can you properly check hourOfDay in an if statement?
Use a comparison:
```java
if (hourOfDay == 1) {…}
if (hourOfDay > 0) {…}
// Or use a boolean variable:
if (isMorning) {…}
~~~
if (x = 5) is a valid way to check if x equals 5.
False – it assigns 5 to x instead of comparing.
else if conditions are checked only if the previous if was false.
True – else if skips checking if an earlier condition was true.
Java allows any integer in an if condition (e.g., if (5)).
False – Java requires a boolean expression (true/false).
Would you like any adjustments or additional questions?
What is the disadvantage of the traditional instanceof check in Java? What were you able to do now that you couldnt do before
It requires an extra explicit casting step after checking the type.
What does this old-style code do?
```java
if (num instanceof Integer) {
Integer intNum = (Integer) num;
System.out.println(“It’s integer: “ + intNum);
}
~~~
Checks if num is an Integer, then manually casts and assigns it before use.
How does pattern matching simplify instanceof checks?
It combines type-checking and variable assignment in one step.
Rewrite this code using pattern matching:
```java
if (obj instanceof String) {
String s = (String) obj;
System.out.println(s);
}
~~~
```java
if (obj instanceof String s) {
System.out.println(s);
}
~~~
Is this check necessary? Why or why not?
```java
if (obj instanceof String s && s != null) {…}
~~~
No! instanceof already returns false for null, so the s != null check is redundant.
What does instanceof return if the object is null?
false (no NullPointerException is thrown).
Pattern matching in Java allows using the matched variable outside its if block.
False – The variable is scoped only to the block where it’s declared.
instanceof throws a NullPointerException if the object is null.
False – It safely returns false.
Pattern matching eliminates the need for explicit casting after instanceof.
True – The variable is auto-cast to the matched type.
Why does this code fail?
```java
Object obj = “Hello”;
if (obj instanceof String s) {
System.out.println(s);
}
System.out.println(s.length()); // Error?
~~~
s is out of scope outside the if block.
What is the output of this code?
```java
Object obj = null;
if (obj instanceof String s) {
System.out.println(s);
} else {
System.out.println(“Not a String or null”);
}
~~~
“Not a String or null” (because instanceof returns false for null).
Would you like more examples or refinements?