250+ TOP MCQs on Reading Console Input and Answers

Java MCQs on reading console inputs in Java Programming Language.

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

Answer: a
Clarification: 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()

Answer: c
Clarification: 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

Answer: a
Clarification: None.

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

Answer: a
Clarification: FileInputStream implements InputStream.

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

  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

Answer: d
Clarification: “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:

$ javac Input_Output.java
$ java Input_Output
Hello stop World

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

  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

Answer: d
Clarification: 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

Answer: c
Clarification: 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

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

$ javac Input_Output.java
$ java Input_Output
abc'

Global Education & Learning Series ? Java Programming Language.

  250+ TOP MCQs on Java.lang Introduction and Answers

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top