(Mock Test) Mock Exam Question SCJP 310-035

Mock Test : Mock Exam Question SCJP 310-035

21. Does the following code compile ?
class Parent{} class DerivedOne extends Parent{} class DerivedTwo extends Parent{} public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); DerivedOne d1 = new DerivedOne(); Parent p1 = new DerivedOne(); DerivedTwo some1 = (DerivedTwo) p1; DerivedTwo some2 = (DerivedTwo) d1; } }

The line:
DerivedTwo some2 = (DerivedTwo) d1;
fails to compile. A reference of type DerivedOne (the object d1 is of type DerivedOne) cannot point to an object of type DerivedTwo, so the cast from DerivedOne to DerivedTwo performed in the above line will fail.

The line:
DerivedTwo some1 = (DerivedTwo) p1;
passes the compilation but throws a run time exception because the p1 reference really points to an object of type DerivedOne.

22. You have the following classes defined in 2 separate files. Why the class CDummy2 does not compile ?
FILE CDummy.java
public class CDummy { static class SomeStaticNested{ void printIt() { System.out.println("Message from static nested: Ola !"); } } class SomeNested{ void printIt() { System.out.println("Message from nested: Ola !"); } } }

FILE CDummy2.java
public class CDummy2 { public static void main(String args[]) { System.out.println("CDummy."); CDummy.SomeStaticNested ssm = new CDummy.SomeStaticNested(); ssm.printIt(); CDummy.SomeNested sm = new CDummy().new SomeNested(); sm.printIt(); } }

There is nothing wrong with the class CDummy2. It compile just fine.

23. Does the following code compile ?
new Thread(new Thread());
Yes. The class Thread implements the interface Runnable. There is no Thread() constructor that takes a Thread as an argument but there are constructors that take a Runnable reference.

24. Does the following code compile ?
public class CDummy implements Runnable { void run() { } }
No. The run() method is public in the interface Runnable; you cannot assign a weaker access privilege. (in this case you try to assign a package level access to the method run())

25. What is the output of the following code ?
Vector a = new Vector(); a.add("Teo"); System.out.println(a[0]);
The code does not compile. The Vector cannot be accessed like an array.

26. What is the output of the following code ?
class SimpleJava { public static void main(String args[]) { System.out.println("SimpleJava ...."); int arr[] = new int[3] {1,2,3}; System.out.println("arr[0] = " + arr[0]); } }

The code does not compile. Upon creation the array can be initialized with a list specified between {} but in this case it is illegal to specify its dimension. (the dimension is implicitly equal with the number of elements specified in the list)

27. What is the output of the following code ?
FILE 1
package name.mihaiu.test1; public class CTest1 { static String name = "CTest1"; }

FILE 2
package name.mihaiu.test2; public class CTest2 extends name.mihaiu.test1.CTest1 { public static void main(String args[]) { System.out.println("Class name =" + name); } }

The code does not compile. The static variable name has package access level. In order to be accessible from another package (in our case "name.mihaiu.test2") it must have public level access.

28. Can you provide a constructor for an anonymous inner class ? Yes/No
No. A constructor, by definition, has the same name as its class. Since an anonymous class has no name you cannot provide a constructor for it.

29. Can you have protected member functions in an interface ?
No. By definition an interface represents the part of the class that is accesible to clients, therefore it must be public.

30. What kind of primitives can be used in a switch instruction ? (enumerate them)
Answer: "byte", "char", "short" and "int".

31. What is the return value of assert ?
There is no return value; assert is a Java keyword and not a function.

32. What is the output of the following code ?
abstract public class CDummy { public static void main(String argv[]) { System.out.println("CDummy."); } abstract void doIt(); }

CDummy.

Since the abstract class is never instantiated and the abstract method doIt() is never called, the code compiles & run without issues.

33. What is the output of the following code ?
public class CDummy { public static void main(String args[]) { try { testX(); } catch (Exception ee) { System.out.println("Exception !"); } } public static int testX() throws ArithmeticException { try { throw new ArithmeticException(); }finally { System.out.println("Finally"); return 4; } } }

"Finally". Since there is a return statement in the finally block, the generated exception is scrapped.

34. Does the following code compile ?
public class CDummy { };
Yes ! The semicolon at the end of the class definition does not seem to bother the java compiler !

35. Does the following code compile ? Yes/No
import java.io.IOException; class CBase { public static int testX2() throws IOException { return 1;} } public class CDummy extends CBase { public static void main(String args[]) { } public static char testX2() throws IOException { return 7;} }

No ! The overridden method differs only by the return type !

36. Does the following code compile ?
import java.io.IOException; class CBase { public static int testX2() throws IOException { return 1;} } public class CDummy extends CBase { public static void main(String args[]) { } protected static int testX2() throws IOException { return 7;} }



No ! The overridden method attempts to restrict the access level (from public to protected). This is illegal in Java.

37. Does the following code compile ?
import java.io.IOException; class CBase { private static int testX2() throws IOException { return 1;} } public class CDummy extends CBase { public static void main(String args[]) { } protected static char testX2() throws Exception { return 7;} }

Yes ! Because the method testX2() in CBase is private the method is not visible at all in CBase. As a result the method testX2() from CDummy is not in the same namespace as the corresponding method from CBase. If the method textX2() from CBase would be protected then the code will fail to compile for 2 reasons:
1. the overriding function testX2() from the class CDummy differs only by the return type from the overridden function textX2() from the class CBase;
2. the overriding function throws Exception which is not derived from IOException (in fact it is the other way around); 

38. You cannot override final or private methods: True/False.
True. Final methods cannot be overridden by definition while private methods cannot be overridden because they are not visible outside the class itself. (however the method name can be reused)

39. Can you override a variable ?
Yes. In this case the variable is said to shadow the variable from the superclass.

Observation:
The polymorphism does not work for variables. The following code will print 7.
class BookElement {int someVar = 7;} class Chapter extends BookElement {int someVar = 9;} public class CDummy { public static void main(String args[]) { System.out.println("CDummy."); Chapter ch = new Chapter(); BookElement bk = ch; System.out.println(bk.someVar); // it prints 7 } }

The references for variables are computed at compile-time not run-time.

40. Is method synchronization inherited ? True/False.
Answer: False.



comments powered by Disqus