Exam Java Two

99 Questions | Attempts: 161
Share

SettingsSettingsSettings
Java Quizzes & Trivia

Pearlox Corp.
Java 6 test


Questions and Answers
  • 1. 

    The constructor for the Math class is private, so it cannot be instaniated

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
  • 2. 

    class C {public static void main(String[] args) { int x=2; int y=3;   if((y==x++)|(x<++y)){                              System.out.println(x+""+y);        }}}

    • A.

      Prints 34

    • B.

      Prints 33

    • C.

      Compile time error

    • D.

      Runtime exception

    • E.

      None of the above

    Correct Answer
    A. Prints 34
  • 3. 

    interface I{final class C1   {     //1       static int i=9;;  //2 }}class C2 implements I{ public static void main(String a[]){    System.out.println(I.C1.i);  ///3}}

    • A.

      Compile time error at line 1

    • B.

      Compile time error at line 2

    • C.

      Compile time error at line 3

    • D.

      Prints 9

    • E.

      Runtime exception

    Correct Answer
    D. Prints 9
  • 4. 

    class C{public static void main(String[] args) {  try    {        try           {               try                 {                   }                 catch(RuntimeException e)                 {                  }             }           catch(Exception e)           {            }                    }    catch(NullPointerException e)    {        }     finally    {              System.out.println("finally");       }}}

    • A.

      Prints finally

    • B.

      Compile time error

    • C.

      Runtime Exception

    • D.

      None of the above

    Correct Answer
    A. Prints finally
  • 5. 

    class C {  public static void main ( String ka [ ] ) {          while ( false ) {                  System.out.println ( "Value" ) ;          }  }}

    • A.

      Compile time error

    • B.

      Prints Value infinitely

    • C.

      Runtime Exception

    • D.

      None of the above

    Correct Answer
    A. Compile time error
  • 6. 

    Class C {public static void main(String[] args) {  System.out.println(4+5+"String");}}

    • A.

      Prints 9string

    • B.

      Prints 45string

    • C.

      Compile time error

    • D.

      Runtime exception

    • E.

      None of the above

    Correct Answer
    A. Prints 9string
  • 7. 

    class H {  public static void main (String[] args) {    String s1 = "HHH";    StringBuffer sb2 = new StringBuffer(s1);    System.out.print(sb2.equals(s1) + "," + s1.equals(sb2));}}

    • A.

      Prints: false,false

    • B.

      Prints: true,false

    • C.

      Prints: false,true

    • D.

      Prints: true,true

    • E.

      None of the above

    Correct Answer
    A. Prints: false,false
  • 8. 

    The relationship between a class and its superclass is

    • A.

      Has-a

    • B.

      Is -a

    • C.

      None of the above

    Correct Answer
    B. Is -a
  • 9. 

    class A extends Thread {  private int i;  public void run() {i = 1;}  public static void main(String[] args) {    A a = new A();    a.run();    System.out.print(a.i);}}How many threads are created in this Program?

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      0

    • E.

      None of the above

    Correct Answer
    A. 1
  • 10. 

    String objects once created can not be modified

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
  • 11. 

    Which of the following modifiers can be applied to a class that is not a nested class?

    • A.

      Public

    • B.

      Protected

    • C.

      Private

    • D.

      Static

    Correct Answer
    A. Public
  • 12. 

    if(0.0 == -0.0) {  System.out.println("true");}else{    System.out.println("false");}

    • A.

      Prints false

    • B.

      Prints true

    Correct Answer
    B. Prints true
  • 13. 

    class C {public static void main(String[] args) {double d1 = Math.floor(0.5);double d2 = Math.floor(1.5);System.out.print(d1 + "," + d2);}}

    • A.

      Prints: 0.0,1.0

    • B.

      Prints: 0.0,2.0

    • C.

      Prints: 1.0,1.0

    • D.

      Prints: 1.0,2.0

    • E.

      None of the above

    Correct Answer
    A. Prints: 0.0,1.0
  • 14. 

    System.out.println("String".substring(0,4));This statement will Print

    • A.

      Will print "Strin"

    • B.

      Will print "Stri"

    • C.

      Will cause compiler error

    • D.

      None of the above

    Correct Answer
    B. Will print "Stri"
  • 15. 

    if("String".replace('t','T') == "String".replace('t','T'))System.out.println("Equal");elseSystem.out.println("Not Equal");

    • A.

      Will Print Equal

    • B.

      Will Print Not Equal

    • C.

      Compile time error

    • D.

      None of the above

    Correct Answer
    B. Will Print Not Equal
  • 16. 

    Which of the following classes  will not allow unsynchronized read operations by multiple threads?

    • A.

      Vector

    • B.

      TreeMap

    • C.

      TreeSet

    • D.

      Hashmap

    • E.

      HashSet

    Correct Answer
    A. Vector
  • 17. 

    StringBuffer objects once created can not be modified

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
  • 18. 

    Which declaration of the main method below would allow a class to be started as a standalone program. Select the one correct answer

    • A.

      Public static int main(char args[])

    • B.

      Public static void main(String args[])

    • C.

      Public static void MAIN(String args[])

    • D.

      Public static void main(String args)

    • E.

      Public static void main(char args[])

    Correct Answer
    B. Public static void main(String args[])
  • 19. 

    What all gets printed when the following code is compiled and run? Select the three correct answers.      public class xyz {         public static void main(String args[]) {            for(int i = 0; i < 2; i++) {               for(int j = 2; j>= 0; j--) {                  if(i == j) break;                  System.out.println("i=" + i + " j="+j);               }            }         }      }

    • A.

      I=0 j=0

    • B.

      I=0 j=1

    • C.

      I=0 j=2

    • D.

      I=1 j=0

    • E.

      I=1 j=2

    Correct Answer(s)
    B. I=0 j=1
    C. I=0 j=2
    E. I=1 j=2
  • 20. 

      What gets printed when the following code is compiled and run with the following command -      java test 2      Select the one correct answer.      public class test {         public static void main(String args[]) {             Integer intObj=Integer.valueOf(args[args.length-1]);            int i = intObj.intValue();            if(args.length > 1)                System.out.println(i);            if(args.length > 0)               System.out.println(i - 1);            else                System.out.println(i - 2);         }      }             

    • A.

      Test

    • B.

      Test -1

    • C.

      0

    • D.

      1

    • E.

      2

    Correct Answer
    D. 1
  • 21. 

    In Java technology what expression can be used to represent number of elements in an array named arr ? ...........

    Correct Answer
    arr.length
  • 22. 

    How would the number 5 be represented in hex using up-to four characters? ............

    Correct Answer
    0x5
    0x05
    0X05
    0X5
  • 23. 

    Is the following statement true or false. The constructor of a class must not have a return type.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
  • 24. 

    Is the following statement true or false. The constructor of a class must not have a return type.

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
  • 25. 

    What is the number of bytes used by Java primitive long. Select the one correct answer.

    • A.

      The number of bytes is compiler dependent

    • B.

      2

    • C.

      4

    • D.

      8

    • E.

      64

    Correct Answer
    D. 8
  • 26. 

    What is returned when the method substring(2, 4) is invoked on the string "example"? Include the answer in quotes as the result is of type String. .........

    Correct Answer
    "am"
  • 27. 

    Which of the following is correct? Select the two correct answers.

    • A.

      The native keyword indicates that the method is implemented in another language like C/C++.

    • B.

      The only statements that can appear before an import statement in a Java file are comments.

    • C.

      The method definitions inside interfaces are public and abstract. They cannot be private or protected.

    • D.

      A class constructor may have public or protected keyword before them, nothing else.

    Correct Answer(s)
    A. The native keyword indicates that the method is implemented in another language like C/C++.
    C. The method definitions inside interfaces are public and abstract. They cannot be private or protected.
  • 28. 

    What is the result of evaluating the expression 14 ^ 23. Select the one correct answer

    • A.

      25

    • B.

      37

    • C.

      6

    • D.

      31

    • E.

      17

    Correct Answer
    A. 25
  • 29. 

      Which of the following are true. Select the one correct answers

    • A.

      && operator is used for short-circuited logical AND

    • B.

      ~ operator is the bit-wise XOR operator

    • C.

      | operator is used to perform bitwise OR and also short-circuited logical OR.

    • D.

      The unsigned right shift operator in Java is >>.

    Correct Answer
    A. && operator is used for short-circuited logical AND
  • 30. 

     Name the access modifier which when used with a method, makes it available to all the classes in the same package and to all the subclasses of the class.

    Correct Answer
    protected
  • 31. 

    Which of the following is true. Select the two correct answers

    • A.

      A class that is abstract may not be instantiated.

    • B.

      The final keyword indicates that the body of a method is to be found elsewhere. The code is written in non-Java language, typically in C/C++.

    • C.

      A static variable indicates there is only one copy of that variable.

    • D.

      A method defined as private indicates that it is accessible to all other classes in the same package

    Correct Answer(s)
    A. A class that is abstract may not be instantiated.
    C. A static variable indicates there is only one copy of that variable.
  • 32. 

    What all gets printed when the following program is compiled and run. Select the two correct answers.      public class test {         public static void main(String args[]) {             int i, j=1;            i = (j>1)?2:1;            switch(i) {              case 0: System.out.println(0); break;              case 1: System.out.println(1);              case 2: System.out.println(2); break;              case 3: System.out.println(3); break;            }         }      }             

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      3

    Correct Answer(s)
    B. 1
    C. 2
  • 33. 

    What all gets printed when the following program is compiled and run. Select the one correct answer.      public class test {         public static void main(String args[]) {             int i=0, j=2;            do {               i=++i;               j--;            } while(j>0);            System.out.println(i);         }      }             

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      3

    • E.

      The program does not compile because of statement "i=++i;"

    Correct Answer
    C. 2
  • 34. 

    What all gets printed when the following gets compiled and run. Select the two correct answers.      public class test {          public static void main(String args[]) {               int i=1, j=1;              try {                  i++;                   j--;                  if(i == j)                      i++;              }              catch(ArithmeticException e) {                  System.out.println(0);              }              catch(ArrayIndexOutOfBoundsException e) {                  System.out.println(1);              }              catch(Exception e) {                  System.out.println(2);              }              finally {                  System.out.println(3);              }              System.out.println(4);           }      }             

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      3

    • E.

      4

    Correct Answer(s)
    D. 3
    E. 4
  • 35. 

      19. What all gets printed when the following gets compiled and run. Select the two correct answers.      public class test {          public static void main(String args[]) {           String s1 = "abc";          String s2 = "abc";          if(s1 == s2)              System.out.println(1);          else              System.out.println(2);          if(s1.equals(s2))              System.out.println(3);          else              System.out.println(4);          }      }             

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer(s)
    A. 1
    C. 3
  • 36. 

      What all gets printed when the following gets compiled and run. Select the two correct answers.      public class test {          public static void main(String args[]) {           String s1 = "abc";          String s2 = new String("abc");          if(s1 == s2)              System.out.println(1);          else              System.out.println(2);          if(s1.equals(s2))              System.out.println(3);          else              System.out.println(4);          }      }             

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer(s)
    B. 2
    C. 3
  • 37. 

    Which of the following are legal array declarations. Select the three correct answers

    • A.

      Int i[5][];

    • B.

      Int i[][];

    • C.

      int []i[];

    • D.

      int i[5][5];

    • E.

      int[][] a;

    Correct Answer(s)
    B. Int i[][];
    C. int []i[];
    E. int[][] a;
  • 38. 

    What is the range of values that can be specified for an int. Select the one correct answer.

    • A.

      The range of values is compiler dependent.

    • B.

      -231 to 231 - 1

    • C.

      -231-1 to 231

    • D.

      -215 to 215 - 1

    • E.

      -215-1 to 215

    Correct Answer
    B. -231 to 231 - 1
  • 39. 

    How can you ensure that the memory allocated by an object is freed. Select the one correct answer

    • A.

      By invoking the free method on the object

    • B.

      By calling system.gc() method

    • C.

      By setting all references to the object to new values (say null).

    • D.

      Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object

    Correct Answer
    D. Garbage collection cannot be forced. The programmer cannot force the JVM to free the memory used by an object
  • 40. 

    What gets printed when the following code is compiled and run. Select the one correct answer.      public class test {          public static void main(String args[]) {           int i = 1;          do {              i--;          } while (i > 2);          System.out.println(i);          }      }             

    • A.

      0

    • B.

      1

    • C.

      2

    • D.

      -1

    Correct Answer
    A. 0
  • 41. 

    Which of these is a legal definition of a method named m assuming it throws IOException, and returns void. Also assume that the method does not take any arguments. Select the one correct answer.

    • A.

      Void m() throws IOException{}

    • B.

      Void m() throw IOException{}

    • C.

      Void m(void) throws IOException{}

    • D.

      M() throws IOException{}

    • E.

      Void m() {} throws IOException

    Correct Answer
    A. Void m() throws IOException{}
  • 42. 

    Which of the following are legal identifier names in Java. Select the two correct answers.

    • A.

      %abcd

    • B.

      $abcd

    • C.

      1abcd

    • D.

      Package

    • E.

      _a_long_name

    Correct Answer(s)
    B. $abcd
    E. _a_long_name
  • 43. 

    At what stage in the following method does the object initially referenced by s becomes available for garbage collection. Select the one correct answer.      void method X()  {           String r = new String("abc");          String s = new String("abc");          r = r+1; //1          r = null; //2          s = s + r; //3      } //4             

    • A.

      Before statement labeled 1

    • B.

      Before statement labeled 2

    • C.

      Before statement labeled 3

    • D.

      Before statement labeled 4

    • E.

      Never

    Correct Answer
    D. Before statement labeled 4
  • 44. 

    String s = new String("xyz");      Assuming the above declaration, which of the following statements would compile. Select the one correct answer

    • A.

      S = 2 * s;

    • B.

      Int i = s[0];

    • C.

      S = s + s;

    • D.

      S = s >> 2;

    • E.

      None of the above

    Correct Answer
    C. S = s + s;
  • 45. 

    Which of the following statements related to Garbage Collection are correct. Select the two correct answers

    • 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.

      The finalize method of an object is invoked before garbage collection is performed on the object

    Correct Answer(s)
    C. It is possible for a program to make an object available for Garbage Collection
    D. The finalize method of an object is invoked before garbage collection is performed on the object
  • 46. 

    If a base class has a method defined as      void method() { }      Which of the following are legal prototypes in a derived class of this class. Select the two correct answers.

    • A.

      Void method() { }

    • B.

      Int method() { return 0;}

    • C.

      Void method(int i) { }

    • D.

      Private void method() { }

    Correct Answer(s)
    A. Void method() { }
    C. Void method(int i) { }
  • 47. 

    In which all cases does an exception gets generated. Select the two correct answers int i = 0, j = 1;

    • A.

      If((i == 0) || (j/i == 1))

    • B.

      If((i == 0) | (j/i == 1))

    • C.

      If((i != 0) && (j/i == 1))

    • D.

      if((i != 0) & (j/i == 1))

    Correct Answer(s)
    B. If((i == 0) | (j/i == 1))
    D. if((i != 0) & (j/i == 1))
  • 48. 

      Which of the following statements are true. Select the two correct answers

    • A.

      The wait method defined in the Thread class, can be used to convert a thread from Running state to Waiting state

    • B.

      The wait(), notify(), and notifyAll() methods must be executed in synchronized code.

    • C.

      The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.

    • D.

      The Thread class is an abstract class

    Correct Answer(s)
    B. The wait(), notify(), and notifyAll() methods must be executed in synchronized code.
    C. The notify() and notifyAll() methods can be used to signal and move waiting threads to ready-to-run state.
  • 49. 

    Which keyword when applied on a method indicates that only one thread should execute the method at a time. Select the one correct answer

    • A.

      transient

    • B.

      Volatile

    • C.

      Synchronized

    • D.

      Native

    • E.

      static

    Correct Answer
    C. Synchronized
  • 50. 

    Which of the following are true about interfaces. Select the two correct answers.

    • A.

      Methods declared in interfaces are implicitly private.

    • B.

      Variables declared in interfaces are implicitly public, static, and final

    • C.

      An interface can extend any number of interfaces.

    • D.

      The keyword implements indicate that an interface inherits from another

    Correct Answer(s)
    B. Variables declared in interfaces are implicitly public, static, and final
    C. An interface can extend any number of interfaces.

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 30, 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.