An Advanced Quiz On Core Java!

45 Questions | Total Attempts: 645

SettingsSettingsSettings
Please wait...
An Advanced Quiz On Core Java!

JAVA is everywhere, from your microwave oven to DVD player, TV remotes to music players, almost everywhere JAVA is used. Well, cut to short, Core Java focuses on the language basics such as data structures, data types, operators, control statements, string handling, exception handling, etc. Here in this quiz, you will face forty-five questions of the same. So, let's get started.


Questions and Answers
  • 1. 
    Which of the following are uses of Object class?(Choose 3)
    • A. 

      To achieve inheritance at user-defined class level

    • B. 

      To generate String representation of an object

    • C. 

      To get the HashCode for an object

    • D. 

      To achieve polymorphism at user-defined class level

    • E. 

      To handle any Java Object in the name of Object

  • 2. 
    Which of the following options are true for StringBuffer class?(choose 3)
    • A. 

      StringBuffer is extended from String class

    • B. 

      StringBuffer is threadsafe

    • C. 

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

    • D. 

      StringBuffer implements Charsequence interface

    • E. 

      Buffer space in StringBuffer can be shared

  • 3. 
    Consider the following code snippet: class Train { String name = "Shatapdhi"; } class TestTrain { public static void main(String a[]) { Train t = new Train(); System.out.println(t); // Line a System.out.println(t.toString()); // Line b } } Which of the following statements are true?(Choose 3)
    • A. 

      Output of Line a and Line b will be same

    • B. 

      Output of Line a and Line b will be different

    • C. 

      Line a prints the corresponding classname with Object'shashcode in Hexadecimal

    • D. 

      Both Line a and Line b will print the corresponding classnamewith Object's hashcode in Hexa Decimal

    • E. 

      Both Line a and Line b prints "Shatapdhi"

  • 4. 
    Which of the following are true regarding PreparedStatement?(choose 3)
    • A. 

      PreparedStatement are the SQL templates available in thedatabase server, that can be used directly without writingcomplex SQL code

    • B. 

      PreparedStatement cannot be used to create ScrollableResultSet

    • C. 

      Parameters can be passed to PreparedStatement at run-time

    • D. 

      PreparedStatements are precompiled SQL statements thatare faster in execution

    • E. 

      PreparedStatement is the sub interface of Statementinterface

  • 5. 
    Consider the following code snippet: public class TestString10{ public void print() { String s = "Hello"; StringBuffer sb = new StringBuffer("Hello"); concatinateStrings(s, sb); System.out.println(s+" "+sb); } public void concatinateStrings(String str, StringBuffer strBuff){ StringBuffer sk = strBuff; str = str + " world"; sk.append(" world"); } public static void main (String[] args) { TestString10 t = new TestString10(); t.print(); } } What will be the output of the above code snippet?
    • A. 

      Hello Hello world

    • B. 

      World world world

    • C. 

      Hello Hello Hello

    • D. 

      World Hello Hello

    • E. 

      Hello world Hello

  • 6. 
    Consider the following code: 1. public class Circle1 { 2. private String string = "String1"; 3. void work() { 4. String x = "String2"; 5. class Circle2 { 6. public void peepOut() { 7. System.out.println(string); 8. System.out.println(x); 9. } 10. } 11. new Circle2().peepOut(); 12. } 13. 14. public static void main(String args[]) { 15. Circle1 c1 = new Circle1(); 16. c1.work(); 17. } 18. } Which of the following changes made to the above code will make the code to compile and execute properly and gives the following output? String1 String2
    • A. 

      The variable at line 4 should be declared as final

    • B. 

      The variable at line 2 should be declared as final

    • C. 

      The method at line 6 should be defined as final method

    • D. 

      The inner class Circle 2 should be an abstract class

    • E. 

      The object for the inner class Circle2 should be created in main() method

  • 7. 
    Consider s1 and s2 are sets. Which of the following options gives the exact meaning of the method call s1.retainAll(s2)?
    • A. 

      Transforms s1 into the union of s1 and s2

    • B. 

      Transforms s1 into the intersection of s1 and s2.

    • C. 

      Returns true if s2 is a subset of s1

    • D. 

      Transforms s1 into the (asymmetric) set difference of s1 ands2

    • E. 

      Copies elements from s2 to s1

  • 8. 
    Consider the following scenario: A Chat application written in Java, currently works with a general room facility, where the messages posted by the logged user are displayed. A common synchronized object is ued to Queue up the messages received from the users. Now, the application needs additional feature called personal messaging, that enables the user to make one-to-one communication. Which of the following helps to implement the requirement?
    • A. 

      A new synchronized object need to be created for every oneto-one personal messaging request from the user

    • B. 

      The personal messaging feature cannot be added when thereis a general room facility

    • C. 

      A part of the synchronized object that already exists for thegeneral room can be used

    • D. 

      Just two more threads need to be created at the user end

  • 9. 
    Under which of the following scenarios a checked exception is thrown? (Choose 2)
    • A. 

      5th element of an array is accessed, whose size is 3

    • B. 

      A file that actually does not exist, is opened for reading

    • C. 

      An attempt to connect to the database is made but failed.

    • D. 

      Length() method is called on a String object, that is assignedto null

    • E. 

      Given username and password is checked with database andfound invalid

  • 10. 
    Which of the following statement is true?
    • A. 

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

    • B. 

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

    • C. 

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

    • D. 

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

    • E. 

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

  • 11. 
    Consider the following code: class Animal { public String noise() { return "noise"; } } class Dog extends Animal { public String noise() { return "bark"; } } class Cat extends Animal { public String noise() { return "meow"; } } class MakeNoise { public static void main(String args[]) { Animal animal = new Dog(); Cat cat = (Cat)animal; System.out.println(cat.noise()); } } Which of the following option will be the output of the above code snippet?
    • A. 

      Noise

    • B. 

      Bark

    • C. 

      Meow

    • D. 

      Compilation fails

    • E. 

      An exception is thrown at runtime

  • 12. 
    Consider the following code: public class GetArray { public static void main(String args[]) { float invt[][]; float[] prct, grts[]; float[][] sms, hms[]; (// Insert statement1 here) (// Insert statement2 here)  (// Insert statement3 here) } } Which of the following listed statements can be inserted at the above commented lines (// Insert statement1 here, // Insert statement2 here, // Insert statement3 here) to make the program to compile without errors? (Choose 3)
    • A. 

      Grts = new float[1][4];

    • B. 

      Invt = grts;

    • C. 

      Hms = new float[2][5];

    • D. 

      Invt = new float[4][2];

    • E. 

      Grts = new float[1];

  • 13. 
    Consider the following code: public class SwitchCase { public static void main(String args[]) { int x = 10; switch(x) { case 10: System.out.println("10"); case 10: System.out.println("10"); case 20: System.out.println("20"); default: System.out.println("30"); } } } Which of the following will be the output for the above program?
    • A. 

      101020

    • B. 

      1020

    • C. 

      1010

    • D. 

      Compilation Error

    • E. 

      3014

  • 14. 
    Which of the following is the process of creating a new class from an existing class?
    • A. 

      Polymorphism

    • B. 

      Abstraction

    • C. 

      Inheritance

    • D. 

      Reusability

  • 15. 
    Consider the following code snippet: String deepak = "Did Deepak see bees? Deepak did."; Which of the following options will give the output for the method call deepak.charAt(10)?
    • A. 

      None of the listed options

    • B. 

      S

    • C. 

      H

    • D. 

      Space

  • 16. 
    Consider the following code: package com.java.test; public class A { public void m1() {System.out.print("A.m1, ");} protected void m2() {System.out.print("A.m2, ");} private void m3() {System.out.print("A.m3, ");} void m4() {System.out.print("A.m4, ");} } class B { public static void main(String[] args) { A a = new A(); a.m1(); // 1 a.m2(); // 2 a.m3(); // 3 a.m4(); // 4 } } Assume that both of the above classes are stored in a single source file called 'A.java'. Which of the following gives the valid output of the above code?
    • A. 

      Prints: A.m1, A.m2, A.m3, A.m4,

    • B. 

      Compile-time error at 3.

    • C. 

      Compile-time error at 4.

    • D. 

      Compile-time error at 2.

    • E. 

      Compile-time error at 1.

  • 17. 
    From a Collection object c, another Collection object needs to be created. It should contain the same elements in the same order as that of source object c, but with all duplicates eliminated. Which of the following options provide the valid code to accomplish the above given scenario?
    • A. 

      New HashSet(c);

    • B. 

      All of the listed options

    • C. 

      New LinkedTreeSet(c);

    • D. 

      New LinkedHashSet(c);

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

      NavigableSet

    • B. 

      NavigableList

    • C. 

      NavigableMap

    • D. 

      Deque

  • 19. 
    Which of the following modifier cannot be applied to the declaration of a field (member of a class)?
    • A. 

      Public

    • B. 

      Final

    • C. 

      Abstract

    • D. 

      Private

    • E. 

      Protected

  • 20. 
    Which of the following statements are correct regarding Instance Block?(Choose 3)
    • A. 

      A class can have more than one instance block

    • B. 

      An instance block cannot initialise the class members

    • C. 

      Instance blocks are executed only when the instances arecreated from main() method of that class

    • D. 

      Instance blocks are executed before constructors

    • E. 

      Instance blocks are executed for every created instance

  • 21. 
    Which of the following is the correct syntax for Annotation declaration?
    • A. 

      Interface author{@String name(),String date()}

    • B. 

      @interface author{@String name();@String date();}

    • C. 

      Interface author{String name(),String date()}

    • D. 

      Interface @author{String name(),String date()}

    • E. 

      @interface author{String name();String date();}

  • 22. 
    Consider the following code: class Resource { } class UserThread extends Thread { public Resource res; public void run() { try { synchronized(res) { System.out.println("Planet"); res.wait(); Thread.sleep(1000); res.notify(); System.out.println("Earth"); } } catch(InterruptedException e) { } } } public class StartUserThreads { public static void main(String[] args) { Resource r = new Resource(); UserThread ut1 = new UserThread(); ut1.res = r; UserThread ut2 = new UserThread(); ut2.res = r; ut1.start(); ut2.start(); } } Which of the following will give the correct output for the above code?
    • A. 

      Runtime Error "IllegalThreadStateException"

    • B. 

      Compile-time Error

    • C. 

      Prints the following output 2 times: Planet (waits for 1000 milli seconds) Earth

    • D. 

      Prints the following output: Planet Planet (get into long wait. Never ends)

    • E. 

      Prints the following output 1 time: Planet (waits for 1000 milli seconds) Earth

  • 23. 
    Which of the following options gives the relationship between a Spreadsheet Object and Cell Objects?
    • A. 

      Polymorphism

    • B. 

      Association

    • C. 

      Inheritance

    • D. 

      Aggregation

    • E. 

      Persistence

  • 24. 
    Consider the following program: class joy extends Exception { } class smile extends joy { } interface happy { void a() throws smile; void z() throws smile; } class one extends Exception { } class two extends one { } abstract class test { public void a()throws one { } public void b() throws one { } } public class check extends test { public void a() throws smile { System.out.println("welcome"); throw new smile(); } public void b() throws one { throw new two(); } public void z() throws smile { throw new smile(); } public static void main(String args[]) { try { check obj=new check(); obj.b(); obj.a(); obj.z(); } catch(smile s) { System.out.println(s); }catch(two t) { System.out.println(t.getClass()); }catch(one o) { System.out.println(o); }catch(Exception e) { System.out.println(e); } } } What will be the output of the above program?
    • A. 

      Throws a compile time exception as overridden method a() does not throw exception smile

    • B. 

      Welcome class two

    • C. 

      Two

    • D. 

      Throws a compile time exception as overridden method z() does not throw exception smile

    • E. 

      Class two

  • 25. 
    Consider the following code snippet: public class TasteIt{ public void show() { System.out.println("one"); } public static void main(String[] args) { TasteIt t = new TasteIt() { public void show() { System.out.println("two"); super.show(); } }; t.show(); } } Which of the following option will be the output for the above code snippet?
    • A. 

      One two

    • B. 

      Two one

    • C. 

      Compilation fails. Cannot use super keyword inside an anonymous class

    • D. 

      Runtime exception. Cannot find the super class version of show() method

Back to Top Back to top