Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Monday, September 14, 2009

Java Quiz -16

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 a method that must ensure that its parameter is not null:
1. public void test(Object data) {
2. // check for null data
...
10. System.out.println(data.getClass());
11. }
What, inserted at line 2, is the appropriate way to handle a null data?

A. assert data == null;
B. assert data != null, "data is null";
C. if (data == null) {
throw new AssertionException("data is null");
}
D. if (data == null) {
throw new IllegalArgumentException("data is null");
}

Answer: D

Q: 2 Given:
1. package javaQuiz;
2. public class ClassA {
3. public InnerClassB classb = new InnerClassB();
4. class InnerClassB {
5. public int data1;
6. public int data2;
7. }
8. }
Which statement is true about the class of an object that can reference the variable data1?

A. classb can be any class.
B. No class has access to data1.
C. The class must belong to the javaQuiz package.
D. The class must be a subclass of the class ClassA.

Answer: C

Q: 3 Which four statements are true? (Choose four.)

A. Has-a relationships can be implemented using instance variables.
B. Is-a relationships can be implemented using the extends keyword.
C. Has-a relationships should never be encapsulated.
D. An array or a collection can be used to implement a one-to-many has-a relationship.
E. The relationship between Movie and Actress is an example of an is-a relationship.
F. Is-a relationships can be implemented using the implements keyword.
G. Has-a relationships should be implemented using inheritance.

Answer: A, B, D, F

Q: 4 Given:
0. public class ClassA {
1.
2. private String data1;
3. private Integer data2;
4. public String data3;
5.
6. public void test(String data1,
7. String data3,
8. Integer data2) {
9. this.data1 = data1;
10. this.data3 = data3;
11. this.data2 = data2;
12. }
13. }
Which statement is true?

A. The code demonstrates polymorphism.
B. The class is fully encapsulated.
C. The data3 variable breaks encapsulation.
D. The data1 and data2 variables break polymorphism.
E. The test method breaks encapsulation.

Answer: C

Q: 5 Given:
0. abstract class ClassA {
1. abstract void test1();
2. void test2() { }
3. }
4. class ClassB extends ClassA {
5. void test1() { }
6. void test2() { }
7. }
8. class ClassC extends ClassB { void test3() { } }
and:
ClassA classA = new ClassB();
ClassC classB = new ClassC();
ClassA classC = new ClassC();
What are four valid examples of polymorphic method calls? (Choose four.)

A. classA.test2();
B. classC.test2();
C. classC.test3();
D. classC.test1();
E. classB.test3();
F. classA.test1();

Answer: A, B, D, F

Q: 6 Given:
1. interface ClassA { public void test(); }
...
10. class ClassB {}
...
20. class ClassC extends ClassB {
21. ClassD classD;
22. }
...
30. class ClassE extends ClassC implements ClassA{
31. public void test() {}
32. }
...
40. class ClassF implements ClassA{
41. public void test() {}
42. }
Which three are true? (Choose three.)

A. ClassF is-a ClassB
B. ClassF is-a ClassA
C. ClassC is-a ClassB
D. ClassC is-a ClassA
E. ClassF has-a ClassB
F. ClassE has-a ClassD
G. ClassE has-a ClassA

Answer: B, C, F

Q: 7 Given:
1. public class ClassA {
2. protected int test(int data) { return 0; }
3. }
4. class ClassB extends ClassA {
5. // insert code here
6. }
Which five methods, inserted independently at line 5, will compile? (Choose five.)

A. public int test(int data1) { return 0; }
B. private int test(int data1) { return 0; }
C. private int test(long data1) { return 0; }
D. protected long test(int data1) { return 0; }
E. protected int test(long data1) { return 0; }
F. protected long test(long data1) { return 0; }
G. protected long test(int data1, int data2) { return 0; }

Answer: A, C, E, F, G

Q: 8 Given:
1. public class ClassA {
2. protected static int test(int data1, int data2) { return data1 * data2;}
3. }
and:
10. public class ClassB extends ClassA{
11. public static int test(int data1, int data2) {
12. int data3 = super.test(data1, data2);
13. return data3;
14. }
15. }
and:
20. ClassB classB = new ClassB ();
21. System.out.println(classB.test(3,4));
22. System.out.println(ClassB.test(2,2));
What is the result?

A. 12
4
B. The code runs with no output.
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 11.
E. Compilation fails because of an error in line 12.
F. Compilation fails because of an error in line 21.

Answer: E

Q: 9 Given:
1. class ClassA {
2. java.util.ArrayList data1;
3. public final void testData(String data2) {
4. data1.add(data2);
5. }
6. }
7. public class ClassB extends ClassA {
8. public void testData(String data2) {
9. System.out.println("Cannot add Data1");
10. }
11. public static void main(String[] args) {
12. ClassA classA = new ClassB();
13. classA.testData("Data2");
14. }
15. }
What is the result?

A. The code runs with no output.
B. A NullPointerException is thrown in Line 4.
C. Compilation fails.
D. Cannot add Data1

Answer: C

Q: 10 Which three statements are true? (Choose three.)

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 only if the subclass is in the same package as ClassA.

Answer: B, E, 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: