Prepare SCJP 1.4 !

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. What are the types of reference variables in Java ?

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

    Class reference, interface reference and array reference.

  2. What is the output of the following code (assume that the class CBase is properly defined)
    Object bc = new CBase(new Integer(4)); System.out.println(bc.getClass());

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

    Answer: CBase

  3. Conversion from a superclass to a subclass requires a cast. True/False

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

    Answer: True

  4. Conversion from a subclass to a superclass requires a cast. True/False

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

    Answer: False

  5. arraycopy vs clone: what are the differences ?

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

    Arraycopy can be used to copy fragments of arrays. clone() just copies the whole lot.

  6. Does the following code compile ?
    long ll = 3434232234234324333;

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

    No. The number does not fit in a long.

  7. Does the following code compile ?
    long ll = 3434232234234324333L;

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

    Answer: Yes.

  8. Does the following code compile ?
    long ll = 3434232234234324333L; float ff = ll;

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

    Yes. A long is automatically promoted to a float.

  9. Can you define 2 non-public classes in one Java file ? Yes/No

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

    Yes. You can define as many as you want non-public Java classes in one file. However, there must be only 1 public class defined in one file and the name of the class must match the name of the file.

  10. What is the output of the following code ?
    protected class SimpleJava { public static void main(String args[]) { System.out.println("SimpleJava ...."); } }

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

    The code does not compile. A class cannot be protected.

  11. What is the output of the following code ?
    private class SimpleJava { public static void main(String args[]) { System.out.println("SimpleJava ...."); } }

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

    The code does not compile. A class cannot be private.

  12. What is the output of the following code ?
    synchronized class SimpleJava { public static void main(String args[]) { System.out.println("SimpleJava ...."); } }

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

    The code does not compile. A class cannot be synchronized.

  13. What is the output of the following code ?
    native class SimpleJava { public static void main(String args[]) { System.out.println("SimpleJava ...."); } }

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

    The code does not compile. A class cannot be native.

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

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

    The code does not compile. A class cannot be final and abstract at the same time. Because the class is final it cannot be extended while being abstract means that it cannot be used without extending it first.

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

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

    "SimpleJava". "strictfp" is a valid class modifier.

  16. Does the following code compile ?
    abstract strictfp class SimpleJava { public static void main(String args[]) { System.out.println("SimpleJava ...."); } abstract void toto() { ;} }

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

    No. An abstract method cannot have a body.

  17. Does the following code compile ?
    class SimpleJava { static int toto; toto = 7; public static void main(String args[]) { System.out.println("SimpleJava ...."); } }

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

    No. The line

    toto = 7;
    is not a declaration. In a class declaration, but outside a block, a nested class or a member function only declarations are allowed.

    Observation:

    In order to compile, the former line must be enclosed in a *static* block because the variable toto is static:

    static {toto = 7;}
  18. A final reference prevents the pointed-to object from being modified. True/False

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

    False. Only the reference itself cannot be changed, not the pointed-to reference.

  19. What is the meaning of the keyword transient ?

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

    Transient attributes are not serialized during object serialization.

  20. What is the meaning of the keyword volatile ?

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

    Volatile attributes are asynchronously modified by other threads or by some hardware. As a result the compiler cannot use any optimizations when reading/writing to this variable.










Best regards,
Razvan MIHAIU




Razvan Mihaiu � 2000 - 2024