Java Programming Quiz

Test your knowledge of Java programming including constructors, generics, collections, threading, serialization, enums, regex, and access modifiers

20 Questions Published

Questions

Question 1 Multiple Choice (Single Answer)

Can a subclass access the private values of the super class

  1. No
  2. Yes
  3. Will have to use an instance
  4. None
Question 2 Multiple Choice (Single Answer)

given x = y--; What will be true after execution?

  1. x>y
  2. x<y
  3. x==y
  4. x++
Question 3 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test { public static void main(String... args) { Pattern p = Pattern.compile("a{3}b?c*"); Matcher m = p.matcher("aaab"); boolean b = m.matches(); System.out.println(b); } }

  1. true
  2. Compile Error
  3. false
  4. NullPointerException
Question 4 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test extends Thread{ static String sName = "good"; public static void main(String argv[]){ Test t = new Test(); t.nameTest(sName); System.out.println(sName); } public void nameTest(String sName){ sName = sName + " idea "; start(); } public void run(){ for(int i=0;i < 4; i++){ sName = sName + " " + i; } } }

  1. good
  2. good idea
  3. good idea good idea
  4. good 0 good 0 1
Question 5 Multiple Choice (Single Answer)

What is the output for the below code ? public class A {} public class B implements Serializable { A a = new A(); public static void main(String... args){ B b = new B(); try{ FileOutputStream fs = new FileOutputStream("b.ser"); ObjectOutputStream os = new ObjectOutputStream(fs); os.writeObject(b); os.close(); }catch(Exception e){ e.printStackTrace(); } } }

  1. Compilation Fail
  2. java.io.NotSerializableException: Because class A is not Serializable
  3. Run properly
  4. Compilation Fail : Because class A is not Serializable.
Question 6 Multiple Choice (Single Answer)

What is the output for the below code running in the same JVM? public class A implements Serializable { transient int a = 7; static int b = 9; } public class B implements Serializable { public static void main(String... args){ A a = new A(); try { ObjectOutputStream os = new ObjectOutputStream( new FileOutputStream("test.ser")); os.writeObject(a); os. close(); System.out.print( + + a.b + " "); ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.ser")); A s2 = (A)is.readObject(); is.close(); System.out.println(s2.a + " " + s2.b); } catch (Exception x) { x.printStackTrace(); } } }

  1. 9 0 9
  2. 9 7 9
  3. 0 0 0
  4. 0 7 0
Question 7 Multiple Choice (Single Answer)

What is the output for the below code ? public enum Test { BREAKFAST(7, 30), LUNCH(12, 15), DINNER(19, 45); private int hh; private int mm; Test(int hh, int mm) { assert (hh >= 0 && hh <= 23) : "Illegal hour."; assert (mm >= 0 && mm <= 59) : "Illegal mins."; this.hh = hh; this.mm = mm; } public int getHour() { return hh; } public int getMins() { return mm; } public static void main(String args[]){ Test t = new BREAKFAST; System.out.println(t.getHour() +":"+t.getMins()); } }

  1. 7:30
  2. Compile Error - an enum cannot be instantiated using the new operator
  3. 12:50
  4. 19:45
Question 8 Multiple Choice (Single Answer)

What is the output for the below code ? public class A { public A() { System.out.println("A"); } } public class B extends A implements Serializable { public B() { System.out.println("B"); } } public class Test { public static void main(String... args) throws Exception { B b = new B(); ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile")); save.writeObject(b); save.flush(); ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile")); B z = (B) restore.readObject(); } }

  1. A B A
  2. A B A B
  3. B B
  4. A B
Question 9 Multiple Choice (Single Answer)

. What is the output for the below code ? public class Test{ public static void main(String argv[]){ Test1 pm1 = new Test1("One"); pm1.run(); Test1 pm2 = new Test1("Two"); pm2.run(); } } class Test1 extends Thread{ private String sTname=""; Test1(String s){ sTname = s; } public void run(){ for(int i =0; i < 2 ; i++){ try{ sleep(1000); }catch(InterruptedException e){} yield(); System.out.println(sTname); } } }

  1. Compile error
  2. One One Two Two
  3. One Two One Two
  4. One Two
Question 10 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test extends Thread{ public static void main(String argv[]){ Test b = new Test(); b.start(); } public void run(){ System.out.println("Running"); } }

  1. Compilation clean and run but no output
  2. Compilation and run with the output "Running"
  3. Compile time error with complaint of no Thread import
  4. Compile time error with complaint of no access to Thread package
Question 11 Multiple Choice (Single Answer)

What is the output for the below code ? public class Tech { public void tech() { System.out.println("Tech"); } } public class Atech { Tech a = new Tech() { public void tech() { System.out.println("anonymous tech"); } }; public void dothis() { a.tech(); } public static void main(String... args){ Atech atech = new Atech(); atech.dothis(); }

  1. anonymous tech
  2. Compile Error
  3. Tech
  4. anonymous tech Tech
Question 12 Multiple Choice (Single Answer)

What is the output for the below code ? public class Outer { private String x = "Outer variable"; void doStuff() { String z = "local variable"; class Inner { public void seeOuter() { System.out.println("Outer x is " + x); System.out.println("Local variable z is " + z); } } } }

  1. Outer x is Outer variable.
  2. Compile Error
  3. Local variable z is local variable
  4. Outer x is Outer variable Local variable z is local variable
Question 13 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test { public static void main(String... args) { for(int i = 2; i < 4; i++) for(int j = 2; j < 4; j++) assert i!=j : i; } }

  1. The class compiles and runs, but does not print anything.
  2. The number 2 gets printed with AssertionError
  3. The number 3 gets printed with AssertionError
  4. compile error
Question 14 Multiple Choice (Single Answer)

What is the output for the below code ? public class NameBean { private String str; NameBean(String str ){ this.str = str; } public String toString() { return str; } } import java.util.HashSet; public class CollClient { public static void main(String ... sss) { HashSet myMap = new HashSet(); String s1 = new String("das"); String s2 = new String("das"); NameBean s3 = new NameBean("abcdef"); NameBean s4 = new NameBean("abcdef"); myMap.add(s1); myMap.add(s2); myMap.add(s3); myMap.add(s4); System.out.println(myMap); } }

  1. das abcdef abcdef
  2. das das abcdef abcdef
  3. das abcdef
  4. abcdef abcdef
Question 15 Multiple Choice (Single Answer)

Synchronized resizable-array implementation of the List interface is _____________?

  1. Vector
  2. ArrayList
  3. Hashtable
  4. HashMap
Question 16 Multiple Choice (Single Answer)

What is the output for the below code ? public class Test { public static void main(String argv[]){ ArrayList list = new ArrayList(); ArrayList listStr = list; ArrayList listBuf = list; listStr.add(0, "Hello"); StringBuffer buff = listBuf.get(0); System.out.println(buff.toString()); } }

  1. Hello
  2. Compile error
  3. java.lang.ClassCastException
  4. null
Question 17 Multiple Choice (Single Answer)

What is the output for the below code ? import java.util.LinkedList; import java.util.Queue; public class Test { public static void main(String... args) { Queue q = new LinkedList(); q.add("newyork"); q.add("ca"); q.add("texas"); show(q); } public static void show(Queue q) { q.add(new Integer(11)); while (!q.isEmpty ( ) ) System.out.print(q.poll() + " "); } }

  1. Compile error : Integer can't add
  2. newyork ca texas 11
  3. newyork ca texas
  4. newyork ca
Question 18 Multiple Choice (Multiple Answers)

What are the new features added to Java 1.5?

  1. Boxing and UnBoxing
  2. Generics
  3. Enhanced for
  4. Iterator
Question 19 True/False

Key word "transient" is used so that the state of the object is not saved... (T/F)

  1. True
  2. False
Question 20 True/False

Can you call one constructor from another if a class has multiple constructors

  1. True
  2. False