Friday, October 26, 2007

Java interview questions Part26


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?

5 1

5 2

5 3

8 1

8 2

8 3

10 2

10 3


6.
Given:

class Titanic {
public static void main(String[] args) {
Boolean bl = true;
boolean b2 = false;
boolean b3 = true;
if((bl & b2) | (b2 & b3) & b3)
System.out.print("alpha ");
if((bl = false) | (b1 & b3) | (bl | b2))
System.out.print("beta "};
}
}

What is the result?

beta

alpha

alpha beta

Compilation fails.

No output is produced.

An exception is thrown at runtime.


7.
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?

9 foo47 86foo

9 foo47 4244foo

9 foo425 86foo

9 foo425 4244foo

72 foo47 86foo

72 foo47 4244foo

72 foo425 86foo

72 foo425 4244foo

Compilation fails.


8.
Place the fragments into the code to produce the output 33. Note, you must use each fragment exactly once.

CODE:

class Incr {
public static void main(String[] args) {
Integer x = 7;
int y = 2 ;

X ___ ___;
___ ___ ___;
___ ___ ___;
___ ___ ___;

System.out.println(x);
}
}

FRAGMENTS:

Y
Y
Y
Y

y
x
x

-=
*=
*=
*=



9.
Given:

1. class Maybe {
2. public static void main(String[] args) {
3. boolean bl = true;
4. boolean b2 = false;
5. System.out.print(!false ^ false);
6. System.out.print(" " + (!b1 & (b2 = true)));
7. System.out.println(" " + (b2 ^ b1));
8. }
9. }

Which are true?

Line 5 produces true.

Line 5 produces false.

Line 6 produces true.

Line 6 produces false.

Line 7 produces true.

Line 7 produces false.


10.
Given:

class Sixties {
public static void main(String[] args) {
int x = 5; int y = 7 ;
System.out.print(((y * 2) % x));
System.out.print(" " + (y % x));
}
}

What is the result?

1 1

1 2

2 1

2 2

4 1

4 2

Compilation fails.

An exception is thrown at runtime

0 comments:

Advertisement

 

Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com