Java Concepts

40 Questions | Attempts: 169
Share

SettingsSettingsSettings
Java Quizzes & Trivia

BASICS


Questions and Answers
  • 1. 

    Which of the following are true about ResultSet? (Choose 2)  

    • A.

      Atleast one record should be there in the ResultSet on opening a query (or) table

    • B.

      Not all ResultSets are updatable

    • C.

      The ResultSet object contains null, if there are no records in the table

    • D.

      All ResultSet, are Scrollable

    • E.

      It is possible to delete records through ResultSet

    Correct Answer(s)
    B. Not all ResultSets are updatable
    E. It is possible to delete records through ResultSet
  • 2. 

    Mr.Vijay is working for a Software Company. He needs to save and reload objects from a Java application. He needs to write a module to accomplish the same. Which of the following options can be used to accomplish the above requirement?  

    • A.

      Writable interface

    • B.

      Serializable interface

    • C.

      Cloneable interface

    • D.

      ObjectSerializable interface

    • E.

      Readable interface

    Correct Answer
    B. Serializable interface
  • 3. 

    class InOut{ String s= new String("Between"); public void amethod(final int iArgs){ int iam; class Bicycle{ public void sayHello(){ ...Line 1 } }//End of bicycle class }//End of amethod public void another(){ int iOther; } }  

    • A.

      System.out.println(iOther);

    • B.

      System.out.println(iam);

    • C.

      System.out.println(iArgs);

    • D.

      System.out.println(s);

    Correct Answer(s)
    C. System.out.println(iArgs);
    D. System.out.println(s);
  • 4. 

    public class UnwiseThreads implements Runnable { public void run() { while(true) { } } public static void main(String args[]) { UnwiseThreads ut1 = new UnwiseThreads(); UnwiseThreads ut2 = new UnwiseThreads(); UnwiseThreads ut3 = new UnwiseThreads(); ut1.run(); ut2.run(); ut3.run(); } }  

    • A.

      Compilation error "ut2.run() is never reached"

    • B.

      The code compiles and runs 3 non ending non daemon threads

    • C.

      The code compiles but runs only 1 non ending, non daemon thread

    • D.

      Runtime Error "IllegalThreadStateException"

    Correct Answer
    C. The code compiles but runs only 1 non ending, non daemon thread
  • 5. 

    Consider the following code: class Planet { } class Earth extends Planet { } public class WelcomePlanet { public static void welcomePlanet(Planet planet) { if (planet instanceof Earth) { System.out.println("Welcome!"); } else if (planet instanceof Planet) { System.out.println("Planet!"); } else { System.exit(0); } } public static void main(String args[]) { WelcomePlanet wp = new WelcomePlanet(); Planet planet = new Earth(); welcomePlanet(planet); } } Which of the following will be the output of the above program?

    • A.

      An exception is thrown at runtime

    • B.

      Planet!

    • C.

      The code runs with no output

    • D.

      Welcome!

    • E.

      Compilation fails

    Correct Answer
    D. Welcome!
  • 6. 

    Given the following object hierarchy and code for the upgrade method: java.lang.Object  +----mypkg.BaseWidget  |  +----TypeAWidget //  the following is a method in the BaseWidget class 1. public TypeAWidget upgrade( ){  2. TypeAWidget A = (TypeAWidget) this 3. return A;  4. }   Which of the following will be the result of the below statements? 5. BaseWidget B = new BaseWidget

    • A.

      A runtime ClassCastException would be generated in line 2.

    • B.

      The compiler would object to line 2.

    • C.

      As this referes to the BaseWidget, a parent can accept its child

    • D.

      After line 6 executes, the object referred to as A will in fact be a TypeAWidget.

    Correct Answer
    A. A runtime ClassCastException would be generated in line 2.
  • 7. 

    Which of the following options are true for StringBuffer class?(choose 3)  

    • A.

      'capacity' property indicates the maximum number of characters that a StringBuffer can have

    • B.

      StringBuffer implements Charsequence interface

    • C.

      StringBuffer is extended from String class

    • D.

      Buffer space in StringBuffer can be shared

    • E.

      StringBuffer is threadsafe

    Correct Answer(s)
    A. 'capacity' property indicates the maximum number of characters that a StringBuffer can have
    B. StringBuffer implements Charsequence interface
    E. StringBuffer is threadsafe
  • 8. 

        Consider the following partial code: 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; } }

    • A.

      The class is fully encapsulated

    • B.

      The setCardInformation method breaks encapsulation

    • C.

      The code demonstrates polymorphism

    • D.

      The ownerName variable breaks encapsulation

    • E.

      The cardID and limit variables break polymorphism

    Correct Answer
    D. The ownerName variable breaks encapsulation
  • 9. 

    Public class TestString9 { public static void main(String st[]){ String s1 = "java"; String s2 = "java"; String s3 = "JAVA"; s2.toUpperCase(); s3.toUpperCase(); boolean b1 = s1==s2; boolean b2 = s1==s3; System.out.print(b1); System.out.print(" "+b2); } }

    • A.

      False true

    • B.

      True false

    • C.

      Runtime error

    • D.

      False false

    • E.

      Runtime error

    Correct Answer
    B. True false
  • 10. 

    import java.io.*; public class IOCode2 { public static void main(String args[]) throws FileNotFoundException { // Insert Code here System.out.println("Welcome to File Programming"); } } Which of the following code snippets when substituted to the comment line (// Insert Code here), will redirect the output generated by the System.out.println() methods, in the above code?    

    • A.

      System.setOut(new FileWriter("C:/Data"));

    • B.

      System.setOut(new PrintStream("C:/Data"));

    • C.

      System.out.redirectOutput(new PrintStream("C:/Data"));

    • D.

      System.redirectOutput(new PrintStream("C:/Data"));

    • E.

      System.out.setOut(new PrintStream("C:/Data"));

    Correct Answer
    B. System.setOut(new PrintStream("C:/Data"));
  • 11. 

         public class ExceptionType  { public static void main(String args[]) { String s = null;  try { System.out.println(s.length());  } catch(Exception e) { System.out.println("Exception 1");  } Finally  { try  { generateException(); } catch(Exception e) { System.out.println("Exception 2");  } } } static void generateException() throws IllegalArgumentException { throw new IllegalArgumentException();  } Which of the following statements are true regarding the above given program? (Choose 3)   }

    • A.

      The output "Exception 2" is because of the exception thrown programmatically

    • B.

      The output "Exception 1" is because of the Exception thrown programmatically

    • C.

      The output "Exception 1" is because of the Exception thrown by JVM

    • D.

      The Exception thrown by generateException() method is an Unchecked Exception

    • E.

      The output "Exception 2" is because of the Exception thrown by JVM

    Correct Answer(s)
    A. The output "Exception 2" is because of the exception thrown programmatically
    C. The output "Exception 1" is because of the Exception thrown by JVM
    D. The Exception thrown by generateException() method is an Unchecked Exception
  • 12. 

    public class TestStart implements Runnable { boolean stoper = true; public void run() { System.out.println ("Run method Executed"); } public static void main (String[] argv) { TestStart objInt = new TestStart(); Thread threadX = new Thread(objInt); threadX.start(); threadX.start(); } } What will be the output of the above program?  

    • A.

      Compiles and executes successfully Prints "Run method executed"

    • B.

      Compilation Error

    • C.

      Compiles and on execution Prints "Run method executed" then throws Runtime exception

    • D.

      Compiles and on execution Prints "Run method executed"

    Correct Answer
    C. Compiles and on execution Prints "Run method executed" then throws Runtime exception
  • 13. 

    class A extends Thread { private int i; public void run() {i = 1; } public static void main(String[] args) { A a = new A(); a.start(); System.out.print(a.i); }} What will be the output of the above program?  

    • A.

      Prints 0

    • B.

      Prints: 01

    • C.

      Prints: 10

    • D.

      Prints 1

    • E.

      Compile-time error

    Correct Answer
    A. Prints 0
  • 14. 

      import java.util.*; public class Code10  {  { final Vector v; v=new Vector();  } public Code10() { }  public void codeMethod() { System.out.println(v.isEmpty()); } public static void main(String args[]) { new Code10().codeMethod();  } }

    • A.

      Runtime error: NullPointerException

    • B.

      Compilation error: cannot find the symbol

    • C.

      Prints: false

    • D.

      Compilation error: v is not initialised inside the constructor

    • E.

      Prints: true

    Correct Answer
    B. Compilation error: cannot find the symbol
  • 15. 

    A Java application needs to stream a video from a movie file. Which of the following options gives the correct combination of stream classes that can be used to implement the above requirement?  

    • A.

      InputStreamReader and FileInputStream

    • B.

      FileInputStream and FilterInputStream

    • C.

      LineInputStream and BufferedInputStream

    • D.

      FileReader and BufferedReader

    • E.

      FileInputStream and BufferedInputStream

    Correct Answer
    E. FileInputStream and BufferedInputStream
  • 16. 

    Which of the following options are true about abstract implementations in Collections?(choose 3)  

    • A.

      It provides static factory class

    • B.

      All major implementations like Hashtable, Vectors are supported

    • C.

      They provide hooks for custom implementations

    • D.

      All major interfaces are supported

    • E.

      Map is not supported

    Correct Answer(s)
    A. It provides static factory class
    C. They provide hooks for custom implementations
    D. All major interfaces are supported
  • 17. 

    interface i1 { int i = 0; } interface i2 { int i = 0; } class inter implements i1, i2 { public static void main(String[] a) { System.out.println(i); } }

    • A.

      Runtime Error

    • B.

      Compilation Error

    • C.

      No output

    • D.

      Prints: 0

    Correct Answer
    B. Compilation Error
  • 18. 

    import java.util.*; public class TestCol8{ public static void main(String argv[]){ TestCol8 junk = new TestCol8(); junk.sampleMap(); } public void sampleMap(){ TreeMap tm = new TreeMap(); tm.put("a","Hello"); tm.put("b","Java"); tm.put("c","World"); Iterator it = tm.keySet().iterator(); while(it.hasNext()){ System.out.print(it.next()); } } } What will be the output of the above code snippet?    

    • A.

      Compile error

    • B.

      Runtime error

    • C.

      HWJ

    • D.

      Abc

    • E.

      HelloJavaWorld

    Correct Answer
    D. Abc
  • 19. 

    public class Exp4 { static String s = "smile!.."; public static void main(String[] args) { new Exp4().s1(); System.out.println(s); } void s1() { try { s2(); } catch (Exception e) { s += "morning"; } } void s2() throws Exception { s3(); s += "evening"; s3(); s += "good"; } void s3() throws Exception { throw new Exception(); } } What will be the output of the above program?    

    • A.

      Smile!..morning

    • B.

      Smile!..

    • C.

      Smile!..morningevening

    • D.

      Smile!..eveningmorning

    • E.

      Smile!..morningeveninggood

    Correct Answer
    A. Smile!..morning
  • 20. 

    import java.io.*; class Test { int a = 10; } class Test2 extends Test implements Serializable { int b; public String toString() { return "a = " + a + ", " + "b = " + b; } } public class IOCode5 { public static void main(String args[]) throws FileNotFoundException, IOException, ClassNotFoundException { ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("C:/ObjectData")); Test2 t1 = new Test2(); t1.a = 20; t1.b = 30; out.writeObject(t1); out.close(); ObjectInputStream in = new ObjectInputStream(new FileInputStream("C:/ObjectData")); Test2 t2 = (Test2) in.readObject(); // Line 1 System.out.println(t2); } } What will be the output of the above code snippet?    

    • A.

      A = 10, b = 30

    • B.

      A = 0, b = 30

    • C.

      A = 20, b = 30

    • D.

      A = 10, b = 0

    • E.

      Throws TransientException at the commented line (// Line 1)

    Correct Answer
    A. A = 10, b = 30
  • 21. 

    Consider the following code snippet: abstract class Director { protected String name; Director(String name) { this.name = name; } abstract void occupation(); } class FilmDirector extends Director { FilmDirector(String name) { super(name); } void occupation() { System.out.println("Director " + name + " directs films"); } } public class TestDirector { public static void main(String[] args) { FilmDirector fd = new FilmDirector("Manirathnam"); fd.occupation(); new Director("Manirathnam") { void occupation() { System.out.println("Director " + name + " also produces films"); } }.occupation(); } } Which of the following will be the output of the above code snippet?    

    • A.

      Compilation fails at TestDirector class

    • B.

      B. Prints: Director Manirathnam also produces films

    • C.

      Prints: Director Manirathnam directs films Director Manirathnam also produces films

    • D.

      Prints: Director Manirathnam directs films

    • E.

      Runtime Error

    Correct Answer
    C. Prints: Director Manirathnam directs films Director Manirathnam also produces films
  • 22. 

    Consider the following code: public class WrapIt { public static void main(String[] args) { new WrapIt().testC('a'); } public void testC(char ch) { Integer ss = new Integer(ch); Character cc = new Character(ch); if(ss.equals(cc)) System.out.print("equals "); if(ss.intValue()==cc.charValue()) { System.out.println("EQ"); } } }   Which of the following gives the valid output for the above code?

    • A.

      Prints: equals

    • B.

      Compile-time error: Integer wrapper cannot accept char type

    • C.

      Prints: EQ

    • D.

      Compile-time error: Wrapper types cannot be compared using equals

    • E.

      Prints: equals EQ

    Correct Answer
    C. Prints: EQ
  • 23. 

    Which of the following statements are valid 3 dimensional character array creations?(Choose 2)  

    • A.

      Char[][][] charArray = {{'a', 'b'}, {'c', 'd'}, {'e', 'f'}};

    • B.

      Char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};

    • C.

      Char[][][] charArray = {{'a', 'b'}, {'c', 'd'}, {'e'}};

    • D.

      Char[2][2][] charArray = {'a', 'b'};

    • E.

      Char[][][] charArray = new char[2][2][];

    Correct Answer(s)
    B. Char[][][] charArray = {{{'a', 'b'}, {'c', 'd'}, {'e', 'f'}}};
    E. Char[][][] charArray = new char[2][2][];
  • 24. 

    class TestString4 { public static void main(String args[]) { String s1 = "Its Great"; String s2 = "Its Tricky"; System.out.print(s1.concat(s2).length() + " "); System.out.print(s1.concat(s2.substring(1, s1.length())).length()); } } What will be the output of the following code snippet?    

    • A.

      18 20

    • B.

      17 19

    • C.

      17 19

    • D.

      19 17

    • E.

      17 17

    Correct Answer
    D. 19 17
  • 25. 

    What methods does the java.lang.Runtime class provide related to memory management?(Choose 3)  

    • A.

      To invoke Garbage collector

    • B.

      To create new memory locations

    • C.

      To query the total memory and free memory

    • D.

      To dump the objects to storage device

    • E.

      To run finalize methods explicitly

    Correct Answer(s)
    A. To invoke Garbage collector
    C. To query the total memory and free memory
    E. To run finalize methods explicitly
  • 26. 

    Which of the following statement is true?  

    • A.

      To call the wait() method, a thread must own the lock of the object on which the call is to be made.

    • B.

      To call the yield() method, a thread must own the lock of the object on which the call is to be made.

    • C.

      To call the sleep() method, a thread must own the lock of the object which the call is to be made.

    • D.

      To call the wait() method, a thread must own the lock of the current thread.

    • E.

      To call the join() method, a thread must own the lock of the object on which the call is to be made

    Correct Answer
    A. To call the wait() method, a thread must own the lock of the object on which the call is to be made.
  • 27. 

    Which of the following statements are true? (choose all that apply)   

    • A.

      All exceptions are thrown by JVM

    • B.

      All RuntimeException are thrown by JVM

    • C.

      JVM cannot throw user-defined exceptions

    • D.

      All exceptions are thrown programmatically from the code or API

    • E.

      JVM thrown exceptions can be thrown programmatically

    Correct Answer(s)
    C. JVM cannot throw user-defined exceptions
    E. JVM thrown exceptions can be thrown programmatically
  • 28. 

    1. public class DagRag { 2. public static void main(String [] args) { 3. 4. int [][] x = new int[2][4]; 5. 6. for(int y = 0; y < 2; y++) { 7. for(int z = 0; z < 4; z++) { 8. x[y][z] = z; 9. } 10. } 11. 12. dg: for(int g = 0; g < 2; g++) { 13. rg: for(int h = 0; h < 4; h++) { 14. System.out.println(x[g][h]); 15. 16. } 17. System.out.println("The end."); 18. 19. } 20. 21. } 22. } Which of the following code snippet when inserted at lines 15 and 18 respectively, will make the above program to generate the below output? 0 1 2 3 The end.      

    • A.

      A. if(h==3) break rg; if(g==0) break dg;

    • B.

      If(h > 3) break dg; if(g > 0) break rg;

    • C.

      If(g==3) break rg; if(h==0) break dg;

    • D.

      If(h > 3) break dg; if(g > 0) break dg;

    Correct Answer
    A. A. if(h==3) break rg; if(g==0) break dg;
  • 29. 

    Consider the following code: class UT1 { static byte m1() { final char c = 'u0001'; return c; } static byte m3(final char c) {return c;} public static void main(String[] args) { char c = 'u0003'; System.out.print(""+m1()+m3(c)); } } Which of the following gives the valid output of the above code?  

    • A.

      Prints: 4

    • B.

      Compile-time error

    • C.

      None of the listed options

    • D.

      Run-time error

    • E.

      Prints: 13

    Correct Answer
    B. Compile-time error
  • 30. 

    Which of the following options give the names of data structures that can be used for elements that have ordering, but no duplicates? (Choose 2)  

    • A.

      TreeSet

    • B.

      ArrayList

    • C.

      List

    • D.

      Set

    • E.

      SortedSet

    Correct Answer(s)
    A. TreeSet
    E. SortedSet
  • 31. 

    Consider the following program: class UserDefinedException extends Error { } public class TasteIt { public static void main(String args[]) { try { try { throw new Error(); } catch(UserDefinedException u1) { throw u1; } catch(Exception e1) { System.out.println("This is the required output"); } finally { throw new UserDefinedException(); } } catch(UserDefinedException u2) { System.out.println("This is not the output"); } catch(Error e2) { System.out.println("This is the output"); } } } What will be the output for the above program?    

    • A.

      Runtime Error

    • B.

      This is not the output

    • C.

      Compile-time error

    • D.

      This is the output

    • E.

      This is the required output

    Correct Answer
    B. This is not the output
  • 32. 

    1. class Test { 2. public static void main(String args[]) { 3. double d = 12.3; 4. Dec dec = new Dec(); 5. dec.dec(d); 6. System.out.println(d); 7. } 8. } 9. class Dec{ 10. public void dec(double d) { d = d - 2.0d; } 11. } Which of the following gives the correct value printed at line 6?  

    • A.

      Prints: 12.3

    • B.

      Prints: 10.3

    • C.

      Prints: -2.0

    • D.

      Prints: 0.0

    Correct Answer
    A. Prints: 12.3
  • 33. 

    Consider the following code snippet: import java.util.*; public class TestCol4 { public static void main(String[] args) { Set h = new HashSet(); h.add("One"); h.add("Two"); h.add("Three"); h.add("Four"); h.add("One"); h.add("Four"); List l = new ArrayList(); l.add("One"); l.add("Two"); l.add("Three"); h.retainAll(l); System.out.println("Size:" + l.size() + h.size()); } } What will be the output of the above code snippet?  

    • A.

      Size: 33

    • B.

      Size: 36

    • C.

      Compilation error

    • D.

      Size: 66

    Correct Answer
    A. Size: 33
  • 34. 

    Consider the following program: import java.io.*; public class CrypticCatch { public static void main(String[] args) throws Exception { try { try { try { throw new FileNotFoundException(); } catch(Exception e3) { throw e3; } } catch(IOException e2) { throw e2; } } catch(FileNotFoundException e1) { System.out.println("File not found exception caught"); } System.out.println("Exception handled successfully"); } } What will be the output of the above program?  

    • A.

      Exception handled successfully

    • B.

      File not found exception caught Exception handled successfully

    • C.

      Compile time error. Since exceptions should be caught in reversed hierarchy order

    • D.

      File not found exception caught

    • E.

      Runtime error

    Correct Answer
    B. File not found exception caught Exception handled successfully
  • 35. 

    The purpose of Weak Reference Type object is _______________.  

    • A.

      To keep objects alive provided there is enough memory

    • B.

      To delete objects from a container if the clients are no longer referencing them and memory is tight

    • C.

      To allow clean up after finalization but before the space is reclaimed

    • D.

      To keep objects alive only while they are in use (reachable) by clients

    Correct Answer
    D. To keep objects alive only while they are in use (reachable) by clients
  • 36. 

    Consider the following partial code: interface A { public int getValue(); } class B implements A { public int getValue() { return 1; } } class C extends B { // insert code here } Which of the following code fragments, when inserted individually at the commented line (// insert code here), makes use of polymorphism? (Choose 3)  

    • A.

      Public void add(B b) { b.getValue(); }

    • B.

      Public void add(C c) { c.getValue(); }

    • C.

      Public void add(A a, B b) { a.getValue(); }

    • D.

      Public void add(C c1, C c2) { c1.getValue(); }

    • E.

      Public void add(A a) { a.getValue(); }

    Correct Answer(s)
    A. Public void add(B b) { b.getValue(); }
    C. Public void add(A a, B b) { a.getValue(); }
    E. Public void add(A a) { a.getValue(); }
  • 37. 

    Consider the following Statements: Statement A:The threads are scheduled using fixed priority scheduling. Statement B:Thread priority can be set after it is created using the public int setPriority() method declared in the Thread class. Which of the following statements is correct?  

    • A.

      Both Statement A and B are false

    • B.

      Statement A is true and Statement B is false

    • C.

      Both Statement A and B are true

    • D.

      Statement A is false and Statement B is true

    Correct Answer
    B. Statement A is true and Statement B is false
  • 38. 

    class Garbage { } 2. class GC1 { 3. public static void main(String a[]) { 4. Garbage s = new Garbage(); 5. { 6. s = new Garbage(); 7. } 8. s = new Garbage(); 9. } 10. } Which of the following options gives the correct combination of lines that makes objects eligible for garbage Collection?  

    • A.

      Lines: 8

    • B.

      Lines: 6, 8

    • C.

      Lines: 4, 6, 8

    • D.

      None of the object is eligible for Garbage Collection

    • E.

      Lines: 4, 6

    Correct Answer
    A. Lines: 8
  • 39. 

    import java.io.*; public class IOCode2 { public static void main(String args[]) throws FileNotFoundException { // Insert Code here System.out.println("Welcome to File Programming"); } } Which of the following code snippets when substituted to the comment line (// Insert Code here), will redirect the output generated by the System.out.println() methods, in the above code?  

    • A.

      System.setOut(new FileWriter("C:/Data"));

    • B.

      System.redirectOutput(new PrintStream("C:/Data"));

    • C.

      System.out.setOut(new PrintStream("C:/Data"));

    • D.

      System.out.redirectOutput(new PrintStream("C:/Data"));

    • E.

      System.setOut(new PrintStream("C:/Data"));

    Correct Answer
    E. System.setOut(new PrintStream("C:/Data"));
  • 40. 

    From JDK 1.6, which of the following interfaces is also implemented by java.util.TreeSet class?  

    • A.

      NavigableSet

    • B.

      Deque

    • C.

      NavigableMap

    • D.

      NavigableList

    Correct Answer
    A. NavigableSet

Quiz Review Timeline +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Feb 14, 2013
    Quiz Edited by
    ProProfs Editorial Team
  • Mar 20, 2012
    Quiz Created by
    Seemantabasak123
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.