What are the compound assignments you need to know?
-= - extract and assign; \+= - add and assign; *= - multiply and assign; /= - divide and assign;
y = y - 5 -> y -= 5; y = y * 5 -> y *= 5;
What is the result of the following:
int y = 5;
y *= 1 + 3;
20
When using compound assignments the part after the = will be resolved first.
What are the relational operators?
== equal != not equal > greater than >= greater than or equal < less than <= less than or equal
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
What is the result?
int result = 1 + 5 * 2 / 2 % 2 + (5 + 5);
System.out.println(result);
12
Left to right. Parantheses first. Also remember that *, / and % have a higher precedence than the + and - operators.
What is the result:
int players = 0;
System.out.println(players++);
System.out.println(players);
System.out.println(++players);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.
What is the the value of z?
int z = 5;
if(++z > 5 || ++z > 6) z++
z = 7.
|| is a short circuit operator, if the first one matches to true the second expression will not be evaluated.
What is the the value of z?
int z = 5;
if(++z > 5 | ++z > 6) z++
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.
What does the ^ operator do?
exclusive-OR operator.
if((1 == 1) ^ (2 == 2)) -> wil result in false. Only one of the expressions should result in true.
Which operators will always evaluate all the operands? && | || ? : %
All mathematical operators evaluate all the operands.
and %,
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);
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.
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]);
}
}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].
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 == d)
(a == “java”)
a.equals(c)
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);
1, 4, 5
3 wil result in 10.0.
Which of the following statements will compile without any error?
1,2,3,4
Which of the following statements are true?
1 & 3