Ch3 Operators Flashcards

1
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
2
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
3
Q

What is the data type of y?

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

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
4
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
5
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
6
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
7
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
8
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
9
Q

What is the output of this code? why?

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

A

In Java, comparing null with null results in true.

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?

System.out.println(null instanceof Object);

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
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.

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

The following code snippet will print ‘true’.

short s = Short.MAX_VALUE;
char c = s;
System.out.println( c == Short.MAX_VALUE);

A

This will not compile because a short “variable” can never be assigned to a char without explicit casting. A short “constant” i.e. a short variable defined as final, can be assigned to a char only if the value fits into a char.

short s = 1;
byte b = s; => this will also not compile because although value is small enough to be held by a byte but the Right Hand Side i.e. s is a variable and not a constant.

final short s = 1;
byte b = s; => This is fine because s is a constant and the value fits into a byte.

final short s = 200;
byte b = s; => This is invalid because although s is a constant but the value does not fit into a byte.

Implicit narrowing occurs only for byte, char, short, and int. Remember that it does not occur for long, float, or double. So, this will not compile: int i = 129L; However, an implicit widening conversion from long to a float or a double is valid:

long l = 10L;
float f = l; //valid
double d = l; //valid

17
Q

What will the following code print?

public class TestClass{
static char ch;
static float f;
static boolean bool;
public static void main(String[] args){
System.out.print(f);
System.out.print(“ “);
System.out.print(ch);
System.out.print(“ “);
System.out.print(bool);
}
}

A. 0.0 false
B. 0.0ffalse
C. 0.0 0 false
D. 0.0true
E. 0.0ftrue
F. 0.0f 0 true

A

A.

This question tests you on two aspects -

the default values that are given to variables of various primitive types. You should remember that all numeric types, including char, get the value of 0 (or 0.0 for float and double) and boolean gets the value of false.
how the value is printed by System.out.print method - java.lang.System class has a public static variable named out, which is of class java.io.PrintStream.The PrintStream class has multiple print/println methods for printing out primitive values as well as objects.
For byte, short, and int, these print/println methods simply print the integer value as it is.

For char, the print/println methods translate the character into one or more bytes according to the platform’s default character encoding. That is why while printing a char value of 0, a blank space is printed instead of 0 (even though the char’s integral value is 0).

For long, float, and double values, these print/println methods use the respective primitive wrapper class’s toString method to get the String representation of that primitive. For example, to print the value of a float variable f, it internally calls Float.toString(f). Now, this method doesn’t append an “f” at the end of a float value. That is why a float value of 0.0 is printed as 0.0 and not 0.0f.