Java MCQs on reading console inputs in Java Programming Language.
1. Which exception is thrown by read() method? Answer: a 2. Which of these is used to read a string from the input stream? Answer: c 3. Which of these class is used to read characters and strings in Java from console? Answer: a 4. Which of these class is implemented by FilterInputStream class? Answer: a 5. What will be the output of the following Java program if input given is “Hello stop World”? a) Hello 6. What will be the output of the following Java program? a) Hello 7. What will be the output of the following Java program? a) xello 8. What will be the output of the following Java program if input given is “abc’def/’egh”? a) abc’ Global Education & Learning Series ? Java Programming Language.
a) IOException
b) InterruptedException
c) SystemException
d) SystemInputException
Clarification: read method throws IOException.
a) get()
b) getLine()
c) read()
d) readLine()
Clarification: None.
a) BufferedReader
b) StringReader
c) BufferedStreamReader
d) InputStreamReader
Clarification: None.
a) InputStream
b) InputOutputStream
c) BufferedInputStream
d) SequenceInputStream
Clarification: FileInputStream implements InputStream.
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"));
}
}
b) Hello stop
c) World
d) Hello stop World
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
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);
}
}
b) World
c) Helloworld
d) Hello World
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
class output
{
public static void main(String args[])
{
StringBuffer s1 = new StringBuffer("Hello");
s1.setCharAt(1,x);
System.out.println(s1);
}
}
b) xxxxx
c) Hxllo
d) Hexlo
Clarification: None.
Output:
$ javac output.java
$ java output
Hxllo
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 != ''');
}
}
b) abcdef/’
c) abc’def/’egh
d) abcqfghq
Clarification: ’ is used for single quotes that is for representing ‘ .
Output:
$ javac Input_Output.java
$ java Input_Output
abc'
