This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two Mock Exam Q

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two questions from John Hunt Mock exam.

Q.No.25.

Which of the following is a legal return type of a method overloading the following method:
public void add(int a) {�}
A. void
B. int
C. Can be anything
Select the most appropriate answer.

Ans given is C.
It can�t be anything, because if it is void, it will be overriding.

Q.No. 30

What class must an inner class extend:

A. The top level class
B. The Object class
C. Any class or interface
D. It must extend an interface

Select the most appropriate answer
Ans. Given is C.
I don�t think it is C. Because - 1.Class cannot extend interface. 2.final class can not be extended.

Looks like, A and B is correct.

Please confirm both questions.

Thank you in advance.
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

It can�t be anything, because if it is void, it will be overriding.


Is public void add (int a, String s) method overload or override for the method public void add (int a) ? That should give you hint why the answer is return type can be anything

Answer to the second question I guess u r correct.


Hope that helps ya.
 
Muralidhar Mandapati
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the second question..
I think that inner class can extends any class other than final classes and can implement any interface.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply Jay / Murladhar,
Agree, Jay, public void add (int a, String s) is not overriding, but public void add(int a) is..
So, it is true or false based on parameters ! And, in the example given, parameters are not considered, so i don't think we should consider parameters if it is not given in question at all!

If they want to consider parameters, then they might ask full sentence, instead of single world like - void, int etc..
 
Jay Pawar
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nitin,
Basic rule for overloading is that method name should remain same and parameter list should be different , there is no restriction on the return type of the overloaded method.
Here is extract from K&B book which tells the rules for overloaded methods


Overloaded methods must change the argument list.
Overloaded methods can change the return type.
Overloaded methods can change the access modifier.
Overloaded methods can declare new or broader checked exceptions.
A method can be overloaded in the same class or in a subclass.



Hope that helps ya
[ November 26, 2004: Message edited by: Jay Pawar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic