aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance" Watch "Inheritance" New topic
Author

Inheritance

putti don
Greenhorn

Joined: Jul 28, 2005
Posts: 20
Hi,
I found this question in one of the mock exam...

Question :
What is the result of compiling & executing the following code ?

class Object {

String str ;

Object ( String str ) {
this . str = str ;
}

public String toString ( ) {
return str ;
}
};

class Ques extends Object{

Ques ( String str ) {
super ( str ) ;
}

public static void main ( String args [ ] ) {
Ques x = new Ques ( " My Object " ) ;
System . out .println ( x. str ) ;
}
};

Options :

a . Compiler error since you cannot redeclare a class that already exists
b . Compiler error since the extends clause does know which Object class to extend from
c . Prints something that denotes the state of the object x
d . Prints My Object

ANS is option d. But when i compiled this program i got error. can anyone please explain what is the correct output?

Thanks.
Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518

can anyone please explain what is the correct output?


(D) is correct.

(A) is not valid because we are not redeclaring an existing class. Sun's Object class is in java.lang but this Object class is in the default package.

(B) is incorrect because the Object class is in the same package as the Ques class so the compiler prefers it over java.lang.Object.

(C) is incorrect because str is a String field of the Object class.
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
Try removing the simicolons at the end of your class declarations.
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
it is compiling and printing My Object to the console.....


Thanks & Regards<br /> <br />-Srikanth
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by Thomas Drew:
Try removing the simicolons at the end of your class declarations.


Thomas, I dont think semicolons at the end of class decleration affect the proper functioning of the program.


Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inheritance
 
Similar Threads
jtips ques
Few Tricky Ques.
ques
Dan's ques - anonymous class
classpath Ques