Improve your Java skills: SCJP 310-035

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

Page 1: SCJP questions 1-20

Page 2: SCJP questions 21-40

Page 3: SCJP questions 41-53





The Java language is becoming more and more popular because of its power and simplicity. Programmers that want to prove their Java skills by taking Sun's Java certification exam for Java 1.4 platform (SCJP 310-035) can use this material to boost their knowledge.

Read the introduction from my first essay on the subject in order to understand how to use this material.

My experience on passing the SCJP exam may be useful to you. Feel free to take a look.


  1. How can you rapidly compute ~j ?

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

    Answer: ~j = (-j) - 1;

  2. What is the output of the following code ?
    Vector ts = new Vector(); ts.add("abc"); ts.add("abd"); Iterator it = ts.iterator(); ts.remove("abc"); it.remove(); while(it.hasNext()) { System.out.println("val = " + it.next()); }

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

    The line:

    it.remove();
    throws a java.util.ConcurrentModificationException exception because the vector was modified *after* the creation of the iterator it by the line:
    ts.remove("abc");

    In order to avoid this behavior only call the method remove() from the iterator.

  3. What is the output of the following code ?
    public class CDummy extends Vector { public static void main(String args[]) { System.out.println("CDummy."); CDummy dmy = new CDummy(); dmy.add("Unu"); dmy.add("Doi"); System.out.println(dmy); } }

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

    [Unu, Doi]
  4. Does the name of a thread have to be unique ? Yes/No

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

    Answer: No.

    Multiple threads with the same name can execute simultaneously.

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

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

    CDummy.

    The class compiles and run without issues. The JLS states that the run function must be public, static, return void and take as parameters an array of strings. The above defined function meets the requirements.

  6. Will an empty file compile ? Yes/No

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

    Yes, there will be no compilation error but the compiler will not create a class file. (at least this is the behavior with jdk 1.4)

  7. What is the output of the following code ?
    Integer ii = new Integer(10); System.out.println(ii instanceof (Object));

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

    The code does not compile. It is not allowed to surround the type with parenthesis. You can only surround the object:

    System.out.println((ii) instanceof Object);
  8. Does the following code compile ?

    Integer arr [] = new Integer[5]; arr.length = 7;

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

    No. The length attribute is final. This attribute is assigned when the array is created. Any attempt to modify it will be forbidden by the compiler.

  9. What is the output of the following code ?
    class Short { public static void main(String args[]) { System.out.println("CDummy."); java.lang.Short sh = new java.lang.Short((short)34); System.out.println(sh); } }

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

    CDummy. 34

    The class name "Short" does not collide with the class java.lang.Short because they are not in the same package.

  10. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); StringBuffer sb = new StringBuffer("Some"); sb.append(" text"); System.out.println(sb); } }

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

    Some text

    The method append from StringBuffer directly modifies the underlying object (StringBuffer is *not* immutable).

  11. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); String [][] text = new String [10][]; System.out.println(text[0][0]); } }

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

    The code prints "CDummy." then it will throw a NullPointerException. This is happening because text[0] is null and the above code tries to dereference this null value.

  12. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); Byte b1 = new Byte((byte)100); Byte b2 = new Byte((byte)10); System.out.println("" + b1.byteValue() + b2.byteValue()); } }

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

    CDummy. 10010

    Pay attention: the above operation concatenates strings.

  13. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); Byte b1 = new Byte((byte)100); Byte b2 = new Byte(b1); System.out.println("" + b1.byteValue() + b2.byteValue()); } }

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

    The code does not compile ! There is no Byte constructor that takes as a parameter a Byte. This is true for all std wrapper classes: Boolean, Byte, Character, Short, Integer, Long, Float, Double.

  14. What is the output of the following code ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); Byte b1 = new Byte((byte)20); Byte b2 = new Byte((byte)30); System.out.println(b1.byteValue() + b2.byteValue() + ""); } }

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

    CDummy. 50

    Because the string "" is the last in the printed expression the whole expression is converted to a string after the addition of b1 and b2.

  15. Does the following code compile ?
    import java.io.IOException; public class SimpleJava { public static void main(String args[]) throws IOException { } }

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

    Yes. If you try to catch a checked exception (like IOException) from an empty try block the code will not compile. The compiler will issue the following error:

    exception java.io.IOException is never thrown in body of corresponding try statement

    This is happening because IOException is a checked exception. Unfortunately the same check does not apply for a function's "throws" clause that never throws the checked exception that it declares to throw. In our case the function main() declares that it throws the checked exception IOException, yet it is clear that the main() function will never throw that exception.

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

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

    The code does not compile. Transient variables are not serialized. Local variables are not serialized anyway so it makes no sense to apply this qualifier to a local variable.

  17. Does the following code throw an exception at runtime ? (Yes/No)
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); Float toto = new Float("10F"); System.out.println("toto = " + toto); } }

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

    No. It is legal to feed the Float constructor with a string ending in 'f' or 'F'.

  18. How do you get the length of a string ?

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

    Answer: using the length() method.

  19. How do you compute the intersection of 2 collections ?

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

    Using the member function someCollection.retainAll(Collection col). This function retains in the original collection someCollection only the elements that are also found in the collection col.

  20. What is the output of the following code ? (it is contained in one source file)
    public interface IFace {} public class CDummy implements IFace { public static void main(String args[]) { System.out.println("CDummy."); } }

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

    The code does not compile. You cannot have in one source file 2 public classes or interfaces or a mix of them.










Best regards,
Razvan MIHAIU




Razvan Mihaiu � 2000 - 2024