File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes mughal mock question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "mughal mock question" Watch "mughal mock question" New topic
Author

mughal mock question

may leung
Greenhorn

Joined: Nov 28, 2000
Posts: 23
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

Joined: Nov 30, 2000
Posts: 56
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

Joined: Nov 21, 2000
Posts: 40
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 ");
}
}

 
Consider Paul's rocket mass heater.
 
subject: mughal mock question