Selections Flashcards

1
Q

What does &&, ||, and ! mean in boolean expressions?

A

&& means AND
|| means OR
! means NOT

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

What 3 types of selection statements are there?

A

if-else statements, switch-statements, and conditional expressions

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

What types can be in a switch expression?

A

char, byte, short, int, or String

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

What happens if you omit a break statement in a case?

A

The program will continue to the next case, instead of exiting the switch construct

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

What is 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0?

A. true
B. false
C. other

A

C. other.

There is no guarantee that 1.0 + 1.0 + 1.0 + 1.0 + 1.0 == 5.0 is true, becuase floating-point numbers are approximated.

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

Analyze the following code:

boolean even = false;
if (even = true) {
System.out.println(“It is even!”);
}

A. The program has a compile error.
B. The program has a runtime error.
C. The program runs, but displays nothing.
D. The program runs and displays It is even!

A

The correct answer is D
Explanation: It is a common mistake to use the = operator in the condition test. What happens is that true is assigned to even when you write even = true. So even is true. The program compiles and runs fine and prints ‘It is even!’.

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

Code 1:

int number = 45;
boolean even;

if (number % 2 == 0)
even = true;
else
even = false;

Code 2:

int number = 45;
boolean even = (number % 2 == 0);

A. Code 1 has compile errors.
B. Code 2 has compile errors.
C. Both Code 1 and Code 2 have compile errors.
D. Both Code 1 and Code 2 are correct, but Code 2 is better.

A

D. Both Code 1 and Code 2 are correct, but Code 2 is better.

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

Which of the Boolean expressions below is incorrect?

A. (true) && (3 => 4)
B. !(x > 0) && (x > 0)
C. (x > 0) || (x

A

A, D and E are incorrect.

Explanation: a: (3 => 4) should be (3 >= 4), d: (x = 0) should be (x == 0), and e: should be (-10

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

Suppose x=10 and y=10 what is x after evaluating the expression (y > 10) && (x++ > 10).

A. 9
B. 10
C. 11

A

B. 10

Explanation: For the && operator, the right operand is not evaluated, if the left operand is evaluated as false.

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

Suppose x=10 and y=10 what is x after evaluating the expression (y >= 10) || (x++ > 10).

A. 9
B. 10
C. 11

A

B. 10

Explanation: For the || operator, the right operand is not evaluated, if the left operand is evaluated as true.

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

What is y after the following switch statement is executed?

int x = 3; int y = 4;
switch (x + 3) {
  case 6: y = 0;
  case 7: y = 1;
  default: y += 1;
}
A. 1
B. 2
C. 3
D. 4
E. 0
A

B. 2

Case 6 is invoked first, but since there’s no break statement in case 6, all the other cases are executed as well.

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

What will be displayed by the following switch statement?

char ch = 'a';
    switch (ch) {
      case 'a':
      case 'A':
        System.out.print(ch); break;
      case 'b':
      case 'B':
        System.out.print(ch); break;
      case 'c':
      case 'C':
        System.out.print(ch); break;
      case 'd':
      case 'D':
        System.out.print(ch);
    }
A. abcd
B. a
C. aa
D. ab
E. abc
A

B. a

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

Analyze the following program fragment:

int x;
double d = 1.5;

switch (d) {
  case 1.0: x = 1;
  case 1.5: x = 2;
  case 2.0: x = 3;
}

A. The program has a compile error because the required break statement is missing in the switch statement.
B. The program has a compile error because the required default case is missing in the switch statement.
C. The switch control variable cannot be double.
D. No errors.

A

C. The switch control variable cannot be double.

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

Analyze the following code fragments that assign a boolean value to the variable even.

Code 1: 
if (number % 2 == 0)
  even = true;
else 
  even = false;

Code 2:
even = (number % 2 == 0) ? true: false;

Code 3:
even = number % 2 == 0;

A. Code 2 has a compile error, because you cannot have true and false literals in the conditional expression.
B. Code 3 has a compile error, because you attempt to assign number to even.
C. All three are correct, but Code 1 is preferred.
D. All three are correct, but Code 2 is preferred.
E. All three are correct, but Code 3 is preferred.E. All three are correct, but Code 3 is preferred.

A

E. All three are correct, but Code 3 is preferred.

Explanation: Code 3 is the simplest. Code 1 and Code 2 contain redundant code.

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

What is the syntax of conditional expressions?

A

boolean-expr ? expr1 : expr2;

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

What is the output, you you enter the numbers 9, 8 and 6?

public class Test{
    public static void main(String[] args) {
        java.util.Scanner input = new java.util.Scanner(System.in);
        double x = input.nextDouble();
        double y = input.nextDouble();
        double z = input.nextDoubl();
        System.out.println((x > y && y > z) ? "sorted" : "not sorted");
    }
}
A

“sorted”