Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Thursday, August 27, 2009

Java Quiz -11

Hi friends,

Today I would like to post few more questions to test your capability.
Dont worry guys, I will give you the answer too! Try to answer it yourself without referring to the answers to test yourself.

Q: 1 Given:
1. public class ClassA {
2. public static void main(String[] args) {
3. String myProp = /* insert code here */
4. System.out.println(myProp);
5. }
6. }
and the command line:
java -Dprop.custom=apple ClassA
Which two, placed on line 3, will produce the output apple? (Choose two.)
A. System.getProperty("prop.custom");
B. System.property("prop.custom");
C. System.getProperties().getProperty("prop.custom");
D. System.load("prop.custom");
E. System.getenv("prop.custom");

Answer: A, C

Q: 2 Given:
1. class ClassA {
2. ClassB classBobj;
3. public ClassA() { booch = new ClassB(this); }
4. }
5.
6. class ClassB {
7. ClassA classAobj;
8. public ClassB( ClassA s) { classAobj = s; }
9. }
And the statements:
10. public static void main(String[] args) {
11. ClassA classAmainObj = new ClassA();
12. classAmainObj = null;
13. // more code here
14. }
Which statement is true about the objects referenced by classAmainObj, classAobj, and classBobj immediately after line
12 executes?
 
A. Only the object referenced by classAobj is eligible for garbage collection.
B. None of these objects are eligible for garbage collection.
C. The objects referenced by classAobj and classBobj are eligible for garbage collection.
D. Only the object referenced by classBobj is eligible for garbage collection.
E. Only the object referenced by classAmainObj is eligible for garbage collection.

Answer: E

Q: 3 Given:
1. public class ClassA {
2.
3. static public void main(String [] args) {
4. for(int x = 1; x < args.length; x++) {
5. System.out.print(args[x] + " ");
6. }
7. }
8. }
and the command line invocation:
java ClassA a b c
What is the result?

A. a b
B. b c
C. a b c
D. Compilation fails.
E. An exception is thrown at runtime.

Answer: B

Q: 4 A developer is creating a class ClassA, that needs to access class ClassB. The
ClassB class is deployed in a JAR named quiz.jar. Which three, taken independently, will allow the developer to use the ClassB class while compiling the ClassA class? (Choose three.)

A. The JAR file is located at $JAVA_HOME/jre/classes/quiz.jar.
B. The JAR file is located at $JAVA_HOME/jre/lib/ext/quiz.jar.
C. The JAR file is located at /foo/quiz.jar and a classpath environment variable is set that includes
/foo/quiz.jar/ClassB.class.
D. The JAR file is located at /foo/quiz.jar and a classpath environment variable is set that includes
/foo/quiz.jar.
E. The JAR file is located at /foo/quiz.jar and the ClassA class is compiled using javac -cp /foo/quiz.jar/ClassB
ClassA.java.
F. The JAR file is located at /foo/quiz.jar and the ClassA class is compiled using javac -d /foo/quiz.jar
ClassA.java
G. The JAR file is located at /foo/quiz.jar and the ClassA class is compiled using javac -classpath
/foo/quiz.jar ClassA.java

Answer: B, D, G

Q: 5 Given:
enum Example { Questions,Answers,Test }
Which statement is true?
A. The expressions (Questions == Questions) and Questions.equals(Questions) are both guaranteed to be true.
B. The expression (Questions < Answers) is guaranteed to be true and Questions.compareTo(Answers) is guaranteed to be less
than Questions.
C. The Example values cannot be used in a raw java.util.HashMap; instead, the programmer must use a
java.util.EnumMap.
D. The Example values can be used in a java.util.SortedSet, but the set will NOT be sorted because enumerated types do NOT implement java.lang.Comparable.

Answer: A


Q: 6 Given:
1. import java.util.*;
2. public class ClassA {
3. public static void main(String[] args) {
4. PriorityQueue data = new PriorityQueue();
5. data.add("Test");
6. data.add("Questions");
7. data.add("Answers");
8. System.out.println(data.poll() + ":" + data.peek());
9. }
10. }
What is the result?
A. Questions:Questions
B. Test:Questions
C. Questions:Answers
D. Answers:Questions
E. Test:Test
F. Test:Answers


Answer: C

Q: 7 Given:

1. Object [] data = {
2. new Integer(12),
3. new String("foo"),
4. new Integer(5),
5. new Boolean(true)
6. };
7. Arrays.sort(data);
8. for(int i=0; i9. System.out.print(data[i].toString());
10. System.out.print(" ");
11. }
What is the result?
A. Compilation fails due to an error in line 1.
B. Compilation fails due to an error in line 7.
C. A ClassCastException occurs in line 7.
D. A ClassCastException occurs in line 5.
E. The value of all four objects will be printed.


Answer: C

Q: 8 Given:
1. import java.util.*;
2. public class ClassA {
3. private String s;
4. public ClassA(String s) { this.s = s; }
5. public static void main(String[] args) {
6. HashSet test = new HashSet();
7. ClassA classAobj1 = new ClassA("aardvark");
8. ClassA classAobj2 = new ClassA("aardvark");
9. String s1 = new String("aardvark");
10. String s2 = new String("aardvark");
11. test.add(classAobj1); test.add(classAobj2); test.add(s1); test.add(s2);
12. System.out.println(test.size()); } }
What is the result?
A. 0
B. 1
C. 2
D. 3
E. 4
F. Compilation fails.
G. An exception is thrown at runtime.

Answer: D  

Q: 9 Given:
1. public class ClassA {
2. private long data1;
3. private long data2;
4.
5. // class ClassA methods
6. }
A programmer is developing a class ClassA, that will be used as a ClassA in a standard java.util.HashMap.
Which two methods should be overridden to assure that ClassA works correctly as a ClassA? (Choose two.)

A. public int hashCode()
B. public boolean equals(ClassA k)
C. public int compareTo(Object o)
D. public boolean equals(Object o)
E. public boolean compareTo(ClassA k)


Answer: A, D

Q: 10 Given a pre-generics implementation of a method:
1. public static int data(List list) {
2. int data = 0;
3. for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
4. int i = ((Integer)iter.next()).intValue();
5. data += i;
6. }
7. return data;
8. }
Which three changes must be made to the method data to use generics? (Choose three.)

 
A. remove line 4
B. replace line 4 with "int i = iter.next();"
C. replace line 3 with "for (int i : intList) {"
D. replace line 3 with "for (Iterator iter : intList) {"
E. replace the method declaration with "data(List intList)"
F. replace the method declaration with "data(List intList)"

 Answer: A, C, F

Hope you found this useful! Kindly give me your comments regarding the same.Many more Quizzes are yet to be posted! Kindly make use of them as much as possible!
 

No comments: