What is the result?

Given:

public class Spock {
public static void main(String[] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
if ((tail > distance) ^ ((story * 2) == tail))
System.out.print(“1”);
if ((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print(“2”);
}
}

What is the result?

Given:

public class Spock {
public static void main(String[] args) {
Long tail = 2000L;
Long distance = 1999L;
Long story = 1000L;
if ((tail > distance) ^ ((story * 2) == tail))
System.out.print(“1”);
if ((distance + 1 != tail) ^ ((story * 2) == distance))
System.out.print(“2”);
}
}

What is the result?

A.
1

B.
2

C.
12

D.
Compilation fails.

E.
No output is produced.

F.
An exception is thrown at runtime.

Explanation:
No output since both if statements include ^ (X-OR operator). If was used an || (OR operator) instead of ^ (X-OR operator) the result will be answer A (1).



Leave a Reply 3

Your email address will not be published. Required fields are marked *


Alvaro

Alvaro

Correct answer is option E, execution doesn’t pass through Sysout lines. Operator (^) is XOR, which means that condition will be true only when each sub-condition be exclusively negation of the other one. For the first IF both sub-conditions are true, hence, resultant eXclusive OR evaluation is false. For the second IF, both sub-conditions are false, hence, resultant eXclusive OR evaluation is false too.

Kanthi

Kanthi

Option E is correct.