You must have a Javascript-aware web browser in order to take this mock correctly or you won't be able to check your answers.
Please be advised that the questions are deliberately not easy so don't take it until you think you are ready :)
Question 1. What is the output of the following code when compiled and run? Select two correct answers.
public class Question01 {
	public static void main(String[] args){
		int y=0;				//line 1
		int x=z=1;				//line 2
		System.out.println(y+","+x+","+z);	//line 3
	}
}
A. Prints 0,1,1
B. Error during compilation at line 1
C. Prints 0,0,1
D. Error during compilation at line 3
E. Error during compilation at line 2

Question 2. What is the output of the following code when compiled and run? Select one correct answer.
public class Question02 {
	public static void main(String[] args){
		int j = 017;
		int i = (byte)j >> 2;				//line 1
		System.out.println(Integer.toBinaryString(i));	//line 2
	}
}
A. Prints 3
B. Error during compilation at line 1
C. Error during compilation at line 2
D. Prints 11
E. Prints 0

Question 3. Select one correct statement regarding the code given below.
import java.awt.*;
public class Question03 {
	public static void main(String[] args){
		Frame f = new Frame("Question 3");
		f.setLayout(new GridLayout(2,3));
		for(int i=0;i<(int)Math.round(6.51);i++){
			f.add(new Button(""+(i+1)));
		}
		f.pack();
		f.validate();
		f.setVisible(true);
	}
}
A. Compiles fine, a new frame appears and it looks like this:
1357
246
B. Compiles fine, a new frame appears and it looks like this:
123
456
C. Compiles fine, a new frame appears and it looks like this:
0123
456
D. Compiles fine, a new frame appears and it looks like this:
1234
567
E. The code doesn't compile.

Question 4. Select three correct statements.
A. The garbage collection thread will die just after the last user thread dies.
B. The garbage collection can be forced by invoking System.gc().
C. The garbage collection thread is a non-deamon thread.
D. The finalize() method is invoked at most once by the JVM for any given object.
E. The finalize() method may resurrect the object upon which it has been invoked.

Question 5. What is the output of the following code when compiled and run? Select one correct answer.
import java.io.*;
public class Question05 {
	public static void main(String[] args) {
		Question05Sub myref = new Question05Sub();
		try{
			myref.test();
		}catch(IOException ioe){}
	}
	void test() throws IOException{
		System.out.println("In Question05");
		throw new IOException();
	}
}
class Question05Sub extends Question05 {
	void test() {
		System.out.println("In Question05Sub");
	}
}
A. Prints:
	In Question05Sub
B. Prints:
	In Question05
C. Prints:
	In Question05
In Question05Sub
D. Prints:
	In Question05Sub
In Question05
E. The code does not compile.

Question 6. What is the output of the following code when compiled and run with the following command line: java Question06 two three? Select two correct answers.
public class Question06 {
	public static void main(String[] args) throws Exception {
		int i=2;
		boolean b = true;
		throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2));
	}
}
A. The exception message is Values are:false,3,true
B. The exception message is Values are:true,2,false
C. The exception message is Values are:false,2,true
D. The final value of b is false
E. An exception is thrown at runtime.

Question 7. Select two correct statements about the code given below?
class A{}
class B extends A implements E{}	//line 1
class C extends A{}
class D extends B{}
interface E{}
public class Question07 {
	public static void main(String[] args) {
		A a = new D();	//line 2
		C c = new C();	//line 3
		E e = (E)a;	//line 4
		B b = (B)e;	//line 5
	}
}
A. The code compiles without error and runs fine.
B. Compilation error on line 1 because interface E is not yet declared (forward-referencing).
C. Compilation error on line 4 because class A does not implement interface E.
D. The cast on line 4 is mandatory.
E. The cast on line 5 is not mandatory.

Question 8. How many objects are eligible for garbage collection immediately after line 1? Select one correct answer.
public class Question08 {
	public static void main(String[] args) throws Exception {
		Question08 q08 = new Question08();
		q08.doSomething();	//line 1
		Thread.sleep(20000);
	}

	public void doSomething(){
		Object[] objArray = new Object[2];
		for(int i=0;i<objArray.length;i++){
			objArray[i] = new Object();
		}
	}
}
A. 0
B. 1
C. 2
D. 3
E. 4

Question 9. What is the output of the following code when compiled and run? Select one correct answer.
public class Question09 {
	public static void main(String[] args) {
		try {
			int i = (int)(Math.random()*10);
			if(i<=5)
				System.out.println("i = "+i);
			else
				throw new Exception("i > 5");
		} catch (Exception e){
			System.err.println(e.getMessage()+" (i="+i+")");
		}
	}
}
A. The output cannot be determined.
B. Compilation error.
C. An exception is thrown at runtime.
D. Output is i = 2
E. Output is i > 5 (i=6)

Question 10. What is the output of the following code when compiled and run? Select one correct answer.
public class Question10 {
	public static void main(String[] args) {
		new Question10().doSomething();
	}

	public void doSomething(){
		int i=5;
		Thread t = new Thread(new Runnable(){
			public void run(){
				for(int j=0;j<=i;j++){
					System.out.print(" "+j);
				}
			}
		});
		t.start();
	}
}
A. Prints 0 1 2 3 4
B. Compilation error.
C. No output.
D. IllegalThreadStateException is thrown at runtime.
E. Prints 0 1 2 3 4 5

Question 11. What is the output of the following code when compiled and run? Select one correct answer.
public class Question11 {
	public static void main(String[] args) {
		StringBuffer buf1 = new StringBuffer("Hello W");
		addSomeStuff(buf1);
		System.out.println(buf1.toString());
	}

	public static void addSomeStuff(StringBuffer buf){
		StringBuffer b = buf.replace(6,10,"orld");
		System.out.println(b.delete(0,1).toString());
	}
}
A. Prints
	Hello World
Hello World
B. Prints
	Hello orld
Hello orld
C. Prints
	Hello orld
ello orld
D. Prints
	ello orld
ello orld
E. Compilation error.

Question 12. What is the output of the following code when compiled and run? Select two correct answers.
import java.io.*;
public class Question12 {
	public static void main(String[] args) throws IOException{
		RandomAccessFile raf = new RandomAccessFile("test.dat","rw");
		raf.writeInt(0x01);
		raf.writeShort(0x23);
		raf.writeByte(0x45);
		raf.seek(3);
		short a1 = raf.readShort();
		short a2 = raf.readShort();
		System.out.println("0x"+Integer.toHexString(a1)+", 0x"+Integer.toHexString(a2));
	}
}
A. EOFException thrown at runtime because we are trying to read beyond the end of the file.
B. Prints 0x100, 0x2345
C. Prints 0x1, 0x23
D. Prints 0x0, 0x100
E. The code compiles fine.

Question 13. What is the output of the following code when compiled and run? Select two correct answers. (Note: when an instance of a Vector is printed, its content appear between square brackets [])
import java.util.*;
public class Question13 {
	public static void main(String[] args) {
		Vector col = new Vector();
		col.add(new Integer(1));
		col.add(new Integer("2"));
		col.add(new Float(3.2d));	//line 1
		col.add(col.elementAt(1));
		col.setElementAt(col.elementAt(2),0);
		System.out.println(col);
	}
}
A. Compilation error on line 1.
B. Only line 1 won't compile.
C. The code compiles and runs fine.
D. Prints [3.2, 2, 3.2, 2]
E. Prints [1, 2, 3.2, 2]

Question 14. A FileInputStream is created as shown in the code below. Select two correct statements.
FileInputStream out = new FileInputStream("test14.dat");
A. If the file "test14.dat" does not exist, then one is created in the directory where the application is residing.
B. The code may throw a FileNotFoundException.
C. Instead of specifying the file name as a String, we also have the possibility of providing a File object or a RandomAccessFile object.
D. out can then be used to read from the specified file.
E. out cannot be chained to another InputStream.

Question 15. What is the output of the following code when compiled and run? ('\u0048' is 'H' and '\u000a' is a linefeed (LF)). Select two correct answers.
public class Question15 {
	public static void main(String[] args) {
		String s = "\u0048ello\u000aWorld!";
		System.out.println(s);
	}
}
A. Prints:
	Hello
World!
B. Prints Hello World!(linefeed is ignored).
C. Compilation error.
D. Compiles fine, but an exception is thrown at runtime.
E. Unicode characters like '\u0048' may be used to create String literals.

Question 16. What is the output of the following code when compiled and run? Select two correct answers.
public class Question16 {
	public static void main(String[] args) {
		int i = 2;
		try {
			if((i/=(int)Math.floor(Math.random())) > 1)
				System.out.println("No arithmetic exception");
		} catch (ArithmeticException ae){
			System.err.println("Arithmetic exception caught");
		}
	}
}
A. Compilation error.
B. No arithmetic exception will never be printed.
C. The final value of i is 2.
D. The final value of i is 0.
E. The try-catch block is necessary for the code to compile.

Question 17. What is the output of the following code when compiled and run? Select two correct answers.
public class Question17 {
	public static void main(String[] args) throws Exception{
		Thread t1 = new Thread(getRunnable(3));
		Thread t2 = new Thread(getRunnable(4));
		t1.join();
		System.out.println("End");
	}

	public static Runnable getRunnable(final int id){
		return new Runnable(){
			public void run(){
				for(int i=0;i<id;i++){
					System.out.print(" "+i);
				}
			}
		};
	}
}
A. The output will always be the same whatever the underlying platform is.
B. Prints: End
C. Prints:
	0 1 2
End
D. The output cannot be determined.
E. Compilation error.

Question 18. What is the output of the following code when compiled and run? Select two correct answers.
public class Question18 {
	public static void main(String[] args){
		for(int i=0;i<10;i++){
			System.out.println(getPrimitive(127));	//line 1
		}
	}
	public static int getPrimitive(byte b){	//line 2
		return (short)(Math.random()*b);	//line 3
	}
}
A. Compilation error on line 1.
B. Compilation error on line 2.
C. Compilation error on line 3.
D. Line 3 compiles fine.
E. Prints 10 random numbers between 0 and 127.

Question 19. Select three correct statements.
A. A static method may override another static method.
B. A static method cannot override a non-static method.
C. A non-static method cannot override a static method.
D. A non-static method may be overloaded by a static method.
E. A synchronized method cannot be overridden.

Question 20. Select three correct statements about the following code.
public class Question20 {
	public static void main(String[] args) {
		Question20 myref = new Question20Sub();
		try{
			myref.test();
		}catch(Exception e){}
	}
	void test() throws Exception{
		System.out.println("In Question20");
		throw new Exception();
	}
}
class Question20Sub extends Question20 {
	void test() {
		System.out.println("In Question20Sub");
	}
}
A. The try-catch block that encloses myref.test(); is mandatory for the code to compile.
B. Prints: In Question20
C. Prints: In Question20Sub
D. Method test() in class Question20Sub has no obligation to declare a throws clause.
E. An exception is thrown at runtime.

Question 21. Select one correct statement about the following code.
public class Question21 {
	public static void main(String[] args) {
		int i=3;
		System.out.println(getBoolean()? i=2*i++:i+++ ++i);
	}
	//Heads or tail?
	public static boolean getBoolean(){
		if((int)(Math.random()*2)==0)
			return false;
		else
			return true;
	}
}
A. Prints randomly 6 or 8 at each execution.
B. Prints randomly 7 or 8 at each execution.
C. Always prints 6 at each execution.
D. Prints randomly 6 or 11 at each execution.
E. Compilation error.

Question 22. Select two correct statements about the following code.
import java.awt.*;
public class Question22 extends Frame{
	public Question22(String name){ super(name); }
	public static void main(String[] args) {
		Question22 q22 = new Question22("Question 22");
		q22.setLayout(new BorderLayout());
		q22.add(new Button("CENTER"),BorderLayout.NORTH);
		q22.add(new Button("EAST"),BorderLayout.SOUTH);
		q22.add(new Label("WEST"));
		q22.add(new Button("EAST"),BorderLayout.WEST);
		q22.add(new Button("SOUTH"),BorderLayout.NORTH);
		q22.add(new Button("NORTH"),BorderLayout.EAST);
		q22.pack();
		q22.validate();
		q22.setVisible(true);
	}
}
A. There is no subclass of Component in the center region of the frame.
B. In the north region of the frame, we see a button whose label is CENTER.
C. Two visible buttons have the word EAST on their label.
D. Four buttons are displayed within the frame.
E. Compilation error.

Question 23. What happens when class Question23 is compiled and run? (Note: the code is in the file Question23.java). Select two correct answers.
public class Question23 {
	public static void main(String[] args){}
	public void doSomething(){
		System.out.println("In Question23");
	}
}
class Question23Sub extends Question23 {
	//main method
	public static void main(String[] args) {
		Question23 q23 = new Question23();
		q23.doSomething();
	}
	public void doSomething(){
		System.out.println("In Question23Sub");
	}
}
A. Nothing is printed.
B. Prints: In Question23
C. Prints: In Question23Sub
D. Compilation error.
E. The method main can be hidden.

Question 24. Select two correct statements about the following code.
public class Question24 {
	public static void main(String[] args){
		Question24 q24 = null;
		int i = q24.throwDice(); //line 1
		int j = getDice().throwDice(); //line 2
		System.out.println("Result: "+i+","+j);
	}
	private static int throwDice(){
		return 1+(int)(Math.random()*6);
	}
	private static Question24 getDice(){
		return null;
	}
}
A. Compilation error.
B. The code compiles but throws a NullPointerException at line 1 when run.
C. The code compiles but throws a NullPointerException at line 2 when run.
D. The program will always print Result: x,y, where x and y are two numbers between 1 and 6.
E. The code compiles fine.

Question 25. What is the output of the following code when compiled and run? Select two correct answers.
public class Question25 {
	static class Question25Inner {private int i=2;}
	static Question25Inner inner = new Question25Inner();
	public static void main(String[] args){
		Question25[] q25 = new Question25[2];
		for(int i=0;i<q25.length;i++)
			q25[i] = new Question25();
		q25[0].inner.i = 3;
		System.out.println(q25[0].inner.i+","+q25[1].inner.i);
	}
}
A. Prints: 3,2
B. Compilation error (i is not accessible).
C. Prints: 3,3
D. There are q25.length instances of class Question25Inner.
E. Question25Inner is called a "top-level nested class".

Question 26. Select one correct statement about the following code.
import java.util.*;
public class Question26 {
	public static void main(String[] args){
		Set s1 = new HashSet(3);
		Set s2 = new HashSet(3);
		s1.add("string 1");
		s1.add(null);
		s1.add("string 2");
		s1.add(null);
		s2.add("string 3");
		s2.add(null);
		s2.add("string 2");
		s2.add(null);
		s1.retainAll(s2);
		System.out.println(s1);
	}
}
A. NullPointerException thrown at runtime.
B. The output is composed of the tokens string 1, string 2 and null (maybe in a different order).
C. Compilation error.
D. The output is composed of the tokens string 2 and null (maybe in a different order).
E. The output is composed of the tokens string 2, null and null (maybe in a different order).

Question 27. What is the output of the following code when compiled and run? Select two correct answers.
import java.io.*;
public class Question27 {
	public static void main(String[] args) throws IOException{
		FileOutputStream fos = new FileOutputStream("test27.dat");
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		DataOutputStream dos = new DataOutputStream(bos);
		dos.writeByte(0xFF);
		dos.writeShort(0xFF00);
		dos.writeInt(0x12345678);
		dos.flush();
		dos.close();
		FileInputStream fis = new FileInputStream("test27.dat");
		BufferedInputStream bis = new BufferedInputStream(fis);
		DataInputStream dis = new DataInputStream(bis);
		short i = dis.readShort();
		byte j = dis.readByte();
		int k = dis.readInt();
		System.out.println(i & j);
		dis.close();
	}
}
A. Prints: -1
B. Prints: 0
C. Prints a large negative number.
D. Compilation error.
E. The file test27.dat contains 7 bytes.

Question 28. Select one correct statement about the following code.
public class Question28 {
	public static void main(String[] args){
		int j=2;
		//line 1
		lab1:for(int i=1;i<j++;i++){
			//line 2
			lab2:for(int k=0;k<j--;k++){
				//line 3
				if(j>k/i)
					break lab1;
			}
		}
		System.out.println(j);
	}
}
A. Prints: 0
B. Prints: 1
C. Prints: 2
D. Prints: 3
E. Compilation error.

Question 29. What is the output of the following code when compiled and run? Select two correct answers.
import java.awt.*;
public class Question29 extends Frame{
	private Container getContainer(){return this;}
	public Question29(String name){
		super(name);
		this.setLayout(new BorderLayout());
		this.add(new Button("B1"),BorderLayout.NORTH);
		this.add(new Button("B2"),BorderLayout.SOUTH);
		this.add(this.getContainer(),BorderLayout.CENTER);
		this.add(new Button("B3"),BorderLayout.EAST);
		this.add(new Button("B4"),BorderLayout.WEST);
		this.add(new Button("B5"),BorderLayout.WEST);
		this.pack();
		this.validate();
		this.setVisible(true);
	}
	public static void main(String[] args){
		Question29 q29 = new Question29("Question 29");
	}
}
A. Compilation error.
B. Compiles fine but throws an exception at runtime since you cannot add two buttons to the same region (B4 and B5).
C. Four buttons B1 to B4 are displayed respectively in the north, south, east and west regions of the frame.
D. q29 instanceof Component yields true.
E. Compiles fine but throws an exception at runtime since you cannot add a frame to itself.

Question 30. Select one correct statement about the following code.
class Question30Super{
	public static void printSomething(){
		System.out.println("In Question30Super");
	}
}
public class Question30 extends Question30Super {
	public static void main(String[] args){
		Question30Super q30 = new Question30();
		q30.printSomething();
	}
	public static void printSomething(){
		System.out.println("In Question30");
	}
}
A. Prints: In Question30Super
B. Prints: In Question30
C. Compilation error.
D. Compiles fine but an exception is thrown at runtime.
E. None of the above.

Question 31. Select two correct statements.
A. A static method method can declared abstract.
B. A protected method can be overridden to have "default" accessibility (that is, no accessibility modifier).
C. A abstract method can be declared private.
D. A synchronized method can be overridden by a non-synchronized one.
E. A native method can be declared static.

Question 32. Select one correct statement about the following code.
public class Question32{
	public static void main(String[] args){
		String[] s1 = new String[]{null,"Hello"};
		String s2 = s1[1].intern();
		String s3 = s2.toString();
		if(s3 == s2)
			System.out.println("Equal");
	}
}
A. Compilation error.
B. Prints nothing.
C. Prints: Equal
D. A NullPointerException is thrown at runtime.
E. It is possible to change the content of a String object after the latter's creation.

Question 33. What is the output of the following code when compiled and run? Select two correct answers.
class MyRunnable implements Runnable {
	int i=0;
	static int j=0;
	public void run(){
		int k=0;
		increment(k);
		System.out.println(k); //line 1
	}
	private void increment(int k){i++; j++; k++;}
}
public class Question33{
	public static void main(String[] args) throws Exception{
		MyRunnable r = null;
		Thread t1 = new Thread(r=new MyRunnable());
		Thread t2 = new Thread(r);
		t1.start();	t2.start();
		t2.join();	t1.join();
		System.out.println(r.i+","+r.j); //line 2
	}
}
A. A NullPointerException is thrown at runtime.
B. The statement on line 1 always prints 1.
C. The statement on line 1 always prints 0.
D. The value of j on line 2 is 2.
E. The value of i on line 2 is 1.

Question 34. What is the output of the following code when compiled and run? Select one correct answer.
public class Question34{
	public static void main(String[] args){
		long i = 0x181>>010<<03;
		System.out.println(i ^ 0);
	}
}
A. Prints a big negative number.
B. Prints: 8
C. Prints: 0
D. Compilation error.
E. None of the above.

Question 35. What is the output of the following code when compiled and run? Select one correct answer.
import java.awt.*;
public class Question35 extends Frame{
	public Question35(String name){
		super(name);
		this.setLayout(new GridLayout(3,3));
		for(int i=0;i<9;i++)
			if(i%2==0)
				this.add(new Button("B"+(i+1)));
		this.pack();
		this.validate();
		this.setVisible(true);
	}
	public static void main(String[] args){
		Question35 q35 = new Question35("Question 35");
	}
}
A. Compiles fine, a new frame appears and it looks like this:
B0B2B4
B6B8
B. Compiles fine, a new frame appears and it looks like this:
B1B3B5
B7B9
C. Compiles fine, a new frame appears and it looks like this:
B1B3
B5B7
B9
D. Compiles fine, a new frame appears and it looks like this:
B0B2
B4B6
B8
E. The code doesn't compile.

Question 36. Select two correct statements about the following code.
abstract class F{}
class G extends F{}
class H extends G{}
public class Question36 {
	public static void main(String[] args){
		F[] f1 = new G[2];
		F[] f2 = new H[0];	//line 1
		G[] g1 = new H[1];
		H[] h1 = (H[])f2;	//line 2
		G[] g2 = (G[])f1;  //line 3
		G[] g3 = h1; //line 4
		f1 = (F[])g1; //line 5
	}
}
A. Compilation error on line 3.
B. The explicit cast on line 2 is needed for the code to compile.
C. The explicit cast on line 5 is needed for the code to compile.
D. A ClassCastException is thrown at runtime.
E. The array creation expression on line 1 is legal even if the given size is 0.

Question 37. What is the output of the following code when compiled and run? Select two correct answers.
//file Question37Super.java
package A;
public class Question37Super {
	protected int i=2;
}
//file Question37Sub.java
package B;
import A.*;
public class Question37Sub extends Question37Super {
	public static void main(String[] args){
		Question37Sub q37sub = new Question37Sub();
		q37sub.goFetch();
	}
	public void goFetch(){
		System.out.println("in goFetch(): i="+i);
		class GoFetchLocal{
			public void goFetch(){
				System.out.println("in GoFetchLocal.goFetch(): i="+i); //line 1
			}
		}
		new GoFetchLocal().goFetch();
	}
}
A. Compilation error at line 1 since i is not accessible in class GoFetchLocal.
B. Prints:
	in goFetch(): i=2
in GoFetchLocal.goFetch(): i=2
C. Compiles fine but IllegalAccessError is thrown at runtime.
D. GoFetchLocal is called a local inner class.
E. GoFetchLocal is called a local nested class.

Question 38. Select one correct statement about the following code.
public class Question38 {
	public static void main(String[] args){
		while(false);		//line 1
		if(false);		//line 2
		do{}while(false);	//line 3
		for(;false;);		//line 4
	}
}
A. Compilation error on lines 1,2 and 4.
B. Compilation error on lines 2,3 and 4.
C. Compilation error on lines 1,2 and 3.
D. Compilation error on lines 1 and 4.
E. The code compiles and runs fine.

Question 39. What is the output of the following code when compiled and run? Select one correct answer.
class A{
	int i;
	A(){}
}
class B extends A{
	B(int i){ this((short)i); }
	private B(short j){i=j;}
}
public class Question39 extends B{
	Question39(int i){super(i);}
	public static void main(String[] args){
		System.out.println(new Question39(3).i);	//line 1
	}
}
A. Prints: 0
B. Prints: 3
C. Prints nothing.
D. Compilation error.
E. An IllegalAccessError is thrown at runtime because i is not accessible on line 1.

Question 40. Select one correct statement about the following code.
public class Question40{
	int i;
	String name;
	Question40(int j){
		i=j;//line 1
		this(j+i+" Question40");//line 2
	}
	private Question40(String n){name = n;}
	public static void main(String[] args){
		System.out.println(new Question40(2));
	}
	public String toString(){return name;}
}
A. Prints: 22 Question40
B. Prints: 4 Question40
C. Prints something like Question40@17d257 (17d257 may be different depending on the platform).
D. Compilation error on line 1.
E. None of the above.

Question 41. What is the output of the following code when compiled and run? Select one correct answer.
public class Question41{
	public static void main(String[] args){
		Object[] obj = new Object[3];
		for(int i=0;i<obj.length;i++)
			obj[i] = (i%2==0)?new Object():obj[i-1];
		if(obj[0] == obj[1] &  (obj[1]=obj[2])!=null)
			System.out.print("1 ");
		if(obj[1] == obj[2] && (obj[2]=obj[0])!=null)
			System.out.print("2 ");
		if(obj[1] == obj[0] || (obj[0]=obj[1])==null)
			System.out.print("3 ");
		if(obj[2] == obj[0] |  (obj[0]=obj[2])!=null)
			System.out.print("4 ");
		System.out.println((obj[0]==obj[1])+" "+(obj[1]==obj[2])+" "+(obj[0]==obj[2]));
	}
}
A. Compilation error.
B. Prints: 1 3 4 true false true
C. Prints: 1 2 4 false false true
D. Prints: 3 4 true true true
E. None of the above.

Question 42. What is the output of the following code when compiled and run? Select one correct answer.
public class Question42{
	Question42(byte b){System.out.println("in byte");}
	Question42(short s){System.out.println("in short");}
	Question42(char c){System.out.println("in char");}
	Question42(int i){System.out.println("in int");}
	public static void main(String[] args){
		Question42 q42 = new Question42(2);
	}
}
A. Prints: in int
B. Prints: in char
C. Prints: in byte
D. Prints: in short
E. Compilation error because of ambiguous constructor invocation.

Question 43. What is the output of the following code when compiled and run? Select two correct answers.
public class Question43{
	static int i=1;
	int j = k;	//line 1
	static int k=2;
	public static void main(String[] args){
		Question43 q43 = new Question43();
		System.out.println(q43.i+" "+q43.j+" "+q43.k);
	}
}
A. Compilation error because of illegal forward reference on line 1.
B. Prints: 1 0 2
C. Prints: 1 2 2
D. The code compiles correctly and runs fine.
E. The code would compile if j was declared static too.

Question 44. Select one correct statement about the following code.
public class Question44{
	public static void main(String[] args){
		String[] str = new String[0];
		int[] i = new int[0];
		aMethod(str);	//line 1
		bMethod(i);		//line2
	}
	public static void aMethod(Object[] o){
		System.out.print("in aMethod");
	}
	public static void bMethod(long[] l){
		System.out.println(" in bMethod");
	}
}
A. Declaring an array of length 0 is illegal in Java. (How good is an array if we can't put any elements in it?)
B. The code compiles fine and prints in aMethod in bMethod when run.
C. A ClassCastException is thrown at runtime.
D. Compilation error at line 1.
E. Compilation error at line 2.

Question 45. What is the output of the following code when compiled and run? Select two correct answers.
public abstract class Question45{
	public static void main(String[] args){
		Question45.bMethod(); //line 1
	}
	public static void bMethod(){
		System.out.println("in bMethod");
	}
}
A. Class Question45 cannot be declared abstract since it doesn't have any abstract methods.
B. Prints: in bMethod
C. Compilation error on line 1 since we cannot invoke methods of an abstract class.
D. The code compiles and runs fine.
E. Method bMethod is implicitely abstract since class Question45 is abstract.

Question 46. What is the output of the following code when compiled and run? Select one correct answer.
public class Question46{
	public static void main(String[] args) throws Exception{
		try{
			int i=5/(int)Math.random();
			System.out.println("i="+i);
		} catch(ArithmeticException ae) {
			throw new Exception("thrown from first catch");
		} catch(Exception e){
			throw new Exception("thrown from second catch");
		} finally {
			return;
		}
	}
}
A. An exception is thrown and the exception message thrown from first catch is diplayed with the exception stack trace.
B. An exception is thrown and the exception message thrown from second catch is diplayed with the exception stack trace.
C. Prints nothing.
D. Compilation error.
E. Prints i=? where ? stands for the generated random value.

Question 47. What is the output of the following code when compiled and run? Select one correct answer.
class A{
	int i=0;
	public synchronized void aMethod(){
		System.out.print(i+++" "); //line 1
	}
}
public class Question47 implements Runnable{
	A a = new A();
	public static void main(String[] args) throws Exception {
		Thread t1 = new Thread(new Question47());
		Thread t2 = new Thread(new Question47());
		t1.start();
		t1.join();
		t2.start();
	}
	public void run(){
		for(int i=0;i<3;i++)
			a.aMethod();
	}
}
A. Prints: 0 1 2 3 4 5
B. Prints: 0 1 2 0 1 2
C. Prints: 1 2 3 4 5 6
D. Prints the digits 0 1 2 3 4 5 in a random order.
E. Compilation error.

Question 48. What is the output of the following code when compiled and run? Select one correct answer.
public class Question48{
	public static void main(String[] args) {
		int i = 4*6-3/2<<2*5>>>1%2-4^3;
		System.out.println(i);
	}
}
A. Prints: 0
B. Prints: 1
C. Prints: 2
D. Prints: 3
E. Compilation error.

Question 49. What is the output of the following code when compiled and run? Select one correct answer.
public class Question49 {
	public static void main(String[] args) {
		boolean b = false;
		String s = (b=!b)?(b=!b)?"Hello":"hello":(b=!b)?"world":"World";
		System.out.println(s);
	}
}
A. Prints: Hello
B. Prints: hello
C. Prints: Helloworld
D. Prints: helloWorld
E. Compilation error.

Question 50. Select two methods that correctly override the following method? (Assume that all necessary packages have been imported).
protected int aMethod(int i) throws IOException {...}
A. int aMethod(int i) throws IOException{...}
B. protected int aMethod(int i) throws FileNotFoundException{...}
C. public int aMethod(int i) throws Exception{...}
D. public int aMethod(byte i) throws IOException{...}
E. protected int aMethod(int i) throws IOException,FileNotFoundException{...}

Question 51. What is the output of the following code when compiled and run? Select one correct answer.
public class Question51 {
	public static void main(String[] args) {
		switch(throwDice()){
			case 1:
			case 2:
			case 3: System.out.println("low");
			case 4:
			case 5:
			case 6: System.out.println("high"); break;
		}
	}
	public static long throwDice(){
		return (int)(Math.random()*7);
	}
}
A. If low is part of the output, then high will also be.
B. high will always be part of the output.
C. low will always be part of the output.
D. If high is part of the output, then low will also be.
E. Compilation error.

Question 52. What is the output of the following code when compiled and run? Select two correct answers.
class A {
	static int i=0;
	public synchronized void aMethod(){ i++; }
	public void bMethod(){
		synchronized(this){ i--; }
	}
}
public class Question52 implements Runnable {
	A a = new A();
	public static void main(String[] args) {
		Thread t1 = new Thread(new Question52());
		Thread t2 = new Thread(new Question52());
		t1.start(); t2.start();
	}
	public void run(){
		for(int i=0;i<10000000;i++){
			a.aMethod(); a.bMethod();
		}
		System.out.print("a.i="+a.i+" ");
	}
}
A. The output will always be a.i=0 a.i=0
B. The output cannot be determined.
C. The output will always be a.i=0 a.i=-1
D. t1 and t2 access the same member variable a.i.
E. Compilation error.

Question 53. What is the output of the following code when compiled and run? Select one correct answer.
class A{}
class B extends A{}
class C extends B{}
class D extends A{}
public class Question53 {
	static A a1,a2,a3;
	public static void main(String[] args) {
		if((a1=new D()) instanceof C && (a2=new B()) instanceof A)
			System.out.print("1 ");
		if(a2 instanceof B & (a3=new C()) instanceof A)
			System.out.print("2 ");
		if(a3 instanceof B | (a2=new D()) instanceof A)
			System.out.print("3 ");
		if(a2 instanceof C || (a2=new D()) instanceof C)
			System.out.print("4 ");
	}
}
A. Prints : 1 2
B. 3 4
C. 1 3 4
D. 3
E. Compilation error.

Question 54. What is the output of the following code when compiled and run? Select two correct answers.
public class Question54 {
	static int i;
	int j;
	static {
		System.out.print("("+i+")");
		i=3;
		System.out.print("("+i+")");
	}
	{
		System.out.print("("+i+","+j+")");
		j=2;
		System.out.print("("+i+","+j+")");
	}
	Question54(){
		System.out.print("("+i+","+j+")");
	}
	public static void main(String[] args) {
		System.out.print("("+i+")");
		Question54 q54 = new Question54();
		System.out.print("("+q54.i+","+q54.j+")");
	}
}
A. Prints: (3)(3)(3)(3,2)(3,2)(3,2)(3,2)
B. Prints: (0)(3)(3)(3,0)(3,2)(3,2)(3,2)
C. Prints: (0)(3,0)(3,2)(3)(3)(3,2)(3,2)
D. Prints: (3)(3,3)(3,2)(3)(3)(3,2)(3,2)
E. The code compiles and runs fine.

Question 55. What is the output of the following code when compiled and run? Select one correct answer.
public class Question55 {
	public static void main(String[] args) {
		Thread t = new Thread(new Runnable(){
			static int i;
			static {i=0;}
			public void run(){
				for(int j=0;j<5;j++)
					System.out.println(i=i++);
			}
		});
		t.start();
	}
}
A. Prints: 01234
B. Prints: 12345
C. Prints: 00000
D. Prints: 11111
E. Compilation error.

Question 56. What is the output of the following code when compiled and run? Select two correct answers.
public class Question56 {
	public static void main(String[] args) {
		int i=0;
		while(i++<5){
			switch(i%2){
				default:System.out.print("default ");break;
				case 1:System.out.print("1 ");break;
			}
		}
	}
}
A. Prints: 1 default 1 default 1
B. Prints: default 1 default 1 default
C. Prints: 1 1 default 1 default
D. Prints: default default 1 default 1
E. The code compiles and runs fine.

Question 57. Which of the following statements are infinite loops? Select two correct answers.
A. for(int i=10;i>0;i-=-2);
B. for(int i=0;i<012;i=i++);
C. for(int i=0;i<0;i--);
D. for(int i=0;(i++^--i)==0;i++);
E. for(int i=010;i==10;i+=0);

Question 58. Select two methods that correctly overload the following method? (Assume that all necessary packages have been imported).
byte bMethod(short i) throws Exception {...}
A. int bMethod(int i) throws IOException{...}
B. protected int bMethod(short s) throws FileNotFoundException{...}
C. private String aMethod(byte b,short s) throws Exception{...}
D. char bMethod(String s) throws RuntimeException{...}
E. int bMethod(short sh){...}

Question 59. What is the output of the following code when compiled and run? Select two correct answers.
public class Question59 {
	public static void main(String[] args) {
		String String = "String";	//line 1
		int Question59 = 2;			//line 2
		Object:for(int main=0;main<Question59*String.length();main+=Question59){ //line 3
			System.out.print(String.charAt(main)+" "); //line 4
			if(main>Question59)		//line 5
				break Object;		//line 6
		}
	}
}
A. Prints: t i g
B. Prints: S r n
C. Compilation error at lines 1, 2 and 3.
D. Compilation error at lines 4, 5 and 6.
E. The code compiles and runs fine.


Many thanks to the following people who have generously contributed to this work by pointing out mistakes:
Corey McGlone, Rob Ross, Rajinder Yadav, Johannes de Jong, Manish Hatwalne, Aymen Esawey, Marilyn deQueiroz, Eric Low, Chulachit Chulamaneechote, Shishio San, Marlene Miller, Steve Lovelace

Automatically generated by MoKreator 1.0 © 2002 Valentin Crettaz