Given:
class Inner{
private int x;
public void setX(int x){this.x=x;}
public int getX(){return x;}
}
class Outer{
private Inner y;
public void setY(Inner y){this.y=y;}
public Inner getY(){return y;}
}
public class Threado {
public static void main(String[] args) {
Outer o = new Outer();
Inner i = new Inner();
int n = 10;
i.setX(n);
o.setY(i);
//insert code here
System.out.println(o.getY().getX());
}
}
Which three code fragments, added individually at line //insert code here, produce the output 100?
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 );
Execution:
If put option A in place of //insert code here, we will only get 10 as output
If put option B in place of //insert code here, then we will get 100 as output, as "i" object is set with 100 value and o.getY().getX() is accessing that value.
If put option C in place of //insert code here, then we will get 100 as output, as o.getY() is returning object of Inner class and setX is setting value into same object.
If put option E in place of //insert code here, then we will get 10 as output, as we are declaring a new object of Inner and setting 100 value in it, but printing object which contain value of previous Inner object.
If put option F in place of //insert code here, then we will get 100 as output as it is updating "o" object(Outer class) and printing value for it
Answer:
B. i.setX( 100 );
F. i = new Inner(); i.setX( 100 ); o.setY( i );
C. o.getY().setX( 100 )
class Inner{
private int x;
public void setX(int x){this.x=x;}
public int getX(){return x;}
}
class Outer{
private Inner y;
public void setY(Inner y){this.y=y;}
public Inner getY(){return y;}
}
public class Threado {
public static void main(String[] args) {
Outer o = new Outer();
Inner i = new Inner();
int n = 10;
i.setX(n);
o.setY(i);
//insert code here
System.out.println(o.getY().getX());
}
}
Which three code fragments, added individually at line //insert code here, produce the output 100?
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 );
Execution:
If put option A in place of //insert code here, we will only get 10 as output
If put option B in place of //insert code here, then we will get 100 as output, as "i" object is set with 100 value and o.getY().getX() is accessing that value.
If put option C in place of //insert code here, then we will get 100 as output, as o.getY() is returning object of Inner class and setX is setting value into same object.
If put option E in place of //insert code here, then we will get 10 as output, as we are declaring a new object of Inner and setting 100 value in it, but printing object which contain value of previous Inner object.
If put option F in place of //insert code here, then we will get 100 as output as it is updating "o" object(Outer class) and printing value for it
Answer:
B. i.setX( 100 );
F. i = new Inner(); i.setX( 100 ); o.setY( i );
C. o.getY().setX( 100 )
No comments:
Post a Comment