jobs4timesLogo jobs4timesLogo

Operators

Question : 1

Given :
public class Test{
public static void main(String[] args){
 int x=5;
 boolean b1=true;
 boolean b2=false;
 
if((x==4) && !b2)
System.out.println("1");
System.out.println("2");
if((b2=true) && b1)
System.out.println("3");
}
}
What is the result ?
  1. 2
  2. 3
  3. 1 2
  4. 2 3
  5. 1 2 3
  6. Compilation fails
  7. An exception is thrown at runtime

Answer : D

Question : 2

Given the command line java Pass2 and :
public class Pass2{
public void main(String[] args){
 int x=6;
Pass2 p=new Pass2();
p.doStuff(x);
System.out.println("main x="+x);
}

void doStuff(int x) {
System.out.println("doStuff x="+ x++);
}
}
What is the result ?
  1. Compilation fails
  2. An exception is thrown at runtime
  3. doStuff x=6 main x=6
  4. doStuff x=6 main x=7
  5. doStuff x=7 main x=6
  6. doStuff x=7 main x=7

Answer : B

Question : 3

Given :
public class Pass{
public static void main(String[] args){
 int x=5;
Pass p=new Pass();
p.doStuff(x);
System.out.println("main x="+x);
}

void doStuff(int x) {
System.out.println("doStuff x="+ x++);
}
}
What is the result ?
  1. Compilation fails
  2. An exception is thrown at runtime
  3. doStuff x=6 main x=6
  4. doStuff x=5 main x=5
  5. doStuff x=5 main x=6
  6. doStuff x=6 main x=5

Answer : D

Question : 4

Given :
public class ClassA{
public int getValue(){
 int value=0;
 boolean setting=true;
 String title="Hello";
 if(value||(setting && title=="Hello")){return 1;}
 if(value==1 & title.equals("Hello")){return 2;} 
}

public static void main(String[] args){
 ClassA a=new ClassA();
 a.getValue(); 
}
}
What is the result ?
  1. 1
  2. 2
  3. Compilation fails
  4. The code runs with no output
  5. An exception is thrown at runtime

Answer : C

Question : 5

Given :
public class Batman{
int squares=81;
public static void main(String[] args){
new Batman().go();
}

void go(){
 incr(++squares);
 System.out.println(squares);
}
void incr(int squares){ squares+=10;} 
}
What is the result ?
  1. 81
  2. 82
  3. 91
  4. 92
  5. Compilation fails
  6. An exception is thrown at runtime

Answer : B

Question : 6

Given :
class ClassA{
public int numberOfInstances;
protected ClassA(int numberOfInstances){
 this.numberOfInstances=numberOfInstances;
}
}
public class ExtendedA extends ClassA{	
protected ExtendedA(int numberOfInstances) {
 super(numberOfInstances);
}
public static void main(String[] args){
 ExtendedA ext=new ExtendedA(420);
 System.out.println(ext.numberOfInstances);
} 
}
Which statement is true ?
  1. 420 is the output
  2. An exception is thrown at runtime.
  3. All constructors must be declared public.
  4. Constructors CANNOT use the private modifier
  5. Constructors CANNOT use the protected modifier

Answer : A

Question : 7

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.println("1");
if(((distance+1!=tail)^((story*2)==distance)))
System.out.println("2");
}
}
What is result ?
  1. 1
  2. 2
  3. 12
  4. Compilation fails
  5. No output is produced
  6. An exception is thrown at runtime

Answer : E

Question : 8

Given :
class Hexy{
public static void main(String[] args){
 Integer i=42;
 String s=(i<42)?"life":(i>50)?"universe":"everything";
 System.out.println(s);
}
}
What is the result ?
  1. null
  2. universe
  3. everything
  4. Compilation fails
  5. An exception is thrown at runtime.

Answer : D

  • D is correct, this is a ternary nested in a ternary with a little unboxing thrown in. Both of the ternary expressions are false.
  • A, B, C, E, F are incorrect based on the above.

Question : 9

Given :
class Example{
public static void main(String[] args){
 Short s=15;
 Boolean b;
 // insert code here
}
}
?
  1. b=(Number instanceof s);
  2. b=(s instanceof Short);
  3. b=s.instanceof(Short);
  4. b=(s instanceof Number);
  5. b=s.instanceof(Object);
  6. b=(s instanceof String);

Answer : B, D

  • B, D correctly use boxing and instanceof together.
  • A is incorrect because the operands are reversed.
  • C, E use incorrect instanceof syntax.
  • F is wrong because Short isn't in the same inheritance tree as String.

Question : 10

Given :
class Comp{
public static void main(String[] args){
 float f1=2.3f;
 float[][] f2={{42.0f},{1.7f,2.3f},{2.6f,2.7f}};
 float[] f3={2.7f};
 Long x=42L;
 //insert code here
 System.out.println("true");
}
}

And the following five code fragments :
F1. if(f1==f2)
F2. if(f1==f2[2][1])
F3. if(x==f2[0][0])
F4. if(f1==f2[1,1])
F5. if(f3==f2[2])
What is true ?
  1. One of them will compile, only one will be true.
  2. Two of them will compile, only one will be true.
  3. Two of them will compile, Two will be true.
  4. Three of them will compile, only one will be true.
  5. Three of them will compile, exactly two will be true.
  6. Three of them will compile, exactly three will be true.

Answer : D

  • D is correct. Fragments F2, F3, and F5 will compile, and only F3 is true.
  • A, B, C, E, F are incorrect. F1 is incorrect because you can't compare a primitive to an array. F4 is incorrect syntax to access an element of a two-dimensional array.

Question : 11

Given :
class Fork{
public static void main(String[] args){
 if(args.length==1|args[1].equals("test")){
  System.out.println("test case");
 }
 else{
  System.out.println("production :"+args[0]);
 }
}
}

And the command-line invocation :
java Fork live2
What is the result ?
  1. test case
  2. production
  3. test case live2
  4. Compilation fails
  5. An exception is thrown at runtime

Answer : E

  • E is correct. Because the short circuit(||) is not used, both operands are evaluated. Since args[1] is past the args array bounds, an java.lang.ArrayIndexOutOfBoundsException is thrown.
  • A, B, C, D are incorrect based on the above.

Question : 12

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 result ?
  1. 5 1
  2. 5 1
  3. 5 3
  4. 8 1
  5. 8 2
  6. 8 3
  7. 10 2
  8. 10 3

Answer : E

  • E is correct. The first two times the if test runs, both x and y are incremented once (the x++ is not reached untill 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, H are incorrect based on the above.

Question : 13

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.println("alpha");
if((b1=false)|(b1&b3)|(b1|b2))
System.out.println("beta"); 
}
}
What is the result ?
  1. beta
  2. alpha
  3. alpha beta
  4. Compilation fails
  5. No output is produced
  6. An exception is thrown at runtime.

Answer : E

  • 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, F are incorrect based on the above.

Question : 14

Given :
public 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 ?
  1. 9 foo47 86foo
  2. 9 foo47 4244foo
  3. 9 foo425 86foo
  4. 9 foo425 4244foo
  5. 72 foo47 86foo
  6. 72 foo47 4244foo
  7. 72 foo425 86foo
  8. 72 foo425 4244foo
  9. Compilation fails

Answer :G

  • 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, F are incorrect based on the above.

Question : 15

Place the fragments into the code to produce the output 33. Note, you must use each fragment exactly once.
class Incr {
public static void main(String[] args) {
Integer x=7;
int y=2;
x - -;
- - -;
- - -;
- - -;	
System.out.println(x);
}
}
Fragments :
yyyy
yxx
-=*=*=*=
Answer :
class Incr {
public static void main(String[] args) {
Integer x=7;
int y=2;
x*=x;
y*=y;
y*=y;
x-=y;	
System.out.println(x);
}
}
Yeah, we know it's kind of puzzle-y, but you might encounter something like it on the real exam.

Question : 16

1 class Maybe {
2 public static void main(String[] args) {
3 boolean b1=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 is true ?
  1. Line 5 produces true.
  2. Line 5 produces false.
  3. Line 6 produces true.
  4. Line 6 produces false.
  5. Line 7 produces true.
  6. Line 7 produces false.

Answer : A, D, F

  • A, D, F is correct. The ^(xor) returns true if exactly one operand is true. The ! inverts the operand's boolean value. On line 6, b2=true is an assignment not a comparison, and it's evaluated because & does not short-circuit it.
  • B, C, E are incorrect based on the above.

Question : 17

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. 1 2
  3. 2 1
  4. 2 2
  5. 4 1
  6. 4 2
  7. Compilation fails
  8. An exception is thrown at runtime

Answer : F

  • F is correct. The % operator returns the remainder of a division operation.
  • A, B, C, D, E, G, H are incorrect based on the above.

Question : 18

class Scoop{
static int thrower()throws Exception{ return 42;}
public static void main(String[] args){
try{
int x=thrower();	
}catch(Exception e){
x++;
}finally{
	System.out.println("x="+ ++x);
}
}//main
}
What is the result ?
  1. x=42
  2. x=43
  3. x=44
  4. Compilation fails
  5. The code runs with no output

Answer : D

  • D is correct. The variable x is only in scope with in the try code block, it's not in scope in the catch or finally blocks.
  • A, B, C, E are incorrect based on the above.

Question : 19

class Alien{
String invade(short ships){ return "a few"; }
String invade(short... ships){ return "many"; }
}
class Defender{
public static void main(String[] args){
System.out.println(new Alien().invade(7));
}//main
}
What is the result ?
  1. many
  2. a few
  3. Compilation fails
  4. The output is not predictable

Answer : C

  • C is correct, compilation fails. The var-args declaration is fine, but invade takes a short, so the argument 7 needs to be cast to a short. With in the cast, the answer is B, 'a few'

Question : 20

1 class Dims{
2 public static void main(String[] args){
3 int[][] a={{1,2},{3,4}};
4 int[] b=(int[])a[1];
5 Object o1=a;
6 int[][] a2=(int[][])o1;
7 int[] b2=(int[])o1;
8 System.out.println(b[1]);
9 }}
What is the result ?
  1. 2
  2. 4
  3. An exception is thrown at runtime
  4. Compilation fails due to an error on line 4
  5. Compilation fails due to an error on line 5
  6. Compilation fails due to an error on line 6
  7. Compilation fails due to an error on line 7

Answer : C

  • C is correct. A ClassCastException is thrown at line 7 because o1 refers to an int[][] not an int[]. If line 7 was removed, the output would be 4.

Question : 21

class Eggs{
int doX(Long x,Long y){return 1;}
int doX(long... x){return 2;}
int doX(Integer x,Integer y){return 3;}
int doX(Number n,Number m){return 4;}
public static void main(String[] args){
new Eggs().go();
}//main
void go(){
short s=7;
System.out.print(doX(s,s)+" ");
System.out.println(doX(7,7));
}}
What is the result ?
  1. 1 1
  2. 2 1
  3. 3 1
  4. 4 1
  5. 2 3
  6. 3 3
  7. 4 3

Answer : G

  • G is correct. Two rules apply to the first invocation of doX(). You can't widen and then box in one step, and var-args are always chosen last. Therefore you can't widen shorts to either ints or longs, and then box them to Integers and Longs. But you can box shorts to Shorts and then widen them to Numbers, and this takes priority over using a var-arg method. The second invocation uses a simple box from int to Integer.

Question : 22

class Mixer{
Mixer(){}
Mixer(Mixer m){m1=m;}
Mixer m1;
public static void main(String[] args){
Mixer m2=new Mixer();
Mixer m3=new Mixer(m2); m3.go();
Mixer m4=m3.m1; m4.go();
Mixer m5=m2.m1; m5.go();
}
void go(){System.out.print("hi ");}
}
What is the result ?
  1. hi
  2. hi hi
  3. hi hi hi
  4. Compilation fails
  5. hi, followed by an exception
  6. hi hi, followed by an exception.

Answer : F

  • F is correct. The m2 object's m1 instance variable is never initialized, so when m5 tries to use it a NullPointerException is thrown.

Question : 23

1 class Zippy{
2 String[] x;
3 int[] a[]={{1,2},{1}};
4 Object c=new long[4];
5 Object[] d=x;
6 }
What is the result ?
  1. Compilation succeeds.
  2. Compilation fails due only to an error on line 3.
  3. Compilation fails due only to an error on line 4.
  4. Compilation fails due only to an error on line 5.
  5. Compilation fails due to an error on lines 3 and 5.
  6. Compilation fails due to an error on lines 3, 4, and 5.

Answer : A

  • A is correct, all of these array declarations are legal. Lines 4 and 5 demonstrate that arrays.

Question : 24

class Fizz{
int x=5;
public static void main(String[] args){
final Fizz f1=new Fizz();
Fizz f2=new Fizz();
Fizz f3=FizzSwitch(f1,f2);
System.out.println((f1==f3)+" "+(f1.x==f3.x));
}
static Fizz FizzSwitch(Fizz x, Fizz y) {
final Fizz z=x;
z.x=6;
return z;
}}
What is the result ?
  1. true true
  2. false true
  3. true false
  4. false false
  5. Compilation fails.
  6. An exception is thrown at runtime.

Answer : A

  • A is correct. The references f1, z, and f3 all refer to the same instance of Fizz. The final modifier assures that a reference variable cannot be reffered to a different object, but final doesn't keep the object's state from changing.

Question : 25

class Knowing{
static final long tooth=343L;
static long doIt(long tooth){
System.out.print(++tooth+" ");
return ++tooth;
}
public static void main(String[] args){
System.out.print(tooth+" ");
final long tooth=340L;
new Knowing().doIt(tooth);
System.out.println(tooth);
}}
What is the result ?
  1. 343 340 340
  2. 343 340 342
  3. 343 341 342
  4. 343 341 340
  5. 343 341 343
  6. Compilation fails
  7. An exception is thrown at runtime

Answer : D

  • D is correct. There are three different long variables named tooth. Remember that you can apply the final modifier to local variables, but in this case the 2 versions of tooth marked final are not changed. The only tooth whose value changes is the one not marked final. This problem demonstrates a bad practice known as shadowing.

Question : 26

class Bigger{
public static void main(String[] args){
// insert code here
}
}
class Better{
enum Faster{ Higher, Longer };
}
Which, inserted independently at line 3, will compile ?
  1. Faster f=Faster.Higher;
  2. Faster f=Better.Faster.Higher;
  3. Better.Faster f=Better.Faster.Higher;
  4. Bigger.Faster f=Bigger.Faster.Higher;
  5. Better.Faster f2; f2=Better.Faster.Longer;
  6. Better b; b.Faster=f3; f3=Better.Faster.Longer;

Answer : C, E

  • C and E are correct syntax for accessing an enum from another class.
BACK