Anda di halaman 1dari 2

Mock test: http://www.coderanch.com/how-to/java/ScjpMockTests 1. Pay attention to import statement for any exception. 2. Pay attention to class visibility.

If an interface is declared in a different package than the class that implements it, be aware that the interface must be declare public , so that that class can use it. 3. Pay attention to those instance variables. They may not be initialized to any object and they are null. Will null pointer exception thrown in the code? 4. Pay attention to protected constructor. If the constructor is protected, its child class can use super() in their constructor declaration, but the child clas s cannot instantiate object using the protected constructor. 5. Pay attention to local variables. They are not initialized and when the code use them, there will be a null pointer exception. There may be a warning from Ec lipse telling you those local variables are not initialized. 6. Pay attention to == with autoboxed objects. If two Integer objects are create d via autoboxing, eg Integer a = 10 ; Integer b =10 ; a==b? True. If Integer a = 129, Integer b = 129, will a==b? False. For more detail, refer to autoboxing section. 7. Pay attention to variable scope. If the variables are declared in a method/cl ass, if there is a for loop that declare another variable with the same name, it won't compile. 8. Pay attention to printing variables that are declared inside a block like for loop or while loop. Those variable are out of scope. Using them outside the sco pe will not compile. 9. Pay attention to ++a and a++. For example , array[++i][i++], what will be the output? 10. Pay attention to protected variable that are called by the child class thru reference, eg In child class, Parent p = new Parent(); p.x .... Child class cann ot access its parent's variable by using their reference, but they can only inhe rit it. 11. Pay attention to override/overloaded methods. The tricky thing is about decl aring exception in overriden method, using public/protected/private for overridi ng method, overriding final methods ... 12. Polymorphism does not apply to static method or static variable. eg. Parent p = new Parent(); Parent c = new Child(); There are static methods s() declared in both classes. p.s() and c.s() will call the same methods. the Child c does no t call child's static method, it call the parent's static method. 13. For concurrency, if two different instances of X class are locked inside the synchronized method, this syn. method can still be executed concurrently by mul tiple threads. 14. When a join() is called, t.join() is called inside a main method. t thread w ill finish before the main. And the tricky part is....join() throws Interrupted exception. It must be declared or handled. 15. non-static syn. methods and static syn methods can be executed concurrently. 16. You may be asked if there is a deadlock in the syn. method. You may see a sy n block inside another syn block. Pay attention if the two objects being locked are the same or different. If the two objects being locked are refering to the s ame object, deadlock won't happen. For more example, read KB's practice exam. 17. Pay attention to garbage collecting static variables. These variables are ne ver GCed. 18. Pay attention to passing an integer to a method as a short argument. You can do this short s = 7; static void method(short s); method (s). However, if you d o this method(7) won't compile because 7 is an integer. 19. Pay attention to println statment. println(1+2+3+"a") outputs 6a while print

ln("a"+1+2+3) outputs a123. How about println("a"+a==a) where a is a string? Wil l you get a false? Or will you get a true? Or will you get "a" true in the outpu t? 20. Pay attention to this statement assert(x>1) : j=12 ? Will it compile, yes. H ow about (assert>1) : new Integer()? Yes, compiles. How about (assert >1) : Inte ger a? No. The reason is if the assert is false, it will print 12 or an integer object, but it cannot print a on the screen. 21. Pay attention to return type of methods that involves generic parameters. Fo r example, E e ; public E get() { //insert return type here } ; Is return e comp iled? Yes. Is return new Object() compiled? No. The compiler does not know what E will be at compile time. It may not be object, so it won't compile. 22. How about this : public ArrayList<Hotel> get() {...} ...What is the return t ype? It must be new ArrayList<Hotel>() . If Inn extends Hotel, we cannot put new ArrayList<Inn>(). Because ArrayList<Inn> does not extend ArrayList<Hotel>. For detail, read KB's b ook. 23. If we have a List<? extends Animal> alist = .... ; can we do this alist.add( new Animal())? No. You are not allowed to add any object except null to a list t hat is declared using ?. How about List<? super Animal> alist = ....? Can you do this alist.add(new Objec t()) ? No. The compiler does not let you add object because the object can be a Dog/Cat. How about alist.add(new Animal()). Yes. The compiler only allows you to add Animal type when you declare List<? super Animal>. If you add Animal cat = new Cat(); it works. 24. Is it compiles ? for ( int i=0, j=0; i<10 & j<10 ; ) ? For loop does not nec essary need an increment statement. 25. When you want to compile using javac . The classpath must be set to the pare nt directory of the package that needs to be compiled. For example, if you have xcom/A.class under foo/test directory. Suppose your current directory is foo. ja vac -classpath test test/xcom/B.java. works when B is defined to be in xcom pack age and B needs A. For most of the time, I always mistakenly think that the -cla sspath should be test/xcom in order to find A. 26. How about java command ? java -classpath test xcom.B works? I think so. The "java" needs to find both A and B classes. It can be done by setting classpath t o test, not test/xcom. B's full name is xcom.B, not B. Pay attention to the diff erence between javac and java when it comes to -classpath. javac -classpath .... (the class files that B needs).... ...(the file path of B)..... For java -cp ... (the class file path B needs and B itself!) ....full name of B...... This is the most confusing part.

Anda mungkin juga menyukai