Java

Document Sample
Java
Shared by: cskohila
Stats
views:
469
posted:
4/11/2009
language:
Czech
pages:
36
Sun Certified Java Programmer(SCJP 1.4)

JavaBeat Home SCJP 1.4 Home Objectives Forums Mock Exams Online Mock Exam Resources



Mock Exams MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions 1 5 9 13 17 MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions 2 6 10 14 18 MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions 3 7 11 15 19 MockQuestions MockQuestions MockQuestions MockQuestions MockQuestions 4 8 12 16 20



#1 Given:

1. public abstract class Prod { 2. public abstract void prmth1(); 3. public static void prmth2() { 4. int mth2 = 30; 5. System.out.println("prmth2 = " + mth2); 6. } 7. public abstract void prmth3(); 8. }



What is the result?



(1) Compilation succeeds (2) Compilation fails because of an error on line 1 (3) Compilation fails because of an error on line 3 (4) Compilation fails because of an error on line 7

Answer : -------------------------



#2 Given:

1. public class ClassA { 2. public static void main(String [] args) { 3. 4. switch(x) { 5. default: 6. System.out.println("Here it is."); 7. } 8. } 9. }



The ClassA class can be compiled successfully by inserting one of three possible options on line 3. When inserted separately, which three will allow compilation to succeed? (Choose three.)



(1) int x = 6; (2) short x = 3; (3) char x = 'y'; (4) long x = 354;



(5) boolean x = true;

Answer : -------------------------



#3 Given:

1. public class NewGarb { 2. public static Object getIt() { 3. Object rg = new Integer(3); 4. Object dg[][] = new Object[1][2]; 5. dg[0][1] = rg; 6. dg[0][0] = rg; 7. rg = null; 8. return rg; 9. } 10. }



Which statement is true?



(1) The NewGarb class will not compile (2) The getIt() method must not be declared as static (3) The NewGarb class compiles, but an exception is received because dg is not set to null (4) The rg object is eligible for garbage collection after a call to the getIt() method has returned

Answer : -------------------------



#4 Given:

1. public class SetFor { 2. 3. public static void main(String [] args) { 4. 5. System.out.println("You will need to use " + c); 6. } 7. }



Which two additions will individually allow compilation to succeed? (Choose two.)



(1) "char c;" placed on line 2 (2) "char c;" placed on line 4 (3) "char c = 'f';" placed on line 2 (4) "char c = 'f';" placed on line 4 (5) "static char c;" placed on line 2 (6) "char c = new char();" placed on line 4

Answer : -------------------------



#5 Given:

1. public class TeSet { 2. public static void main(String args[]) { 3. int m = 2; 4. int p = 1; 5. int t = 0; 6. for(;p m) { 8. m = p + t; 9. } 10. } 11. System.out.println("t equals " + t); 12. } 13. }



What is the resulting value of t?



(1) 2 (2) 4 (3) 6 (4) 7

Answer : -------------------------



#6



Given:



1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23.



class IntType { public String getType(String a, int b, char c) { String holdit1 = new String(); holdit1 = a; return holdit1; } } class OverType extends IntType { String holdit2 = new String(); holdit2 = holdit2.concat("This is ").concat(a); return holdit2; } public static void main(String args[]) { OverType ot = new OverType(); String x = new String("x"); int y = 1; char z = 'b'; System.out.println(ot.getType(x, y, z)); } }



When inserted on line 10, which line will override the getType method, allowing compilation to succeed and the output "This is x"?



(1) public Char getType(String a, int b, char c) { (2) public Char getType(int b, String a, char c) { (3) public String getType(String a, int b, char c) { (4) public String getType(int b, String a, char c) {

Answer : -------------------------



#7 Given:

1. public class MyThread implements Runnable { 2. private String holdA = "This is "; 3. private int[] holdB = {1,2,3,4,5,6,7,8,9,10}; 4. 5. public static void main(String args[]) { 6. MyThread z = new MyThread(); 7. (new Thread(z)).start(); 8. (new Thread(z)).start(); 9. } 10. 11. public synchronized void run() { 12. for(int w = 0;w 0) { try {wait();} catch (InterruptedException ie) {} } } } public static void main(String[] args) {



new K().m1(); } }



When the processing of line 2 begins how many objects of type J that were created at line 1 are eligible for garbage collection?



(1) 0 (2) 1 (3) 4 (4) 5 (5) Can not be determined without more information (6) Compiler error (7) Run time error (8) None of the above

Answer : -------------------------



#22 What is the output of the following code when compiled and run? Select two correct answers

1 public class Sample { 2 public static void main(String[] args){ 3 int y=0; 4 int x=z=1; 5 System.out.println(y+","+x+","+z); 6 } 7}



(1) Prints 0,1,1 (2) Error during compilation at line 3 (3) Prints 0,0,1 (4) Error during compilation at line 4 (5) Error during compilation at line 5

Answer : -------------------------



#23 What is the output of the following code when compiled and run? Select one correct answer

1 public class Sample { 2 public static void main(String[] args){ 3 int j = 017; 4 int i = (byte)j >> 2; 5 System.out.println(Integer.toBinaryString(i)); 6 } 7}



(1) Prints 3 (2) Error during compilation at line 4 (3) Error during compilation at line 5 (4) Prints 11 (5) Prints 0

Answer : -------------------------



#24 Select three correct statements: (1) The garbage collection thread cannot outlive the last user thread (2) The garbage collection can be forced by invoking System.gc(). (3) The garbage collection thread is a non-deamon thread (4) The finalize() method is invoked at most once by the JVM for any given object (5) The finalize() method may resurrect the object upon which it has been invoked

Answer : -------------------------



#25 What is the output of the following code when compiled and run? Select one correct answer.

import java.io.*; public class TechnoSample { public static void main(String[] args) { TechnoSampleSub myref = new TechnoSampleSub(); try{ myref.test(); }catch(IOException ioe){} } void test() throws IOException{ System.out.println("In TechnoSample"); throw new IOException(); } } class TechnoSampleSub extends TechnoSample { void test() { System.out.println("In TechnoSampleSub"); } }



(1) Prints:

In TechnoSampleSub



(2) Prints:

In TechnoSample



(3) Prints:

In TechnoSample In TechnoSampleSub



(4) Prints:

In TechnoSampleSub In TechnoSample



(5) The code does not compile

Answer : -------------------------



#26 What is the output of the following code when compiled and run with the following command line:

java TechnoSample two three? Select two correct answers.



public class TechnoSample { public static void main(String[] args) throws Exception { int i=2; boolean b = true; throw new Exception("Values are:"+(b!=b)+","+(i=args.length)+","+(b=i==2)); } }



(1) The exception message is Values are:false,3,true (2) The exception message is Values are:true,2,false (3) The exception message is Values are:false,2,true (4) The final value of b is false (5) An exception is thrown at runtime

Answer : -------------------------



#27 Select two correct statements about the code given below?

class A{} class B extends A implements E{} //line 1 class C extends A{} class D extends B{} interface E{} public class Question07 { public static void main(String[] args) { A a = new D(); //line 2 C c = new C(); //line 3 E e = (E)a; //line 4 B b = (B)e; //line 5 } }



(1) The code compiles without error and runs fine



(2) Compilation error on line 1 because interface E is not yet declared (forward-referencing) (3) Compilation error on line 4 because class A does not implement interface E (4) The cast on line 4 is mandatory (5) The cast on line 5 is not mandatory

Answer : -------------------------



#28 How many objects are eligible for garbage collection immediately after line 1? Select one correct

answer.



public class TechnoGC { public static void main(String[] args) { TechnoGC tGC = new TechnoGC(); tGC.doSomething(); //line 1 Thread.sleep(20000); } public void doSomething(){ Object[] objArray = new Object[2]; for(int i = 0 ; i 5"); } catch (Exception e){ System.err.println(e.getMessage()+" (i="+i+")"); } } }



(1) The output cannot be determined (2) Compilation error (3) An exception is thrown at runtime (4) Output is i = 2 (5) Output is i > 5 (i=6)

Answer : -------------------------



#30 What is the output of the following code when compiled and run? Select one correct answer.

public class TechnoSample { public static void main(String[] args) { new TechnoSample().doSomething(); } public void doSomething(){ int i=5; Thread t = new Thread(new Runnable(){ public void run(){ for(int j=0;j 1) System.out.println("No arithmetic exception"); } catch (ArithmeticException ae){ System.err.println("Arithmetic exception caught"); } } }



(1) Compilation error (2) No arithmetic exception will never be printed (3) The final value of i is 2 (4) The final value of i is 0 (5) The try-catch block is necessary for the code to compile

Answer : -------------------------



#35 What is the output of the following code when compiled and run? Select two correct answers.

public class TechnoSample { public static void main(String[] args) throws Exception{ Thread t1 = new Thread(getRunnable(3)); Thread t2 = new Thread(getRunnable(4)); t1.join(); System.out.println("End"); } public static Runnable getRunnable(final int id){ return new Runnable(){ public void run(){ for(int i = 0; i > 2 as a whole. Thus, j is downcast to byte and then upcast to int again before the shifting.Briefly, the cast has no effect here. Then, the binary sequence of 15 is 00000000 00000000 00000000 00001111, which, shifted 2 bits to the right, yields 00000000 00000000 00000000 00000011. Finally, the binary sequence, 11, is printed. Note that the prefixed 0's are dismissed. ****



[24] 1,4,5 Explanation: The garbage collection thread is a deamon thread. The latter die when there are no more users threads running. The garbage collection cannot be forced. ****



[25] 5 Explanation: The code does not compile because no IOException is thrown when invoking myref.test(). Note that myref's declared and runtime types are TechnoSampleSuband thus no dynamic lookup will be performed. However, if you change the declared type to TechnoSample, the code will compile and the correct answer would be A because method test() isoverridden in TechnoSampleSub ****



[26] 3,5 Explanation: Do not mix b!=b and b=!b. In the former, we check if b's value is different from b's value (?!) which is clearly false. In the latter, we assign b's opposite value to itself, that is, if b is true, then after b=!b, b ends up being false.Moreover, be aware that b=i==2 is evaluated as b=(i==2) because operator = has the lowest precedence. Finally, note that the arguments to the Exception constructor are evaluatedfrom the left to the right. First, b!=b is evaluated, then i=args.length (args.length is 2, so i keeps its value), and finally, b=i==2. ****



[27] 1,4 Explanation: First, pay attention to the class hierarchy (B and C are sibling classes!!) Then, there is no such thing as forwardreferencing issues when using interfaces declared later in the compilation unit.On line 4, we are dealing with an object whose runtime type is D which implements interface E. The cast is mandatory, though, since the reference type (A) is not assignmentcompatible with the reference type E. The cast on line 5 is mandatory for the same reasons. ****



[28] 4 Explanation: We declare an array of Object of length two. We then initialize each element to a new Object. We have 2 objects in the array and the array itself (which is an object, too!), that makes 3. ****



[29] 2 Explanation: The code does not compile because i (declared in the try block!) is not in scope when accessed from the catch block. ****



[30] 2 Explanation: The code does not compile because the anonymous inner class (new Runnable(){...}) tries to access the non-final local variable i ****



[31] 4 Explanation: Whenever you perform some operation upon a String object, you will get a new String object if the operation had some effect on the original String, we say that a String is immutable and that its sequence of characters cannot be changed.Here, we are dealing with a StringBuffer which is an implementation of a mutable String, that is you can modify the character sequence it contains. Whenever you perform an operation upon a StringBuffer, the same StringBuffer(whose content has been modified) is returned. The next important thing to remember is that when a reference to an object is passed as argument to a method, a copy of the reference is actually passed to the method. In this case,buf1 is passed to addSomeStuff and replace is invoked upon it. The changes operated by replace are affecting buf1. We then store the reference to buf (which references the same object as buf1 into a local StringBuffer called b. ****



[32] 3,4 Explanation: This question mainly tests your knowledge of the various wrapper classes' constructors. The most notorious thing here is that we can actually give a double as an argument to the Float constructor. Otherwise, the question tests your ability to understand how Collection classes work. The effect of add(), elementAt() and setElementAt() is trivial, just have a look at the API of the Vector class for more information ****



[33] 3,5 Explanation: This shows you that Unicode characters are processed very early during the lexical parsing of a Java source file. Here LF, which is a line terminator, isactually translated to a line terminator and this results in malformed Java code. For instance, the example given above is actually interpreted as: String s = "Hello World"; You can notice that the first line is unterminated and the compiler lets you know that. ****



[34] 2,3 Explanation: An ArithmeticException is always thrown when executing this code because Math.random() returns a double between 0.00000 and 0.999999 and Math.floor() just removes the decimal part (the part after the dot).So, the result of the combination of Math.floor(Math.random()) is always 0.0.The cast to int guarantees that 0 will be returned, and an integer division by 0 does always throw an ArithmeticException. ****



[35] 1,2 Explanation: We wait on Thread t1 to finish before printing End but none of the threads are started so only End is printed. Moreover, the same output will be printed whichever the platform is. ****



[36] 1,4 Explanation: Line 1 does not compile because getPrimitive() takes a byte and we pass it an int. In a normal assignment (byte b = 127;) this would work because 127 is in the range for byte values and the compiler implicitely does a norrowing conversion.But this is not the case in method invocations. A quote from JLS 5.3: "The designers of the Java programming language felt that including these implicit narrowing conversions would add additional complexity to the overloaded method matchingresolution process". This speaks for itself. Line 3 compiles fine because we have to do with a widening primitive conversion form short to int which is perfectly straightforward. ****



[37] 2,3,4 Explanation: Overriding is for non-static methods and hiding is for static methods. So the following statements are the only true statements about hiding and overriding:

   



a a a a



static method (in a subclass) may hide another static method (in a superclass) static method (in a subclass) cannot hide a non-static method (in a superclass) non-static method (in a subclass) may override another non-static method (in a superclass) non-static method (in a subclass) cannot override a static method (in a superclass)



****



[38] 1,3,4 Explanation: myref is an instance of class TechnoSampleSub referenced by a variable of type TechnoSample. Method test() in class TechnoSample is overridden in class TechnoSampleSub, thus the one to be invoked is the one declared in class TechnoSampleSub(Polymorphism!). Moreover, test() has no obligation to declare a throws clause (see overriding rules!). The try-catch block is mandatory because myref could as well reference an instanceof class TechnoSample and in that case the method test() to be invoked would be the one declared in class TechnoSample which throws an exception. ****



[39] 2 Explanation: 1) is false because we know that the data in d2 was changed. 3) is false because we know that the data in d1 was not changed. The names d1 and d2 are used in both main and method to be confusing. They are different and stored on the stack in different plaAll the interesting stuff that happen in the Example class is in method. main simply initializes some data and prints the results. In method, the followning happens: 1.d2 has its year set to 100 (really 2000, as 2.Object d1 is set to be the same as d2. This is a change of the actual reference, not in the data at d1. Both of these line are perfectly legal, and do not result in a compilation error, so d) is false. I will also point out here that e) is String context. toString() is defined by the Object class and so it is available on all classes in Java. Most non-trivial classes override toString() to return more explicit information about themselves. ****



[40] 1 Explanation: ****



[41] 1,2,3 Explanation: Because the variable oak is declared as static only one copy of it will exist. Thus it can be changed either through the name of its class or through the name of any instance of that class.Because it is created as an integer it canot be assigned a fractional component without a cast. ****



[42] 4 Explanation: The method fermin only receives a copy of the variable i and any modifications to it are not reflected in the version in the calling method.The post increment operator ++ effectivly modifes the value of i after the initial value has been assiged to the left hand side of the equals operator. ****



[43] 1,3,4 Explanation: The options for this question might look suspiciously easy if you are not aware of the effects of the post-increment operators. The ++ and -- operations for examples 1 and 4 only come into effect after the outputoperations, ie after whatever else is done to them on that line of code. Option 2 should be fairly obvious as you should know that the single quote characters indicate a char value, ie storing the character ratherthan the numberical value for 0. ****



[44] 1 Explanation: A top leve (non nested) class cannot be private.



****



[45] 3 Explanation: he default layout manager for a Frame is the BorderLayout manager. This Layout manager defaults to placing components in the centre if no constraint is passed with the call to the add method. ****



[46] 4 Explanation: If the for loop were defined in a method called: public void run() and the call in the main method had been to b.start(), the list of values from 0 to 9 would have been output. ****



[47] 4 Explanation: Note that && is short-circuit logical AND operator. If first operand (before &&) is false (or evaluated to false) then the other operand will not be evaluated.This illustrates that the Output +=10 calculation was never performed because processing stopped after the first operand was evaluated to be false.If you change the value of b1 to true, processing occurs as you would expect and the output would be "We are equal 20". ****



[48] 1 Explanation: When we pass references in Java what actually gets passed is the value of that reference (i.e. memory address of the object being referenced and not the actual object called by that reference) and it gets passed as value (i.e a copy of thereference is made). Now when we make changes to the object referenced by that reference it reflects on that object even outside of the method being called but any changes made to the reference itself is not reflected on that reference outside ofthe method which is called. ****



[49] 1 Explanation: The basic principle is that a method cannot be overridden to be more private. Since the method is being overridden to be friendly (default modifier) it can only be private or friendly in the superclass.Also, if the method in superclass is left as it is (i.e. friendly access) the method in subclass can be friendly, protected or public. ****



[50] 1 Explanation:



Exception :java.lang.IllegalAccessExcption must be caught or placed in the throws clause of the throwMethod(), i.e. the declaration of throwMethod() bechanged to "static void throwMethod() throws IllegalAccessExcption". Thus compilation error will occur. JavaBeat 2005, India (www.javabeat.net) Submit a Site - Directory - Submit Articles




Share This Document


Related docs
Other docs by cskohila
Java
Views: 469  |  Downloads: 74
Jabber Messenger
Views: 116  |  Downloads: 4
java
Views: 1354  |  Downloads: 167
by registering with docstoc.com you agree to our
privacy policy

You are almost ready to download!

You are almost ready to download!