Java Questions & Answers – Reading Console Input

This section of our 1000+ Java MCQs focuses on reading console inputs in Java Programming Language.

1. Which exception is thrown by read() method?
a) IOException
b) InterruptedException
c) SystemException
d) SystemInputException
View Answer

Answer: a
Explanation: read method throws IOException.

2. Which of these is used to read a string from the input stream?
a) get()
b) getLine()
c) read()
d) readLine()
View Answer

Answer: c
Explanation: None.

3. Which of these class is used to read characters and strings in Java from console?
a) BufferedReader
b) StringReader
c) BufferedStreamReader
d) InputStreamReader
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

4. Which of these class is implemented by FilterInputStream class?
a) InputStream
b) InputOutputStream
c) BufferedInputStream
d) SequenceInputStream
View Answer

Answer: a
Explanation: FileInputStream implements InputStream.

5. What will be the output of the following Java program if input given is “Hello stop World”?

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  1.     class Input_Output 
  2.     {
  3.         public static void main(String args[]) throws IOException
  4.         {	 
  5.             string str;
  6.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  7.             do
  8.             {
  9.                 str = (char) obj.readLine();
  10. 	        System.out.print(str);
  11.             } while(!str.equals("strong"));
  12.         }
  13.     }

a) Hello
b) Hello stop
c) World
d) Hello stop World
View Answer

Answer: d
Explanation: “stop” will be able to terminate the do-while loop only when it occurs singly in a line. “Hello stop World” does not terminate the loop.
Output:

advertisement
$ javac Input_Output.java
$ java Input_Output
Hello stop World

6. What will be the output of the following Java program?

advertisement
  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.              StringBuffer c = new StringBuffer("Hello");
  6.              StringBuffer c1 = new StringBuffer(" World");
  7.              c.append(c1);
  8.              System.out.println(c);
  9.         }
  10.     }

a) Hello
b) World
c) Helloworld
d) Hello World
View Answer

Answer: d
Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
Output:

$ javac output.java
$ java output
Hello World

7. What will be the output of the following Java program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         { 
  5.            StringBuffer s1 = new StringBuffer("Hello");
  6.            s1.setCharAt(1,x);
  7.            System.out.println(s1);
  8.         }
  9.     }

a) xello
b) xxxxx
c) Hxllo
d) Hexlo
View Answer

Answer: c
Explanation: None.
Output:

$ javac output.java
$ java output
Hxllo

8. What will be the output of the following Java program if input given is “abc’def/’egh”?

  1.     class Input_Output
  2.     {
  3.         public static void main(String args[]) throws IOException
  4.         {	 
  5.             char c;
  6.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  7.             do
  8.             {
  9.                 c = (char) obj.read();
  10. 	        System.out.print(c);
  11.             } while(c != '\'');
  12.         }
  13.     }

a) abc’
b) abcdef/’
c) abc’def/’egh
d) abcqfghq
View Answer

Answer: a
Explanation: \’ is used for single quotes that is for representing ‘ .
Output:

$ javac Input_Output.java
$ java Input_Output
abc'

Sanfoundry Global Education & Learning Series ? Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.