Branching ch.3 Flashcards
myInt == 42
OK
myChar == ‘q’
OK
myDouble == 3.26
NOT OK
myString == “Hello”
NOT OK
=>
not valid operator
!
not valid operator
<>
not valid operator
Can you use use = in an if-else expression?
No, should be ==
How to detect a range?
Use if-else-if-else
How do if-else-if-else structures work?
Each expression only needs to indicate the upper range part; if execution reaches an expression, the lower range part is implicit from the previous expressions being false.
What is the range for if x < 10 else if x < 20 else if x < 30 else
0-9
10-19
20-29
30+
In an if else if else if else structure is the second branch taken if the first if is not false?
the second branch expression is only reached if the first expression is false. So the second branch is taken if userAge < 16 is false (so 16 or greater) AND userAge is < 25, meaning userAge is between 16 - 24 (inclusive).
Which approach uses a logical operator to detect if x is in the range 1 to 99.
0 < x < 100
(0 < x ) && (x < 100)
(0 < x) && (x > 100)
(0 < x) AND (x < 100)
Write expression: x is in the range -4 to +4
(x > -5) && (x < 5)
What happens when you omit the break statement in switch statements? Common error
Omitting the break statement for a case will cause the statements within the next case to be executed. Such “falling through” to the next case can be useful when multiple cases, such as cases 0, 1, and 2, should execute the same statements.
str1 less than str2 returns what?
Negative number
What is the expression for str1 less than str2?
str1.compareTo(str2) < 0
str1 equal to str2 returns what?
0
What is the expression for str1 equal to str2?
str1.compareTo(str2) == 0
str1 greater than str2 returns what?
Positive number
What is the expression for str1 greater than str2?
str1.compareTo(str2) > 0
A programmer can compare strings while ignoring case using
str1.equalsIgnoreCase(str2) and str1.compareToIgnoreCase(str2).
What value does the variable str have at the end of the program segment below? String str = "Hello World!"; if (str.length() > 6) { str = "Changed!"; } // What value does str have here?
“Changed!”
Which of the following is a boolean expression that is true if the variable a has the value of 3?
3 == a
a == 3
Which of the following is the reason to NOT use equality (==) to test double values against each other?
Double values in Java are approximate values, not exact, so rounding errors can occur.
What value does the variable answer have after this code is executed?
boolean x = false, y = false;
boolean answer = x || !y;
true
What value does the variable answer have after this code is executed?
boolean x = false, y = true, z = true;
boolean answer = y && ( x || !z );
false
Which of the following is a correct way for checking if two String values stored in the variables str1 and str2 are equal?
str2.equals(str1)
str1.equals(str2)
NOT str2 == str1
str1 = “normal”
str2 = “parallel”
if (str1.compareTo(str2) < 0)
str1 = str2;
str 1 = “normal”
str 2 = “parallel”
str 1 = “parallel”
str 2 = “parallel”
Which of the following are reference types? Scanner int char String
Scanner
String
Why can’t we use the equality operator (==) when comparing variables of reference types?
Location, location, location!
Because the variables hold the locations in memory of the objects, and the equality operator (==) tests if the two locations are the same not the values of the objects.
What is the main difference between primitive types and reference types?
Variables of primitive types hold numbers that we use directly, variables of reference types hold numbers that are locations in memory where objects are stored.
What gets printed to the screen when the following code segment executes? int score = 1024; String msg = "Default"; if (score > 300) { msg = "More than 300!"; } else if (score > 500) { msg = "More than 500!"; } else if (score > 1000) { msg = "More than 1000!"; } System.out.println(msg);
More than 300!
Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number: "); int num = Integer.parseInt(keyboard.nextLine()); while ( CONDITION ) { System.out.println("That number is positive!"); System.out.print("Enter another: "); num = Integer.parseInt(keyboard.nextLine()); }
num>=0
0<=num
0 <= num
num >= 0
String str = ""; int idx = 0; while (idx < 3) { if (idx % 2 == 0) { str = str + "a"; } else { idx = idx + 1; } } System.out.println(str);
No output will happen because this loop has a bug in it.