Java Multiple Choice Questions & Answers on Reading Console Input for Freshers

1. Which exception is thrown by read() method?

a) IOException
b) InterruptedException
c) SystemException
d) SystemInputException
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()
Answer: c
3. Which of these class is used to read characters and strings in Java from console?

a) BufferedReader
b) StringReader
c) BufferedStreamReader
d) InputStreamReader
Answer: a
4. Which of these classes are used by Byte streams for input and output operation?

a) InputStream
b) InputOutputStream
c) Reader
d) All of the mentioned
Answer: a

Explanation: Byte stream uses InputStream and OutputStream classes for input and output operation.
5. Which of these class is implemented by FilterInputStream class?

a) InputStream
b) InputOutputStream
c) BufferedInputStream
d) SequenceInputStream
Answer: a

Explanation: FileInputStream implements InputStream.
6. Which of these class is used to read from a file?

a) InputStream
b) BufferedInputStream
c) FileInputStream
d) BufferedFileInputStream
Answer: c
7. What is the output of this program if input given is “Hello stop World”?
  1.     class Input_Output {
  2.         public static void main(String args[]) throws IOException {  
  3.             string str;
  4.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  5.             do {
  6.                 str = (char) obj.readLine();
  7.          System.out.print(str);
  8.             } while(!str.equals("strong"));
  9.         }
  10.     }
a) Hello
b) Hello stop
c) World
d) Hello stop World
Answer: d

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.
8. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.              StringBuffer c = new StringBuffer("Hello");
  5.              StringBuffer c1 = new StringBuffer(" World");
  6.              c.append(c1);
  7.              System.out.println(c);
  8.         }
  9.     }
a) Hello
b) World
c) Helloworld
d) Hello World
Answer: d

Explanation: append() method of class StringBuffer is used to concatenate the string representation to the end of invoking string.
9. What is the output of this program?
  1.     class output {
  2.         public static void main(String args[])
  3.         { 
  4.            StringBuffer s1 = new StringBuffer("Hello");
  5.            s1.setCharAt(1,x);
  6.            System.out.println(s1);
  7.         }
  8.     }
a) xello
b) xxxxx
c) Hxllo
d) Hexlo
Answer: c
10. What is the output of this program if input given is “abc’def/’egh”?
  1.     class Input_Output {
  2.         public static void main(String args[]) throws IOException {  
  3.             char c;
  4.             BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
  5.             do {
  6.                 c = (char) obj.read();
  7.          System.out.print(c);
  8.             } while(c != '\'');
  9.         }
  10.     }
a) abc’
b) abcdef/’
c) abc’def/’egh
d) abcqfghq
Answer: a

Explanation: \’ is used for single quotes that is for representing ‘ .

Related

C# Questions & Answers on Introduction of Reflections for Freshers

1. Which feature enables to obtain information about the use and capabilities of types at runtime? a) Runtime type IDb) Reflectionc) Attributesd) None of the mentioned Answer: b Explanation: Refl...

Linux Bash Shell Questions & Answers on Arithmetic Expressions for Freshers

1. Which built-in command performs integer arithmetic in bash shell? a) letb) getc) setd) none of the mentioned Answer: a 2. Which expression use the value of the enclosed arithmetic expression? ...

C# Question & Answers on Runtime Type for Freshers

1. Which mechanism among the following helps in identifying a type during the execution of a program? a) Reflectionb) Runtime type IDc) Both Reflection & Runtime type IDd) None of the mentioned...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item