Prepare your Sun Java 1.4 certification exam ! (SCJP 310-035)

Author: Razvan MIHAIU (please remove '_rem' and 'rem_')
Date: 17/10/2004

Page 1: SCJP questions 1-20

Page 2: SCJP questions 21-40

In order to prepare for any exam it is very useful to try to answer to questions related to the exam material (assuming that you have a solid theoretical base). This technique has proven to be very efficient for me for at least 3 reasons:

  1. it allowed me to grasp very well difficult concepts, because I had not only to understand the concept, but also to document and explain my point of view;
  2. since you will analyze a subject from the 'other side' - the teacher's side - you will have to anticipate possible misunderstandings from the people who will read your material;
  3. if you choose to publish your material you can receive feedback from your readers;

Recently, I passed Sun's Java certification exam for Java 1.4 platform (SCJP 310-035). As part of my preparation effort I wrote a lot of material that could be useful for other test takers. As my spare time will allow, I will post the material on my web site.

Before using this preparation material please read below:

  1. I have done my best to ensure that the material is error free, but my best might not be enough. If you spot any errors do not hesitate to contact me. (see the contact page for details)
  2. this is not a mock exam. The questions that you will encounter in this material are in a pseudo random order. This material is not build with the Sun's objectives in mind. Instead, each time I saw something difficult or worth to remember I wrote down the information in Q&A format.
  3. each question has an answer and an explanation, unless the question is trivial in which case only the answer is given. The answer can be free-form; like I said earlier the point of the material is to boost your Java knowledge not to simulate the real exam.

Feel free to write me if you find any omissions, errors or if you have something interesting to say about this extensive subject: "Java certification". Best of luck with your exam preparation !

  1. What is the output of the following code ?

    int someNum = 4096L; System.out.println(someNum);


    The code does not compile. The literal 4096L is of type long so a possible loss of precision can occur when converting to int.

  2. What is the output of the following code ?
    short someNum = 32767; System.out.println(someNum);



    The code compiles because the compiler is smart enough to compute that the number 32767 will fit in a short. A quiet conversion from int to short takes place.

  3. What is the output of the following code ?
    String strTemp = 'c'; System.out.println(strTemp);


    The code does not compile: a char cannot be used to initialize a String

  4. '\u004F' is the Unicode equivalent for new line character '\n'. Can this Unicode character be used inside strings literals instead of '\n' ? Yes/No


    No. The Unicode representation is translated to new line by the *compiler* which breaks the code. The escape sequence '\n' is not translated by the compiler but by the runtime code which makes this approach OK.

  5. What is the output of the following code:
    System.out.println( 5 * -1);


  6. What is the output of the following code:
    int a = 2, b=3; ++a += ++a; b++ += ++b; System.out.println(a); System.out.println(b);


    b. The pre/post increment operator cannot appear on the left side of an expression.

  7. What is the output of the following code:

    int a = 2; a += a++; a++; System.out.println(a);



    a += a++; // here a = 2 + 2; the incrementation a++ is LOST;

  8. What is the output of the following code:
    byte bb = 7; bb = bb + ((byte) 5); System.out.println(bb);


    Answer: b

    The + operator operates on integers. The byte operands are converted to int prior to execution, thus the result of the addition is an int. An explicit cast (from int to byte) is required in order to compile the code.

  9. What is the type of the number 1.7565 ?


    Answer: b

  10. What is the output of the following code:
    float rr = -5; float result = (float) Math.sqrt(rr); if (result == Float.NaN) {System.out.println("Not a number");} else {System.out.println("OK");}


    Answer: b (you cannot compare NaN values; the only way to detect those is to use the function Float.isNaN(x))

  11. Can strictfp be applied to both classes and methods ? Yes/No



  12. What is the output of the following code:
    int nn = 100; System.out.println( nn + 1 + " result " );


    Answer: a

  13. Which line causes a compiler error ?
    line1 boolean bb = true; line2 System.out.println(~bb); line3 System.out.println(!bb);


    line2 (this operator can only be applied to int)

  14. What is the output of the following code ?
    int bb = -4; System.out.println((bb >> 1) > 0); System.out.println((bb >>> 1) > 0);


    Answer: false, true

  15. What is the output of the following code ?
    int z = 4; int a = z << 4097; int b = z << -1; System.out.println(a); System.out.println(b);



    The first shift is effectively by 1 because only the first 5 bits are considered when shifting the second shift is by 31 positions (the codification of -1 is 11111111111111111); as a result all the bits are shifted out => the result is 0

  16. What is the output of the following code ?
    int a=0, b = 5; int c = a > b ? a : b = 7; System.out.println(a); System.out.println(b);


    Answer:c. The ternary operator ?: has higher precedence that the assignment operator.

  17. What is the output of the following code ?
    Integer x1 = new Integer(5); Integer x2 = x1; Integer x3 = new Integer(5); if (x1 == x2) System.out.println("x1 is equal with x2"); if (x1 == x3) System.out.println("x1 is equal with x3");


    "x1 is equal with x2". Obviously, the references x1 and x2 point to the same object while the x3 reference points to some other object.

  18. What is wrong with the following code ?
    public class CDummy { public static main(String args[]) { System.out.println("CDummy..."); } }


    The void return type is missing from the 'main' function signature.

  19. Does the following code compile ? Yes/No
    int x = 2; if (x == 2) System.out.println("match"); if (x == 2) { System.out.println("match"); }


    Yes. The use of brackets {} is not compulsory.

  20. What is the output of the following code ?

    Long longL1 = new Long(5); Integer intI1 = new Integer(5); if (longL1.equals(intI1)) System.out.println("longL1 is equal with intI1"); else System.out.println("longL1 is NOT equal with intI1");


    'longL1 is NOT equal with intI1' (this is happening because the equals method tests to see if the parameter received is an instance of a 'Long' - 'Integer' is not an instance of a 'Long')

Best regards,

Razvan Mihaiu � 2000 - 2024