aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question Frm k&B chp 5 Self test  Qno :- 8 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 "Question Frm k&B chp 5 Self test  Qno :- 8" Watch "Question Frm k&B chp 5 Self test  Qno :- 8" New topic
Author

Question Frm k&B chp 5 Self test Qno :- 8

Akhilesh Yadav
Ranch Hand

Joined: Apr 04, 2006
Posts: 46
public class A {
public int baz(int x ) {
System.out.println("A");
return x*2;
}
}


public class B extends A{
public static void main(String [] args){
B a = new B();
long x = a.doStuff(7);
System.out.println(x);
}

public long doStuff (int x){
return x*3;
}
}

As per K&B Answer is Compliatiopn Fail at line

public long doStuff(intx)

but when i wrote the prog and complied it and run it its giving out put 21
iam bit confused which answer is right 21 or compilre time error
Sreeram Desigan
Greenhorn

Joined: Apr 07, 2006
Posts: 23
you are correct there is no error in the about code.
I guess compiler error would have occured if the method baz(int) in base class (A) was actually dostuff(int). In that case the derived class B is trying to override dostuff method in A with a long return type. Which is an error.
Changchun Wang
Ranch Hand

Joined: Feb 15, 2006
Posts: 83
maybe you will complie and run it again

you should notice the methods doStuff() in class A and CLASS Bit is wrong way to override the method
Sandeep Singh Kunwar
Ranch Hand

Joined: Feb 27, 2006
Posts: 52
you are correct there is no error in the about code.
I guess compiler error would have occured if the method baz(int) in base class (A) was actually dostuff(int). In that case the derived class B is trying to override dostuff method in A with a long return type. Which is an error.


Exactly


OfCourse Learner...<br />SCJP 5, SCWCD1.4<br />Blogs: <a href="http://sandeepkunwar.blogspot.com" target="_blank" rel="nofollow">http://sandeepkunwar.blogspot.com</a>
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8813
    
    5
Hey Akhilesh,

I'm really confused, because the code you showed us is different than the code in the book?

BTW, this question is from the older 1.4 book, not the 1.5 book.

Can you take another look and let us know what's going on?

Thanks,

Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Vishal Chawla
Greenhorn

Joined: Apr 06, 2006
Posts: 28
now if a question like this is on the exam, do we assume that the class A and B are in different files or do we assume that they are in the same file. If they are in the same file, then wont we get compile time error for having two public classes in the same file.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8813
    
    5
On the real exam all the code listings in the multiple choice questions will have line numbers. If more than one class is listed, and the line numbers don't repeat, then the classes are in the same file.
Vishal Chawla
Greenhorn

Joined: Apr 06, 2006
Posts: 28
Originally posted by Bert Bates:
If more than one class is listed, and the line numbers don't repeat, then the classes are in the same file.


wait.. if more than one class is listed and the line numbers dont repeat, then they should be in a different file right.. I mean if line numbers repeat, then they should be in the same file..
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8813
    
    5
Let's look at two examples:

Example 1: two classes in one file



Example 2: two classes in two files



hth,

Bert

p.s. BTW we still don't really know what the original question on this thread was all about
[ April 11, 2006: Message edited by: Bert Bates ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question Frm k&B chp 5 Self test Qno :- 8