• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mughal mock question

 
may leung
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Base {
int i;
Base(){
add(1);
}
void add (int v){
i+=v;
}
void print(){
System.out.println (i);
}
}
class Extension extends Base{
Extension(){
add(2);
}
void add(int v){
i += v*2;
}
}

public class Test {
public static void main (String args []) {
bogo(new Extension());
}
static void bogo(Base b){
b.add(8);
b.print();
}
}
a. 9
b. 18
c. 20
d.21
e.22
the ans is 22 but I am quite confuse in this question, can anyone explain in detail?
Thanks~
 
Sivaram Ghorakavi
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go thro this and try to solve the problem you own!!! Have fun...
http://developer.java.sun.com/developer/TechTips/2000/tt1205.html#tip2
Originally posted by may leung:
class Base {
int i;
Base(){
add(1);
}
void add (int v){
i+=v;
}
void print(){
System.out.println (i);
}
}
class Extension extends Base{
Extension(){
add(2);
}
void add(int v){
i += v*2;
}
}

public class Test {
public static void main (String args []) {
bogo(new Extension());
}
static void bogo(Base b){
b.add(8);
b.print();
}
}
a. 9
b. 18
c. 20
d.21
e.22
the ans is 22 but I am quite confuse in this question, can anyone explain in detail?
Thanks~

 
venu gopal
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May,
Please execute this program for answer.
class Base {
int i;
Base(){
System.out.print("Base constructor will be executed first. In Base() constructor the value of i = ");
add(1);
System.out.println(">>>> Here The add() method of extension will executed because of override i = 0 + 2");
System.out.println("Base() constructor ends ");
}
void add (int v){
i+=v;
print();
}
void print(){
System.out.print (i);
}
}
class Extension extends Base{
Extension(){
System.out.print("Extension constructor will be executed next. In Extension() constructor the value of i = ");
add(2);
System.out.println(" >>>> Here The add() method of extension will execute because of override i = 2 +( 2 * 2) ");
System.out.println("Extension constructor ends");
}
void add(int v){
i += v*2;
print();
}
}
public class Test {
public static void main (String args []) {
bogo(new Extension());
}
static void bogo(Base b){
System.out.print("After b.add(8) The value of i = ");
b.add(8);
//b.print();
System.out.println(" >>>> Here The add() method of extension will execute because of override i = 6 + 2 * 8 ");
}
}

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic