Saturday, 7 January 2012

Question 77 : Variable Scope

Click the Exhibit button. Which three code fragments, added individually at line 29, produce the output 100?
10. class Inner {
11. private int x;
12. public void setX(int X) { this.x=x; }
13. public int getX() {return x;}
14. }
15.
16. class Outer {
17. private Inner Y;
18. public void setY( Inner y ) { this.y = y; }
19. public Inner getY() { return Y; }
20. }
21. 
22. public class Gamma {
23. public static void main( String[] args ) {
24. Outer o = new Outer();
25. Inner i = new Inner();
26. int n = 10;
27. i,setX( n );
28. o.setY( i );
29. // insert code here
30. System.out.println( o.getY().getX() );
31. }
32. }
Choose any 3.

A. n = 100;
B. i.setX( 100 );
C. o.getY().setX( 100 );

D. i = new Inner(); i.setX( 100 );
E. o.setY( i ); i = new Inner(); i.setX( 100 );
F. i = new Inner(); i.setX( 100 ); o.setY( i );

2 comments:

  1. how is it possible to call any method by any other method i.e, o.getY().getX()

    ReplyDelete
    Replies
    1. o.getY() method will return the Inner class Object.And then we are getting the integer value of the Inner class with the Inner class Object.

      o.getY().getX(); ==> y.getX(); //y is the Inner Class Object.

      Delete