Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Tuesday, September 1, 2009

Java Quiz -13

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. void waitTest() {
2. Object obj = new Object();
3. synchronized (Thread.currentThread()) {
4. obj.wait();
5. obj.notify();
6. }
7. }
Which statement is true?

A. This code may throw an InterruptedException.
B. This code may throw an IllegalStateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless "obj.wait()" is replaced with "((Thread) obj).wait()".
E. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally.

F. A call to notify() or notifyAll() from another thread may cause this method to complete normally.

Answer: B

Q: 2 Given:
1. public class TestClassA implements Runnable {
2. public static void main (String[] args) throws Exception {
3. Thread t = new Thread(new TestClassA());
4. t.start();
5. System.out.print("Started");
6. t.join();
7. System.out.print("Complete");
8. }
9. public void run() {
10. for (int test = 0; test < 4; test++) {
11. System.out.print(test);
12. }
13. }
14. }
What can be a result?

A. An exception is thrown at runtime.
B. The code executes and prints "Started0123Complete".
C. The code executes and prints "StartedComplete".
D. Compilation fails.
E. The code executes and prints "StartedComplete0123".


Answer:B

Q: 3 Which ClassB code fragments will execute the method doData() in a separate
thread? (Choose two.)
A. new Thread() {
public void run() { doData(); }
};
B. new Thread() {
public void start() { doData(); }
};
C. new Thread() {
public void start() { doData(); }
}.run();
D. new Thread() {
public void run() { doData(); }
}.start();
E. new Thread(new Runnable() {
public void run() { doData(); }
}).run();
F. new Thread(new Runnable() {
public void run() { doData(); }
}).start();


Answer: D, F

Q: 4 Given:
1. interface A { public int getData(); }
2. class ClassB implements A {
3. public int getData() { return 1; }
4. }
5. class ClassC extends B {
6. // insert code here
7. }
Which ClassC code fragments, inserted individually at line 4, make use of polymorphism? (Choose three.)
A. public void add( ClassC c) { c.getData(); }
B. public void add( ClassB b) { b.getData(); }
C. public void add(A a) { a.getData(); }
D. public void add(A a, ClassB b) { a.getData(); }
E. public void add( ClassC c1, ClassC c2) { c1.getData(); }


Answer: B, C, D

Q: 5 Given:
1. public class ClassA {
2.
3. private String design;
4. private Integer data;
5. public String test;
6.
7. public void setClassdata(String design,
8. String test,
9. Integer data) {
10. this.design = design;
11. this.test = test;
12. this.data = data;
13. }
14. }
Which statement is true?
A. The class is fully encapsulated.
B. The code demonstrates polymorphism.
C. The test variable breaks encapsulation.
D. The design and data variables break polymorphism.
E. The setClassdata method breaks encapsulation.


Answer: C

Q: 6 Given:
1. package test;
2.
3. class ClassA {
4. public String data = "Java Quiz";
5. }
What can directly access and change the value of the variable data?
A. any class
B. only the ClassA class
C. any class in the test package
D. any class that extends ClassA


Answer: C

Q: 7 Given:
1. class ClassA { public String quiz() { return "answers"; } }
2. class ClassB extends ClassA {
3. public String quiz() { return "Test Answers"; }
4. }
5. class ClassC extends ClassA {
6. public String quiz() { return "Test Data"; }
7. }
...
11. ClassA ClassA = new ClassB();
12. ClassC ClassC = (ClassC)ClassA;
13. System.out.println(ClassC.quiz());
What is the result?
A. answers
B. Test Answers
C. Test Data
D. Compilation fails.
E. An exception is thrown at runtime.


Answer: E

Q: 8 Which three statements are true?
A. A final method in class ClassA can be abstract if and only if ClassA is abstract.
B. A protected method in class ClassA can be overridden by any subclass of ClassA.
C. A private static method can be called only within other static methods in class ClassA.
D. A non-static public final method in class ClassA can be overridden in any subclass of ClassA.
E. A public static method in class ClassA can be called by a subclass of ClassA without explicitly referencing the class ClassA.
F. A method with the same signature as a private final method in class ClassA can be implemented in a subclass of ClassA.
G. A protected method in class ClassA can be overridden by a subclass of A only if the subclass is in the same package as ClassA.


Answer: B, E, F

Q: 9 Which two statements are true about has-a and is-a relationships? 
A. Inheritance represents a has-a relationship.
B. Inheritance represents an is-a relationship.
C. Instance variables can be used when creating a has-a relationship.
D. Interfaces must be used when creating a has-a relationship.


Answer: B, C

Q: 10 Given:
11. Runnable r = new Runnable() {
12. public void testData() {
13. System.out.print("Test Quiz Data");
14. }
15. };
16. Thread t = new Thread(r) {
17. public void testData() {
18. System.out.print("Answer it yourself");
19. }
20. };
21. t.start();
What is the result?
A. Test Quiz Data
B. Answer it yourself
C. Compilation fails.
D. The code runs with no output.
E. An exception is thrown at runtime.


Answer: B

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: