250+ TOP MCQs on Writing Console Output and Answers

Java MCQs on writing console outputs in Java Programming Language.

1. Which of these class contains the methods print() & println()?
a) System
b) System.out
c) BUfferedOutputStream
d) PrintStream

Answer: d
Clarification: print() and println() are defined under the class PrintStream, System.out is the byte stream used by these methods .

2. Which of these methods can be used to writing console output?
a) print()
b) println()
c) write()
d) all of the mentioned

Answer: d
Clarification: None.

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

Answer: b
Clarification: Character streams uses Writer and Reader classes for input & output operations.

4. Which of these class is used to read from a file?
a) InputStream
b) BufferedInputStream
c) FileInputStream
d) BufferedFileInputStream

Answer: c
Clarification: None.

5. What will be the output of the following Java program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             String a="hello i love java";
  6.             System.out.println(indexof('i')+" "+indexof('o')+" "+lastIndexof('i')+" "+lastIndexof('o') ));
  7.         }
  8.     }

a) 6 4 6 9
b) 5 4 5 9
c) 7 8 8 9
d) 4 3 6 9

Answer: a
Clarification: indexof(ā€˜cā€™) and lastIndexof(ā€˜cā€™) are pre defined function which are used to get the index of first and last occurrence of
the character pointed by c in the given array.
Output:

$ javac output.java
$ java output
6 4 6 9

6. What will be the output of the following Java program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             char c[]={'a','1','b',' ','A','0'];
  6.             for (int i = 0; i < 5; ++i)
  7.             {
  8. 	        if(Character.isDigit(c[i]))
  9.                     System.out.println(c[i]" is a digit");
  10.                 if(Character.isWhitespace(c[i]))
  11.                    System.out.println(c[i]" is a Whitespace character");
  12.                 if(Character.isUpperCase(c[i]))
  13.                    System.out.println(c[i]" is an Upper case Letter");
  14.                 if(Character.isUpperCase(c[i]))
  15.                    System.out.println(c[i]" is a lower case Letter");
  16.                 i = i + 3;
  17.             }
  18.         }
  19.     }

a)

   a is a lower case Letter
     is White space character

b)

   b is a lower case Letter
     is White space characte

c)

   a is a lower case Letter
   A is a upper case Letter

d)

   a is a lower case Letter
   0 is a digit

Answer: a
Clarification: Character.isDigit(c[i]),Character.isUpperCase(c[i]),Character.isWhitespace(c[i]) are the function of library java.lang
they are used to find weather the given character is of specified type or not. They return true or false i:e Boolean variable.
Output:

$ javac output.java
$ java output  
a is a lower case Letter
  is White space character

7. What will be the output of the following Java program?

  1.     class output
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.            StringBuffer s1 = new StringBuffer("Hello");
  6.            StringBuffer s2 = s1.reverse();
  7.            System.out.println(s2);
  8.         }
  9.     }

a) Hello
b) olleH
c) HelloolleH
d) olleHHello

Answer: b
Clarification: reverse() method reverses all characters. It returns the reversed object on which it was called.
Output:

$ javac output.java
$ java output
olleH

Leave a Reply

Your email address will not be published. Required fields are marked *