Java Questions & Answers – Writing Console Output

This section of our 1000+ Java MCQs focuses 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: d
Explanation: None.

3. Which of these classes are used by character streams 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.
advertisement
advertisement

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

Answer: c
Explanation: None.

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
  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
View Answer

Answer: a
Explanation: 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:

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

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

advertisement
  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
View Answer
Answer: a
Explanation: 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
View Answer

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

$ javac output.java
$ java output
olleH

Sanfoundry Global Education & Learning Series – Java Programming Language.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.