Chapter 3 Flashcards

1
Q

What is the output of each print line?

int parkAttandance = 0;

System.out.println(++parkAttandance);
System.out.println(parkAttandance);
System.out.println(parkAttandance–);
System.out.println(parkAttandance);

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

What is the output?

int lion = 3;
int tiger = ++lion * 5 / lion–;
System.out.println(“lion is “ + lion);
System.out.println(“tiger is “ + tiger);

A
  • lion is 3
  • tiger is 5

int tiger = ++lion * 5 / 3; //lion is assigned value of 2
int tiger = 3 * 5 / 3; //lion is assigned value of 3

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

What is the data type of x * y?

int x = 1;
long y = 33;
var z = x * y;
A

Since one of the values is long and the other is int and since long is larger than int, the int value is promoted to a long and the resulting value is long.

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

What is the data type of x + y?

double x = 33.21;
float y = 2.1;
var z = x + y;
A

This code will not compile because float y = 2.1 is required to be postfixed with an ‘f’. Floating-point literals are by default a double.

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

What is the data type of x * y?

short x = 10;
short y = 3;
var z = x * y;
A

x and y will both be promoted to int before the binary multiplication operation, resulting in an output type of int.

To have an output type of short again, use casting:
var z = (short) x * y;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the data type of w * x / y?

short w = 14;
float x = 13;
double y = 30;
var z = w * x / y;
A

First, w will automatically be promoted to an int because it is a short and it is being used in an arithmetic binary operation.

The promoted w value will then be promoted to a float so it can be multiplied with x.

The result of w * x will then be promoted to a double so it can be divided by y, resulting in a double value.

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

What is the data type of y?

short x = 10;
var y = 10++;
A

Unary operators are excluded from the numeric promotion rules, resulting in a short data type.

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

Does the following code compile?

short mouse = 10;
short hamster = 3;
short capybara = mouse * hamster;

A

Short values are automatically promoted to in when applying any arithmetic operator, with the resulting value being an int. The code does not compile.

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

What is the value of wolf and coyote? why?

long wolf = 5;
long coyote = (wolf = 3);
System.out.println(wolf);
System.out.println(coyote);

A
  • wolf = 3;
  • coyote = 3;

The result of an assignment operator is an expression in and of itself, equal to the value of the assignment. wolf = 3 also returns the value 3.

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

What is the output of this code? why?

boolean test = false;
if (test = true) {
    System.out.println("true");
} else {
    System.out.println("false");
}
A

The variable test is assigned a value of true (instead of testing if it is true), which also returns true. The output of this code is “true”.

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

What does the binary and equality operator ‘==’ do when applied to primitives?

A

Returns true if the two values represent the same value.

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

What does the binary and equality operator ‘!=’ do when applied to primitives?

A

Returns true if the two values represent different values.

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

What does the binary and equality operator ‘==’ do when applied to objects?

A

Returns true if the two values reference the same object.

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

What does the binary and equality operator ‘!=’ do when applied to objects?

A

Returns true if the two values do not reference the same object.

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

True or false: The following code returns true:

5 == 5.00

A

If the numeric values are of different data types, the values are automatically promoted.

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

What is the value of polar? why?

boolean bear = false;
boolean polar = (bear = true);

A

The expression is assigning the value of true to bear, therefore polar is also assigned the value of true.

17
Q

What is the output of this code? why?

File monday = new File("test.txt");
File tuesday = new File("test.txt");
File wednesday = tuesday;

System.out.println(monday == tuesday);
System.out.println(tuesday == wednesday);

A
  • false
  • true

monday and tuesday do not reference to the same object, resulting in false. tuesday and wednesday do reference the same object, resulting in true.

18
Q

What is the output of this code? why?

System.out.println(null == null);

A

In Java, comparing null with null results in true.

19
Q

What is the output of this code? why?

System.out.println(null instanceof Object);

A

Calling instanceof on the null literal or any null reference always returns false.

20
Q

What is the output of this code? why?

System.out.println(null instanceof null);

A

This code does not compile because null is used on the right side of the instanceof operator.

21
Q

What is the output of this code? why?

boolean eyesClosed = true;
boolean breathingSlowly = true;

boolean resting = eyesClosed | breathingSlowly;
boolean asleep = eyesClosed & breathingSlowly;
boolean awake = eyesClosed ^ breathingSlowly;

System.out.println(resting);
System.out.println(asleep);
System.out.println(awake);

A
  • resting = true
  • asleep = true
  • awake - false

AND is only true if both operands are true.
Inclusive OR is only false if both operands are false.
Exclusive OR is only true if the operands are different.

22
Q

What is the different between the logical operator & or | and the Short-Circuit operators && and ||, respectively?

A

The Short-circuit operators are nearly identical to the logic operators & and |, except that the right side of the expression may never be evaluated if the final result can be determined by the left side of the expression.

23
Q

What is the output of this code? why?

int rabbit = 6;
boolean bunny = (rabbit >= 6) || (++rabbit <= 7);
System.out.println(rabbit);

A

Because rabbit >= 6 is true, the increment operator on the right side of the expression is never evaluated, so the output is 6.

24
Q

What change, when applied independently, would allow the following code to compile?

3: long ear = 10;
4: int hearing = 2 * ear;

A) No change.
B) Cast ear on line 4 to int.
C) Change the data type of ear on line 3 to short.
D) Cast 2 * ear on line 4 to int.
E) Change the data type of hearing on line 4 to short.
F) Change the data type of hearing on line 4 to long.

A

B, C, D, F

25
Q

Why does the following code not work but using a compound assignment operator (*=), it does?

long goat = 10;
int sheep = 5;
sheep = sheep * goat;

A

It doesn’t work with a simple assignment operator because the value trying to be assigned to sheep is a long. Using a compound assignment operator (*=) it does work because Java will automatically cast the resulting value to a long.

26
Q

What is the output of the following code snippet?

4: int pig = (short) 4;
5: pig = pig++;
6: long goat = (int) 2;
7: goat -= 1.0;
8: System.out.println(pig + “ - “ + goat);

A) 4 - 1
B) 4 - 2
C) 5 - 1
D) 5 - 2
E) The code does not compile
F) None of the above
A

A

27
Q

What is the output of the following code?

public class MyClass {
    static long test(double fruit, float vegetable) {
        return (int) fruit + vegetable;
    }
    public static void main(String args[]) {
      int y = 4;
      int x = (y = 1);
      System.out.println(test((long)x, (float) y));
    }
}
A

The code cannot be compiled because the cast ‘(int) fruit + vegetable’ only casts fruit to int but not the whole expression.

28
Q

What is the output of the following code?

short height = 1, weight = 3;
short zebra = (byte) weight * (byte) height;
double ox = 1 + height * 2 + weight;
long giraffe = 1 + 9 % height + 1;
System.out.println(zebra);
System.out.println(ox);
System.out.println(giraffe);
A

The code does not compile because of the second line. multiplying weight and height will result in an int, which does not fit in a short, unless casted.

29
Q

How many lines of the following code contain compiler errors?

int note = 1 * 2 + (long)3;
short melody = (byte)(double)(note*=2);
double song = melody;
float symphony = (float)((song == 1_000f) ? song * 2L : song);

A

One. The following expression results in a long and must be casted to an int before it can be assigned to note.

int note = 1 * 2 + (long)3;