Operators & Statements Flashcards

1
Q

Is the following assignment valid? short = short + short;

A

No. short is always promoted to int before being used in an arithmetic binary operation, which cannot be assigned back to short.

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

Is the following assignment valid? int = byte + short;

A

Yes. byte and short are automatically promoted to int before added.

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

Is the following assignment valid? short = short++;

A

Yes. Unary operators don’t promote shorts.

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

Is the following assignment valid? float f = 4.3;

A

No. floating-point literals are assumed to be doubles unless postfixed with ‘f’, which cannot be assigned to float.

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

Is the following assignment valid? double = short + float;

A

Yes. short is promoted to int automatically, then to float to be added, then the result is promoted to double to be assigned.

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

Is the following assignment valid? boolean b = !0;

A

No. The negation operator is not valid on a numeric value.

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

Is the following assignment valid? int i = 5.0;

A

No. 5.0 is treated as a double, which cannot be assigned to an int unless casted.

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

Is the following assignment valid? short s = 35000;

A

No. 35000 exceeds the maximum value of short and can’t be assigned unless casted, and then overflowing would apply.

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

Is the following assignment valid? long l = 35000;

A

Yes.

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

Is the following assignment valid? long l = 2500000000;

A

No. 2500000000 exceeds the maximum value of an int. An ‘L’ must follow the literal value to be treated as a long literal.

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

Is the following assignment valid? boolean b = int == null;

A

No. null cannot be compared to primitives

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

Is the following assignment valid? boolean b = Boolean == null;

A

Yes.

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

Given int x = 2;, What is the value of the following expression?

x++ + (2 + –x) * ++x;

A
  1. The unary operators are evaluated left to right regardless of parentheses, which simplifies to:
    2 + (2 + 2) * 3;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which of the following types is not supported by switch statements? byte/Byte, short/Short, char/Character, int/Integer, long/Long, enum, String.

A

long/Long (enum support added in Java 5, String in Java 7)

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

What is a Java operator?

A

It is a special symbol that can be applied to a set of variables, values, or literals that returns a result.

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

What is an operand in the context of an operator in Java?

A

An operand is one of set of variables, values, or literals, which can be used to return a result from an operator.

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

What are the 3 flavors of Java operators?

A

Unary, binary, and ternary.

18
Q

How many operands can a unary operator be applied to?

A

1

19
Q

How many operands can a binary operator be applied to?

A

2

20
Q

How many operands can a ternary operator be applied to?

A

3

21
Q

What is the order of operator precedence in Java?

A
Post-unary operators
Pre-unary operators
Other unary operators
Multiplication/Division/Modulus
Addition/Subtraction
Shift operators
Relational operators
Equal to/not equal to
Logical operators
Short-circuit logical operators
Ternary operators
Assignment operators
22
Q

When does Java not follow the rules of operator precedence?

A

When the order of operation is overridden with parentheses.

23
Q

If all operators are equal, how does Java evaluate an expression?

A

From left to right.

24
Q

What are the arithmetic operator?

A

+, -, * /, %, ++, –

25
Q

Which primitives are operands for arithmetic operators?

A

All except boolean.

Note: A String is not a primitive though some texts treat it that way. The arithmetic + and += perform concatenation on strings, not arithmetic.

26
Q

What is one of the rules of numeric promotion in Java?

A

If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.

27
Q

What is another rules of numeric promotion in Java?

A

If one of the value is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value’s data type.

28
Q

What is yet another rule of numeric promotion in Java?

A

Smaller data types, namely byte, short, and char, are first promoted to int any time they’re used with a Java binary arithmetic operator, even if neither of the operands is int.

29
Q

What is the other rule of numeric promotion in Java?

A

After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.

30
Q

What does the ‘+’ unary operator do?

A

Indicates that a number is positive (although numbers in Java are assumed to be positive unless accompanied by a negative unary operator.)

31
Q

What does the ‘-‘ unary operator do? What is it called?

A

It indicates a literal number is negative or negates an expression. It is called the negation operator.

32
Q

What does the ‘++’ operator do?

A

Increments a value by 1.

33
Q

What does the ‘–’ operator do?

A

Decrements a value by 1.

34
Q

What does the ‘!’ operator do? What is it called?

A

It inverts a Boolean’s logical function. It is called the logical complement operator.

35
Q

Can you apply the logical complement operator to an arithmetic expression?

A

No.

36
Q

Can you apply the negation operator to a boolean expression?

A

No.

37
Q

What is the ‘++’ operator called?

A

The increment operator.

38
Q

What is the ‘–’ operator called?

A

The decrement operator.

39
Q

What is it called if the increment operator or decrement operator is placed before and operand?

A

The pre-increment or pre-decrement operator.

40
Q

What is it called if the increment operator or decrement operator is placed after an operand?

A

The post-increment or post-decrement operator.

41
Q

Which operator has higher precedence in the order of operations, the pre-increment/pre-decrement operators or the post-increment/post-decrement operators?

A

The pre-increment/pre-decrement operators.