Chapter 4: Operators Flashcards

1
Q

What are the compound assignments you need to know?

A
-= - extract and assign;
\+= - add and assign;
*= - multiply and assign;
/= - divide and assign;
y = y - 5 -> y -= 5;
y = y * 5 -> y *= 5;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the result of the following:
int y = 5;
y *= 1 + 3;

A

20

When using compound assignments the part after the = will be resolved first.

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

What are the relational operators?

A
== equal
!= not equal
> greater than
>= greater than or equal
< less than
<= less than or equal
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q
What is the result of the following instanceof comparisons:
class A implements Foo {}
class B extends A {}
class C extends A {}

A a = new A(); B b = new B(); C c = new C();

a instanceof A // 1
b instanceof A // 2
b instanceof Foo // 3
c instanceof B // 4
B instanceof B // 5
null instanceof String // 6
A
  1. True
  2. True
  3. True, indirect implementation of interface Foo
  4. Compiler Error wrong hierachy tree.
  5. Compiler error, instanceof only works on instances not class refs.
  6. False, always false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the result?
int result = 1 + 5 * 2 / 2 % 2 + (5 + 5);
System.out.println(result);

A

12

Left to right. Parantheses first. Also remember that *, / and % have a higher precedence than the + and - operators.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the result:
int players = 0;
        System.out.println(players++);
        System.out.println(players);
        System.out.println(++players);
A

0
1
2

Postfix ++ or –, the value is modified after the variable is used.
Prefix ++ or –., the value is modified before the variable is used.

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

What is the the value of z?
int z = 5;
if(++z > 5 || ++z > 6) z++

A

z = 7.

|| is a short circuit operator, if the first one matches to true the second expression will not be evaluated.

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

What is the the value of z?
int z = 5;
if(++z > 5 | ++z > 6) z++

A

z = 8.
| is a non-short circuit operator, regardless of the the result of the first expression the JVM will still evaluate the second expression.

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

What does the ^ operator do?

A

exclusive-OR operator.

if((1 == 1) ^ (2 == 2)) -> wil result in false. Only one of the expressions should result in true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q
Which operators will always evaluate all the operands?
&amp;&amp;
|
||
? :
%
A

All mathematical operators evaluate all the operands.

and %,

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
Integer i = new Integer(42);
Long ln = new Long(42);
Double d = new Double(42.0);

Which of the following options are valid code fragments?

i == ln; 
ln == d; 
i.equals(d);
d.equals(ln);
ln.equals(42);
A

Valid

i. equals(d);
d. equals(ln);
ln. equals(42); // Autobox to Int.

Invalid
i == ln; // Will give compile errors.
ln == d; // Will give compile errors.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
class Test
{
   public static void main(String[] args)
   {
      int k = 1;
      int[] a = { 1 };
      k += (k = 4) * (k + 2);
      a[0] += (a[0] = 4) * (a[0] + 2);
      System.out.println( k + " , " + a[0]);
   }
}
A

25, 25
The value 1 of k is saved by the compound assignment operator += before its right-hand operand (k = 4) * (k + 2) is evaluated. Evaluation of this right-hand operand then assigns 4 to k, calculates the value 6 for k + 2, and then multiplies 4 by 6 to get 24. This is added to the saved value 1 to get 25, which is then stored into k by the += operator. An identical analysis applies to the case that uses a[0].

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

Which of the following expressions will evaluate to true if preceded by the following code?

    String a = "java";
    char[] b = { 'j', 'a', 'v', 'a' };
    String c = new String(b);
    String d = a;

(a == d)
(b == d)
(a == “java”)
a.equals(c)

A

(a == d)
(a == “java”)
a.equals(c)

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

Which of the following code snippets will print exactly 10?

1.	Object t = new Integer(106);
	int k = ((Integer) t).intValue()/10;
	System.out.println(k);
2.	System.out.println(100/9.9);
3.	System.out.println(100/10.0);
4.	System.out.println(100/10);
5. 	System.out.println(3 + 100/10*2-13);
A

1, 4, 5

3 wil result in 10.0.

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

Which of the following statements will compile without any error?

  1. System.out.println(“a”+’b’+63);
  2. System.out.println(“a”+63);
  3. System.out.println(‘b’+new Integer(63));
  4. String s = ‘b’+63+”a”;
  5. String s = 63 + new Integer(10);
A

1,2,3,4

  1. Since the first operand is a String all others (one by one) will be converted to String.”ab” + 63 => “ab63”
  2. Since the first operand is a String all others (one by one) will be converted to String.”a” + ‘b’ => “a63”
  3. Since the first operand of + one is of numeric type, its numeric value of 98 will be used. Integer 63 will be unboxed and added to 98. Therefore, the final value will be int 161.
  4. Since the first one is numeric type so, ‘b’+63 = 161, 161+”a” = 161a.
  5. Since none of ‘+’ the operands is a String, the + operator will not generate a String. However, due to auto-unboxing, it will generate an int value of 73.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Which of the following statements are true?

  1. System.out.println(1 + 2 + “3”); would print 33.
  2. System.out.println(“1” + 2 + 3); would print 15.
  3. System.out.println(4 + 1.0f); would print 5.0
  4. System.out.println(5/4); would print 1.25
  5. System.out.println(‘a’ + 1 ); would print b.
A

1 & 3

  1. operator + is left associative so evaluation of (1 + 2 + “3” ) is as follows: ( 1 + 2 ) + “3” -> 3 + “3” -> “33”.
  2. evaluation of (“1” + 2 + 3) is as follows : (“1” + 2) + 3 -> “12” + 3 -> “123”.
  3. (4 + 1.0f ) evaluates as 4.0f + 1.0f ->5.0f -> 5.0
  4. (5/4) performs integer division because both 5 and 4 are integers, resulting in the value 1.
  5. Both operands in the expression ( ‘a’ + 1 ) will be promoted to int => 97 + 1 = 98