Java Questions & Answers – Input & Output Basics

This section of our 1000+ Java MCQs focuses on creating threads in Java Programming Language.

1. What does AWT stands for?
a) All Window Tools
b) All Writing Tools
c) Abstract Window Toolkit
d) Abstract Writing Toolkit
View Answer

Answer: c
Explanation: AWT stands for Abstract Window Toolkit, it is used by applets to interact with the user.

2. Which of these is used to perform all input & output operations in Java?
a) streams
b) Variables
c) classes
d) Methods
View Answer

Answer: a
Explanation: Like in any other language, streams are used for input and output operations.

3. Which of these is a type of stream in Java?
a) Integer stream
b) Short stream
c) Byte stream
d) Long stream
View Answer

Answer: c
Explanation: Java defines only two types of streams – Byte stream and character stream.
advertisement

4. Which of these classes are used by Byte streams for input operation?
a) InputStream
b) InputOutputStream
c) Reader
d) All of the mentioned
View Answer

Answer: a
Explanation: Byte stream uses InputStream and OutputStream classes for input and output operation.

5. Which of these classes are used by character streams for output operations?
a) InputStream
b) Writer
c) ReadStream
d) InputOutputStream
View Answer

Answer: b
Explanation: Character streams uses Writer and Reader classes for input & output operations.
Free 30-Day Python Certification Bootcamp is Live. Join Now!

6. Which of these class is used to read from byte array?
a) InputStream
b) BufferedInputStream
c) ArrayInputStream
d) ByteArrayInputStream
View Answer

Answer: d
Explanation: ByteArrayInputStream: This class is designed to allow reading byte streams from a byte array. It provides methods to read bytes from the array, such as read() which reads the next byte of the ByteArrayInputStream.

7. What will be the output of the following Java program if input given is ‘abcqfghqbcd’?

  1.     import java.io.*;
  2.     class Input_Output
  3.     {
  4.         public static void main(String args[]) throws IOException
  5.         {	
  6.             char c;
  7.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  8.             do
  9.             {
  10.                 c = (char) obj.read();
  11. 	        System.out.print(c);
  12.             } while(c != 'q');
  13.         }
  14.     }

a) abcqfgh
b) abc
c) abcq
d) abcqfghq
View Answer

Answer: c
Explanation: The do-while loop in the program only prints the output from the stream from the beginning to the q character including q.
Output:

advertisement
$ javac Input_Output.java
$ java Input_Output
abcq

8. What will be the output of the following Java program if input given is “abc’def/’egh”?

  1. import java.io.*;   
  2. class Input_Output
  3. {
  4.     public static void main(String args[]) throws IOException
  5.     {	
  6.         char c;
  7.         BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  8.         do
  9.         {
  10.             c = (char) obj.read();
  11.             System.out.print(c);
  12.         } while(c!='/');
  13.     }
  14. }

a) abc’
b) abcdef/’
c) abc’def/’egh
d) abc?def/
View Answer

Answer: a
Explanation: Since / is used to terminate the loop to print the character from stream. And the ‘ use in the statement without \ it will not be recognized without an escape sequence.
Output:

$ javac Input_Output.java
$ java Input_Output
abc'

9. 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.              System.out.println(c.length());
  7.         }
  8.     }

a) 4
b) 5
c) 6
d) 7
View Answer

Answer: b
Explanation: length() method is used to obtain length of StringBuffer object, length of “Hello” is 5.
Output:

$ javac output.java
$ java output
5

Sanfoundry Global Education & Learning Series – Java Programming Language.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can find me on LinkedIn, watch my free Youtube Masterclasses, or join my Telegram discussions.

I also mentor professionals - especially those in their 40s to 60s, who are navigating career shifts, midlife transitions, or simply looking to rediscover purpose in their work and life. Learn more here.