frequently asked questions on operators
Friday, January 29, 2010
6:06 AM
,
0 Comments
Labels: repeated questions in scjp exam , scjp interview quesions , SCJP MATERIAL , SCJP Mock Exam , scjp objects , scjp sun certification material , Sun Certified Java Programmer (SCJP)
Labels: repeated questions in scjp exam , scjp interview quesions , SCJP MATERIAL , SCJP Mock Exam , scjp objects , scjp sun certification material , Sun Certified Java Programmer (SCJP)
9. Given:
class Foozit {
public static void main(String[] args) {
Integer x = 0;
Integer y = 0;
for(Short z = 0; z < 5; z++)
if((++x > 2) || (++y > 2))
x++;
System.out.println(x + " " + y);
} }
What is the result?
A. 5 1 B. 5 2 C. 5 3 D. 8 1
E. 8 2 F. 8 3 G. 10 2 H. 10 3
Answer:
-> E is correct. The first two times the if test runs, both x and y are incremented once (the x++ is not reached until the third iteration). Starting with the third iteration of the loop, y is never touched again, because of the short-circuit operator.
-> A, B, C, D, F, G, and H are incorrect based on the above.
10. Given:
class Titanic {
public static void main(String[] args) {
Boolean b1 = true;
boolean b2 = false;
boolean b3 = true;
if((b1 & b2) | (b2 & b3) & b3)
System.out.print("alpha ");
if((b1 = false) | (b1 & b3) | (b1 | b2))
System.out.print("beta ");
} }
What is the result?
A. beta
B. alpha
C. alpha beta
D. Compilation fails.
E. No output is produced.
F. An exception is thrown at runtime.
Answer:
-> E is correct. In the second if test, the leftmost expression is an assignment, not
a comparison. Once b1 has been set to false, the remaining tests are all false.
-> A, B, C, D, and F are incorrect based on the above.
11. Given:
class Feline {
public static void main(String[] args) {
Long x = 42L;
Long y = 44L;
System.out.print(" " + 7 + 2 + " ");
System.out.print(foo() + x + 5 + " ");
System.out.println(x + y + foo());
}
static String foo() { return "foo"; }
}
What is the result?
A. 9 foo47 86foo
B. 9 foo47 4244foo
C. 9 foo425 86foo
D. 9 foo425 4244foo
E. 72 foo47 86foo
F. 72 foo47 4244foo
G. 72 foo425 86foo
H. 72 foo425 4244foo
I. Compilation fails.
Answer:
-> G is correct. Concatenation runs from left to right, and if either operand is a String, the operands are concatenated. If both operands are numbers they are added together. Unboxing works in conjunction with concatenation.
-> A, B, C, D, E, F, H, and I are incorrect based on the above.



0 Response to "frequently asked questions on operators"
Post a Comment