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
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
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
Explanation: None.
4. Which of these class is implemented by FilterInputStream class?
a) InputStream
b) InputOutputStream
c) BufferedInputStream
d) SequenceInputStream
View Answer
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
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:
$ javac Input_Output.java
$ java Input_Output
Hello stop World
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
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
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
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.
- Practice Information Technology MCQs
- Apply for Java Internship
- Practice Programming MCQs
- Check Java Books
- Practice BCA MCQs