SCJP Q&A

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

Page 1: SCJP questions 1-20

Page 2: SCJP questions 21-40

Page 3: SCJP questions 41-61


  1. Which modifiers (from the following list) can be used with constructors:

    public, protected, private, abstract, static, final, native, synchronized, strictfp

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

    public, protected, private

    Observation:

    Make the class "strictfp" in order to have "strictfp" constructors.

  2. Does the following code compile ?
    public someConstructor() { int sum = 0; super(0,0); }

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

    No. The call to the superclass constructor - thru super() - must be the first line in the constructor that calls it.

  3. Does the following code compile ?
    class Base { int type; Base(int n) { type = n ; } } class Derived extends Base { String strTemp = ""; }

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

    No. The derived class "Derived" has a default constructor which always tries to call the default constructor from the base class. The problem is that the base class does not have a default constructor. (a constructor that takes no parameters)

  4. Does the following code compile ?

    Assume that the next function is a constructor:

    public someConstructor() { int sum = 0; this(0,0); }

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

    No. The call to another constructor from the same class must be on the first line in the calling constructor:

    public someConstructor() { this(0,0); int sum = 0; }
  5. What is the output of the following code ?
    public class CDummy implements IDummy { public static void main(String args[]) { System.out.println("CDummy."); toto++; System.out.println(toto); } } interface IDummy { int toto = 7; }

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

    The code does not compile ! The variable toto defined in the interface IDummy is final (even if the word final does not appear before it) Since the variable is final in cannot be incremented.

  6. Does the following code compile ?
    interface IDummy { protected int toto = 7; }

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

    No. All the attributes from a class are public static final by default and those modifiers cannot be changed.

  7. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); String toto = null; System.out.println(toto); } }

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

    CDummy. null

    Observation:

    The reference "toto" is not de-referenced, it is only printed thus there will not be any runtime error because the reference is null.

  8. Which code will print an error message when an assertion fails ?
    • a. assert condition: "condition failed";
    • b. assert condition: System.out.println("condition failed");

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

    a. There are 2 forms of assertions:

    assert (condition); assert (condition): expression;

    The expression from the second form of "assert" cannot be of type void. Since the println() function returns void it cannot be used in an assert statement. For this reason option "b" will fail to compile.

  9. What is the output of the following code ?
    public class CDummy { static { xx = 9; } public static void main(String args[]) { System.out.println("xx = " + xx); } static int xx = 7; }

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

    7

    Explanation:

    The static variable is defined at the entry in the class (just like all class variables). After that the static initializers are executed in the 'textual' order:

    first: xx = 9; second: xx = 7;
  10. What is the output of the following code ?
    static public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); } }

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

    The class will not compile. Classes cannot be static.

  11. Can interfaces have constructors ? Yes/No.

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

    No. An interface cannot be instantiated. As a result of this there is no point in having a constructor.

  12. What is the output of the following code ?
    public class CDummy extends CBaseDummy { public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); dmy.printf(); } public void printf() { System.out.println("printf2"); } } class CBaseDummy { static public void printf() { System.out.println("printf"); } }

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

    The code does not compile. The function printf() from CDummy is trying to override the function printf() from base without preserving the static qualifier. (static is not part of the function signature and that means that you cannot overload/override a function just by the static qualifier)

  13. What is the output of the following code ?
    public class CDummy extends CBaseDummy { public static void main(String args[]) { System.out.println("CDummy."); CBaseDummy baseDmy = new CDummy(); baseDmy.printf(); } static public void printf() { System.out.println("print from CDummy."); } } class CBaseDummy { static public void printf() { System.out.println("print from CBaseDummy."); } }

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

    print from CBaseDummy.

    Since the printf() function is static it cannot access the "this" pointer. Consequently, it cannot access the virtual table specific to each object. In this case the virtual function call mechanism (late binding) cannot work. As a result the function from the base is called because the reference is of type base.

  14. Does the following code compile ? Yes/No
    abstract class CBaseDummy { CBaseDummy() {} abstract static public void printf() { System.out.println("printf"); } }

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

    No. Valid reasons:

    1. an abstract class cannot have a body;
    2. a static function cannot access the "this" pointer, as a result it cannot make use of the virtual function mechanism. Since it cannot use the virtual function mechanism a static function *must* be defined in the current class because it makes no sense to define a static method in a derived class, since the users of the base class (the initial class) cannot make use of the implementation from the derived class. OTOH the abstract qualifier dictates that the function to which it is applied will be defined *only* in the derived classes. Hence static and abstract qualifiers are incompatible.

      To be more clear: by definition an abstract function must make use of the virtual function mechanism, while at the same time a static function cannot use this mechanism because it cannot access the hidden "this" pointer.

  15. Does the following code compile ?
    interface IDummy { synchronized void doSomething(); int toto = 7; }

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

    No. A method in an interface cannot be synchronized because no instances of an interface are possible. Since there are no instances there is no 'this' pointer to synchronize to.

  16. Does the following code compile ?
    interface IDummy { protected void doSomething1(); private void doSomething2(); int toto = 7; }

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

    No. A method declared in an interface cannot be protected or private. It cannot be private because derived classes will not be able to access it while at the same time the method cannot be defined in the interface. Practically private methods would be completely useless.

    Since methods are only meaningful to derived classes there is no point to make the method protected. Aside from the derived classes the method declarations are not useful, so in this particular case public and protected have the same significance. To avoid confusion only public was allowed.

  17. Can abstract classes have constructors ?

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

    Yes. Constructors are needed because abstract classes can have state. This is the different from interfaces that do not have an internal state hence they do not have constructors.

  18. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); double m1 = -5.6; double m2 = 3.4; System.out.println("ceil(" + m1 + ") = "); System.out.println(Math.ceil(m1)); System.out.println("ceil(" + m2 + ") = "); System.out.println(Math.ceil(m2)); System.out.println("floor(" + m1 + ") = "); System.out.println(Math.floor(m1)); System.out.println("floor(" + m2 + ") = "); System.out.println(Math.floor(m2)); } }

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

    -5 4 -6 3

    ceil() - the smallest integer that is bigger than the argument

    floor() - the biggest integer that is smaller than the argument

  19. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); double m1 = -5.6; int result = 0; result = Math.ceil(m1); System.out.println("ceil(" + m1 + ") = "); System.out.println(result); } }

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

    The code does not compile. The return value of ceil() is double not int. A cast from double to int is required. The same holds true for the function floor().

  20. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); double m1 = -5.6; int result = 0; result = Math.round(m1); System.out.println("ceil(" + m1 + ") = "); System.out.println(result); } }

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

    The code does not compile. The return value of round() is long not int. A cast from long to int is required.

  21. What is the output of the following code ?
    int k = 11; switch(k) { default: System.out.println("Default"); case 10: System.out.println("10"); case 20: System.out.println("20"); }

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

    Default 10 20









Best regards,
Razvan MIHAIU




Razvan Mihaiu � 2000 - 2017