004 - Operators Flashcards
Which operator is evaluated from right to left?
The assignment operator
Which is the order of precedence for operators?
postfix unary multiplicative additive shift relational equality bitwise AND bitwise exclusive OR bitwise inclusive OR logical AND logical OR ternary assignment
Which are the postfix operators?
expr++
expr–
Which are the unary operators?
\++expr --expr \+expr -expr ~ !
Which are the
multiplicative operators?
*
/
%
Which are the additive operators?
+
-
Which are the shift operators?
>
|»_space;>
Which is the type comparison operators?
instanceof
Which are the equality and relational operators?
==
!=
>
>=
Which are the bitwise operators?
Bitwise inclusive AND &
bitwise exclusive OR ^
bitwise inclusive OR |
Which are the logical operators?
logical AND &&
logical OR ||
Which are the ternary operators?
?
:
Which are the assignment operators?
= \+= -= *= /= %= &= ^= |= >= >>>=
What is concatenation and how is it used?
Joining two strings together
By using the operator +
What can a unary operator do?
operations such as
incrementing/decrementing a value by one,
negating an expression,
or inverting the value of a boolean.
Where can the increment and decrement operators be used?
As prefix or postfix
What is the difference between a prefix and a postfix increment/decrement operator?
the prefix version (++result) evaluates to the incremented value,
the postfix version (result++) evaluates to the original value.
What is the difference between = and ==?
= assignment operator
== equality operator
What is short-circuiting behavior?
the second operand is evaluated only if needed.
It is used with conditional-AND and conditional-OR operators (&& and ||)
What is ?: and how is it used?
The ternary operator or if-then-else statement
If someCondition is true, assign the value of value1 to result. Otherwise, assign the value of value2 to result.”
When is useful to use the ? : operator?
if it makes the code more readable;
E.g., when the expressions are compact and without side-effects (such as assignments).
In which direction are operators evaluated?
From left to right, except for the assignment operator
Which operator can be used to test if an object is an instance of a class, of a subclass, or of a class that implements a particular interface.
instanceof
What is null an instance of?
Nothing
Which operator inverts a bit pattern. When applied to any of the integral types, makes every “0” a “1” and every “1” a “0”.?
The unary bitwise complement operator “~”.
Which operator shifts a bit pattern to the left?
The signed left shift operator “
Which operator shifts a bit pattern to the right?
The signed right shift operator “»” shifts a bit pattern to the right,
Which operator shifts a zero into the leftmost position?
The unsigned right shift operator “»>” shifts a zero into the leftmost position.
Which operator performs a bitwise AND operation?
The bitwise & operator
Which operator performs a bitwise inclusive OR operation?
The bitwise ^ operator
Which operator performs a bitwise exclusive OR operation?
The bitwise | operator
Consider the following code snippet.
arrayOfInts[j] > arrayOfInts[j+1]
Which operators does the code contain?
Relational > (greater than)
Arithmetical additional + (addition)
Consider the following code snippet.
int i = 10;
int n = i++%5;
a. What are the values of i and n after the code is executed?
b.What are the final values of i and n if instead of using the postfix increment operator (i++), you use the prefix version (++i))?
a.
i = 11
n = 0
b.
i = 11
n = 1
To invert the value of a boolean, which operator would you use?
! (Logical complement)
Which operator is used to compare two values, = or == ?
== comparison
= assignment
Explain the following code sample: result = someCondition ? value1 : value2;
If someCondition is true, the result will be assigned the value of value1. If it is false, the result will be assigned the value value2
Change the following program to use compound assignments: class ArithmeticDemo {
public static void main (String[] args){ int result = 1 + 2; // result is now 3 System.out.println(result); result = result - 1; // result is now 2 System.out.println(result); result = result * 2; // result is now 4 System.out.println(result); result = result / 2; // result is now 2 System.out.println(result); result = result + 8; // result is now 10 result = result % 7; // result is now 3 System.out.println(result); } }
class ArithmeticDemo {
public static void main (String[] args){ int result = 3; System.out.println(result); result -= 1; // result is now 2 System.out.println(result); result *= 2; // result is now 4 System.out.println(result); result /= 2; // result is now 2 System.out.println(result); result += 8; // result is now 10 result %= 7; // result is now 3 System.out.println(result); } }
In the following program, explain why the value "6" is printed twice in a row: class PrePostDemo { public static void main(String[] args){ int i = 3; i++; System.out.println(i); // "4" \++i; System.out.println(i); // "5" System.out.println(++i); // "6" System.out.println(i++); // "6" System.out.println(i); // "7" } }
The code System.out.println(++i); evaluates to 6, because the prefix version of ++ evaluates to the incremented value. The next line, System.out.println(i++); evaluates to the current value (6), then increments by one. So “7” doesn’t get printed until the next line.
pensar
think
thought
thought