chapter 3 Flashcards

1
Q

What is an operator?

A

A special symbol that can be applied to a set of variables, values, or literals, and 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 of variable the operator is being applied to.

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

In general, what are the three types of operators available in Java?

A

unary, binary, and ternary.

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

What is the order of operator precedence?

A
post-unary operators: foo++, foo--;
pre-unary operators: ++foo, --foo;
unary operators: -,!,~+,(type)
mult/div/mod: *,/,%
add/sub: +,-
shift operators: <>,>>>
relational operators: ,<=,>=,instanceof
equal/not equal: ==, !=
logical operators: &amp;,^,|
short-circuit logical ops: &amp;&amp;, ||
ternary operators: boolean exp? exp1: exp2
assignment ops: =, +=,-=,*=,/=,%=,&amp;=,^=,|=,<<=,>>=,>>>=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is a unary operator?

A

An operator that requires exactly one operand to function.

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

List the unary operators in Java.

A
! -> Inverts a boolean logical value
\+ -> Indicates a number is positive
- -> Indicates a literal number is negative, or negates an expression
\++ -> increments a value by 1
-- -> decrements a value by 1
(type) casts a value to a specific type
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a binary operator?

A

An operator that takes two arguments.

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

List the binary operators.

A

+ -> Adds two numeric values.
- -> Subtracts two numeric values.
* -> Multiplies two numeric values.
/ -> Divides one numeric value by another.
% -> Returns the remainder after division of one numeric value by another.

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

What is the only primitive type arithmetic operators cannot be applied to?

A

boolean.

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

What are the only arithmetic operators that can be applied to String types?

A

+, +=

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

When parentheses are used in the exam, what are two things to pay attention to?

A

Are parentheses balanced?

How do the parentheses affect order of operations?

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

What are the 4 numeric promotion rules for primitives used during arithmetic operations?

A
  1. If two values differ in type, the value with the smaller type will be promoted to the larger.
  2. If one value is integral and the other floating-point, the integral will be promoted to the floating-point value’s data type.
  3. Any type smaller than an int will first be promoted to int when used with a Java binary arithmetic operator.
  4. The resulting value of some operation will have the same data type as its promoted operands.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is an assignment operator?

A

A binary operator that modifies the variable on the left side of the operator with the result of the value on the right side of the equation.

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

What is casting?

A

A unary operation where one data type is explicitly interpreted as another data type.

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

When casting is present on the OCM exam, what are some things one pay attention to?

A

parentheses and return types.

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

What are the compound assignment operators in Java?

A

*=
/=

17
Q

Provide an example of a compound operator handling required casting.

A

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

18
Q

Is the result of an assignment an expression itself?

A

Yes, it is equal to the value of the assignment;

19
Q

Describe the two equality operators

A

==
primitives: returns true if the two values represent the same value
objects: returns true if the two values reference the same object
!=
primitives: returns true if the two values represent different values
objects: returns true if the two values do not reference the same object

20
Q

What are the three scenarios where equality operators are used?

A
  1. Comparing two numeric of primitive types. If values are different data types, values are promoted.
  2. Comparing two boolean values.
  3. Comparing two objects, including null and String values.
21
Q

Describe the relational operators.

A
< Returns true if the value on the left is strictly less than the value on the right.
<= Returns true if the value on the left is less than or equal to the value on the right.
> Returns true if the value on left is strictly greater than the value on the right.
>= Returns true if the value on the left is greater than or equal to the value on the right.
a isinstanceof b Returns true if the reference that a points to is an instance of a class/subclass/class that implements a particular interface , as named in b.
22
Q

What happens when you call instanceof on a null variable?

A

It always returns false.

23
Q

Describe the three logical operators.

A

& : Logical AND is true only if both values are true.
| : Inclusive OR is true if at least one of the values is true.
^ : Exclusive XOR is true only if one value is true and the other is false.

24
Q

What types may logical operators be applied to?

A

numeric and boolean types.

25
Q

When applied to numeric types what are logical operators referred to as?

A

bitwise operators.

26
Q

What are some tips to remember how logical operators work?

A

& is only true if both operands are true.
| is only false if both operands are false.
^ is only true if the operands are different.

27
Q

Describe the short-circuit operators.

A

&& : Short-circuit AND is true only if both values are true. If the left side is false, then the right side will not be evaluated.
|| : Short-circuit OR is true if at least one of the values is true. If the left side is true, then the right side will not be evaluated.

28
Q

What is the form of the ternary operator?

A

booleanExpression? expression: expression;