Page 1 of 1
working with methods..
Posted: Sun Feb 16, 2014 3:44 am
by arjun gaur
with reference to a question on "working with methods"
ques-What is the correct declaration for an abstract method 'add' accessible to any class, takes no arguments and returns nothing?
(Use only one space between words)
1.public void add();
2.abstract add();
3.abstract public void add()throws Exception
4.abstract public void add(){}
5.abstract null add();
well,the solution says (3) is the correct declaration of an abstract method.
Explanation-An abstract method must have the abstract keyword and must not have a method body
But don't you think option (1) can also be correct ? cause if we talk about interfaces then by default methods are public and abstract .even in the question there's no mention about an interface or an abstract class.so option (1) can also be correct.
Re: working with methods..
Posted: Sun Feb 16, 2014 9:36 am
by admin
You are right. It should be made clear in the question that it is talking about a class and not an interface.
thank you for your feedback!
Re: working with methods..
Posted: Mon Feb 17, 2014 1:30 am
by arjun gaur
Thnx

suggest me one thing ,should i've gone for those 6 mock tests first?cause i've already started doing those objective -wise tests that present 20 random questions everytime you take the test and then i was thinking that i'll go for the full mock tests after practicing for each objective separately.
Re: working with methods..
Posted: Mon Feb 17, 2014 4:53 am
by admin
The best way to use our tool is explained here:
http://enthuware.com/index.php/how-to-p ... tification
Now that you've already take objective wise tests, the efficacy of the Standard Tests will be reduced quite a bit because questions will be repeated. This is explained here:
http://enthuware.com/index.php/gettings ... iple-times
HTH,
Paul.
Re: working with methods..
Posted: Tue Feb 18, 2014 1:30 am
by arjun gaur
Thnx.
handling Exceptions
Posted: Thu Feb 20, 2014 10:36 am
by arjun gaur
Ques- What two changes can you do, independent of each other, to make the following code compile:
class PortConnector {
public PortConnector(int port) {
if (Math.random() > 0.5) {
throw new IOException();
}
throw new RuntimeException();
}}
public class TestClass {
public static void main(String[] args) {
try {
PortConnector pc = new PortConnector(10);
} catch (RuntimeException re) {
re.printStackTrace();
}
}}
(select 2 options)
1.add throws IOException to the main method.
2.add throws IOException to PortConnector constructor.
3.add throws IOException to the main method as well as to PortConnector constructor.
4.Change RuntimeException to java.io.IOException.
5.add throws Exception to PortConnector constructor and change catch(RuntimeException re) to catch(Exception re) in the main method.
Answer-(3) and (5)
Explanation :IOException is a checked exception and since the PortConnector constructor throws IOException, this exception (or its superclass) must be present in the throws clause of the constructor.
So,why can't option (2) and (5) be correct.
Re: working with methods..
Posted: Thu Feb 20, 2014 10:47 am
by admin
Did you try it out?
Re: working with methods..
Posted: Thu Feb 20, 2014 11:01 am
by arjun gaur
didn't get you .i went for options (2) and (5) .and it says incorrect.although i haven't tried to run it.
Re: working with methods..
Posted: Thu Feb 20, 2014 8:37 pm
by admin
Please try running the code. You will see why 2 is not correct. See the difference between option 2 and 3.
Re: working with methods..
Posted: Fri Feb 21, 2014 3:45 am
by arjun gaur
yeah!! it's correct
working with inheritance
Posted: Fri Feb 21, 2014 3:47 am
by arjun gaur
ques-An overriding method must have a same parameter list and the same return type as that of the overridden method.
Ans-False
Explanation-This would have been true prior to Java 1.5. But from Java 1.5, an overriding method is allowed to change the return type to any subclass of the original return type, also known as covariant return type. This does not apply to primitives, in which case, the return type of the overriding method must match exactly to the return type of the overridden method.
agreed,although overriding methods can change return type to a subtype of the type declared by the Overriden method.BUT THIS DOES NOT APPLY TO PRIMITIVES.But, the question doesn't specify anything about primitives or object types.So "True" can also be correct.It should have been made clear in the question.
Re: working with methods..
Posted: Fri Feb 21, 2014 11:15 am
by admin
Please create a new thread and quote the question id so that I can look it up. Even better, just click on the discuss button from the question screen and a topic related to that question will be opened.