• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chapter 5,question-8,(k&b)

 
Sambit Banerjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

i think line 7 needs some modification,the arg in Integer.parseInt() should be in double quotes.and barring option b all the options seem to be right but in the answer it is given only options a and c are correct.
please check this and reply.
 
Jiang zhixiao
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static int parseInt(String s)
throws NumberFormatException

return int,any questions?
 
Sambit Banerjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jiang,sorry i could not get you.please discuss it in a bit details.
 
Pranav Bhatt
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sambit,
Why do you think it should be in double quotes? arg is already in String format though its a number, thats why you need to parse it for int. One more thing you have not posted the option so people here wont be able to identify the correct ones
[ September 02, 2008: Message edited by: Pranav Bhatt ]
 
ankit kumar
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Ping extends Utils {
public static void main(String [] args) {
Utils uu = new Ping();
System.out.print(uu.getInt(args[0]));
}
int getInt(String arg) {
return Integer.parseInt(arg);
}
}
class Utils {
int getInt(String x) throws Exception { return 7; }
}
And the following three possible changes:
C1. Declare that main() throws an Exception.
C2. Declare that Ping.getInt() throws an Exception.
C3. Wrap the invocation of getInt() in a try / catch block.
Which change(s) allow the code to compile? (Choose all that apply.)
A. Just C1 is sufficient.
B. Just C2 is sufficient.
C. Just C3 is sufficient.
D. Both C1 and C2 are required.
E. Both C1 and C3 are required.
F. Both C2 and C3 are required.
G. All three changes are required.


and the ans is A&C
 
Sambit Banerjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ pranav,
actually i was getting the follwing compiler error:
Ping.java.18:cannot find symbol
symbol:method parseInt(String)
location:class java.lang.Integer
return Integer.parseInt(arg);
1 error.
so i thought that double quotes should be used.
 
Bert Bates
author
Sheriff
Posts: 8905
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sambit,

What's the version of the compiler you are using?
 
Sambit Banerjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ bert,
its jdk1.6.0_07.
 
Sambit Banerjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me anything about the confusion.
 
Bert Bates
author
Sheriff
Posts: 8905
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sambit -

I think you should post your EXACT CODE!

Thanks,

Bert
 
Sambit Banerjee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bert,as you have asked for my jdk version i thought i have a problem with it.So,i have checked it in my friend's pc and i found no problem with Integer.parseInt(arg).
But i have checked the options also and still find that except option b all the options compile the code.
Here's the exact code.

 
Puneet Nahata
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are confused with the options. D,E,F,G are not possible.
Consider option E. It says "Both C1 and C3 are required". But actually either of them will suffice(though there is no harm if you apply both options while testing in code. It will work fine.)


Originally posted by Sambit Banerjee:
Bert,as you have asked for my jdk version i thought i have a problem with it.So,i have checked it in my friend's pc and i found no problem with Integer.parseInt(arg).
But i have checked the options also and still find that except option b all the options compile the code.
Here's the exact code.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic