Chapter 3 Flashcards

1
Q

Analyze the following code.

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

The code displays It is even!

The code displays nothing.

The code is wrong. You should replace if (even) with if (even == true).

The code is wrong. You should replace if (even) with if (even = true).

A

The code displays nothing.

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

Assume x = 4 and y = 5, which of the following is true?

x < 5 && y < 5
x < 5 || y < 5
x > 5 && y > 5
x > 5 || y > 5

A

x < 5 || y < 5

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

What is 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5?

A

There is no guarantee that 1 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1 == 0.5 is true.

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

The __________ method immediately terminates the program.

A

System.exit(0);

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

The “less than or equal to” comparison operator in Java is __________.

A

<=

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

The equal comparison operator in Java is __________.

A

==

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

What is 1 + 1 + 1 + 1 + 1 == 5?

A

true

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

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

A

10

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

What is the output of the following code?

int x = 0;
if (x < 4) {
     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
10
Q

Which of the following are so called short-circuit operators?

+=
&
||
|

A

||

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

Which of the following code displays the area of a circle if the radius is positive.

if (radius != 0) System.out.println(radius * radius * 3.14159);
if (radius >= 0) System.out.println(radius * radius * 3.14159);
if (radius > 0) System.out.println(radius * radius * 3.14159);
if (radius <= 0) System.out.println(radius * radius * 3.14159);

A

if (radius > 0) System.out.println(radius * radius * 3.14159);

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

Analyze the following code:

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

The program has a compile error.

The program has a runtime error.

The program runs fine, but displays nothing.

The program runs fine and displays It is even.

A

The program runs fine and displays It is even.

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

Which of the following is a possible output from invoking Math.random()?

  1. 43
  2. 5
  3. 0
  4. 0
A
  1. 5

0. 0

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

Assume x = 4, which of the following is true?

!(x == 4)
x != 4
x == 5
x != 5

A

x != 5

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

In Java, the word true is ________.

A

a Boolean literal

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