Short Q&A session (SCJP 310-035)

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





This Q&A session is useful if you prepare to take Sun's Java certification exam for Java 1.4 platform (SCJP 310-035).

This is not a mock exam ! Read more about it in my first essay on the subject.

Feel free to read more about my SCJP experience.


  1. What is the output of the following code ?
    public class CDummy { final static int CT__TOM; public static void main(String args[]) { System.out.println("CDummy."); break; System.out.println("end"); } }

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

    The code does not compile. The 'break' statement must be enclosed in a loop or a switch statement.

  2. What is the output of the following code ?
    public class CDummy { final static int CT__TOM; public static void main(String args[]) { System.out.println("CDummy."); int ii = 3; switch(ii) { case 3: continue; case 4: } System.out.println("end"); } }

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

    The code does not compile. The 'continue' statement must be enclosed in a loop.

  3. What is the output of the following code ?
    class SimpleJava { int ii = 5; System.out.println("ii = " + ii++); public static void main(String args[]) { System.out.println("SimpleJava ...."); SimpleJava sj = new SimpleJava(); System.out.println("ii = " + sj.ii); } }

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

    The code does not compile. In a class, outside a block of code, or inner class or method you can have only declarations. Thus the line:

    System.out.println("ii = " + ii++);
    will not compile.

  4. Will the following code compile ?
    class SimpleJava { public static void main(String args[]) { System.out.println("SimpleJava ...."); int superArr [] [] [] = new int [] [] [10]; } }

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

    No. In a multidimensional array creation you must specify the left most dimension:

    int superArr [] [] [] = new int [10] [] [];

    The above code translates to creating an array of size 10 that contains references to a bi-dimensional array. Again, when you will specify the dimensions of the bi-dimensional array you must specify its left most dimension because a bi-dimensional array is in fact an array of references to arrays, hence you must specify how many such references you need. The above algorithm will go on and on for all the dimensions.

    Bottom line:

    Always specify the left most dimension for an array.

  5. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); boolean bb = true; int ii = (int) bb; System.out.println("ii = " + ii); } }

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

    The code does not compile ! The boolean type can be cast only to itself. Additionally, the boolean type can be converted to a String (pay attention: I said converted not cast)

    boolean bb = false; boolean bb1 = (boolean) bb; // cast - OK String str = (String) bb; // cast - not OK String str2 = "" + bb; // convert - OK
  6. How many times will the code below prints the string "run()" ?

    class CDummy implements Runnable { public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); Thread th = new Thread(dmy); th.start(); th.start(); th.start(); } public void run() { System.out.println("run()"); } }

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

    Once ! When the call to start() will be made again on the same thread in main, the exception IllegalThreadStateException will be thrown, thus the main thread will die. However the child thread created by the first call to start() will finish his job.

  7. What is the output of the following code ?
    public class CDummy { static int ii = -10; public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); Thread th = new Thread(dmy); th.start(); } public void run() { System.out.println("ii = " + ii); } }

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

    The code does not compile ! There is no thread constructor that takes a CDummy() object as its argument. Even if the class correctly defines the run() method the code will fails to compile.

  8. What is happening when attempting to compile the next 2 files: (the files are in the same directory)

    FILE 1:

    package SomePackage; class CP1 { void myMethod() {System.out.println("CP1->method");} }

    FILE 2:

    class CP2 extends CP1 { void myMethod() {System.out.println("CP2->method");} }

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

    The first class is compiled without problems. The second one fails to compile because the class file generated by the class CP1 does not contain the class "CP1" but the class "SomePackage.CP1". Obviously when compiling the class "CP2" the compiler looks for a class named "CP1" located in the same package. (the default package)

  9. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); StringBuffer sb1 = new StringBuffer("abcd"); StringBuffer sb2 = new StringBuffer("abcd"); if (sb1.equals(sb2)) { System.out.println("TRUE"); } else { System.out.println("FALSE"); } } }

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

    "FALSE". The equals method from StringBuffer only compares references and not content !! Stupid, but this is the way it is.

  10. Will the following code compile ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); int arr[]; Object arr2[] = {arr}; } }

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

    No. The reference 'arr' is a local variable thus it must be initialized before its use.

  11. How can you modify the following Float:
    Float ff = new Float(3.14);

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

    You cannot. All the wrapper classes are immutable.

  12. What is the output of the following 2 classes ?

    Class A:

    public class CDummy { final int CT__TOM = 1; CDummy() { CT__TOM = 11; } public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); System.out.println("CT__TOM = " + dmy.CT__TOM); } }

    Class B:

    public class CDummy { final int CT__TOM; CDummy() { CT__TOM = 11; } public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); System.out.println("CT__TOM = " + dmy.CT__TOM); } }

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

    The first class does not compile because the variable CT__TOM is already initialized and the constructor attempts to modify it. The second class prints 11.

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

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

    The code does not compile. The static variable CT__TOM is already initialized with the default (zero) value; for this reason the assignment

    CT__TOM = 5;
    will fail at compile time. Such an assignment can be made only in a constructor *if* the variable was not already explicitly initialized. (see the previous question)

  14. Can you declare an inherited method as abstract ?

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

    Yes. There is no problem doing that: the current class (that contains the abstract method) cannot be instantiated. In order to be useful a new class must be derived from the current abstract class. (a class that has an abstract method is abstract)

    Side effect:

    In java only methods from the parent class can be accessed by the derived classes; that means that a child class CANNOT access methods from a grandparent class. In the case when an inherited method is made abstract the method itself cannot have a body. Also, the class itself cannot be instantiated. The problem is that the derived classes (from the current abstract class) will only be able to access the method from its direct parent - which is abstract. In practice that means that in such a case child classes cannot access the methods defined in the parent classes: (the chain constructed with super.someMethod() is broken where the abstract class is defined) all the functionality is lost !

    The following code will not compile:

    class Base { public void printIt() { System.out.println("Base"); } } class DerivedA extends Base { public void printIt() { System.out.println("DerivedA"); } } class DerivedB extends DerivedA { public void printIt() { super.super.printIt(); // super.super is an illegal statement } } public class CDummy { public static void main(String argv[]) { CDummy dmy = new CDummy(); DerivedB dvb = new DerivedB(); dvb.printIt(); } }

    There is no way to "chain" several "super" together and reach back higher into the parent class hierarchy. You can reference your immediate parent class and that's it.

  15. The integer value of 'A' is 65. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); System.out.println(1 + 'A' + 2); } }

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

    68

    Attention ! 'A' is a character, not a string.

  16. What is the output of the following code ?
    interface IBase1 {} interface IBase2 {} interface IDev implements IBase1, IBase2 {} public class CDummy implements IDev { public static void main(String args[]) { System.out.println("CDummy."); } }

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

    The code does not compile. An interface extends another interface it does *not* implement another one.

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

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

    10 20
  18. What is the output of the following code ?
    public class CDummy extends CBase { int toto = 9; static int moto = 19; public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); CBase cb = new CDummy(); System.out.println(dmy.toto); System.out.println(dmy.moto); System.out.println(cb.toto); System.out.println(cb.moto); } } class CBase { int toto = 7; static int moto = 17; }

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

    9 19 7 17









Best regards,
Razvan MIHAIU




Razvan Mihaiu � 2000 - 2024