Reading Console Input
Reading Console Input
Reading Console Input
This section of our 1000+ Java MCQs focuses on reading console inputs in Java
Programming Language.
Answer: a
Explanation: read method throws IOException.
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.
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
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
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'