(Mock Test) Mock Exam Question SCJP 1.4
Mock Test : Mock Exam Question SCJP 1.4
41. 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
42. 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.
43. 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)
44. 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)
45. 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.
46. Can you attach many threads to a class implementing Runnable ? True/False.
Answer: True
47. 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.
48. 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.
49. 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.
50. What are nested classes ?
Any class defined inside another class.
51. What are inner classes ?
Non-static nested classes.
52. How many types of nested classes there are ?
Static nested classes and inner classes.
53. 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.
- guru's blog
- Login to post comments
