Java Programming Quiz
Test your knowledge of Java programming including constructors, generics, collections, threading, serialization, enums, regex, and access modifiers
Questions
Can a subclass access the private values of the super class
- No
- Yes
- Will have to use an instance
- None
given x = y--; What will be true after execution?
- x>y
- x<y
- x==y
- x++
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); } }
- true
- Compile Error
- false
- NullPointerException
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; } } }
- good
- good idea
- good idea good idea
- good 0 good 0 1
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(); } } }
- Compilation Fail
- java.io.NotSerializableException: Because class A is not Serializable
- Run properly
- Compilation Fail : Because class A is not Serializable.
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(); } } }
- 9 0 9
- 9 7 9
- 0 0 0
- 0 7 0
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()); } }
- 7:30
- Compile Error - an enum cannot be instantiated using the new operator
- 12:50
- 19:45
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(); } }
- A B A
- A B A B
- B B
- A B
. 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); } } }
- Compile error
- One One Two Two
- One Two One Two
- One Two
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"); } }
- Compilation clean and run but no output
- Compilation and run with the output "Running"
- Compile time error with complaint of no Thread import
- Compile time error with complaint of no access to Thread package
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(); }
- anonymous tech
- Compile Error
- Tech
- anonymous tech Tech
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); } } } }
- Outer x is Outer variable.
- Compile Error
- Local variable z is local variable
- Outer x is Outer variable Local variable z is local variable
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; } }
- The class compiles and runs, but does not print anything.
- The number 2 gets printed with AssertionError
- The number 3 gets printed with AssertionError
- compile error
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); } }
- das abcdef abcdef
- das das abcdef abcdef
- das abcdef
- abcdef abcdef
Synchronized resizable-array implementation of the List interface is _____________?
- Vector
- ArrayList
- Hashtable
- HashMap
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()); } }
- Hello
- Compile error
- java.lang.ClassCastException
- null
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() + " "); } }
- Compile error : Integer can't add
- newyork ca texas 11
- newyork ca texas
- newyork ca
What are the new features added to Java 1.5?
- Boxing and UnBoxing
- Generics
- Enhanced for
- Iterator
Key word "transient" is used so that the state of the object is not saved... (T/F)
- True
- False
Can you call one constructor from another if a class has multiple constructors
- True
- False