Saturday, October 12, 2013

SCJP Questions


1)


class Mammal{
String name="furry";
String makeNoise(){ return "generic noise";}

}
class Zebra extends Mammal{

Sring name="stripes";
String makeNoise(){return "bray";}

}

public class ZooKeeper{
public static void main(String [] args){ new ZooKeeper().go();}

void go(){

Mammal m = new Zebra();
System.out.println(m.name + m.makeNoise());
}

}

What is the Result ?
2)SCJP:

public class Dark{

    int x =3;
    public static void main(String[] args){

        new Dark().go1();
    }

   void go1(){

       int x;
       go2(++x);
   }

  void go2(int y ){

     int x = ++y;
    System.out.println(x);
  }

}

What is the Result ?

3)
import java.io.*;
class Player {
Player() { System.out.print("p"); }
}
class CardPlayer extends Player implements Serializable {
CardPlayer() { System.out.print("c"); }
public static void main(String[] args) {
CardPlayer c1 = new CardPlayer();
try {
FileOutputStream fos = new FileOutputStream("play.txt");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(c1);
os.close();
FileInputStream fis = new FileInputStream("play.txt");
ObjectInputStream is = new ObjectInputStream(fis);
CardPlayer c2 = (CardPlayer) is.readObject();
is.close();
} catch (Exception x ) { }
}
}
4)
import java.io.*;

class Keyboard { }
public class Computer implements Serializable {
  private Keyboard k = new Keyboard();
  public static void main(String[] args) {
    Computer c = new Computer();
    c.storeIt(c);
  }
  void storeIt(Computer c) {
    try {
      ObjectOutputStream os = new ObjectOutputStream(
         new FileOutputStream("myFile"));
      os.writeObject(c);
      os.close();
      System.out.println("done");
    } catch (Exception x) {System.out.println("exc"); }
  }
}
 
5)
What is output ?

class Cat { }
class Dog {
public static void main(String [] args) {
Dog d = new Dog();
System.out.println(d instanceof Cat);
}
}
 
6)
 

class Boxing2 {
static Integer x;
public static void main(String [] args) {
doStuff(x);
}
static void doStuff(int z) {
int z2 = 5;
System.out.println(z2 + z);
} }
 
7)
package javaimp;
   class Ouch {
   static int ouch = 7;
    public static void main(String[] args) {
     new Ouch().go(ouch);
      System.out.print(" " + ouch);
    }
    void go(int ouch) {
     ouch++;
     for(int ouch = 3; ouch < 6; ouch++)
       ;
     System.out.print(" " + ouch);
   }
 }
What is the result?
  1. 5 7
  2. 5 8
  3. 8 7
  4. 8 8
  5. Compilation fails

8)

class MyOuter2{
private String x = "Outer2";
void doStuff(){
String z = " local variable";
class MyInner{

public void seeOuter(){

System.out.println("Outer x is "+x);
System.out.println("Local var z is "+z);
}
}
}
}

What is the result ?
 
 
 

No comments:

Post a Comment