SCJP Exam 1.4

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

  1. What is the output of the following program ?

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

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

    Dummy. 1 1 1
  2. What is wrong with the following code ? (there is a logical not syntactical error)
    public void coolFunction() { String tmp; synchronized(tmp) { doSyncWork(); } }

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

    The synchronization takes place on a local variable. This is useless because all threads have their local copy of a local variable. As it is the code is not synchronized at all.

  3. What is the output of the following code ?
    try { System.out.println("Test !"); } catch(InterruptedException ee) { System.out.println("The main thread was interrupted !"); }

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

    The code does not compile because the exception InterruptedException is never thrown. (InterruptedException is checked)

  4. What is the output of the following code ?
    try { System.out.println("Test !"); } catch(IndexOutOfBoundsException ee) { System.out.println("The main thread was interrupted !"); }

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

    Test !

    Attention: IndexOutOfBoundsException is not checked (for this reason the code compiles)

  5. Can you attach many threads to a class extending Thread ? True/False.

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

    Answer: True.

    This is a tricky question. Bear in mind that the class Thread implements the Runnable interface. As a result you could always write something like this:

    class MyThread extends Thread{} MyThread th = new MyThread(); new Thread(th).start(); new Thread(th).start(); new Thread(th).start();

    Thus to the object "th" you can attach many threads.

    OTOH it is true that you cannot call many times the start() method on the same object:

    th.start(); th.start();
    because the code will fail at runtime.

  6. Can you attach many threads to a class implementing Runnable ? True/False.

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

    Answer: True

  7. Will the following code compile ? Yes/No
    class CDummy { int someInt; void printData(String input) { synchronized (someInt) { System.out.println(input); } } }

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

    No ! Synchronization can only be done on Object(s) not primitives.

  8. When an IllegalMonitorStateException is thrown ?

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

    When the wait() or notify() functions are called on an Object on which the current thread does not have a lock.

  9. Does the following code compile ? Yes/No
    public class CDummy { static int someVar1 = 9; int someVar2 = 11; public static void main(String args[]) { System.out.println("CDummy."); NestedTwo ns2 = new CDummy().new NestedTwo(); System.out.println(ns2.getOuterVariable2()); } class NestedTwo { static int getOuterVariable1() {return someVar1;} int getOuterVariable2() {return someVar2;} } }

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

    No. Inner classes cannot have static attributes or members. The function getOuterVariable1() is static.

  10. What are nested classes ?

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

    Any class defined inside another class.

  11. What are inner classes ?

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

    Non-static nested classes.

  12. How many types of nested classes there are ?

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

    Static nested classes and inner classes.

  13. How can you define a static inner class ?

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

    Inner classes are not static by definition, nor do they allow any static member functions or attributes.


If you wish to add something feel free to use the forums.









Best regards,
Razvan MIHAIU






Razvan Mihaiu © 2000 - 2009