Related Topics
Take Another Quiz

Java 6 Test

40 Questions
Java Quizzes & Trivia

Pearlox Corp. Java 6 Test

Questions and Answers
  • 1. 
    What will be the output if you compile and run the following code ?(Choose the correct answer)public class test {   public static void main(String args[]) {       String str1="abc";      String str2="def";      String str3=str1.concat(str2);      str1.concat(str2);      System.out.println(str1);   }}
    • A. 

      Abc

    • B. 

      Def

    • C. 

      Abcabc

    • D. 

      Abcdef

    • E. 

      Defabc

  • 2. 
    The number of characters in an object of a class String is given by(Choose the correct answer)
    • A. 

      The member variable called size

    • B. 

      The member variable called length

    • C. 

      The method size() returns the number of characters.

    • D. 

      The method length() returns the number of characters.

    • E. 

      The method getSize() returns the number of characters.

  • 3. 
    Which method defined in Integer class can be used to convert an Integer object to primitive int type.(Choose the correct answer)
    • A. 

      ValueOf

    • B. 

      IntValue

    • C. 

      GetInt

    • D. 

      GetInteger

    • E. 

      ToInt

  • 4. 
    Which are valid Java keywords.(Choose all that apply)
    • A. 

      Distinct

    • B. 

      Synchronized

    • C. 

      Volatile

    • D. 

      Goto

    • E. 

      Construct

  • 5. 
    What is the number of bytes used by the primitive type long. (Select the best answer)
    • A. 

      8

    • B. 

      16

    • C. 

      32

    • D. 

      64

    • E. 

      128

  • 6. 
    What is the name of the Collection interface used to represent elements in a sequence (in a particular order). (Choose the correct answer)
    • A. 

      Collection

    • B. 

      Set

    • C. 

      List

    • D. 

      Map

    • E. 

      HashMap

  • 7. 
    Which of the following statements about arrays are true.(Choose all that apply)
    • A. 

      Arrays in Java are objects.

    • B. 

      It is not possible to assign one array to another.

    • C. 

      Array elements are indexed from 1 to size of the array.

    • D. 

      If a method tries to access an array element beyond its range, a compile warning is generated.

    • E. 

      Arrays size must be specified at creation time.

  • 8. 
    What will be the output if you compile and run the following code ?public class Main {public static void main(String[] args) {  int i1=1; switch(i1){     case 1:              System.out.println("one");     case 2:              System.out.println("two");     case 3:              System.out.println("three");}}}
    • A. 

      One two three

    • B. 

      One

    • C. 

      One two

    • D. 

      Compile error.

    • E. 

      None of the above

  • 9. 
    What will be the output if you compile and run the following code ?public class Main {    public static void main(String[] args) {          char c = 'a';                     switch(c){             case 1:                      System.out.println("one");break;             case 'a':                      System.out.println("two");break;             case 3:                      System.out.println("three");        }    }}
    • A. 

      One two three

    • B. 

      One

    • C. 

      Two

    • D. 

      Three

    • E. 

      Compile error.

  • 10. 
    What will be the output if you compile and run the following code ?public class Main {    public static void main(String[] arg) {    method1(arg);    }    public void method1(String... arguments) {    System.out.println(arguments);    }}
    • A. 

      Cannot make a static reference to the non-static method method1.

    • B. 

      Compile error method main is not correct

    • C. 

      Compile error method method1 is not correct

    • D. 

      Method1 must be declared with String not String...

    • E. 

      This code compiles and run without errors.

  • 11. 
    What will be the output if you compile and run the following code ?public class Main{ private static int i; public static void main(String[] args){ System.out.println(i); }}
    • A. 

      Error, Variable i may not have been initialized

    • B. 

      0

    • C. 

      1

    • D. 

      Null

    • E. 

      None of the above

  • 12. 
    What will be the output if you compile and run the following code ?public class Main { public static void main(String[] args){ int vest[]=new int[]{1,2,3}; System.out.println(vest[1]); }}
    • A. 

      1

    • B. 

      Error vest is referenced before it is initialized

    • C. 

      2

    • D. 

      Error: size of array must be defined

    • E. 

      None of the above

  • 13. 
    What will be the output if you compile and run the following code ?public class Test { public static void main(String[] arg){ int anar[]=new int[5]; System.out.println(anar[0]); }}
    • A. 

      Error: anar is referenced before it is initialized

    • B. 

      Null

    • C. 

      0

    • D. 

      5

    • E. 

      4

  • 14. 
    What will be the output if you compile and run the following code ?abstract class Super { abstract void amethod(); static int i;}public class Test extends Super { public static void main(String... args){ int[] arr = new int[5]; for(i=0;i < arr.length;i++) System.out.println(arr[i]); }}
    • A. 

      0 1 2 3 4 is printed

    • B. 

      Compile error, arr is used before it is initialized

    • C. 

      Compile error, Test must be declared abstract

    • D. 

      ArrayIndexOutOfBoundes Exception

    • E. 

      None of the above.

  • 15. 
    What will be the output if you compile and run the following code ?int i=1; switch (i) { case 0: System.out.println("number"); break; case 1: System.out.println("one"); case 5: System.out.println("two");case 10: System.out.println("three"); default: System.out.println("default"); }
    • A. 

      One

    • B. 

      One, default

    • C. 

      One, two, three, default

    • D. 

      Default

    • E. 

      Two

  • 16. 
    What will be the output if you compile and run the following code ?int h=15;switch (h) {         default:         System.out.println("default");         case 20:         System.out.println("20");         break;         case 21:         System.out.println("21");         case 22:         System.out.println("22");        }
    • A. 

      Default

    • B. 

      Default, 20

    • C. 

      20, 21

    • D. 

      No output is displayed

    • E. 

      Compiler error default must be defined last.

  • 17. 
    Which of the following statements are correct ?
    • A. 

      Methods cannot be overriden to be more restrictive for example private

    • B. 

      Static methods cannot be overloaded

    • C. 

      Private methods cannot be overloaded

    • D. 

      Abstract methods cannot be overriden in subclasses.

    • E. 

      Final methods cannot be overriden in subclasses.

  • 18. 
    Which of the following methods can be legally inserted in place of the comment //Insert Method Here ..class Main{ public void method1(int i) { }}public class Sub extends Main{ public static void main(String[] args){ } //Insert Method Here ..}
    • A. 

      Void method1(int k) throws Exception {}

    • B. 

      Void method1(long i)throws Exception {}

    • C. 

      void method1(long i){}

    • D. 

      Public void method1(int j) throws Exception {}

    • E. 

      Public void method1(double d) throws Exception {}

  • 19. 
    What will be the output if you compile and run the following code ?String str=new String("honey");int begin=1;int end=3;System.out.println(str.substring(begin,end));
    • A. 

      Hon

    • B. 

      Ho

    • C. 

      On

    • D. 

      Runtime Exception is thrown

    • E. 

      None of the above

  • 20. 
    Given the following declarationsString s1=new String("1")String s2=new String("2");String s3=new String("true");String s3=new String("false");Which of the following are legal operations?
    • A. 

      S3=s1 + s2;

    • B. 

      S3=s1-s2;

    • C. 

      S3=s1 & s2;

    • D. 

      S3=s1 && s2

    • E. 

      S3=s3 && s4

  • 21. 
    What will happen when you attempt to compile and run the following code?class TestRunnable implements Runnable {    int k = 0;    public int run() {        while (true) {            k++;            System.out.print("k=" + k);        }        return 50;    }}
    • A. 

      It will compile fine.

    • B. 

      It will not compile.

    • C. 

      The code will compile but throws an exception at runtime.

    • D. 

      Comipler errors because while cannot take a parameter of true.

    • E. 

      None of the above.

  • 22. 
    Which of the following statements about this code are true?class Testing extends Thread{    public void run(){        for(int i =0; i < 2; i++){            System.out.println(i);        }        }    }public class Sub{    public static void main(String[] args){        Sub m = new Sub();        m.go(new Testing());        }    public void go(Testing t){        t.start();        }    }
    • A. 

      This program compiles and run fine, but it has no output.

    • B. 

      Compiler error, class Testing has no start method

    • C. 

      Compiles fine and output 0 followed by 1

    • D. 

      Compiles fine but fails at runtime.

    • E. 

      Compiler error, class Testing doesnt provide legal override for method run.

  • 23. 
     What will happen if you attempt to compile and run the following code?.public class Main {    static int j = 20;    public static void main(String[] args) {        int i = 10;        Main m = new Main();        m.m1(i);        System.out.println(i);        System.out.println(j);    }    public void m1(int x) {        x = x * 2;        j = j * 2;    }}
    • A. 

      Compiler error, static access from non static context.

    • B. 

      20 and 40

    • C. 

      10 and 40

    • D. 

      10 and 20

    • E. 

      20 and 20

  • 24. 
    What will happen if you attempt to compile and run the following code?.public class Main {    public static void main(String[] args) {        Main m = new Main();        m.m1();    }    public void m1() {        int ii = 012;        System.out.println(ii);    }}.............
  • 25. 
    You have a requirement to create a class that will store unique object elements. And you do not need to sort these elements.Which interface might be most suitable to meet your requirement?
    • A. 

      Set

    • B. 

      List

    • C. 

      Map

    • D. 

      ArrayList

    • E. 

      Collection

  • 26. 
    What will happen if you attempt to compile and run the following code?.public class Main {    private int ii;    public static void main(String[] args) {        Main m = new Main();        m.m1();    }    public static void m1() {        System.out.println(ii);    }}
    • A. 

      A value of 0 will be printed to the standard output

    • B. 

      Nothing will be printed

    • C. 

      Compiler error occured

    • D. 

      A Compiler error occured complaining of the scope of variable ii

    • E. 

      Runtime exception is thrown.

  • 27. 
    Which declares a valid main method. (Choose all that apply)
    • A. 

      Public static void Main(String[] args)

    • B. 

      Static void main(String args...)

    • C. 

      public static void main(String... args)

    • D. 

      Public void main(String args)

    • E. 

      Public static void main(String arguments[])

  • 28. 
    Given the following declarations, which of the assignments given below would compile.(Choose all that apply)int k = 20;boolean b = true;float f = 2.5F;double d = 2.5;
    • A. 

      B = (boolean) k;

    • B. 

      F = d;

    • C. 

      D = k;

    • D. 

      K = (int)5.5D;

    • E. 

      F = 2.8;

  • 29. 
    Which defines a legal method declaration ?(Choose all that apply)
    • A. 

      Void m1() throws Exception{}

    • B. 

      Void m2(int) throw Exception{}

    • C. 

      Private m3() {}

    • D. 

      Volatile m4() throws IOException{}

    • E. 

      Private synchronized void m5() throws Exception {}

  • 30. 
     Which are legal identifier names in Java. (Choose all that apply)
    • A. 

      True

    • B. 

      $a_

    • C. 

      1a

    • D. 

      A1

    • E. 

      _this_is_a_lengthy_variable_name_in_java_

  • 31. 
    void method() { }Which of the following declares a legal override of this method in a sub class.(Choose all that apply)
    • A. 

      Void method() { }

    • B. 

      Int method() { return 0;}

    • C. 

      Void method(int i) { }

    • D. 

      private void method() { }

    • E. 

      Protected void method() { }

  • 32. 
    Which keyword when applied on a method indicates that only one thread can execute this method at a time. (Choose the correct answer)
    • A. 

      Transient

    • B. 

      Volatile

    • C. 

      Synchronized

    • D. 

      Native

    • E. 

      Static

  • 33. 
    Which of these statements are true. (Choose all that apply)
    • A. 

      For each try block there must be at least one catch block defined

    • B. 

      A try block may be followed by any number of finally blocks

    • C. 

      A try block must be followed by at least one finally or catch block

    • D. 

      If both catch and finally blocks are defined, catch block must appear before finally

    • E. 

      A try block may be defined inside a catch block

  • 34. 
    What gets printed when the following program is compiled and run? (Choose the correct answer)class Main {    public static void main(String[] args) {        int i;        do {            i++;        }        while(i < 0);        System.out.println(i);    }}
    • A. 

      The program does not compile as i is not initialized

    • B. 

      The program compiles but does not run.

    • C. 

      The program compiles and runs but does not print anything

    • D. 

      The program prints 0

    • E. 

      The program prints 1

  • 35. 
    What gets printed when the following program is compiled and run? (Choose the correct answer)class Main {    static int i;    public static void main(String... args) {        while (i < 0) {            i--;        }        System.out.println(i);    }}
    • A. 

      The program does not compile as i is not initialized

    • B. 

      The program compiles but does not run

    • C. 

      The program compiles and runs but does not print anything

    • D. 

      The program prints 0

    • E. 

      The program prints 1

  • 36. 
    What gets printed when the following program is compiled and run? public class Main {    public void m1(Object o) {        System.out.println("Object");    }    public void m1(String s) {        System.out.println("String");    }    public static void main(String args[]) {        Main m = new Main();        m.m1(null);    }}
    • A. 

      The code does not compile

    • B. 

      The code compiles fine and prints "Object"

    • C. 

      The code compiles fine and prints "String"

    • D. 

      The code throws an Exception at Runtime

    • E. 

      The output cannot be determined

  • 37. 
    What kind of thread is the Garbage collector thread?
    • A. 

      Daemon thread

    • B. 

      Kernel thread

    • C. 

      User thread

    • D. 

      Worker thread

    • E. 

      Hyper threaed

  • 38. 
    What kind of objects is StringBuffer ?
    • A. 

      Reference object

    • B. 

      Primitive object

    • C. 

      Mutable object

    • D. 

      Immutable object

    • E. 

      Oriented object

  • 39. 
    What Locale object represent?
    • A. 

      Geographical

    • B. 

      Political

    • C. 

      Cultural region

    • D. 

      Language

    • E. 

      All the above

  • 40. 
    What is the best way to findout the time/memory consuming process?
    • A. 

      Using profiler

    • B. 

      Using logger

    • C. 

      Using debuger

    • D. 

      Using sysstat

    • E. 

      Using analizer

Related Topics