Chapter 3 Flashcards

1
Q

What is the lowest tier of operator in terms of precedence.

A

Assignment operators

=, +=, -=, *=, /=, %=, &=, ^=, |=, «=,&raquo_space;-,&raquo_space;>-

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

What are the 3 highest tier of operators in terms of precedence.

A

Post-unary operators, var++, var–
Pre-unary operators, ++var, –var
Other unary operators, -, !, ~, + (type)

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

how many variables or operands does a unery operator require.

A

one.

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

List all the operator types in order (12 in total)

A
Post-unary operators, var++, var--
Pre-unary operators, ++var, --var
Other unary operators, -, !, ~, + (type)
Multiplication/division/modulus, *, /, %
Addition/subtraction, +, -
Shift operators <>, >>>
Relation operators , <=, >=, instanceof
Equal to/not equel to ==, !=  
Logical operators &, ^, | 
Short-circuit logical operators &&, ||
Ternary operators, boolean expression
Assignment operators 
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>-, >>>-
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

what does the following unery operator do : !

A

Flips a boolean value. !False() = true() and vice versa.

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

what does the following unery operator do : -

A

it forces a numeric value into the negative.

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

what is the value of : A if B is 3.

int A = ++B * 5 / B–;

A

a = 5
First the post-unery operator is triggered.
setting B To 2.
Then the pre-unery operator is triggered
Setting B to 3
the equation is then evaluated left to right
3 * 5 / 3 =

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

list all the unary operators

A
! flips a boolean
\+ force a number to a positive value
- force a number to a negative value
\++ 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
9
Q

list all the binary operators

A
\+ Adds two values
- subtracts two numeric values
* multiplies two numeric values
/ devides one numeric value by another
% modules 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
10
Q

what happens when two values have different numeric data types

A
Java will promote the lower value to the higher value.
So 
int + long = long 
or 
byte + int = int
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

what happens when a integral value is part of a operation with a floating value.

A

the integral value is treated as the floating value.
int + double = double
int + float = float

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

what happens to a byte, a short or a char when it is part of a binary operations.

A

They are promoted to ints.
byte + byte = int
byte + char = int
short + char = int

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

what does this assignment operator do : =

A

it assigns a value to a variable.
so
var = (complex method)
var = (result of complex method)

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

what happens when we use casting on values in java.

A
It forces the output of a numeric operation into a certain value. 
var A = short + short 
A = int because of numeric promotion rules.
but 
var B = (short) (short + short) 
B = short because of casting. 
short C = short + short 
//Does not compile 
Because the compiler knows that the numeric promotion rules forces the outcome into a int. Casting is required here to allow the operation to compile.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

when is it required to use casting in java

A

When ever the code goes from a larger numerical data type to a smaller numerical data type.

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

what happens when a numeric value suffers from overflow or underflow.

A
When the outcome of a numeric operation does not fit a datatype, java will 'wrap around' to the lowest negative value and continue counting from there. 
byte maxByte = 128
byte overflowByte = maxByte + 1
byte IsaneByte = maxbyte + maxbyte
overflowByte = -127
insaneByte = 1
17
Q

What are the four compound assigment operators and what do they do.

A

These operators set the value of the left operand by adding, subtracting, multiplying or dividing by the right operand.

+= adds
-+ subtracts
*= multiply
/= divides

18
Q

in what way is a compound assigment operator more then just a short hand to write code.

A
These operators also 'take care' of casting the value to the right type based on the left operand. 
Example
int A = int A * long B 
//does not compile because the outcome is a long and long does not match a int
Int A *= long B
//Does compile, because the operator casts the result into a Int. If the outcome is to big for a int the java "overflow" result will be the value of A.
19
Q

what are the equality operators in java and what do they do.

A
== and !=
== returns true if the left and right operand match. 
!= returns true if the left and right operand DO NOT match. 

Numeric datatypes are promoted for the this operation.
boolean check = int 5 == double 5.00
check = true()

20
Q

what is the value of “B”
boolean A = false
boolean B = (A = true)

A

the value of B = true
This is because of the assignment operator used in the operation that sets the value for B.
Boolean B would have been false if :
boolean B = (A == true)

21
Q

what is the value of A and B

File 1 = new File("new file.txt");
File 2 = new File("new file.txt");
File 3 = 2;
Boolean A = (1 == 2);
Boolean B = (2 == 3);
A

A = false
1 and 2 point to two different objects (File 1 and File 2). based on the same class.
B = true
2 and 3 point to the same refrence of a object. Both point to File 2.

22
Q

what are the 5 relational operators and what do they do.

A

All relational operators return a boolean value based on the relation between de left and right operand.

< true if left is smaller than right
<= true if left is smaller than or equel to right.
> true if left is bigger than right
>= true if left is bigger or quel to right.

A instanceof B = true
if A is a reference to a class, sublass or interface that is named in B

23
Q

how many objects are created in this code snippet.

Integer count = Integer.valueOf(9)
Number num = count
Object ob = count

A

one object (count) is created.

24
Q

what are the three logical operators and how do they work.

A

& = logical AND
returns true if both operands are true.
| = Inclusive OR
returns true if at least one operand is true.
^ = exclusive XOR
returns true if one operand is true and one is false.

25
Q

what are the short circuit operators and what do they do.

A

&& = Short Circuit AND
returns true if both operands are true, in addition if the left operand is false it will not evaluate the right operand.

|| = Short-crcuit OR
returns true if at least one operand is true. If the left operand is true the right operand will not be evaluated.

26
Q

what is the value of “count” after this snippit has been run.

int count = 6;
boolean test = (count >= 6) || (++count <= 7)

A

value = 6
The increment operator on the right side of the expression is never evaluated because of the short circuit OR operator.
If the short circuit AND was used it would have been 7

27
Q

how many operands does a ternary operator require and what is the correct syntax.

A

3 operands.
1 the value to set on the left.
2 the “if” operand, This operand is the value for the first operand if a expression is met between 1 and 2..
3 the else operand, this operand is the value of the first operand if the expression between 1 and 2 is not met.

Syntax
First = (expression) ? SecondOperand : ThirdOperand

28
Q

there are currently 12 different operator types. These can furthermore be devided over 3 groups. Name these groups.

A

Unery (one operand)
Binary (two operands)
Ternary (three operands)