Preparation material for SCJP 310-035

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






This web page is meant to be helpful for the preparation of the Sun's Java certification exam for Java 1.4 platform (SCJP 310-035).

Before working with this material read the recommended instructions about its use.

My experience on passing the SCJP exam may be useful to you.


  1. Can an inner class have a valid main() function ?

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

    No, because a valid main() function is static while the inner classes cannot have static member functions.

  2. Can you access a non-static variable/member function from a top-level class inside a static nested class ? Yes/No.

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

    Answer: No.

  3. Does the following code compile ?
    public class CDummy { int someVar = 11; public static void main(String args[]) { System.out.println("CDummy."); NestedOne ns1 = new NestedOne(); System.out.println(ns1.getOuterVariable2()); } static class NestedOne { int getOuterVariable2() {return someVar;} } }

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

    No. You cannot access a non-static attribute (someVar) from a static nested class.

  4. How many types of inner classes there are ?

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

    There are 2 types of inner classes: named and anonymous.

  5. Are nested classes inherited ?

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

    You cannot speak of inheritance in this context. The variables and the member function of a nested class are not directly accessible in the enclosing class - they are accessible only through the class or an object of that class. That being the case nested classes do not behave like superclasses for the enclosing class. However, they are accessible in the derived classes of the enclosing class.

  6. Does the following code compile ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); } class CDummy { } }

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

    No. The inner class cannot have the same name as the enclosing class.

  7. Does the following code compile ?

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

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

    Yes. The line where a dmy object is created is in fact creating an anonymous inner class that extends the class CDummy.

  8. Does the following code compile ?
    public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); } public void getIt(int input) { class LocalInner { public int getIt() { return input;} } } }

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

    No. The inner class LocalInner cannot access the local variable "input" because it is not final.

  9. Does the following code compile ?
    public class CDummy { int someIntanceVar; public static void main(String args[]) { System.out.println("CDummy."); } public void getIt() { class LocalInner { public int getIt() { return someIntanceVar;} } } }

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

    Yes. Instance variables do not need to be final in order to be accessed directly from an inner class.

  10. What is the output of the following code ?
    public class CDummy { int someVar = 10; public static void main(String args[]) { System.out.println("CDummy."); new CDummy().new NestedOne().printOuterVariable(); } class NestedOne { int someVar = 20; void printOuterVariable() { System.out.println(CDummy.this.someVar); System.out.println(someVar); System.out.println(this.someVar); } } }

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

    CDummy. 10 20 20
  11. Can an anonymous inner class implement 2 interfaces ? True/False.

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

    False. An anonymous inner class can implement only 1 interface.

  12. How can you prevent a class from being instantiated ? (there are at least 2 methods)

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

    1. make the class abstract
    2. make all the constructors private
  13. System.currentTimeMillis() vs Date.getTime(): which one to use ?

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

    System.currentTimeMillis() is preferred because it is faster.

  14. Is it recommended to call the exit() method in a Java applet ?

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

    No ! It is the browser's responsibility to halt the JVM when necessary.

  15. How to get the available memory for the JVM ? How about the free memory for the JVM ?

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

    Runtime rt = Runtime.getRuntime(); System.out.println(rt.totalMemory()); System.out.println(rt.freeMemory());
  16. Does this code compile ?
    class CBase implements Comparable { static int toto; CBase(int toto) { this.toto = toto;} public int CompareTo(int input) { if (toto == input) return 0; if (toto <= input) return -1; return 1; } }

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

    No. The compareTo() method takes an Object as a parameter, not a primitive.

  17. What are the wrapper classes for primitive values that differ with more than 1 character from their primitive names ?

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

    Character & Integer.

  18. Does the following code compile ?
    double dd = 4.5; float ff = dd;

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

    No. The cast from double to float must be specified.

  19. Does the following code compile ?
    byte bb = 3; char cc = bb;

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

    No. The cast from byte to char must be specified because the byte can have negative values while the char cannot.

  20. Does the following code compile ?

    byte bb = 100;

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

    Yes. The compiler checks the range and allows the conversion from int to byte.










Best regards,
Razvan MIHAIU




Razvan Mihaiu � 2000 - 2024