SCJP test

Author: Razvan MIHAIU
razvan_rem@rem_mihaiu.name (please remove '_rem' and 'rem_')
From: www.mihaiu.name
Date: 22/10/2004

Page 1: SCJP questions 1-18

Page 2: SCJP questions 19-36

  1. What is the output of the following code ?

    public class CDummy extends CBase { static X x1 = new X("x1"); public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); } } class CBase { X x2 = new X("x2"); static X x3 = new X("x3"); CBase() { X x4 = new X("x4"); } } class X { X(String msg) {System.out.println("X::" + msg);} }

    \/\/\/\/\/\/\/\/\/\/

    X::x3 X::x1 CDummy. X::x2 X::x4
  2. What is the difference in behavior of the following 2 programs: (the programs are compiled / run with assertions enabled)

    PROG A:

    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); int x = 7; if (x <= 10) throw new AssertionError("Assert failed !"); } }

    PROG B:

    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); int x = 7; assert x > 10 : "Assert failed !"; } }

    \/\/\/\/\/\/\/\/\/\/

    There is no difference. Since the problem states that the programs are compiled with assertions enabled and run with assertions enabled, the 2 programs will have the same behavior. An assertion can be directly thrown like we see in the PROG A or it can indirectly be thrown by the new assert statement like in PROG B.

  3. Do you need to import packages from java.lang.* ? True/False

    \/\/\/\/\/\/\/\/\/\/

    False. Packages from java.lang.* are automatically imported by the compiler.

  4. How can you prevent an object from being modified in Java ? (declare it as const)

    \/\/\/\/\/\/\/\/\/\/

    You can't. You cannot declare constant objects ! Only constant references are supported by the language:

    // constant references: final Object obj = new Object(); // this is OK // constant object: Object final obj = new Object; // this is not OK (illegal C++ const-style) // constant reference to constant object: final Object final obj = new Object; // this is not OK (illegal C++ const-style)
  5. Are the primitive values passed by reference ? True/False

    \/\/\/\/\/\/\/\/\/\/

    False. The primitive values are passed by value.

  6. Will the following code compile ?
    float f = 1.234;

    \/\/\/\/\/\/\/\/\/\/

    No. A real number literal is of type double by default.

    Solution:

    • a. conversion: float f = (float) 1.234;
    • b. specify the literal type: float f = 1.234f;
  7. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); CBase cb = new CDev(); System.out.println(cb.getVal()); } } class CBase { int someVal = 7; int getVal() { return someVal;} } class CDev extends CBase { int someVal = 9; }

    \/\/\/\/\/\/\/\/\/\/

    7

    The object CDev does not define its own method getVal(); the method from the base class will return value of someVal from its own class.

  8. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); CBase cb = new CDev(); System.out.println(cb.getVal()); } } class CBase { int someVal = 7; int getVal() { return someVal;} } class CDev extends CBase { int someVal = 9; int getVal() { return someVal;} }

    \/\/\/\/\/\/\/\/\/\/

    9

    The object CDev defines its own getVal method. Because the getVal() method gets called polymorphically the method from CDev gets called even if the reference cb is of type CBase. The getVal() method from CDev sees its own someVal variable; as a result number 9 is printed.

  9. What is the output of the following code ?
    import java.util.Vector; public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); int counter = 99; Vector vv = new Vector(); vv.add(++counter); System.out.println(vv.get(0)); } }

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile ! A vector can only handle references and not primitive types.

  10. Will the following code compile ? Yes/No
    public class CDummy { static int ii = jj; static int jj = 6; public static void main(String args[]) { System.out.println("CDummy."); } }

    \/\/\/\/\/\/\/\/\/\/

    No. Forward referencing (like the use of the variable "jj" before declaration) is not supported by the language. However an expression that initializes "jj" can be put anywhere in the class (forward referencing is allowed *only* for initializers):

    public class CDummy { static {jj = 1;} // fordward referencing used for innitialization [static block] static int jj = 7; {kk = 2;} // fordward referencing used for innitialization [non-static block] int kk = 5; public static void main(String argv[]) { CDummy dmy = new CDummy(); System.out.println("jj = " + jj); System.out.println("kk = " + dmy.kk); } }

    The above code prints 7 and 5. The reason for this is the fact that the initializers are executed in textual order:

    step1: jj = 1; step2: jj = 7; step3: kk = 2; step4: kk = 5;

    The following example will print 7 and 21:

    public class CDummy { static {jj = 1;} // fordward referencing used for innitialization [static block] static int jj = 7; {kk = 2;} // fordward referencing used for innitialization [non-static block] int kk = 5; public static void main(String argv[]) { CDummy dmy = new CDummy(); System.out.println("jj = " + jj); System.out.println("kk = " + dmy.kk); } {kk = 21;} }
  11. The printed number is negative or positive ?
    byte b = -1; b = (byte) (b >>> 5); System.out.println("b = " + b);

    \/\/\/\/\/\/\/\/\/\/

    Negative. The >>> shift operator will insert zeroes from the left but since it will insert only 5 zeroes when the number is converted back to byte the first bit will still be 1 which means that as a byte the number is negative.

  12. The printed number is negative or positive ?
    int b = -1; b = b >>> 32; System.out.println("b = " + b);

    \/\/\/\/\/\/\/\/\/\/

    Negative. 32 mod 32 = 0; the number will be shifted with 0 positions. (not shifted at all)

  13. The printed number is negative or positive ?
    int b = -1; b = b >> 5; System.out.println("b = " + b);

    \/\/\/\/\/\/\/\/\/\/

    Negative. The shift operator >> will retain the sign. Since initially the number was negative it will insert 1's on from the left.

  14. What interface guarantees that implementing classes maintains non-unique elements in order: List or SortedSet ?

    \/\/\/\/\/\/\/\/\/\/

    List; a list allows duplicate elements while at the same time guarantees that the order of the elements is retained. (observation: this is not the insertion order - in a list one element can be inserted anywhere, however after the element is inserted his position will not be changed during the life time of the list)

    A SortedSet does NOT allow duplicates. A SortedSet maintains its elements sorted so implicitly ordered.

  15. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); float f = 1.41356f; f++; System.out.println("f = " + f); } }

    \/\/\/\/\/\/\/\/\/\/

    2.41356

    The post increment operator works with float values.

  16. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); float f1 = 1.41356f; float f2 = -0f; System.out.println("f1/f2 = " + (f1/f2)); } }

    \/\/\/\/\/\/\/\/\/\/

    -Infinity

    Please note that the code does not throw an exception.

  17. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); int i1 = 1; int i2 = 0; System.out.println("i1/i2 = " + (i1/i2)); } }

    \/\/\/\/\/\/\/\/\/\/

    It throws an exception: "java.lang.ArithmeticException: / by zero"

  18. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); Byte b = new Byte(123); System.out.println("b = " + b); } }

    \/\/\/\/\/\/\/\/\/\/

    The code does not compile ! There is no Byte() constructor that takes an int parameter.










Best regards,
Razvan MIHAIU




Razvan Mihaiu � 2000 - 2024