Showing posts with label OOPs. Show all posts
Showing posts with label OOPs. Show all posts

OOPs Ques: 41

Ques: 41 
Given:
10. class One {
11. void foo() { }
12. }
13. class Two extends One {
14. //insert method here
15. }

Which three methods, inserted individually at line 14, will correctly complete class Two? (Choose three.)
A. int foo() { /* more code here */ }
B. void foo() { /* more code here */ }
C. public void foo() { /* more code here */ }
D. private void foo() { /* more code here */ }
E. protected void foo() { /* more code here */ }


Answer: B, C, E

OOPs Ques: 40

Ques: 40 Given:

11. public class ItemTest {
12. private final int id;
13. public ItemTest(int id) { this.id = id; }
14. public void updateId(int newId) { id = newId; }
15.
16. public static void main(String[] args) {
17. ItemTest fa = new ItemTest(42);
18. fa.updateId(69);
19. System.out.println(fa.id);
20. }
21. }


What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The attribute id in the Item object remains unchanged.
D. The attribute id in the Item object is modified to the new value.
E. A new Item object is created with the preferred value in the id attribute.


Answer: A

OOPs Ques: 39

Ques: 39 Given:

1. interface TestA { String toString(); }
2. public class Test {
3. public static void main(String[] args) {
4. System.out.println(new TestA() {
5. public String toString() { return "test"; }
6. });
7. }
8. }


What is the result?

A. test
B. null
C. An exception is thrown at runtime.
D. Compilation fails because of an error in line 1.
E. Compilation fails because of an error in line 4.
F. Compilation fails because of an error in line 5.
Answer: A

OOPs

Ques: 37 
Given:
1. class TestA {
2.    public void start() { System.out.println("TestA"); }
3. }
 
4. public class TestB extends TestA {
5.    public void start() { System.out.println("TestB"); }
6.   public static void main(String[] args) {
7.     ((TestA)new TestB()).start();
8.   }
9. }

What is the result?
A. TestA
B. TestB
C. Compilation fails.
D. An exception is thrown at runtime.
Answer: B

OOPs 1

Ques 17: Given: 
 class One {
    public One() { System.out.print(1); }
 } 
class Two extends One {
    public Two() { System.out.print(2); }
 }
 class Three extends Two {
    public Three() { System.out.print(3); }
 }
 public class Numbers{
    public static void main( String[] argv ) { new Three(); }
 }

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