You are on page 1of 7

Name:______________________________ Company:______________________________

Java Test Questions


No of Questions: 20 Time: 30 mins

1. What shall be the output of Point p after calling method in i) and ii). Value of Point p was P(700, 800) before the method calls. i) static void changePoint ( Point p) { p.x = 100; p.y=200; } ii) static void changePoint(Point p) { p=new Point(100,200); } Ans: i) Point p(100,200), ii) Point p(700,800) 2. A class without a method can be run by JVM if its ancestor class has following method
public static void main (String args [])

a. True b. False Ans: a 3. Which of the following statements related to Garbage Collection are correct? a. It is possible for a program to free memory at a given time. b. Garbage Collection feature of Java ensures that the program never runs out of memory. c. It is possible for a program to make an object available for Garbage Collection. d. Garbage Collection runs in a high Priority thread. Ans: c 4. Name the collection interface used to represent collections that maintain unique elements. Ans: Set 5. What will be output from the following statements: a. System.out.println(1+2+3); b. System.out.println (1+2+3); Ans: a) 33, b) 123

6. If you have reference variable of parent class type and you assign a child class object to that variable and invoke static method. Which method will be invoked? a. Parent b. Child Ans: Parent 7. Map implements collection. a. True b. False Ans: b 8. Can an interface be final? a. True b. False Ans: b 9. Can an anonymous class implement an interface and extend a class at the same time? a. True b. False Ans: b 10. What will be the output from following program?
public class test { public static void main (String args []) { Thread t = new Thread (new Runnable () { public void run () { try { for (int i=1; i < 5; i++) { System.out.print (" " + i); wait (1000); } } catch (Exception ex) {ex.printStackTrace ();} } }); t.start (); } }

a. 12345 b. 1 2 3 4 5 c. Compilation Error d. Runtime Error

Ans: d

11. What will be the output from the following program


public class test { public static void main (String args []) { Boolean bool1 = new Boolean(true); Boolean bool2 = new Boolean(false); Boolean bool3 = new Boolean("false"); Boolean bool4 = new Boolean(bool1); System.out.print (bool1.equals (bool4)); System.out.print (" ") System.out.print (bool2 == bool3); System.out.print (" ") System.out.println (bool1 == bool4); } }

a. b. c. d.
Ans: b

true true true true false false true true false Compilation Error

12. How will following program behave?


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) {ex.printStackTrace () ;} } }

a. b. c. d.
Ans: d

Object b is successfully serialized Compilation Error Object a is not serialized Runtime exception

13. What shall be the output of the below program?

public class Test { public static void main(String[] args) {

} }

Integer i = null; int j = i; System.out.println (j);

a. null b. Compilation Error c. Runtime exception Ans: c 14. What shall be the output of the below program
public class Test { public static void main(String [] args) { Hashtable ht = new Hashtable<String, String> (); ht.put ("C", "SEE"); ht.put (1000, "SEE"); ht.put ("SEE", 20.01); System.out.print (ht.get ("C") + " "); System.out.print (ht.get (1000) + " "); System.out.print (ht.get ("SEE")); } }

a. b. c. d. Ans: b

SEE SEE SEE SEE SEE 20.01 Compilation Error Runtime Exception

15. The code in finally block will never get executed in the following program?
try { if (choice) { while (true) } else { system .exit(1): } }finally { codetocleanup(); }

a. True b. False Ans: a 16. Which is false regarding transient variable a. Transient variable can be declared as final in a class

b. Transient variable cannot be serialized c. Transient variable can be defined in an interface d. Transient variable can be declared as static in a class Ans: c 17. Write a code snippet to direct Java Program messages to the system console, but error messages, say to a file? Ans: PrintStream ps = new PrintStream(new FileOutputStream(filename)) System.setErr(ps);

18. Override toString() method for the following class A


Class B { String str; } Class A { Int a; Int b; String s1; String s2; B b; }

Ans: public String toString() { StringBuffer sb = new StringBuffer(); sb.append(a).append(b).append(s1).append(s2).append(str); return sb.toString(); }

19. Write a code snippet to open a jdbc connection to Oracle Database. Ans: try { Class.forName(com.oracle.jdbc.OracleDriver); Connection con = DriverManager.getConnection(url, username, passwd); }catch(ClassNotFoundException e) { throw e; }catch(SQLException e) { throw e; }

20. Write a code snippet to sort a list containing Object of Class A.


Class A { String s1; String s2;

} Ans: List list = new ArrayList(); list.add(new A()); list.add(new A()); list.add(new A()); list.add(new A()); Collections.sort(list); Class A { String s1; String s2; public int hashCode () { return s1.hashCode() + s2.hashCode(); } public boolean equals(Object o) { if(this == o) return true; if (o == null) return false; if( o instanceof A) { A a = (A)o; if (s1.equals(a.s1) && s2.equals(a.s2)) return true else return false; }else {

return false; } } }

You might also like