This series of questions has been created to help you prepare for the Sun Certified Programmer for the Java 2 Platform exam.
This practice quiz contains 20 questions, provided by Whizlabs Software.
Question 1:
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); }
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".
Question 2:
In the following pieces of code, A and D will compile without any error. True or false?
A: StringBuffer sb1 = "abcd";
B: Boolean b = new Boolean("abcd");
C: byte b = 255;
D: int x = 0x1234;
E: float fl = 1.2;
True
False
Question 4:
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)); } }
A. prints: Value is - 9
B. prints: Value is - 5
C. Compilation error
D. None of these
Question 5:
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. }
a
b
c
d
e
Question 6:
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. }
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
Question 7:
Consider the class hierarchy shown below:
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?
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.
Question 8:
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. }
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.
Question 9:
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); } } }
A. Compilation error
B. Runtime error
C. Compile successfully, nothing is printed
D. Inside throwMethod. followed by caught: java.lang.IllegalAccessExcption: demo
Question 10:
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); } }
A. 0122
B. 0123
C. Compilation error
D. None of these
Question 11:
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(); } }
A. Z
B. YZ
C. XYZ
D. YXYZ
Question 12:
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()"); } }
A. Derived.amethod()
-1
Derived.amethod()
B. Derived.amethod()
99
Derived.amethod()
C. 99
Derived.amethod()
Compile time error
Question 13:
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); } }
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.
Question 14:
Multiple objects of MyClass (given below) are used in a program that uses multiple Threads to 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(); } }
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.
Question 15:
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");
A. Same
Equals
B. Equals
C. Same
D. The code compiles, but nothing is displayed upon execution.
E. The code fails to compile.
Question 16:
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(); } }
A. Compile time error
B. Run time error
C. prints : Parent's method2()
Parent's method1()
D. prints : Parent's method2()
Child's method1()
Question 17:
Which of the following statements are true?
A. For a given component events will be processed in the order the listeners are added.
B. Using the Adapter approach to event handling means there is a need to create blank method bodies for all event methods.
C. A component may have multiple listeners associated with it.
D. Listeners may be removed once added.
Question 18:
The setForeground() and setBackground() methods are defined in the
_____ class.
Question 19:
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. . . } }
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); }
Question 20:
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. }
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.
Congratulations, you have completed this quiz!
Q1. 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".
Q2. The code segments B and D will compile without any error. A is not a valid way to construct a StringBuffer, you need to create 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 (i.e., 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.
Q3. 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 called 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
Q4. 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.
Q5. 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.
Q6. 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. 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.
Q7. C and D are correct. A and B are obviously incorrect 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; 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. No exception will be thrown because the runtime class of du (that is Truck) can be converted to type FourWheeler without any problem.
Q8. 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.
Q9. 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.
Q10. C is correct. The code on compilation will give compile time error because the scope of variable i is only within "for" loop.
Q11. D is correct. 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.
Q12. 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 the 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.
Q13. 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.
Q14. 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 compile-time or runtime error.
Q15. 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.
Q16. 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.
Q17. C and D are correct. A is not correct because there is no guarantee that listeners will be notified in the order in which they were added. B is not correct because when you use adapter then there is no need to supply blank method bodies for methods you don't want to use. The adapter class implements the listener interface with do nothing methods (empty body methods). C is correct because it is possible to have more than one listener attached to a component. D is correct as you can remove the listeners from the component. Similar to add Listener methods, there are remove Listener methods available.
Q18. The setForeground() and setBackground() methods belong to Component class.
Q19. 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.
Q20. 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.
本文地址:http://com.8s8s.com/it/it14671.htm