• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

mughal exam

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can somebody walks me through this?
------------
What will be written to the standard output when the following program is run?
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 Qd073 {
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
Answer: E

Thanks,
yuki
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yuki Cho:
can somebody walks me through this?
------------
What will be written to the standard output when the following program is run?
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 Qd073 {
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
Answer: E

Thanks,
yuki


Hi Yuki,
Let's walk together.
1 - bogo() is invoked in main()
2 - Extension object is instantiated via 'new' op
3 - Extension's constructor is called
4 - inside Extension(), implicit super() is called
5 - Base's constructor is called
6 - int i is initialized to 0
7 - add(1) is called (note: overriding effect; this is the method add() in Extension). So i = 2 since i += v*2, where i = 0, v = 1
8 - Base() is done
9 - back to Extension's constructor and call add(2). So i = 6 since i += v*2, where i = 2 and v = 2
10 - Extension constructor is done
11 - Back to bogo() and call add(8). So i = 22 since i += v*2, where i = 6 and v = 8
12 - print() is called and that how you got the answer of i = 22.
Hope that helps,
- Lam -

[This message has been edited by Lam Thai (edited May 11, 2001).]
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lam,
Excellent explanation.Thankyou.
Madhuri.
 
Yuki Cho
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you rock Lam. Thanks soooooo much!
-yuki
 
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic