Java Multiple Choice Questions & Answers on Java.io Character Streams for Freshers

1. Which of these stream contains the classes which can work on character stream?

a) InputStream
b) OutputStream
c) Character Stream
d) All of the mentioned
Answer: C

Explanation: InputStream & OutputStream classes under byte stream they are not streams. Character Stream contains all the classes which can work with Unicode.
2. Which of these class is used to read characters in a file?

a) FileReader
b) FileWriter
c) FileInputStream
d) InputStreamReader
Answer: a
3. Which of these method of FileReader class is used to read characters from a file?

a) read()
b) scanf()
c) get()
d) getInteger()
Answer: a
4. Which of these class can be used to implement input stream that uses a character array as the source?

a) BufferedReader
b) FileReader
c) CharArrayReader
d) FileArrayReader
Answer: c

Explanation: CharArrayReader is an implementation of an input stream that uses character array as a source. Here array is the input source.
5. Which of these is a method to clear all the data present in output buffers?

a) clear()
b) flush()
c) fflush()
d) close()
Answer: b
6. Which of these classes can return more than one character to be returned to input stream?

a) BufferedReader
b) Bufferedwriter
c) PushbachReader
d) CharArrayReader
Answer: c

Explanation: PushbackReader class allows one or more characters to be returned to the input stream. This allows looking ahead in input stream and performing action accordingly.
7. What is the output of this program?
  1.     import java.io.*;
  2.     class filesinputoutput {
  3.         public static void main(String args[]) {
  4.             InputStream obj = new FileInputStream("inputoutput.java");
  5.             System.out.print(obj.available());
  6.         }
  7.     }
Note: inputoutput.java is stored in the disk.

a) true
b) false
c) prints number of bytes in file
d) prints number of characters in the file
Answer: c

Explanation: obj.available() returns the number of bytes.
8. What is the output of this program?
  1.     import java.io.*;
  2.     class Chararrayinput {
  3.         public static void main(String[] args) {
  4. 	    String obj  = "abcdef";
  5.             int length = obj.length();
  6.             char c[] = new char[length];
  7.             obj.getChars(0,length,c,0);
  8.             CharArrayReader input1 = new CharArrayReader(c);
  9.             CharArrayReader input2 = new CharArrayReader(c, 0, 3);
  10.             int i;
  11.             try {
  12. 		while ((i = input1.read()) != -1) {
  13.                     System.out.print((char)i);
  14.                 }
  15.        	    } 
  16.             catch (IOException e) {
  17. 	        // TODO Auto-generated catch block
  18.                 e.printStackTrace();
  19. 	    }
  20. 	}
  21.     }
a) abc
b) abcd
c) abcde
d) abcdef
Answer: d
9. What is the output of this program?
  1.     import java.io.*;
  2.     class Chararrayinput {
  3.         public static void main(String[] args) {
  4. 	    String obj  = "abcdef";
  5.             int length = obj.length();
  6.             char c[] = new char[length];
  7.             obj.getChars(0, length, c, 0);
  8.             CharArrayReader input1 = new CharArrayReader(c);
  9.             CharArrayReader input2 = new CharArrayReader(c, 0, 3);
  10.             int i;
  11.             try {
  12. 		while ((i = input2.read()) != -1) {
  13.                     System.out.print((char)i);
  14.                 }
  15.        	    } 
  16.             catch (IOException e) {
  17. 	        // TODO Auto-generated catch block
  18.                 e.printStackTrace();
  19. 	    }
  20. 	}
  21.     }
a) abc
b) abcd
c) abcde
d) abcdef
Answer: a
10. What is the output of this program?
  1.     import java.io.*;
  2.     class Chararrayinput {
  3.         public static void main(String[] args) {
  4. 	    String obj  = "abcdefgh";
  5.             int length = obj.length();
  6.             char c[] = new char[length];
  7.             obj.getChars(0, length, c, 0);
  8.             CharArrayReader input1 = new CharArrayReader(c);
  9.             CharArrayReader input2 = new CharArrayReader(c, 1, 4);
  10.             int i;
  11.             int j;
  12.             try {
  13. 		while ((i = input1.read()) == (j = input2.read())) {
  14.                     System.out.print((char)i);
  15.                 }
  16.        	    } 
  17.             catch (IOException e) {
  18. 	        // TODO Auto-generated catch block
  19.                 e.printStackTrace();
  20. 	    }
  21. 	}
  22.     }
a) abc
b) abcd
c) abcde
d) None of the mentioned
Answer: d

Explanation: No output is printed. CharArrayReader object input1 contains string “abcdefgh” whereas object input2 contains string “bcde”, when while((i=input1.read())==(j=input2.read())) is executed the starting character of each object is compared since they are unequal control comes out of loop and nothing is printed on the screen.

Related

C# Questions & Answers on Pointers Operation – 1 for Freshers

1. What will be the output of the given code? class UnsafeCode { unsafe static void Main() { int m = 10; int *mptr = &m; int **ptr = &mpt...

Linux Device Drivers Major-Minor Numbers Questions & Answers for Freshers

1. The major number identifies the _____ associated with the device. a) driverb) protocolc) portd) none of the mentioned Answer: a 2. The minor number range should be a) 0 to 15b) 0 to 63c) 0 to...

C# Questions & Answers on Unsafe code & Pointers Basics for Freshers

1. Fill up the blank :Pointer variable is used to hold the _________ of the variable a) Valueb) Addressc) Value and Addressd) Name of the variable Answer: b Explanation: By definition. 2. Which ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item