• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

io

 
Prasanna Vijaya
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question in one of the mock exams. Can any body tell me the right answer with some explanation regarding that
String readString() {
char buf[] = new char[80];
Reader in = new InputStreamReader(System.in);
in.read(buf, 0, 80);
return new String(buf);
}
What is wrong with method readString()?
Choice 1
you can not cast InputStreamReader into Reader
Choice 2
String objects can not be instantiated with a character array
Choice 3
logic to catch exceptions for the InputStream statements is missing
Choice 4
an InputStreamReader object can not be bound to standard input
Choice 5
all IO methods must be declared public
question no 2

public int m1(int x) {
int count=1;
try {
count += x;
count += m2(count);
count++;
}
catch(ArithmeticException e) { count -= x; }
finally { count += 3; }
count++;
return count;
}
When m1(2) is invoked, m2() returns a value of 2 and m1() returns ________.
Choice 1
5
Choice 2
6
Choice 3
7
Choice 4
10
Choice 5
nothing. The system exits
 
Nijeesh Balan
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
1.C
A. - false.. Since Reader is the base class and can very well hold the object of it's subclass InputStreamReader..
B. - False.. B'cos String(char[]) is available
C. True.. Since InputStreamReader() is used either the code should have been enclosed in a try/catch block or should have declared the throws clause.
D.- False..
E.- False .. Nothing like that..

2. Choice 4.. Please run the pgm..

Thanks
Nijeesh.
 
krishna kumar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
For the first Question..the answer is C.
When ever u r dealing with I/O streams u have to catch the Exception.
for the second question the answer is simple...
at first
Count =1;
after that u r adding x to count so
count+=x => count = 3;
count+=m2(x)=> count+=2=> count= 5;
count++ => count =6;
here no Exception will be thrown...it will go to finally..
there count+=3 => count = 9;
at last count++ => count =10;
Thanks
 
Vijayalakshmi Chipada
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you explain there is no value for x
how you will get
count+=x => count = 3;
Thank you

------------------
Ch. Vijayalakshmi
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at the question again. There is a value for x.
"When m1(2) is invoked, ..." so x = 2.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic