• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Another IO Question

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, another question....
Given the following code, under which circumstances will the method return false?
public static boolean test(InputStream is) throws IOException{
int value = is.read();
return value == (value & Oxff);
}
Select all valid answers.
a A character of more than 8 bits was read from the stream.
b An I/O error occurred.
c Never
d The end of the input was reached in the input stream.
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure..but the answer 'C' seems to be right...
Thanks,
Aman
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
at the end of file, value = -1.
-1 & oxff will be a positive value.
in this case value != value & ox11. the result will be false. so D is correct.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi Dandan,
Could you please explain me how
-1 & oxff will be a positive value.
Thanks in advance.
mita
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
-1 = 1111 1111 1111 1111 1111 1111
0xff = 0000 0000 0000 0000 1111 1111
so -1 & 0xff will be
0000 0000 0000 0000 1111 1111 which is 255.
 
mita
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Vasansrini.
mita
 
Ranch Hand
Posts: 1871
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lets checkout the options below
a A character of more than 8 bits was read from the stream.
// a programs should use the byte streams, descendants of InputStream and OutputStream, to read and write 8-bit bytes. Now since an InputStream was passed as parameter the program can read only 8 bit bytes as such this is wrong. Remember character streams (Reader and Writer) cannot be passed as parameters to methods that take InputStreams so this is false.
b An I/O error occurred.
// The read() method is declared as
public abstract int read()throws IOException.
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. A subclass must provide an implementation of this method.
Returns : the next byte of data, or -1 if the end of the stream is reached.
Throws : IOException - if an I/O error occurs.
If an I/O exception occurs then the next statements ie (return value == (value & 0xFF) are not executed and the method returns terminating the program or exiting that particular block if the exception is handled. uncomment the lines in the program below and check out the results. this too does not return false.
c Never
// if all options are wrong maybe this one is correct
d The end of the input was reached in the input stream.
// correct. dandan and Vansrini have given appropriate answers for this. However check out the program below.

Regds.
Rahul.
[This message has been edited by Rahul Mahindrakar (edited August 04, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method returns an int while it needs to return a boolean, how can (d) be correct?
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ben,
return value == (value & 0xFF); does not return an int. It returns true if "value is equal to value & oxff"
It returns false otherwise.
true and false are boolean values.
reply
    Bookmark Topic Watch Topic
  • New Topic