Reading Console Input

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

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: d
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.

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�?

class Input_Output
{
public static void main(String args[]) throws IOException
{
string str;
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
do
{
str = (char) obj.readLine();
System.out.print(str);
} while(!str.equals("strong"));
}
}
a) Hello
b) Hello stop
c) World
d) Hello stop World
View Answer

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

class output
{
public static void main(String args[])
{
StringBuffer c = new StringBuffer("Hello");
StringBuffer c1 = new StringBuffer(" World");
c.append(c1);
System.out.println(c);
}
}
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?

class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.setCharAt(1,x);
System.out.println(s1);
}
}
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�?

class Input_Output
{
public static void main(String args[]) throws IOException
{
char c;
BufferedReader obj = new BufferedReader(new
InputStreamReader(System.in));
do
{
c = (char) obj.read();
System.out.print(c);
} while(c != '\'');
}
}
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'

You might also like