• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pl explain these

 
vatsalya rao
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
6) What would be the output of the following code?


private class Trouble{
int count;

public void increment(){
count++;
System.out.println(count);
}

public void decrement(){
count--;
System.out.println(count);
}

public static void main(String args[]){
Trouble tsome=new Trouble();
tsome.increment();
tsome.decrement();
}
}

Whats wrong with this code?The answer given is COMPILE ERROR.Where is the error lying in this code?


2) Which of the following are not a part of a method signature?

(A) Name of Method

(B) Type of Method parameter(s)

(C) Order of method parameter(s)

(D) Return type of method-

The answer given is D.We will specify return type

also in method signature right?Example is int add(int,int);

But why the answer is given like that?



3)8) In the following what access modifier could be placed in the
place of FFFF to let the code compile without error?

class Super
{
protected void test(){
};
}

class Sub extends Super
{
FFFF void test()
{
super.test();
}
}

Choose from the options below:
(i) Replace FFFF by private
(ii) Replace FFFF by public
(iii) Remove FFFF
(iv) Replace FFFF by protected

(A) i,iii are correct

(B) ii,iv are correct

(C) only iii is correct

(D) only ii is correct

Here the answer is B.

Can anybody please explain the uderlying concept for this answer?
 
Michael Jennings
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

First one. Your class
Trouble
must not be private. You cannot have a top-level class as private. A top-level class can be public, abstract or final.

Second one. Return-type is not part of method signature.

Third one. The method test() in class Super can be overloaded in class Sub. When doing so, the accessibility of the overloading method(test() of Sub) cannot be more restricted than the overloaded method(test() of Super).
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Jennings, thank you, but it doesn't help anybody to learn if you simply answer this sort of exam question. You ought to have asked what Vats Rao thought the correct answers were.
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vats rao:
6) What would be the output of the following code?


private class Trouble{
int count;

public void increment(){
count++;
System.out.println(count);
}

public void decrement(){
count--;
System.out.println(count);
}

public static void main(String args[]){
Trouble tsome=new Trouble();
tsome.increment();
tsome.decrement();
}
}

Whats wrong with this code?The answer given is COMPILE ERROR.Where is the error lying in this code?


2) Which of the following are not a part of a method signature?

(A) Name of Method

(B) Type of Method parameter(s)

(C) Order of method parameter(s)

(D) Return type of method-

The answer given is D.We will specify return type

also in method signature right?Example is int add(int,int);

But why the answer is given like that?



3)8) In the following what access modifier could be placed in the
place of FFFF to let the code compile without error?

class Super
{
protected void test(){
};
}

class Sub extends Super
{
FFFF void test()
{
super.test();
}
}

Choose from the options below:
(i) Replace FFFF by private
(ii) Replace FFFF by public
(iii) Remove FFFF
(iv) Replace FFFF by protected

(A) i,iii are correct

(B) ii,iv are correct

(C) only iii is correct

(D) only ii is correct

Here the answer is B.

Can anybody please explain the uderlying concept for this answer?


Compile, run and see what you've got and then, if you have doubts about the results, will be glad to help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic