Java Certification, Programming, JavaBean and Object Oriented Reference Books
Sun Certified Programmer for the Java2 Platform

Mock Exam

QUESTIONS:-
  1. Which of the following are valid assignment expressions, given the following declarations:-

  2. byte b;
    int i = 10;
    long l = 20;
    1. b = i;
    2. b += i;
    3. b++;
    4. b = b + i;
    Answer

  3. State true or false: Interface methods can be declared static
    1. true
    2. false
    Answer

  4. Select the correct answers: If you define a class within an interface,
    1. the class is always public
    2. the class is always static
    3. the class methods cannot call methods declared in the interface
    Answer

  5. State true or false: An abstract class can have a constructor
    1. true
    2. false
    Answer

  6. What is the output of the following piece of code:

  7. class  A { 
    public  A() {
    class  B{
    B()  {
    System.out.println("In no-arg constructor");
    }
    }
    new B();
    }
    public  A(int a) {
    class  B{
    B()  {
    System.out.println("In constructor with an int argument");
    }
    }
    new B();
    }
    public  static void  main( String[] args{
    A a1 = new  A();
    A a2 = new  A(10);
    }
    1. Code does not compile
    2. Code compiles but generates exception at runtime
    3. Code compiles successfully and outputs

    4. In no-arg constructor
      In constructor with an int argument
    Answer

  8. Select all correct answers: Numeric operators that throw the ArithmeticException on integer operands include
    1. +
    2. -
    3. /
    4. %
    5. *
    Answer

  9. What is the output of the following piece of code.

  10. System.out.println("-0.0 == 0.0 returns " + (-0.0 == 0.0));
    System.out.println("0.0 > -0.0 returns " + (0.0 > -0.0));

    ___________________________________

    Answer


  11. Select true or false: The equality operator (==) always returns false if either operand is Nan and the inequality operator (!=) return true if either operand is Nan.
    1. true
    2. false
    Answer

  12. Select the correct answers: You can use an instance of a File class to do the following
    1. delete a file
    2. change current working directory
    3. rename files
    4. create new sub-directories
    Answer

  13. What is the output of the following code:

  14. public  class  Temp{
    public  static void main(String[] args){
    String[] a = null;
    System.out.println("The length of the array is " + a.length):
    }
    }
    1. The code does not compile
    2. The code compiles and prints out "The length of the array is 0";
    3. The code compiles and prints out "The length of the array is null";
    4. The code compiles but generates the NullPointerException at runtime
    Answer

  15. Given the following declaration:

  16. char a;

    Which of the following are valid values that can be assigned to a?

    1. ‘\0’
    2. ‘\u003c’
    3. ‘\349’
    4. \u004
    5. ‘\345’
    Answer

  17. Given the following code, what is the output generated

  18. class  Base {
    public  static void amethod(){
    System.out.println("In base amethod");
    }
    public  void another(){
    System.out.println("In base another");
    }
    static  int staticInt = 10;
    int  instanceVar = 20;
    }
    public  class  Child extends Base {
    public  static void amethod(){
    System.out.println("In child amethod");
    }
    public  void another() {
    System.out.println("In child another");
    }
    static  int staticInt = 30;
    int  instanceVar = 40;
    public  static void main(String[] a ){
    Base b = new Child();
    b.amethod();
    b.another();
    System.out.println("Value of staticInt is = " + b.staticInt);
    System.out.println("Value of instanceVar is = " + b.instanceVar);
    }
    }
    1. Code does not compile
    2. Code compiles and prints out

    3.  

       

      In base amethod

      In child another

      Value of staticInt is 30

      Value of instanceVar is 40

    4. Code compiles and prints out

    5.  

       

      In base amethod

      In child another

      Value of staticInt is 10

      Value of instanceVar is 20

    6. Code compiles and prints out

    7.  

       

      In child amethod

      In child another

      Value of staticInt is 30

      Value of instanceVar is 40

    Answer

  19. Select all correct answers: Modifiers that you CANNOT use with constructors include
    1. private
    2. abstract
    3. static
    4. public
    5. final
    Answer

  20. State true or false: The RandomAccessFile class is compatible with the Stream classes
    1. True
    2. False
    Answer

  21. What is the output of the following piece of code:

  22. class  A{
    public  static void main(String[] args){
    int i = 1;
    int j = 5;
    System.out.println((i++ * j));
    System.out.println("i = " + i + " j = "+ j);
    System.out.println((++i * j));
    System.out.println("i = " + i + " j = " + j);
    }
    }
    1. Code does not compile
    2. Code compiles and prints out

    3. 10
      I = 2 j=5
      15
      I = 3 j = 5
    4. Code compiles and prints out

    5. 5
      I = 2 j=5
      15
      I = 3 j = 5
    Answer

    ANSWERS:-

    1. b, c

    2.  

       
       
       
       
       

      Remember, that a compound assignment expression of the form E1 op= E2 is always equivalent to E1 = (Type) (E1 op E2), where Type is the type of E1.

    3. b

    4.  

       
       
       
       
       

      The only modifiers that are allowed with interface methods are public and abstract. And these are implicit, so you don’t even have to include them.

    5. a,b,c

    6.  

       
       
       
       
       

      All three are right. Classes defined within an interface are implicitly public and static and because you cannot have static methods within an iterface, you cannot refer to the non-static methods from the static class methods.

    7. a
    8. c
    9. c, d

    10.  

       
       
       
       
       

      The only operators that can cause an ArithmeticException are the integer division (/) and modulo (%) operators. Remember that float operations do not raise any exception at all. They may result in NaN or Infinities, instead.

    11. -0.0 == 0.0 returns true

    12.  

       
       
       
       
       

      -0.0 > 0.0 returns false

    13. a
    14. a, c, d
    15. d
    16. a, b, d

    17.  

       
       
       
       
       

      Check out the Java Language Specification for more information on Character Literal and Escape Sequences

    18. c

    19.  

       
       
       
       
       

      Static variables and methods as well as instance variables use the Type of the reference variable to determine the correct variable/method to use. On the other hand, instance methods use the Class of the reference variable to determine the correct method to call

    20. b,c,e

    21.  

       
       
       
       
       

      Unlike methods, a constructor cannot be abstract, static, final, strictfp, native or synchronized.

    22. b
    23. c
    Back to questions
Return to : Java Programming Hints and Tips