Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Sunday, August 30, 2009

Java Quiz-12

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. // insert code here
2. private N min, max;
3. public N getMin() { return min; }
4. public N getMax() { return max; }
5. public void add(N added) {
6. if (min == null || added.doubleValue() < min.doubleValue()) 
7. min = added;
8. if (max == null || added.doubleValue() > max.doubleValue()) 
9. max = added;
10. }
Which two, inserted at line 1, will allow the code to compile? (Choose two.)

A. public class ClassA {
B. public class ClassA {
C. public class ClassA {
D. public class ClassA {
E. public class ClassA {
F. public class ClassA {

Answer: D, F

Q: 2 Given:
1. import java.util.*;
2.
3. public class ClassA{
4. public static void main(String[] args) {
5. ArrayList strings = new ArrayList();
6. strings.add("aAaA");
7. strings.add("AaA");
8. strings.add("aAa");
9. strings.add("AAaa");
10. Collections.sort(strings);
11. for (String data : strings) { System.out.print(data + " "); }
12. }
13. }
What is the result?

A. Compilation fails.
B. aAaA aAa AAaa AaA
C. AAaa AaA aAa aAaA
D. AaA AAaa aAaA aAa
E. aAa AaA aAaA AAaa
F. An exception is thrown at runtime.

Answer: C

Q: 3 Given:
1. public abstract class ClassA {
2. private int x;
3. private int y;
4. public abstract void quiz();
5. public void test(int x, int y) {
6. this.x = x;
7. this.y = y;
8. }
9. }
Which two classes use the ClassA class correctly? (Choose two.)
A. public class ClassB implements ClassA {
private int data;
}
B. public abstract class ClassB extends ClassA {
private int data;
}
C. public class ClassB extends ClassA {
private int data;
public void quiz();
}
D. public abstract class ClassB implements ClassA {
private int data;
public void quiz();
}
E. public class ClassB extends ClassA {
private int data;
public void quiz() {/* code here */}
F. public abstract class ClassB implements ClassA {
private int data;
public void quiz() { /* code here */ }


Answer: B, E

Q: 4 Given
1. class ClassA {
2. static void questions() { /* more code here */ }
3. void answers() { /* more code here */ }
4. }
Which two statements are true? (Choose two.)

A. ClassA.answers() is a valid invocation of answers().
B. ClassA.questions() is a valid invocation of questions().
C. Method answers() can directly call method questions().
D. Method questions() can directly call method answers().


Answer: B, C

Q: 5 Given:
1. class ClassA {
2. public ClassA() { System.out.print(1); }
3. }
4. class ClassB extends ClassA {
5. public ClassB() { System.out.print(2); }
6. }
7. class ClassC extends ClassB {
8. public ClassC() { System.out.print(3); }
9. }
10. public class ClassNumbers{
11. public static void main( String[] args ) { new ClassC(); }
12. }
What is the result when this code is executed?

A. 1
B. 3
C. 123
D. 321
E. The code runs with no output.


Answer: C

Q: 6 Given:
1. class ClassA{
2. public enum Direction { NORTH, SOUTH, EAST, WEST }
3. }
4. public class ClassB{
5. // insert code here
6. }
Which code, inserted at line 5, allows the ClassB class to compile?

A. Direction data = NORTH;
B. ClassA.Direction data = NORTH;
C. Direction data = Direction.NORTH;
D. ClassA.Direction data = ClassA.Direction.NORTH;


Answer: D

Q: 7 Given:
1. class ClassA {
2. public ClassA classAObj() { return this; }
3. }
4. class ClassB extends ClassA {
5. public ClassA classAObj() { return this; }
6. }
7. class ClassC extends ClassB {
8. // insert method here
9. }
Which ClassB methods, inserted individually, correctly complete the ClassC class? (Choose ClassB.)

A. public void classAObj() {}
B. public int classAObj() { return 3; }
C. public ClassB classAObj() { return this; }
D. public ClassA classAObj() { return this; }
E. public Object classAObj() { return this; }


Answer: C, D

Q: 8 Given:
1. public static void main(String[] args) {
2. Object obj = new int[] { 1, 2, 3 };
3. int[] dataArray = (int[])obj;
4. for (int data : dataArray) System.out.print(data + " ");
5. }
What is the result?

A. 1 2 3
B. Compilation fails because of an error in line 2.
C. Compilation fails because of an error in line 3.
D. Compilation fails because of an error in line 4.
E. A ClassCastException is thrown at runtime.


Answer: A

Q: 9 Given:
1. public class ClassA implements Runnable {
2. public void run() {
3. System.out.print("Answer it yourself");
4. }
5. public static void main(String[] args) {
6. Thread t = new Thread(new ClassA());
7. t.run();
8. t.run();
9. t.start();
10. }
11. }
What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints "Answer it yourself".
D. The code executes and prints "Answer it yourselfAnswer it yourself".
E. The code executes and prints "Answer it yourselfAnswer it yourselfAnswer it yourself".

Answer: E

Q: 10 Given:
1. public class ClassA {
2. public static void main (String[] args) throws Exception {
3. Thread.sleep(2000);
4. System.out.println("Java Quiz");
5. }
6. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "Java Quiz".
D. The code executes normally, but nothing is printed.

Answer: C

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: