Ch2: Operators and Statements Flashcards

1
Q

What is an operator?

A

A symbol applied to a set of variables that returns a result

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

What is an operand?

A

The value(s) that operators operate on

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

What is the name for the values an operator operates on?

A

Operands

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

What are the three types of operators (based on operand count)?

A

unary, binary, and ternary

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

What happens if two operators have the same level of precedence?

A

Java guarantees left-to-right evaluation

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

What is the order of operator precedence?

A
  1. Post-unary de/increment (x++)
  2. Pre-unary de/increment (++x)
  3. negation (unary)
  4. Multiplicative
  5. Additive
  6. Shift (bitwise)
  7. Relational (greater than/less than)
  8. Equality Operators
  9. Logical/bitwise operators
  10. Short-circuit logical
  11. Ternary
  12. Assignment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the % operator name and function?

A

Modulus, returns remainder (from devision)

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

What are the rules of numeric promotion when performing operations on primitive (non-unary) numeric types?

A
  1. A smaller type will be promoted to match the larger type
  2. An integral type will be promoted to match the floating-point type
  3. For binary arithmetic operators (not unary), byte, short, and char are always promoted to int
  4. The resulting value of an operator will be the same type as the promoted operands
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the unary operators?

A

++ (increment a value by one)
– (decrement a value by one)
+ (makes a literal positive. Opposite of -)
- (makes a literal negative or negates an expression)
! (inverts a boolean, “logical complement operator”)

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

What does the post-increment operator do?

A

x++

  1. Returns original operand value
  2. Increments operand by one
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the pre-increment operator do?

A

++x

  1. Increments operand by one
  2. Returns the new value
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you cast a larger primitive number type to a smaller primitive data type, and what happens?

A

use (type) (eg, int i = (int) 1.0). Will cause underflow or overflow, which basically wraps around the value

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

Considering numeric promotion, does this throw an error, and why/not:
long x = 10;
int y = 5;
y *= x;

A

No; compound assignment operators will automatically re-cast to the target data type.

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

Considering numeric promotion, does this throw an error, and why/not:
long x = 10;
int y = 5;
y = y * x;

A

Yes; binary operators return the data type of the larger operand type.

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

Do assignment operators have a return value

A

Yes, it returns the value that was assigned to the left-hand operand and therefore can be used like this:
y = (x = 3); // x and y == 3

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

What are the logical / bitwise operators?

A

& - AND
| (pipe) - OR
^ - XOR

17
Q

What are the logical short circuit operators?

A

&& and ||

18
Q

Which scenarios can we use equality operators

A
  1. Comparing numeric primitive types (they will be promoted, as with math operators)
  2. Comparing two boolean values
  3. Comparing two objects (not object references!), including string and null types
    Note: Cannot mix and match String, numeric, boolean, and reference types.
19
Q

What is the switch statement syntax?

A
switch(runtimeValue) {
    case compiletimeValue:
        codeToExecute();
        break;
    ...
    default:
        defaultCase();
}
20
Q

Which data types are supported by switch statement evaluations?

A
Primitive integral classes and their wrappers (NOT boolean or long), String, and emum.
eg:
* int
* byte
* short
* char
* String
* enum
21
Q

In switch statements, what properties much the case values have?

A
  • the case statement value must be a literal, enum constant, or final constant variable
  • must be the same type as the switch value (no promotions or casting will happen)
22
Q

In a switch statement, can a case be a final variable that was passed to the function (eg, can a case be lastName from within class getOrder(final String lastName))

A

No, even though the provided param was final, because it was passed into the method it’s not constant.

23
Q

List all looping control structures

A

while (bool) {…}
do {…} while (bool);
for (initialize; booleanExpression; updateStatement) {…}
for (type instance : java.lang.Iterable) {…} // for-each loop

24
Q

What is the format for labels and break statements?

A
OPTIONAL_LABEL: while (boolExp) {
    //stuff
    break OPTIONAL_LABEL;
}
25
Q

What is the format for the continue statement?

A
OPTIONAL_LABEL: while (boolExp) {
    //stuff
    continue OPTIONAL_LABEL;
}