working with methods..
Moderators: Site Manager, fjwalraven
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
working with methods..
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.
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.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: working with methods..
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!
thank you for your feedback!
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
Re: working with methods..
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.
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.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: working with methods..
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
handling Exceptions
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.
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.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: working with methods..
Did you try it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
Re: working with methods..
didn't get you .i went for options (2) and (5) .and it says incorrect.although i haven't tried to run it.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: working with methods..
Please try running the code. You will see why 2 is not correct. See the difference between option 2 and 3.
If you like our products and services, please help us by posting your review here.
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
Re: working with methods..
yeah!! it's correct
-
- Posts: 20
- Joined: Sun Feb 16, 2014 3:28 am
- Contact:
working with inheritance
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.
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.
-
- Site Admin
- Posts: 10070
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: working with methods..
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.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 89 guests