Java 6 Test

198 Questions | Attempts: 122
Share

SettingsSettingsSettings
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

    Correct Answer
    A. Abc
  • 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.

    Correct Answer
    D. The method length() 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

    Correct Answer
    B. IntValue
  • 4. 

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

    • A.

      Distinct

    • B.

      Synchronized

    • C.

      Volatile

    • D.

      Goto

    • E.

      Construct

    Correct Answer(s)
    B. Synchronized
    C. Volatile
    D. Goto
  • 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

    Correct Answer
    D. 64
  • 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

    Correct Answer
    C. List
  • 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.

    Correct Answer(s)
    A. Arrays in Java are objects.
    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

    Correct Answer
    A. One two three
  • 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.

    Correct Answer
    B. One
  • 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.

    Correct Answer
    A. Cannot make a static reference to the non-static method method1.
  • 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

    Correct Answer
    B. 0
  • 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

    Correct Answer
    C. 2
  • 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

    Correct Answer
    C. 0
  • 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.

    Correct Answer
    C. Compile error, Test must be declared abstract
  • 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

    Correct Answer
    C. One, two, three, default
  • 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.

    Correct Answer
    B. Default, 20
  • 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.

    Correct Answer(s)
    A. Methods cannot be overriden to be more restrictive for example private
    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 {}

    Correct Answer(s)
    B. Void method1(long i)throws Exception {}
    C. void method1(long i){}
    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

    Correct Answer
    C. On
  • 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

    Correct Answer
    A. S3=s1 + s2;
  • 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.

    Correct Answer
    B. It will not compile.
  • 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.

    Correct Answer
    C. Compiles fine and output 0 followed by 1
  • 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

    Correct Answer
    C. 10 and 40
  • 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);    }}.............

    Correct Answer
    10
  • 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

    Correct Answer
    A. Set
  • 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.

    Correct Answer
    C. Compiler error occured
  • 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[])

    Correct Answer(s)
    C. public static 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;

    Correct Answer(s)
    C. D = k;
    D. K = (int)5.5D;
  • 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 {}

    Correct Answer(s)
    A. Void m1() throws Exception{}
    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_

    Correct Answer(s)
    B. $a_
    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() { }

    Correct Answer(s)
    B. Int method() { return 0;}
    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

    Correct Answer
    C. Synchronized
  • 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

    Correct Answer(s)
    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

    Correct Answer
    A. The program does not compile as i is not initialized
  • 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

    Correct Answer
    D. The program prints 0
  • 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

    Correct Answer
    C. The code compiles fine and prints "String"
  • 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

    Correct Answer
    A. Daemon thread
  • 38. 

    What kind of objects is StringBuffer ?

    • A.

      Reference object

    • B.

      Primitive object

    • C.

      Mutable object

    • D.

      Immutable object

    • E.

      Oriented object

    Correct Answer
    C. Mutable object
  • 39. 

    What Locale object represent?

    • A.

      Geographical

    • B.

      Political

    • C.

      Cultural region

    • D.

      Language

    • E.

      All the above

    Correct Answer
    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

    Correct Answer
    A. Using profiler
  • 41. 

    What is the singleton access modifier ?

    • A.

      Private

    • B.

      Public

    • C.

      Protected

    • D.

      Default

    Correct Answer
    A. Private
  • 42. 

    Class C {public static void main(String[] args) {int[]a1[]=new int[3][3]; //3int a2[4]={3,4,5,6}; //4int a2[5]; //5}}What is the result of attempting to compile and run the program ?.

    • A.

      Compiletime error at lines 3,4,5

    • B.

      Compiltime error at line 4,5

    • C.

      Compiletime error at line 3

    • D.

      Runtime Exception

    • E.

      None of the above

    Correct Answer
    B. Compiltime error at line 4,5
  • 43. 

    interface I{void f1(); // 1public void f2(); // 2protected void f3(); // 3private void f4(); // 4}which lines generate compile time errors?

    • A.

      Compiletime error at lines 1,2,3,4

    • B.

      Compiletime error at line 3

    • C.

      Compiletime error at line 1

    • D.

      Compiletime error at lines 3,4

    • E.

      None of the above

    Correct Answer
    D. Compiletime error at lines 3,4
  • 44. 

    class C{int i;public static void main (String[] args) {int i; //1private int a = 1; //2protected int b = 1; //3public int c = 1; //4System.out.println(a+b+c); //5}}

    • A.

      Compiletime error at lines 1,2,3,4,5

    • B.

      Compiletime error at lines 2,3,4,5

    • C.

      Compiletime error at lines 2,3,4

    • D.

      Prints 3

    • E.

      None of the above

    Correct Answer
    B. Compiletime error at lines 2,3,4,5
  • 45. 

    class C {public static void main (String[] a1) {System.out.print(a1[1] + a1[2] + a1[3]);}}What is the result of attempting to compile and run the program? java command A B C

    • A.

      Prints: ABC

    • B.

      Prints BC and Runtime Exception

    • C.

      Prints: BCD

    • D.

      Runtime Exception

    • E.

      None of the above

    Correct Answer
    B. Prints BC and Runtime Exception
  • 46. 

    class C{static int s;public static void main(String a[]){C obj=new C();obj.m1();System.out.println(s);}void m1();{int x=1;m2(x);System.out.println(x+"");}void m2(int x){x=x*2;s=x;}}

    • A.

      Prints 1,2

    • B.

      Prints 2,0

    • C.

      Prints 2,2

    • D.

      Compile time error

    • E.

      None of the above

    Correct Answer
    A. Prints 1,2
  • 47. 

    class C {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");}}}What is the result of attempting to compile and run the program? 

    • A.

      Prints one two three

    • B.

      Prints one

    • C.

      Compile time error

    • D.

      Runtime exception

    • E.

      None of the above

    Correct Answer
    A. Prints one two three
  • 48. 

     Each element must be unique Duplicate elements must not replace old elements. Elements are not key/value pairs.Accessing an element can be almost as fast as performing a similar operation on an array.Which of these classes provide the specified features?

    • A.

      LinkedList

    • B.

      TreeMap

    • C.

      HashMap

    • D.

      HashSet

    • E.

      None of the above

    Correct Answer
    D. HashSet
  • 49. 

    class C1{static interface I{static class C2{}}public static void main(String a[]){C1.I.C2 ob1=new C1.I.C2();System.out.println("object created");}}What is the result of attempting to compile and run the program?

    • A.

      Prints object created

    • B.

      Compile time error

    • C.

      Runtime Excepion

    • D.

      None of the above

    Correct Answer
    A. Prints object created
  • 50. 

    class C1{static class C2{static int i1;}public static void main(String a[]){System.out.println(C1.C2.i1);}}What is the result of attempting to compile and run the program?

    • A.

      Prints 0

    • B.

      Compile time error

    • C.

      Runtime exception

    • D.

      None of the above

    Correct Answer
    A. Prints 0

Quiz Review Timeline +

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

  • Current Version
  • Jan 11, 2013
    Quiz Edited by
    ProProfs Editorial Team
  • Apr 20, 2010
    Quiz Created by
    Pearlox
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.