• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

small doubt in declaration

 
shilpa Reddy
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know this question is very simple but i have a basic doubt.In the below code invade(7) what is the variable type..is 7 by default declared a int..

class Alien {
String invade(short ships) { return "a few"; }
String invade(short... ships) { return "many"; }
}
class Defender {
public static void main(String [] args) {
System.out.println(new Alien().invade(7));
} }
What is the result?
A. many
B. a few
C. Compilation fails.
D. The output is not predictable.
E. An exception is thrown at runtime.

Answer is: C is correct, compilation fails. The var-args declaration is fine, but invade takes a short,
so the argument 7 needs to be cast to a short. With the cast, the answer is B, 'a few'.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shilpa Reddy :
I know this question is very simple but i have a basic doubt.In the below code invade(7) what is the variable type..is 7 by default declared a int.


Yes 7 is 32 bit int literal.

Any other doubt in that?


Naseem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic