• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

io

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic