CertCities.com -- The Ultimate Site for Certified IT Professionals
Post Your Mind in the CertCities.com Forums Share share | bookmark | e-mail
  Microsoft®
  Cisco®
  Security
  Oracle®
  A+/Network+™
  Linux/Unix
  More Certs
  Newsletters
  Salary Surveys
  Forums
  News
  Exam Reviews
  Tips
  Columns
  Features
  PopQuiz
  RSS Feeds
  Press Releases
  Contributors
  About Us
  Search
 

Advanced Search
  Free Newsletter
  Sign-up for the #1 Weekly IT
Certification News
and Advice.
Subscribe to CertCities.com Free Weekly E-mail Newsletter
CertCities.com

See What's New on
Redmondmag.com!

Cover Story: IE8: Behind the 8 Ball

Tech-Ed: Let's (Third) Party!

A Secure Leap into the Cloud

Windows Mobile's New Moves

SQL Speed Secrets


CertCities.com
Let us know what you
think! E-mail us at:



 
 
...Home ... Editorial ... Pop Quiz ..Pop Quiz Article Tuesday: April 1, 2014


Sun Certified Java Programmer (SCJP) Exam
30 questions. Answers and detailed explanations can be found at the end of the quiz.


courtesy of   Whizlabs Software

Questions


1. What will happen when you attempt to compile and run the following code?

public class Static { static { int x = 5; } static int x,y; public static void main(String args[]) { x--; myMethod(); System.out.println(x + y + ++x); } public static void myMethod() { y = x++ + ++x; } } Choices: a. Compiletime error b. prints : 1 c. prints : 2 d. prints : 3 e. prints : 7 f. prints : 8 2. Given the following code, what will be the output? class Value { public int i = 15; } public class Test { public static void main(String argv[]) { Test t = new Test(); t.first(); } public void first() { int i = 5; Value v = new Value(); v.i = 25; second(v, i); System.out.println(v.i); } public void second(Value v, int i) { i = 0; v.i = 20; Value val = new Value(); v = val; System.out.println(v.i + " " + i); } } Choices: a. 15 0 20 b. 15 0 15 c. 20 0 20 d. 0 15 20 3. What will happen when you attempt to compile and run the following code? class MyParent { int x, y; MyParent(int x, int y) { this.x = x; this.y = y; } public int addMe(int x, int y) { return this.x + x + y + this.y; } public int addMe(MyParent myPar) { return addMe(myPar.x, myPar.y); } } class MyChild extends MyParent { int z; MyChild (int x, int y, int z) { super(x,y); this.z = z; } public int addMe(int x, int y, int z) { return this.x + x + this.y + y + this.z + z; } public int addMe(MyChild myChi) { return addMe(myChi.x, myChi.y, myChi.z); } public int addMe(int x, int y) { return this.x + x + this.y + y; } } public class MySomeOne { public static void main(String args[]) { MyChild myChi = new MyChild(10, 20, 30); MyParent myPar = new MyParent(10, 20); int x = myChi.addMe(10, 20, 30); int y = myChi.addMe(myChi); int z = myPar.addMe(myPar); System.out.println(x + y + z); } } Choices: a. 300 b. 240 c. 120 d. 180 e. Compilation error f. None of the above 4. What will be the result of executing the following code? 1. boolean a = true; 2. boolean b = false; 3. boolean c = true; 4. if (a == true) 5. if (b == true) 6. if (c == true) System.out.println("Some things are true in this world"); 7. else System.out.println("Nothing is true in this world!"); 8. else if (a && (b = c)) System.out.println("It's too confusing to tell what is true and what is false"); 9. else System.out.println("Hey this won't compile"); Choices: a. The code won't compile b. "Some things are true in this world" will be printed c. "Hey this won't compile" will be printed d. None of these 5. What will happen when you attempt to compile and run the following code? interface MyInterface { } public class MyInstanceTest implements MyInterface { static String s; public static void main(String args[]) { MyInstanceTest t = new MyInstanceTest(); if(t instanceof MyInterface) { System.out.println("I am true interface"); } else { System.out.println("I am false interface"); } if(s instanceof String) { System.out.println("I am true String"); } else { System.out.println("I am false String"); } } } Choices: a. Compiletime error b. Runtime error c. Prints : "I am true interface" followed by " I am true String" d. Prints : "I am false interface" followed by " I am false String" e. Prints : "I am true interface" followed by " I am false String" f. Prints : "I am false interface" followed by " I am true String" 6. What results from attempting to compile and run the following code? public class Ternary { public static void main(String args[]) { int a = 5; System.out.println("Value is - " + ((a < 5) ? 9.9 : 9)); } } Choices: a. prints: Value is - 9 b. prints: Value is - 5 c. Compilation error d. None of these 7. In the following pieces of code, A and D will compile without any error. True/False? A: StringBuffer sb1 = "abcd"; B: Boolean b = new Boolean("abcd"); C: byte b = 255; D: int x = 0x1234; E: float fl = 1.2; Choices: a. True b. False 8. Considering the following code, Which variables may be referenced correctly at line 12? 1. public class Outer 2. { 3. public int a = 1; 4. private int b = 2; 5. public void method(final int c) 6. { 7. int d = 3; 8. class Inner 9. { 10. private void iMethod(int e) 11. { 12. 13. } 14. } 15. } 16. } Choices: a. a b. b c. c d. d e. e 9. What will be the result of executing the following code? public static void main(String args[]) { char digit = 'a'; for (int i = 0; i < 10; i++) { switch (digit) { case 'x' : { int j = 0; System.out.println(j); } default : { int j = 100; System.out.println(j); } } } int i = j; System.out.println(i); } Choices: a. 100 will be printed 11 times. b. 100 will be printed 10 times and then there will be a runtime exception. c. The code will not compile because the variable i cannot be declared twice within the main() method. d. The code will not compile because the variable j cannot be declared twice within the switch statement. e. None of these. 10. What will happen when you attempt to compile and run the following code? class MyThread extends Thread { public void run() { System.out.println("MyThread: run()"); } public void start() { System.out.println("MyThread: start()"); } } class MyRunnable implements Runnable { public void run() { System.out.println("MyRunnable: run()"); } public void start() { System.out.println("MyRunnable: start()"); } } public class MyTest { public static void main(String args[]) { MyThread myThread = new MyThread(); MyRunnable myRunnable = new MyRunnable(); Thread thread = new Thread(myRunnable); myThread.start(); thread.start(); } } Choices: a. Prints : MyThread: start() followed by MyRunnable:run() b. Prints : MyThread: run() followed by MyRunnable:start() c. Prints : MyThread: start() followed by MyRunnable:start() d. Prints : MyThread: run() followed by MyRunnable:run() e. Compile time error f. None of the above 11. Given the code below, and making no other changes, which access modifiers (public, protected or private) can legally be placed before myMethod() on line 3? If line 3 is left as it is, which keywords can legally be placed before myMethod on line 8? 1. class HumptyDumpty 2. { 3. void myMethod() {} 4. } 5. 6. class HankyPanky extends HumptyDumpty 7. { 8. void myMethod() {} 9. } Choices: a. private or nothing(i.e. leaving it as it is) on line 3. Nothing(i.e. leaving it as it is) or protected or public on line 8. b. public or protected on line 3. private or nothing(i.e. leaving it as it is) on line 8. c. nothing(i.e. leaving it as it is) or protected or public on line 3. private or nothing(i.e. leaving it as it is) on line 8. d. None of the above. 12. What will be the result of executing the following code? // Filename; SuperclassX.java package packageX; public class SuperclassX { protected void superclassMethodX() { } int superclassVarX; } // Filename SubclassY.java 1. package packageX.packageY; 2. 3. public class SubclassY extends SuperclassX 4. { 5. SuperclassX objX = new SubclassY(); 6. SubclassY objY = new SubclassY(); 7. void subclassMethodY() 8. { 9. objY.superclassMethodX(); 10. int i; 11. i = objY.superclassVarX; 12. } 13. } Choices: a.Compilation error at line 5 b. Compilation error at line 9 c. Runtime exception at line 11 d. None of these 13. Consider the class hierarchy shown below:
--------------------------------------------------------------------
class FourWheeler implements DrivingUtilities
class Car extends FourWheeler
class Truck extends FourWheeler
class Bus extends FourWheeler
class Crane extends FourWheeler
----------------------------------------------------------------------

Consider the following code below: 1. DrivingUtilities du; 2. FourWheeler fw; 3. Truck myTruck = new Truck(); 4. du = (DrivingUtilities)myTruck; 5. fw = new Crane(); 6. fw = du; Which of the statements below are true? Choices: a. Line 4 will not compile because an interface cannot refer to an object. b. The code will compile and run. c. The code will not compile without an explicit cast at line 6, because going down the hierarchy without casting is not allowed. d.The code at line 4 will compile even without the explicit cast. e.The code will compile if we put an explicit cast at line 6 but will throw an exception at runtime. 14. What results from the following code? 1. class MyClass 2. { 3. void myMethod(int i) {System.out.println("int version");} 4. void myMethod(String s) {System.out.println("String version");} 5. public static void main(String args[]) 6. { 7. MyClass obj = new MyClass(); 8. char ch = 'c'; 9. obj.myMethod(ch); 10. } 11. } Choices: a. Line 4 will not compile as void methods can't be overridden. b. An exception at line 9. c. Line 9 will not compile as there is no version of myMethod which takes a char as argument. d. The code compiles and produces output: int version. e. The code compiles and produces output: String version. 15. What is the result when you compile and run the following code? public class ThrowsDemo { static void throwMethod() { System.out.println("Inside throwMethod."); throw new IllegalAccessException("demo"); } public static void main(String args[]) { try { throwMethod(); } catch (IllegalAccessException e) { System.out.println("Caught " + e); } } } Choices: a. Compilation error b. Runtime error c. Compile successfully, nothing is printed. d. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo 16. What will be printed when you execute the following code? class X { Y b = new Y(); X() { System.out.print("X"); } } class Y { Y() { System.out.print("Y"); } } public class Z extends X { Y y = new Y(); Z() { System.out.print("Z"); } public static void main(String[] args) { new Z(); } } Choices: a. Z b. YZ c. XYZ d. YXYZ 17. What is the result when you compile and run the following code? public class Test { public void method() { for(int i = 0; i < 3; i++) { System.out.print(i); } System.out.print(i); } } Choices: a. 0122 b. 0123 c. Compilation error d. None of these 18. What will happen when you attempt to compile and run the following code? int Output = 10; boolean b1 = false; if((b1 == true) && ((Output += 10) == 20)) { System.out.println("We are equal " + Output); } else { System.out.println("Not equal! " + Output); } Choices: a. Compilation error, attempting to perform binary comparison on logical data type. b. Compilation and output of "We are equal 10". c. Compilation and output of "Not equal! 20". d. Compilation and output of "Not equal! 10". 19. What will be the result of executing the following code? Given that Test1 is a class. 1. Test1[] t1 = new Test1[10]; 2. Test1[][] t2 = new Test1[5][]; 3. if (t1[0] == null) 4. { 5. t2[0] = new Test1[10] 6. t2[1] = new Test1[10] 7. t2[2] = new Test1[10] 8. t2[3] = new Test1[10] 9. t2[4] = new Test1[10] 10. } 11. System.out.println(t1[0]); 12. System.out.println(t2[1][0]); Choices: a. The code will not compile because the array t2 is not initialized in an unconditional statement before use. b. The code will compile but a runtime exception will be thrown at line 12. c. The code will compile but a runtime exception will be thrown at line 11. d. None of these. 20. What will happen when you attempt to compile and run the following code? class Base { int i = 99; public void amethod() { System.out.println("Base.amethod()"); } Base() { amethod(); } } public class Derived extends Base { int i = -1; public static void main(String argv[]) { Base b = new Derived(); System.out.println(b.i); b.amethod(); } public void amethod() { System.out.println("Derived.amethod()"); } } Choices: a. Derived.amethod() -1 Derived.amethod() b. Derived.amethod() 99 c.Derived.amethod() 99 d. Derived.amethod() e.Compile time error 21. What will be the output on compiling/running the following code? public class MyThread implements Runnable { String myString = "Yes "; public void run() { this.myString = "No "; } public static void main(String[] args) { MyThread t = new MyThread(); new Thread(t).start(); for (int i=0; i < 10; i++) System.out.print(t.myString); } } Choices: a. Compilation Error b. Prints : Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes and so on. c. Prints : No No No No No No No No No No and so on. d. Prints : Yes No Yes No Yes No Yes No Yes No and so on. e. The Output cannot be determined. 22. Multiple objects of MyClass (given below) are used in a program that uses multiple Threadsto create new integer count. What will happen when other threads use the following code? class MyClass { static private int myCount = 0; int yourNumber; private static synchronized int nextCount() { return ++myCount; } public void getYourNumber() { yourNumber = nextCount(); } } Choices: a. The code will give compilation error. b. The code will give runtime error. c. Each thread will get a unique number. d. The uniqueness of the number among different Threads can't be guaranteed. 23. Which of the following lines will print false? 1. public class MyClass 2. { 3. static String s1 = "I am unique!"; 4. public static void main(String args[]) 5. { 6. String s2 = "I am unique!"; 7. String s3 = new String(s1); 8. System.out.println(s1 == s2); 9. System.out.println(s1.equals(s2)); 10. System.out.println(s3 == s1); 11. System.out.println(s3.equals(s1)); 12. System.out.println(TestClass.s4 == s1); 13. } 14. } 15. 16. class TestClass 17. { 18. static String s4 = "I am unique!"; 19. } Choices: a. Lines 10 and 12 b. Line 12 only c. Line 8 and 10 d. None of these 24. What is displayed when the following code is compiled and executed? String s1 = new String("Test"); String s2 = new String("Test"); if (s1==s2) System.out.println("Same"); if (s1.equals(s2)) System.out.println("Equals"); Choices: a. Same Equals b. Equals c. Same d. The code compiles, but nothing is displayed upon execution. e. The code fails to compile. 25. What is displayed when the following is executed? class Parent { private void method1() { System.out.println("Parent's method1()"); } public void method2() { System.out.println("Parent's method2()"); method1(); } } class Child extends Parent { public void method1() { System.out.println("Child's method1()"); } public static void main(String args[]) { Parent p = new Child(); p.method2(); } } Choices: a. Compile time error b. Run time error c. prints : Parent's method2() Parent's method1() d. prints : Parent's method2() Child's method1() 26. How can you replace the comment at the end of main() with code that will write the integers 0 through 9? import java.io.*; class Write { public static void main(String[] args) throws Exception { File file = new File("temp.test"); FileOutputStream stream = new FileOutputStream(file); // write integers here. . . } } Choices: a. DataOutputStream filter = new DataOutputStream(stream); for (int i = 0; i < 10; i++) { filter.writeInt(i); } b. for (int i = 0; i < 10; i++) { file.writeInt(i); } c. for (int i = 0; i < 10; i++) { stream.writeInt(i); } d. DataOutputStream filter = new DataOutputStream(stream); for (int i = 0; i < 10; i++) { filter.write(i); } e. for (int i = 0; i < 10; i++) { stream.write(i); } 27. What results from trying to compile and run the following code? 1. import java.io.*; 2. 3. class MyClass 4. { 5. public static void main(String args[]) 6. { 7. try 8. { 9. FileOutputStream fos = new FileOutputStream("abc"); 10. DataOutputStream dos = new DataOutputStream(fos); 11. dos.writeByte(12); 12. fos.write(100); 13. fos.close(); 14. dos.close(); 15. 16. FileInputStream fis = new FileInputStream("abc"); 17. DataInputStream dis = new DataInputStream(fis); 18. byte b = dis.readByte(); 19. System.out.print(b + " "); 20. int i = dis.readInt(); 21. System.out.println(i); 22. fis.close(); 23. dis.close(); 24. } 25. catch(IOException e) 26. { 27. System.out.println("An exception occurred"); 28. } 29. } 30. } Choices: a. The output is 12 100 b. Compilation error at line 12 because once you chain a DataOutputStream onto the FileOutputStream, you can't write directly to the FileOutputStream. c. An exception occurs at Run time at line 20 because there are only two bytes written in the file "abc" and the code tries to read a byte and then an integer. d. Compilation error occurs at line 20 because there are only two bytes written in the file "abc" and the code tries to read a byte and then an integer. 28. What will happen when you attempt to compile and run the following code? public class MyThread extends Thread { String myName; MyThread(String name) { myName = name; } public void run() { for(int i=0; i<100;i++) { System.out.println(myName); } } public static void main(String args[]) { try { MyThread mt1 = new MyThread("mt1"); MyThread mt2 = new MyThread("mt2"); mt1.start(); // XXX mt2.start(); } catch(InterruptedException ex) { } } } Choices: a.The above code in its current condition will not compile. b. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join(); can be placed at //XXX position. c. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.sleep(100); can be placed at //XXX position. d. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.run(); can be placed at //XXX position. e. In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), there is no need to write any code. 29. What will be printed to the standard output when the following program is run and the Test button is pressed 3 times. import java.awt.*; import java.awt.event.*; public class ActionTest extends Frame { public ActionTest() { Button test=new Button("Test"); add(test); test.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Clicked"); Button b=(Button)e.getSource(); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println("Clicked again"); } }); } }); setSize(100,100); setVisible(true); setTitle("My Frame"); } public static void main(String rgs[]) { new ActionTest(); } } Choices: a. Clicked Clicked again Clicked Clicked again Clicked Clicked again b. Clicked Clicked Clicked c. Clicked Clicked Clicked again Clicked Clicked again Clicked again d. Clicked Clicked again 30. What will be output if you try to compile and run the following code, but there is no file called Hello.txt in the current directory? import java.io.*; public class Test { public static void main(String argv[]) { Test t = new Test(); System.out.println(t.myMethod()); } public int myMethod() { try { FileInputStream dis = new FileInputStream("Hello.txt"); } catch (FileNotFoundException fne) { System.out.println("No such file found"); return -1; } catch(IOException ioe) { } finally { System.out.println("Doing finally"); } return 0; } } Choices: a. No such file found b. No such file found , -1 c. No such file found, Doing finally, -1 d. 0

Answers:

1) D is the correct choice. The above code will not give any compilation error. Note that "Static" is a valid class name. Thus choice A is incorrect. In the above code, on execution, first the static variables (x and y) will be initialized to 0. Then static block will be called and finally main() method will be called. The execution of static block will have no effect on the output as it declares a new variable (int x). The first statement inside main (x--) will result in x to be -1. After that myMethod() will be executed. The statement "y = x++ + ++x;" will be evaluated to y = -1 + 1 and x will become 1. In case the statement be "y =++x + ++x", it would be evaluated to y = 0 + 1 and x would become 1. Finally when System.out is executed "x + y + ++x" will be evaluated to "1 + 0 + 2" which result in 3 as the output. Thus choice D is correct.

2) A is correct. 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 referenced by that reference) and it gets passed as value (i.e a copy of the reference 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 of the method which is called. In the example above when the reference v is passed from method first() to second() the value of v is passed. When we assign the value val to v it is valid only inside the method second() and thus inside the method second() what gets printed is 15 (initial value of i in the object referenced by val), then a blank space and then 0 (value of local variable i). After this when we return to the method first() v actually refers to the same object to which it was referring before the method second() was called, but one thing should be noted here that the value of i in that object (referred by v inside the method first()) was changed to 20 in the method second() and this change does reflect even outside the method second(), hence 20 gets printed in the method first(). Thus overall output of the code in consideration is 15 0 20

3) A is the correct choice. In the code, MyChild class overrides the addMe(int x, int y) method of the MyParent class. And in both the MyChild and MyParent class, addMe() method is overloaded. There is no compilation error anywhere in the above code. On execution, first, the object of MyChild class will be constructed. Please note that there is a super() call from the constructor of MyChild class, which will call the constructor of MyParent class. This will cause the value of z variable of MyChild class to be 30 and x, y variables of MyParent class will become 10 and 20 respectively. The next statement will again call the constructor of MyParent class with same x and y values. This is followed by execution of addMe() method of MyChild class with x as 10, y as 20 and z as 30. Also x and y are inherited by MyChild class from the MyParent class. Thus in the addMe() method of the MyChild class, the value of this.x will be 10, this.y will be 20 and this.z will be 30. The return value of this method will be "10 + 10 + 20 + 20 + 30 + 30", which is equal to 120. Thus x will become 120. This is followed by the invocation of the other addMe() method which takes object reference of the MyChild class. From this method, the method which was called earlier is invoked. This call is exactly the same as the earlier one. Thus the value of y will also be 120 like x. Now the addMe() method of MyParent class is invoked. This method invokes another addMe() method of the same class. Its equivalent to the invocation of addMe(int x, int y) method with x as 10 and y as 20. Also the value of instance variables x and y of My Parent class is 10 and 20 respectively. The value of z will be evaluated to "10 + 10 + 20 + 20", which is equal to 60. Thus the value of x, y and z after all the invocations will be 120, 120 and 60 respectively. As a result of this finally, "120 + 120 + 60" which is equal to 300 will be printed. Thus A is the correct choice.

4) D is correct. This is a very good question to test the concepts of execution flow in case of if conditions. The rule for attaching else statements with if conditions is the same as attaching close brackets with open brackets. A close bracket attaches with the closest open bracket, which is not already closed. Similarly an else statement attaches with the closest if statement, which doesn't have an else statement already, attached to it. So the else statement at line 7 attaches to the if statement at line 6. The else statement at line 8 attaches to the if statement at line 5. The else statement at line 9 attaches to the if statement at line 8. Now let's look at the execution. At line 4 since a is equal to true the execution falls to line 5. At line 5 since b is not true the execution goes to the corresponding else statement at line 8. Now it evaluates the condition inside the if statement. Please note here that an assignment statement also has a value equal to the value being assigned, hence (b = c) evaluates to true and subsequently a && (b = c) evaluates to true and "It's too confusing to tell what is true and what is false" will be printed. Hence the correct answer is choice D.

5) E is the correct choice. The "instanceof" operator tests the class of an object at runtime. It returns true if the class of the left-hand argument is the same as, or is some subclass of, the class specified by the right-hand operand. The right-hand operand may equally well be an interface. In such a case, the test determines if the object at left-hand argument implements the specified interface. In the above case there will not be any compiletime or runtime error. The result of "t instance of MyInterface" will be true as "t" is the object of MyInstanceTest class which implements the MyInstance interface. But the result of "s instanceof String" will be false as "s" refers to null. Thus the output of the above program will be : "I am true interface" followed by " I am false String". Thus choice E is correct and others are incorrect.

6) D is correct. The code compiles successfully. In this code the optional value for the ternary operator, 9.0(a double) and 9(an int) are of different types. The result of a ternary operator must be determined at the compile time, and here the type chosen using the rules of promotion for binary operands, is double. Since the result is a double, the output value is printed in a floating point format. The choice of which value to be printed is made on the basis of the result of the comparison "a < 5" which results in false, hence the variable "a" takes the second of the two possible values, which is 9, but because the result type is promoted to double, the output value is actually written as 9.0, rather than the more obvious 9, hence D is correct.

7) B. The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to creat a StringBuffer object using "new". B is a valid construction of a Boolean (any string other than "true" or "false" to the Boolean constructor will result in a Boolean with a value of "false"). C will fail to compile because the valid range for a byte is -128 to +127 (ie, 8 bits,signed). D is correct, 0x1234 is the hexadecimal representation in java. E fails to compile because the compiler interprets 1.2 as a double being assigned to a float (down-casting), which is not valid. You either need an explicit cast (as in "(float)1.2") or "1.2f", to indicate a float.

8) A, B, C and E are correct. Since Inner is not a static inner class, it has a reference to an enclosing object, and all the variables of that object are accessible. Therefore A and B are correct, even if b is private. Variables in the enclosing method are only accessible when they are marked as final hence c is accessible but not d. E is obviously correct as it is a parameter to the method containing line 12 itself.

9) E is correct. The code will not compile. There is no problem with the declaration of another variable i as both the variables are in disjoint blocks (first one is inside the for loop and its scope ends with the for loop, whereas the second is outside the for loop) and, therefore, different scopes and hence valid. The problem is with the variable j. The two declarations of the variable j are perfectly valid as they are in disjoint blocks and, therefore, different scopes. The error is that both the declarations of j are not available outside the case or default statement, whereas we are trying to assign it to the variable i. Therefore the compiler objects and reports variable j not found.

10) A is the correct choice. In the above code there is not any compilation error. Thus choice E is incorrect. Inside main() method, objects of MyThread and MyRunnable class are created followed by creation of Thread with object of MyRunnable class. Note that MyThread class extends Thread class and overrides the start() method of the Thread class. Thus on execution of "myThread.start()" statement, the start() method of the MyThread class will be executed and as a result "MyThread:start()" will be printed. Had the start() method not there in MyThread class, the start() method of the Thread class would be called which in turn would call the run() method of the MyThread class. On execution of "thread.start();", the start() method of the Thread class would be called which in turn will call the run() method of the class which is passed to Thread constructor (i.e. MyRunnable class). Thus "MyRunnable:run()" will be printed out. Thus choice A is correct.

11) A is correct. 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. Secondly if the method in superclass is left as it is(i.e. friendly access) the method in subclass can be friendly, protected or public.

12) D is correct. When no access modifier is specified for a member, it is only accessible by another class in the package where its class is defined. Even if its class is visible in another package, the member is not accessible there. In the question above the variable superclassVarX has no access modifier specified and hence it cannot be accessed in the packageY even though the class SuperclassX is visible and the protected method superclassMethodX() can be accessed. Thus the compiler will raise an error at line 11.

13) C and D are correct. A and B are obviously wrong because there is nothing wrong in an interface referring to an object. C is correct because an explicit cast is needed to go down the hierarchy. D is correct because no explicit cast is needed at line 4, because we are going up the hierarchy. E is incorrect because if we put an explicit cast at line 6, the code will compile and run perfectly fine, no exception will be thrown because the runtime class of du (that is Truck) can be converted to type FourWheeler without any problem.

14) D is correct. A is incorrect as void methods can be overridden without any problem. B is incorrect as char ch declaration is valid. C is incorrect as char type in java is internally stored as integer and there is a method which takes int as an input. D is correct, on line 9 char ch is widened to an int and passed to int version of the myMethod(). E is incorrect as int version of myMethod() is called.

15) A is correct. Exception :java.lang.IllegalAccessExcption must be caught or placed in the throws clause of the throwMethod(), i.e. the declaration of throwMethod() be changed to "static void throwMethod() throws IllegalAccessExcption". Thus compilation error will occur.

16) D is correct. A difficult but a fundamental question, please observe carefully. Before any object is constructed the object of the parent class is constructed(as there is a default call to the parent's constructor from the constructor of the child class via the super() statement). Also note that when an object is constructed the variables are initialized first and then the constructor is executed. So when new Z() is executed , the object of class X will be constructed, which means Y b = new Y() will be executed and "Y" will be printed as a result. After that constructor of X will be called which implies "X" will be printed. Now the object of Z will be constructed and thus Y y = new Y() will be executed and Y will be printed and finally the constructor Z() will be called and thus "Z" will be printed. Thus YXYZ will be printed.

17) C is correct. The code on compilation will give compile time error because the scope of variable i is only within "for" loop.

18) D is correct. The output will be "Not equal! 10". Please 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".

19) D is correct. Though we cannot use local variables without initializing them (compilation error), there is an exception to it. In case of arrays initialization is supposed to be complete when we specify the leftmost dimension of the array. The problem occurs at runtime if we try to access an element of the array which has not been initialized (specification of size). In the question above the array t2 is initialized before use, therefore there will be no problem at runtime also and the lines 11 and 12 will both print null.

20) B is correct. The reason is that this code creates an instance of the Derived class but assigns it to a reference of a the Base class. In this situation a reference to any of the fields such as i will refer to the value in the Base class, but a call to a method will refer to the method in the class type rather than its reference handle. But note that if the amethod() was not present in the base class then compilation error would be reported as at compile time, when compiler sees the statement like b.amethod(), it checks if the method is present in the base class or not. Only at the run time it decides to call the method from the derived class.

21) E is correct. Please note that there will not be any compilation error when the above code is compiled. Also note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operation system and other running threads, the thread on which start is called will get executed. In the above case it is not guaranteed that the thread will be executed(i.e. run() method will be called), always before "for" loop is executed. Thus the output cannot be determined.

22) C is correct. The use of synchronized ensures that the number generated will not be duplicated, no matter how many Threads are trying to create the number. Thus D is incorrect. A and B are incorrect as the above code will not give any compiletime or runtime error.

23) D is correct. Only line 10 will print false. Strings are immutable objects. That is, a string is read only once the string has been created and initialized, and Java optimizes handling of string literals; only one anonymous string object is shared by all string literals with the same contents. Hence in the above code the strings s1, s2 and s4 refer to the same anonymous string object, initialized with the character string: "I am unique!". Thus s1 == s2 and TestClass.s4 will both return true and obviously s1.equals(s2) will return true. But creating string objects using the constructor String(String s) creates a new string, hence s3 == s1 will return false even though s3.equals(s1) will return true because s1 and s3 are referring to two different string objects whose contents are same.

24) B is correct. Here s1 and s2 are two different object references, referring to different objects in memory. Please note that operator == checks for the memory address of two object references being compared and not their value. The "equals()" method of String class compares the values of two Strings. Thus s1==s2 will return "false" while s1.equals(s2) will return "true". Thus only "Equals" will be printed.

25) C is correct. The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called. Thus C is correct answer.

26) A is correct. In order to write a primitive data type such as an int, you need to use a FilterInputStream subclass such as DataInputStream. This class defines writeInt(), which is perfect for our needs.

27) C is correct. There is nothing wrong in writing to a FileOutputStream even after chaining a DataOutputStream onto it. C is correct as FileOutputStream writes 100 as a byte whereas the code tries to read it as integer. Of course A and D are incorrect (For the reasons just explained).

28) A and B are correct. In its current condition, the above code will not compile as "InterruptedException" is never thrown in the try block. The compiler will give following exception: "Exception java.lang.InterruptedException is never thrown in the body of the corresponding try statement." Note that calling start() method on a Thread doesn't start the Thread. It only makes a Thread ready to be called. Depending on the operating system and other running threads, the thread on which start is called will get executed. After making the above code to compile (by changing the InterruptedException to some other type like Exception), the output can't be predicted (the order in which mt1 and mt2 will be printed can't be guaranteed). In order to make the MyThread class prints "mt1" (100 times) followed by "mt2" (100 times), mt1.join() can be placed at //XXX position. The join() method waits for the Thread on which it is called to die. Thus on calling join() on mt1, it is assured that mt2 will not be executed before mt1 is completed. Also note that the join() method throws InterruptedException, which will cause the above program to compile successfully. Thus choice A and B are correct.

29) C is the correct choice. When the button is clicked for the first time, the actionPerformed method is invoked and it prints "Clicked". In actionPerformed, one more listener is registered with the button. So when the button is clicked for the second time, the actionPerformed of both the listeners is invoked. So it prints "Clicked" and then "Clicked again". Now one more listener is registered with the button. So for the third time the button is clicked, 3 actionPerformed methods are invoked - result is the printing of "Clicked" by first listener,"Clicked again" by the second and "Clicked again" by the third one. Thus C is the correct choice.

30) C is correct. Firstly after construction of Test class, System.out.println(myMethod()) will be invoked. This will invoke myMe thod(). Since Hello.txt is not present in the current directory, FileNotFoundException will be thrown in the try block. It will be caught in the catch block, where first "No such file found" will be printed. Then return -1 will be executed. But please note that before control passes back to main method, finally block will be executed and thus "Doing finally" will be printed. Finally "-1" will be printed.

These questions and answers are provided by Whizlabs Software. To order the full version of this exam simulation click here.


More Pop Quiz:


There are 201 CertCities.com user Comments for “Sun Certified Java Programmer (SCJP) Exam”
Page 1 of 21
1/15/02: James Colin says: I am a regular visitor to your great site. I have been visiting your forums and quizzes. I must say that the kind of questions and explanations in this Quiz are "THE BEST" among all the quizzes provided by you so far. Thanks to you and whiz software for the great work. Keep the great work going. Look forward to seeing more of such quizzes. Regards JC
1/16/02: Sandeep from India says: GREATTTTTT Content!!!
1/27/02: tridip bordoloi from new delhi says: Thats amaiging I love this contest
1/27/02: Suresh Gandhi from India says: Really a good work done by Wizlabs. Though I have already passed the exam, I struggled with 2 questions. Very useful collection for those going to write exam.
2/5/02: Paul says: Agree with all. Quality is really HIGH. Thanks paul
2/6/02: John Da Silva from London says: Very good test, just, enough of those pre-post increment-decrement questions.
2/6/02: John Da Silva from London says: Oh, buy the way, the answer to Q20 is B, yes, but it should be: Derived.amethod() 99 Derived.amethod() not just: Derived.amethod() 99 ;-)
2/11/02: Vasiliy Ruzanov from Russia, Togliatty says: Really good (hm,great;)) test. I like it...
2/25/02: Joe Shi from China says: Valuble and helpful before I attend the SCJP Exam. Thanks.
6/24/02: Abhishek Srivastava from India says: helpful for SCJP Exam, it helps me a lot thanx.
First Page   Next Page   Last Page
Your comment about: “Sun Certified Java Programmer (SCJP) Exam”
Name: (optional)
Location: (optional)
E-mail Address: (optional)
Comment:
   

-- advertisement (story continued below) --

top