Branching ch.3 Flashcards

1
Q

myInt == 42

A

OK

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

myChar == ‘q’

A

OK

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

myDouble == 3.26

A

NOT OK

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

myString == “Hello”

A

NOT OK

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

=>

A

not valid operator

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

!

A

not valid operator

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

<>

A

not valid operator

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

Can you use use = in an if-else expression?

A

No, should be ==

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

How to detect a range?

A

Use if-else-if-else

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

How do if-else-if-else structures work?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
What is the range for 
if x < 10
else if x < 20
else if x < 30
else
A

0-9
10-19
20-29
30+

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
In an if 
else if 
else if 
else 
structure is the second branch taken if the first if is not false?
A

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).

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

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)

A

(0 < x) AND (x < 100)

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

Write expression: x is in the range -4 to +4

A

(x > -5) && (x < 5)

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

What happens when you omit the break statement in switch statements? Common error

A

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.

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

str1 less than str2 returns what?

A

Negative number

17
Q

What is the expression for str1 less than str2?

A

str1.compareTo(str2) < 0

18
Q

str1 equal to str2 returns what?

19
Q

What is the expression for str1 equal to str2?

A

str1.compareTo(str2) == 0

20
Q

str1 greater than str2 returns what?

A

Positive number

21
Q

What is the expression for str1 greater than str2?

A

str1.compareTo(str2) > 0

22
Q

A programmer can compare strings while ignoring case using

A

str1.equalsIgnoreCase(str2) and str1.compareToIgnoreCase(str2).

23
Q
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?
A

“Changed!”

24
Q

Which of the following is a boolean expression that is true if the variable a has the value of 3?

A

3 == a

a == 3

25
Q

Which of the following is the reason to NOT use equality (==) to test double values against each other?

A

Double values in Java are approximate values, not exact, so rounding errors can occur.

26
Q

What value does the variable answer have after this code is executed?
boolean x = false, y = false;
boolean answer = x || !y;

27
Q

What value does the variable answer have after this code is executed?
boolean x = false, y = true, z = true;
boolean answer = y && ( x || !z );

28
Q

Which of the following is a correct way for checking if two String values stored in the variables str1 and str2 are equal?

A

str2.equals(str1)
str1.equals(str2)
NOT str2 == str1

29
Q

str1 = “normal”
str2 = “parallel”
if (str1.compareTo(str2) < 0)
str1 = str2;

A

str 1 = “normal”
str 2 = “parallel”

str 1 = “parallel”
str 2 = “parallel”

30
Q
Which of the following are reference types?
Scanner
int
char
String
A

Scanner

String

31
Q

Why can’t we use the equality operator (==) when comparing variables of reference types?

A

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.

32
Q

What is the main difference between primitive types and reference types?

A

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.

33
Q
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);
A

More than 300!

34
Q
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());
}
A

num>=0
0<=num
0 <= num
num >= 0

35
Q
String str = "";
int idx = 0;
while (idx < 3) {
    if (idx % 2 == 0) {
        str = str + "a";
    }
    else {
        idx = idx + 1;
    }
}
System.out.println(str);
A

No output will happen because this loop has a bug in it.