Which code fragments will compile? (Choose all that apply.)
A. interface Base2 implements Base { }
B. abstract class Class2 extends Base { public boolean m1() { return true; } }
C. abstract class Class2 implements Base { }
D. abstract class Class2 implements Base {public boolean m1() { return (true); } }
E. class Class2 implements Base { boolean m1() { return false; } byte m2(short s) { return 42; } }
This message was edited 1 time. Last update was at by Bear Bibeault
A. "X extends Y" is correct if and only if X is a class and Y is an interface.
B. "X extends Y" is correct if and only if X is an interface and Y is a class.
C. "X extends Y" is correct if X and Y both are classes or both are interfaces.
D. "X extends Y" is correct for all combinations of X and Y being classes and/or interfaces.
This message was edited 2 times. Last update was at by shoeb sattar
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12612
posted
0
What's an "mcq"?
In any case, what do *you* think the answers are, and why? Let's start there.
And welcome to JavaRanch!
This message was edited 1 time. Last update was at by David Newton
pankaj saxena
Ranch Hand
Joined: Nov 21, 2009
Posts: 44
posted
0
Edited to remove direct answer. We try to provide direction, and try to help people learn how to learn--not just give out answers. Please see DoYourOwnHomework.
This message was edited 1 time. Last update was at by David Newton
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
MCQ is of course Multiple Choice Question.
As people will see from the "Beginning Java" title page, simply giving out answers helps nobody, so thank you David for deleting that post.
Please tell us what you think the correct answer is, and let us see what we think about it.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
I earlier wrote:MCQ is of course Multiple Choice Question.
[pedantic mode]But those are not MCQs; they are true-false questions.[/pedantic mode]
Siddhesh Deodhar
Ranch Hand
Joined: Mar 05, 2009
Posts: 114
posted
0
Edited to remove answers
This message was edited 2 times. Last update was at by David Newton
Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
W. Joe Smith
Ranch Hand
Joined: Feb 10, 2009
Posts: 701
posted
0
As noted in a response above, we do not give out direct answers due to our do your own homework policy.
SCJA
When I die, I want people to look at me and say "Yeah, he might have been crazy, but that was one zarkin frood that knew where his towel was."
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
posted
0
Siddhesh, Giving away direct answers is not the norm here.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
To avoid any more direct answers, I shall close this thread. I am pulling rank.
Shoeb Sattar: please PM me with what you think are the correct answers, in a format like "1a 1c 2b 2c 2d", then I shall reopen the thread and you can post them and we shall all see what we think of them.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
I, yesterday wrote:Shoeb Sattar: please PM me . . . then I shall reopen the thread . . .
No reply, no thread opening.
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 25057
posted
0
He replied saying "B" is correct for the first question.
"B" will compile, but the answer is not complete. this is not an MCQ (where there is one correct and 4 wrong answers) but a true-false, so I wasn't simply being pedantic. There may be more than one correct answer.
Please supply your answer for the 2nd question.
Rajeev Trikha
Ranch Hand
Joined: Jan 29, 2010
Posts: 85
posted
0
If you work on the second question first then it would be clear that 'B' cannot be part of the answer to question 1.
When you post a question from a book, mock exam or other source, it is required on JavaRanch that you QuoteYourSources - in other words, please tell us where you copied the question from.
With a question like this it is very easy to compile it and see the answer. If you did compile E then you will learn something about 'weaker access privilege'. That should take you into understanding rules for method overriding.
Rajeev Trikha
Ranch Hand
Joined: Jan 29, 2010
Posts: 85
posted
0
Incidentally I should have also mentioned that in an interface all the methods are implicitly public and abstract whilst all the variables public static and final. This applies whether you state it or not in the definition.
One of the Java rules about method overriding is that you cannot assign weaker privilege to an overridden method. Imagine people invoked your method in good faith that it implements a public method but only to discover that it isn't so in the implementing class. The compiler will never allow you to break a contract.