004 - Operators Flashcards

1
Q

Which operator is evaluated from right to left?

A

The assignment operator

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

Which is the order of precedence for operators?

A
postfix
unary
multiplicative
additive	
shift 
relational
equality	
bitwise AND	
bitwise exclusive OR	
bitwise inclusive OR
logical AND
logical OR
ternary
assignment
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which are the postfix operators?

A

expr++

expr–

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

Which are the unary operators?

A
\++expr 
--expr 
\+expr 
-expr 
~ 
!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which are the

multiplicative operators?

A

*
/
%

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

Which are the additive operators?

A

+

-

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

Which are the shift operators?

A

>

|&raquo_space;>

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

Which is the type comparison operators?

A

instanceof

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

Which are the equality and relational operators?

A

==
!=
>
>=

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

Which are the bitwise operators?

A

Bitwise inclusive AND &
bitwise exclusive OR ^
bitwise inclusive OR |

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

Which are the logical operators?

A

logical AND &&

logical OR ||

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

Which are the ternary operators?

A

?

:

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

Which are the assignment operators?

A
= 
\+= 
-= 
*=
 /= 
%= 
&= 
^= 
|= 
>= 
>>>=
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is concatenation and how is it used?

A

Joining two strings together

By using the operator +

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

What can a unary operator do?

A

operations such as
incrementing/decrementing a value by one,
negating an expression,
or inverting the value of a boolean.

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

Where can the increment and decrement operators be used?

A

As prefix or postfix

17
Q

What is the difference between a prefix and a postfix increment/decrement operator?

A

the prefix version (++result) evaluates to the incremented value,

the postfix version (result++) evaluates to the original value.

18
Q

What is the difference between = and ==?

A

= assignment operator

== equality operator

19
Q

What is short-circuiting behavior?

A

the second operand is evaluated only if needed.

It is used with conditional-AND and conditional-OR operators (&& and ||)

20
Q

What is ?: and how is it used?

A

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.”

21
Q

When is useful to use the ? : operator?

A

if it makes the code more readable;

E.g., when the expressions are compact and without side-effects (such as assignments).

22
Q

In which direction are operators evaluated?

A

From left to right, except for the assignment operator

23
Q

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.

A

instanceof

24
Q

What is null an instance of?

25
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 "~".
26
Which operator shifts a bit pattern to the left?
The signed left shift operator "
27
Which operator shifts a bit pattern to the right?
The signed right shift operator ">>" shifts a bit pattern to the right,
28
Which operator shifts a zero into the leftmost position?
The unsigned right shift operator ">>>" shifts a zero into the leftmost position.
29
Which operator performs a bitwise AND operation?
The bitwise & operator
30
Which operator performs a bitwise inclusive OR operation?
The bitwise ^ operator
31
Which operator performs a bitwise exclusive OR operation?
The bitwise | operator
32
Consider the following code snippet. arrayOfInts[j] > arrayOfInts[j+1] Which operators does the code contain?
Relational > (greater than) | Arithmetical additional + (addition)
33
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
34
To invert the value of a boolean, which operator would you use?
! (Logical complement)
35
Which operator is used to compare two values, = or == ?
== comparison | = assignment
36
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
37
``` 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); } }
38
``` 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.
39
pensar
think thought thought