• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

mughal mock question

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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~
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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~


 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ");
}
}

 
reply
    Bookmark Topic Watch Topic
  • New Topic