This month I took the SCJP exam. In this topic, I will talk about the preparation and examination.
Basically for those who are going to take and who need more information about it.
Refinement
Since there is no Sun anymore, there is no SCJP exam either. Now it is:
1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam.
Training
Preparation took two weeks. Used
only these sources:
')
To try yourself, there are still such resources:
- nikojava.wordpress.com
These blog entries are a collection of exam tasks with answers (although there were also erroneous answers, be careful! If in doubt, give the compiler a check). - http://www.certpal.com/
This is an exam emulator. This is how the exam looks like.
Everything. This is enough to pass the exam (and not with the minimum passing score, but with a very good one). True, this is on condition that after all you programmed in Java and are not confused in the basic constructions.
I just read the above articles. Sometimes I tried something in practice. Tests in the emulator took place once. I watched what was wrong in order not to stumble on the same rake in the future.
Exam
Handed over in Petersburg. The exam costs $ 125 (the organization of the exam itself. A positive result is not guaranteed :)).
At the examination center, we were greeted warmly, drank tea, and were treated to cookies. Photographed. Then the test began. Handed over alone (the room is designed for two people who give in, but I was alone). 150 minutes for 60 questions. This is nothing special - a test like in all emulators.
It is necessary to answer correctly not less than 61% of the questions.
You can not use anything, you can go out. Time does not stop.
The result is immediately. Certificate - in a few weeks by mail.
Tasks
In the style of
"Do you know java" articles
, I will show some interesting and not very ordinary tasks from the exam. Again, first of all for those who want to practice before the exam. After all, the surest way to prepare: to decide, decide and solve.
Answers are given after each question.
1. I / O - know method signatures
What happens after this code is executed?
public class Main{ public static final void main(String[] args) { String path = "somepath";
1. Both the directory and the file will be created.
2. Directory only
3. Will not compile
4. java.io.IOException will be thrown
Answer:
2. Directory only (named somename). Nothing more will happen.
2. Serialization and constructors
What will be the result of executing this code?
import java.io.*; import java.util.*; public class Main { public static final void main(String[] args) throws Exception { ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream("abra-cadabra")); out.writeObject(new C(123)); ObjectInputStream in = new ObjectInputStream( new FileInputStream("abra-cadabra")); System.out.print(in.readObject()); } } class A { public A() { System.out.print("A "); } public A(int number) { System.out.print("1 "); } } class B extends A { public B() { System.out.println("B "); } public B(int number) { super(number); System.out.print("2 "); } } class C extends B implements Serializable { public C() { System.out.print("C "); } public A a = null; public C(int number) { super(number); System.out.print("3 "); } }
1. Will not compile
2. java.io.NotSerializableException is thrown
3. 1 2 3
4. 1 2 3 AB
Answer:
4. 1 2 3 AB
The presence of the variable C in the class C
a = null does not lead to a NotSerializableException, since a = null. If instead of
public A a = null; write
public A a = new A (); - NotSerializableException is thrown away.
In this case, everything will be serialized. During deserialization
, the constructor of the desrialized class
is not called , but if its parents do not implement the Serializable interface, then they will invoke the default constructor.
If in this example A is implemented Serializable, not C, then the output will be
1 2 3If in this example the constructor with no parameters is removed in all three classes, then the
java.io.InvalidClassException: no valid constructor will be thrown away during deserialization, since Java is trying to call the constructor without parameters on the parent.
3. Misleading
What will be the result of executing this code?
import java.util.*; public class Main { public static List getSorted() { List<Integer> sorted = new LinkedList<Integer>(); sorted.add(3); sorted.add(1); sorted.add(2); return sorted; } public static final void main(String[] args) throws Exception { System.out.println(getSorted()); } }
1. 1 2 3
2. 3 2 1
3. 3 1 2
4. Will not compile
5. Runtime error
Answer:
3. 3 1 2.
The fact that the method and the sheet inside the method are called getSorted () and sorted does not mean that LinkedList is also sorted.
4. instanceof
What will be the result of executing this code?
class A { } class B extends A { } class C extends B { } class D { } public class Main { public static final void main(String[] args) { B b = new C(); A a = new C(); D d = new D(); System.out.println(b instanceof A); System.out.println(a instanceof B); System.out.println(d instanceof C); } }
1. true true false
2. true false false
3. Compile Error
Answer:
3. Compile error. This will happen because D and C are located on different branches of the hierarchy and are not reducible to each other.
5. And what is the parameter?
public static void print(List<? extends String> list) {
1. list.add (“Hello, Habrahabr!”);
2. list.add (new Object ());
3. list = new ArrayList ();
4. list = new ArrayList <?> ();
5. list = new ArrayList <Objet> ();
Answer:
3. list = new ArrayList ();
Everything is logical: what if List <T> comes to us, where T is really inherited from String? .. Then when calling list.add (“Hello, Habrahabr!”); Java simply cannot do type conversion. Understanding this, the compiler does not allow compiling code with the string '1'. With 2, 4, 5 - everything is also clear. list = new ArrayList (); - correct code.
6. Exceptions and Method Overrides
What will be the result of executing this code?
class A { public void print() throws Exception { throw new Exception(); } } class B extends A { public void print() { System.out.println("B"); } } public class Main { public static final void main(String[] args) { B b = new B(); b.print(); } }
1. B
2. Nothing
3. Compile Error
4. Exception
Answer:
The override method should not throw a new or wider class exception. Do not throw it at all, he can. So the code will compile and output B.
7. Exceptions and method redefinition - 2
What will be the result of executing this code?
class A { public void print() throws Exception { throw new Exception(); } } class B extends A { public void print() { System.out.println("B"); } } public class Main { public static final void main(String[] args) { A a = new B(); a.print(); } }
1. B
2. Nothing
3. Compile Error
4. Exception
Answer:
3. Compile error.
Since we are in class A, an exception is declared, but in main (...) we do not handle it. The type of the return value (which may not be the same as in the superclass, because there is a covariant return) and the exceptions thrown are checked by the compiler by reference type.
8. Integer ++?
How can I change class A so as not to affect the client class?
class A { private int i = 0; public void add(int i) { update(++i); } private void update(int i) { this.i = i; } } public class Main { public static final void main(String[] args) { new A().add(5); } }
1. Replace ++ i with i ++
2. Replace public void add (int i) with private void add (int i)
3. Replace public void add (int i) with public void add (Integer i)
4. None of the above
Answer:
3. Replace public void add (int i) with public void add (Integer i)
You just need to know that
++ and
- works with Integer. And with Double. And with the float. And with all the numerical wrappers.
Read articles, practice. And everything will turn out. There is nothing complicated about it.
If you have any questions about the exam, I will answer in the comments.