Chapter 3: Selections Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is wrong with the following code?

if i > 0 {

System.out.println(“i is positive”);

}

A

No paraentheses around the boolean expression (i > 0)

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

What can be changed about the following code and why?

if (i > 0) {

System.out.println(“i is positive”);

}

A

Braces can be ommitted if the enclose a single statement.

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

Write a statement that assigns 1 to x if y is greater than 0.

A

if (y > 0){

x = 1;

}

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

Write an if statment that increases pay by 3% if score is greater than 90.

A

if (score > 90) {

pay *= 1.03

}

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

What is the output of the following code?

int x = 0;
if (x x = x + 1;
}
System.out.println(“x is “ + x);

A

“x is 1”

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

Suppose x = 2 and y = 3. Show the output, if any, of the following code. What is the output if x = 3 and y = 2? What is the output if x = 3 and y = 3?

if (x > 2)

if (y > 2) {

int z = x + y;

System.out.println(“z is “ + z);

}

else

System.out.println(“x is “ + x);

A

No output if x = 2 and y = 3. Output is “x is 3” if x = 3 and y = 2. Output is “z is 6” if x = 3 and y = 3.

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

What is wrong in the following code?

if (score >= 60.0)

System.out.println(“D”);

else if (score >= 70.0)

System.out.println(“C”);

else if (score >= 80.0)

System.out.println(“B”);

else if (score >= 90.0)

System.out.println(“A”);

else

System.out.println(“F”);

A

Wrong order. A score of lets say 95, will end up being a D.

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

The following statement will evaluate to…

(x != 1) == !(x == 1);

A

True

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

Write a boolean expression if the number storred in variable num is between 1 and 100.

A

(num > 1) && (num

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

Write a boolean expression for |x-5| < 4.5

A

(x-5 -4.5)

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

Correct:

(-10 < x < 0)

A

(-10 < x) && (x < 0)

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