This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions for you 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 "Questions for you" Watch "Questions for you" New topic
Author

Questions for you

Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
Questions for you (I made these up):

1) Which of the follow modifiers are legal for an interface method (circle all)?

A) public
B) private
C) synchronized
D) protected
E) none of the above

2) What is true about the following code?

A) It will not compile
B) The constructor will set x = 4
C) The overloaded method Doo will never execute with any call
D) The overridden method Doo will work normally
E) The overloaded method Doo will work normally
3)What is true about the following code?

A) It will not compile
B) Any object reference to a Sprinter can call the Race() method
C) Any object reference to a Slowpoke can call the Race() method
D) Any object reference to a TurboTurtle can call the Race() method
[ September 13, 2004: Message edited by: Tom Tolman ]
sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
answer to question 1: only 'public' accessibility modifier is legal to method in interface.

answer to question 2: this code will fail to compile since void cannot be at constructor.

answer to question 3: this code will fail to compile since class TurboTurtle does not know which interface the Race() method it is referring to.
Louie van Bommel
Ranch Hand

Joined: Aug 17, 2004
Posts: 76
Originally posted by sura watthana:

answer to question 2: this code will fail to compile since void cannot be at constructor.

answer to question 3: this code will fail to compile since class TurboTurtle does not know which interface the Race() method it is referring to.


Let me take a try at it

A is wrong coz the code will compile.
B is wrong coz the only constructor that actually gets compiled is a default constructor which contains only public Doo() {super();} and knows nothing about any x.

D talks of an overridden method. There aren't any overridden methods either.

In order for a class to have an overridden method, it needs to have a superclass which contains a method with the same signature and return type as a method in the class. The only superclass to class Doo is Object. It has no method called void Doo(). There is only one method in the Doo class. Therefore it has no overridden methods.

C and E talk about overloaded methods, but there are no overloaded methods. In order to have an overloaded method, there needs to be a method in the class or in one of the superclasses that has a different signature. Object has no Doo method with any signature, and the Doo class has no additional Doo methods, therefore there are no overloaded methods.

My answer is there is no answer. As an aside this class will work normally but it has nothing to do with constructors, overloaded methods, and overridden methods.


Question 3: This will not compile. Too many closed } at the end.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Tom, for question 1 are you restricting the domain to only top-level interfaces? (Ahha! we are talking about the methods declared in the interface, so it doesn't matter if the interface is top-level or declared within a class)


The result of compiling and running question 3 is enlightening (after removing the extraneous "}" and adding a few extra statements to create the object references in the question)
[ September 15, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
sura watthana
Ranch Hand

Joined: Sep 13, 2004
Posts: 77
Thank you Mr. Louie van Bommel for telling me the correct answer.
navneet shrivastava
Ranch Hand

Joined: Jul 09, 2004
Posts: 39
hi All,
I have few good inputs on the subject:

Q 1: only 'public' accessibility modifier is legal to method in interface.
Nav: AGREE

Q 2: this code will fail to compile since void cannot be at constructor.
Nav: NOT AGREE
DESC: if we put a return type in front of a contr..it becaome a method...and is no more a constr. In particular to our class 'Doo' "void Doo(){}" will now be consider as a method by the compiler. Also, it will implicitly insert a default constructor... now these two are an overloaded methos. dont u agree??? jus try the following steps... a)compile the class file, b) decompile it[u cn decompile it using >javap Doo]... so watch out.
[plz correct m if you thnk am wrong smwhr ]

Q 3: This will not compile. Too many closed } at the end.
Nav: AGREE

We as a programmer must dare to think like a compiler.. and thts the word for me.

Thanks
Navneet


+nav
Nanda Kishore
Greenhorn

Joined: Aug 17, 2003
Posts: 9
Question - 3 will compile after removing the extra }. But it will not run. It will throw NoSuchMethodError.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by Nanda Kishore:
Question - 3 will compile after removing the extra }. But it will not run. It will throw NoSuchMethodError.


But give it a main method, create some instances of the class and call the method through the various reference types...
Manikandan Jayaraman
Ranch Hand

Joined: Sep 15, 2004
Posts: 230
===========
But give it a main method, create some instances of the class and call the method through the various reference types...
===========

Tried it out and perfectly works fine! Answer for Q3 is thus B,C,D (considering the extra '}' is removed )

The code ...

====
interface Sprinter
{
public void Race();
}

interface Slowpoke
{
public void Race();
}

class TurboTurtle implements Sprinter, Slowpoke
{
public void Race()
{
System.out.println("Turbo charged");
}

public static void main(String args[])
{
Sprinter s1 = new TurboTurtle();
Slowpoke s2 = new TurboTurtle();
TurboTurtle t = new TurboTurtle();

s1.Race();
s2.Race();
t.Race();
}
}
===

The output ...
===
Turbo charged
Turbo charged
Turbo charged
===


Regards,<br />Mani<br />SCJP 1.4 (95%)<br />SCWCD 1.4 (94%)
kapil munjal
Ranch Hand

Joined: May 11, 2004
Posts: 298
So, finally, the answers are

1. A
2. E
3. BCD

Am I right???

Kaps


Kapil Munjal
SCJP 1.4, SCWCD 1.4
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Kapil,
I feel it's
1> A
2> No Answer ( I agree with Louie's interpretation )
3> BCD


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
It is not true that a class that implements two interfaces with methods with the same signature will not compile. The following compiles:



Another language's, such as C++ (which supports multiple inheritance), compiler would bark at a similar construct.
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
I agree with Louie.

methods and constructors both have signatures but they exist in separate namespaces so neither can overload or override the other.

There is no place in Java syntax where you can choose between using either a class name or a method name. This is good because the presence of a return type, the only way to distinguish constructors from methods, is not part of any signature. If two methods in a class have the same signature but different return types, that's a compile-time error. In this case, the Doo() method would have conflicted with the default Doo() constructor.


Mike Gershman
SCJP 1.4, SCWCD in process
 
GeeCON Prague 2014
 
subject: Questions for you