Java Certification Quiz
Instructions For Quiz
1)What gets printed on the standard output when the class below is compiled and executed. Select the one correct answer.
public class ShortCkt { public static void main(String args[]) { int i = 0; boolean t = true; boolean f = false, b; b = (t | ((i++) == 0)); b = (f | ((i+=2) > 0)); System.out.println(i); }}
2) What gets displayed on the screen when the following program is compiled and run. Select the one correct answer.
public class test { public static void main(String args[]) { boolean x = true; int a; if(x) a = x ? 1: 2; else a = x ? 3: 4; System.out.println(a); }}
3)What is the result when this code is executed?
class One { public One() { System.out.print(1); } } class Two extends One { public Two() { System.out.print(2); } } class Three extends Two { public Three() { System.out.print(3); } } public class Numbers{ public static void main( String[] argv ) { new Three(); } }
4) What is the result?
class TestA { public void start() { System.out.println("TestA"); } }public class TestB extends TestA { public void start() { System.out.println("TestB"); } public static void main(String[] args) { ((TestA)new TestB()).start(); }}
7)What all gets printed when the following program is compiled and run. Select the one correct answer.
public class test { public static void main(String args[]) { int i=0, j=2; do { i=++i; j--; } while(j>0); System.out.println(i); }}
8)What all gets printed when the following gets compiled and run.
public class test { public static void main(String args[]) { String s1 = "abc"; String s2 = new String("abc");
if(s1 == s2) System.out.println(1); else System.out.println(2); if(s1.equals(s2)) System.out.println(3); else System.out.println(4); }}
10)What is the result?
public static void main(String[] args) { 12. Integer i = new Integer(1) + new Integer(2); switch(i) { case 3: System.out.println("three"); break; 15. default: System.out.println("other"); break; }}
11)Which exception or error should be thrown by the virtual machine?
public class ClassA { public void count(int i) { count(++i); }}And:ClassA a = new ClassA(); a.count(3);
12)What is the result?
public static void test(String str) { int check = 4; if (check = str.length()) { System.out.print(str.charAt(check -= 1) +", "); } else { System.out.print(str.charAt(0) + ", "); }}and the invocation:21. test("four");22. test("tee");test("to");
13)How many String objects will be created when this method is invoked?public String makinStrings() { String s = “Fred”; s = s + “47”; s = s.substring(2, 5); s = s.toUpperCase(); return s.toString();}
14)What is the result?
public static void parse(String str) { try { float f = Float.parseFloat(str); } catch (NumberFormatException nfe) { f = 0; } finally { System.out.println(f); }} public static void main(String[] args) { parse("invalid"); }
15)What is the result?
static void test() throws Error { if (true) throw new AssertionError(); System.out.print("test "); } public static void main(String[] args) { try { test(); } catch (Exception ex) { System.out.print("exception "); } System.out.print("end "); }
16)What is the result?
static void test() throws RuntimeException { try { System.out.print("test "); throw new RuntimeException(); } catch (Exception ex) { System.out.print("exception "); }}public static void main(String[] args) { try { test(); } catch (RuntimeException ex) { System.out.print("runtime "); } System.out.print("end ");}
static void test() { try { String x = null; System.out.print(x.toString() + " "); } finally { System.out.print("finally "); }}public static void main(String[] args) { try { test(); } catch (Exception ex) { System.out.print("exception "); }}
18)What is the result?
public enum Title { MR("Mr."), MRS("Mrs."), MS("Ms."); private final String title; private Title(String t) { title = t; } public String format(String last, String first) { return title + " " + first + " " + last; }}public static void main(String[] args){ System.out.println(Title.MR.format("Doe", "John"));}
21)What is the result when the programmer attempts to compile the code and run it with the command java Converter 12?
class Converter { public static void main(String[] args) { Integer i = args[0]; int j = 12; System.out.println("It is " + (j==i) + " that j==i."); }}
22)What is the output?
class Cup { }class PoisonCup extends Cup { } public void takeCup(Cup c) { if (c instanceof PoisonCup) { System.out.println("Inconceivable!"); } else if (c instanceof Cup) { System.out.println("Dizzying intellect!"); } else { System.exit(0); }}And the execution of the statements:Cup cup = new PoisonCup();takeCup(cup);
23)What is the result?
public class Yippee2 { static public void main(String [] yahoo) { for(int x = 1; x < yahoo.length; x++) { System.out.print(yahoo[x] + " "); } }}and the command line invocation:java Yippee2 a b c
24)Which code, inserted at line 14, allows the Sprite class to compile?
class Nav{ public enum Direction { NORTH, SOUTH, EAST, WEST }}public class Sprite{ Line 14. // insert code here}
25)What is required at line 5 in class SomeApp to use the process method of BitUtils?
Given classes defined in two different files:package util;public class BitUtils { public static void process(byte[]) { /* more code here */ }}package app;public class SomeApp { public static void main(String[] args) { byte[] bytes = new byte[256]; // insert code here } }
26)Which code, inserted at line 4, guarantees that this program will output [1, 2]?
import java.util.*; public class Example { public static void main(String[] args) { // insert code here set.add(new Integer(2)); set.add(new Integer(1)); System.out.println(set); } }
27)What is the result?
public static Iterator reverse(List list) { Collections.reverse(list); return list.iterator();} public static void main(String[] args){ List list = new ArrayList(); list.add("1"); list.add("2"); list.add("3"); for (Object obj: reverse(list)) System.out.print(obj + ", ");} public class Example
29)Which code will sort this list in the opposite order of the sort in line?
List list = // more code here Collections.sort(list, new MyComparator());
30)Given:Which method will complete this class?
public class Score implements Comparable { private int wins, losses; public Score(int w, int 1) { wins = w; losses = 1; } public int getWins() { return wins; } public int getLosses() { return losses; } public String toString() { return “”; } // insert code here}
31)programmer iterates over the TreeSet and prints the name of each Drink object.What is the result?
int[] myArray = new int[] {1, 2, 3, 4, 5};
32)Which statement is true?
public class Drink implements Comparable { public String name; public int compareTo(Object o) { return 0; }}and:Drink one = new Drink();Drink two = new Drink();one.name= "Coffee";two.name= "Tea";TreeSet set = new TreeSet();set.add(one);set.add(two);
33)Which statement is true?
public class TestSeven extends Thread {private static int x;public synchronized void doThings() { int current = x; current++; x = current; }public void run() { doThings(); }}public class TestSeven extends Thread {private static int x;public synchronized void doThings() { int current = x; current++; x = current; }public void run() { doThings(); }}public class TestSeven extends Thread {private static int x;public synchronized void doThings() { int current = x; current++; x = current; }public void run() { doThings(); }}public class TestSeven extends Thread {private static int x;public synchronized void doThings() { int current = x; current++; x = current; }public void run() { doThings(); }}
34)Which statement is true?
void waitForSignal() { Object obj = new Object(); synchronized (Thread.currentThread()) { obj.wait(); obj.notify(); }}
35)What can be a result?
public class TestOne implements Runnable { public static void main (String[] args) throws Exception { Thread t = new Thread(new TestOne()); t.start(); System.out.print("Started"); t.join(); System.out.print("Complete"); } public void run() { for (int i = 0; i < 4; i++) { System.out.print(i); } }}
36)What is the result?
Runnable r = new Runnable() { public void run() { System.out.print("Cat"); }};Thread t = new Thread(r) { public void run() { System.out.print("Dog"); }};t.start();
40)What is the result?
public class Test { public static void main(String [] args) { int x = 5; boolean b1 = true; boolean b2 = false; if ((x == 4) && !b2 ) System.out.print("1 "); System.out.print("2 "); if ((b2 = true) && b1 ) System.out.print("3 "); }}
41)What is the result?
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.print(" main x = " + x); } void doStuff(int x) { System.out.print(" doStuff x = " + x++); }}
42)What is the result?
os.writeObject(f); os.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
class Tree { }
43)What is the result?
import java.io.*;public class Forest implements Serializable { private Tree tree = new Tree(); public static void main(String [] args) { Forest f = new Forest(); try { FileOutputStream fs = new FileOutputStream("Forest.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(f); os.close(); } catch (Exception ex) { ex.printStackTrace(); } } }class Tree { }
45)Which code fragment, inserted at line 15, will allow Foo objects to becorrectly serialized and deserialized?
public class Foo implements java.io.Serializable { private int x; public int getX() { return x; } publicFoo(int x) {this.x=x; } private void writeObject( ObjectOutputStream s) throws IOException { // insert code here }}
46)What is the result?
Assuming that the serializeBanana() and the deserializeBanana() methods willcorrectly use Java serialization and given: import java.io.*; class Food implements Serializable {int good = 3;} class Fruit extends Food {int juice = 5;} public class Banana extends Fruit { int yellow = 4; public static void main(String [] args) { Banana b = new Banana(); Banana b2 = new Banana(); b.serializeBanana(b); // assume correct serialization b2 = b.deserializeBanana(); // assume correct System.out.println("restore "+b2.yellow+ b2.juice+b2.good); } // more Banana methods go here 50. }
47)What is the result?
Assuming that the serializeBanana2() and the deserializeBanana2() methodswill correctly use Java serialization and given: import java.io.*; class Food { Food() { System.out.print("1"); } }class Fruit extends Food implements Serializable{ Fruit() { System.out.print("2"); } } public class Banana2 extends Fruit { int size = 42; public static void main(String [] args) { Banana2 b = new Banana2(); b.serializeBanana2(b); // assume correct serialization b = b.deserializeBanana2(b); // assume correct System.out.println(" restored " + b.size + " "); } // more Banana2 methods }
50)What is the result?
51)What is the result?
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.print(" main x = " + x); } void doStuff(int x) { System.out.print(" doStuff x = " + x++); }}
52)What is the result?
public class Pass { public static void main(String [] args) { int x = 5; Pass p = new Pass(); p.doStuff(x); System.out.print(" main x = " + x); } void doStuff(int x) { System.out.print(" doStuff x = " + x++); }}
53)What is the result?
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; } }}And:ClassA a = new ClassA(); a.getValue();
54)What is the result?
Given:interface A { public void aMethod();}interface B{ public void bMethod(); }interface C extends A,B{ public void cMethod();}class D implements B { public void bMethod(){}}class E extends D implements C { public void aMethod(){} public void bMethod(){} public void cMethod(){}}What is the result?
55)Which statement is true?
public class CreditCard{ private String cardID; private Integer limit; public String ownerName; public void setCardInformation(String cardID, String ownerName, Integer limit) { this.cardID = cardID; this.ownerName = ownerName; this.limit = limit; }}
56)What is the result of executing XMLMessage.main?
classes defined in two different files: package packageA; public class Message { String getText() { return "text"; }}and: package packageB; public class XMLMessage extends packageA.Message { String getText() { return "text"; } public static void main(String[] args) { System.out.println(new XMLMessage().getText()); }}
57)What is the result?
public class Foo{ public int a; public Foo() { a = 3; } public void addFive() { a += 5; }}and:public class Bar extends Foo { public int a; public Bar() { a = 8; } public void addFive() { this.a += 5; }} invoked with:Foo foo = new Bar();foo.addFive();System.out.println("Value: " + foo.a);
58)What is the result?
String test = "This is a test";String[] tokens = test.split("s");System.out.println(tokens.length);
59)What is the result?
System.out.format("Pi is approximately %d.", Math.PI);