C# Questions & Answers – Introduction of Console I/O Operations

This set of C# Questions and Answers focuses on “Introduction of Console I/O operations”.

1. Which of the classes provide the operation of reading from and writing to the console in C#.NET?
a) System.Array
b) System.Output
c) System.ReadLine
d) System.Console
View Answer

Answer: d
Explanation: The method for reading and writing to the console in C#.NET is provided by System.Console class. This class gives us access to the standard input, output and standard error streams.

2. Which of the given stream methods provide access to the output console by default in C#.NET?
a) Console.In
b) Console.Out
c) Console.Error
d) All of the mentioned
View Answer

Answer: b
Explanation: The standard output stream Console.Out sends output to the screen by default.

3. Which of the given stream methods provide access to the input console in C#.NET?
a) Console.Out
b) Console.Error
c) Console.In
d) All of the mentioned
View Answer

Answer: c
Explanation: Console.In is an instance of TextReader, and we can use the methods and properties defined by TextReader to access it to read the input from the keyboard.
advertisement
advertisement

4. The number of input methods defined by the stream method Console.In in C#.NET is?
a) 4
b) 3
c) 2
d) 1
View Answer

Answer: b
Explanation: Two basic methods : read() and readline() and third method readkey() introduced in .NET FrameWork 2.0.

5. Select the correct methodS provided by Console.In?
a) Read(), ReadLine()
b) ReadKey(), ReadLine()
c) Read(), ReadLine(), ReadKey()
d) ReadKey(), ReadLine()
View Answer

Answer: c
Explanation: The two method Read() and ReadLine() available in .NET Framework 1.0 and Third method ReadKey() was added by .NET Framework 2.0.

6. Choose the output returned when read() reads the character from the console?
a) String
b) Char
c) Integer
d) Boolean
View Answer

Answer: c
Explanation: Read() returns the character read from the console. It returns the result. The character is returned as an int, which should be cast to char.

7. Choose the output returned when an error condition is generated while read() reads from the console.
a) False
b) 0
c) -1
d) All of the mentioned
View Answer

Answer: c
Explanation: Read() returns –1 on error. This method also throws an IOException on failure.
advertisement

8. Choose the object of TextReader class.
a) Console.In
b) Console.Out
c) Console.Error
d) None of the mentioned
View Answer

Answer: a
Explanation: Console.In is an instance(object) of TextReader class and we can use the methods and properties defined by TextReader to invoke the object console.in.

9. Choose the object/objects defined by the Textwriter class.
a) Console.In
b) Console
c) Console.Error
d) None of the mentioned
View Answer

Answer: c
Explanation: Console.Out and Console.Error are objects of type TextWriter class.
advertisement

10. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      int a = 10, b = 0;
  4.      int result;
  5.      Console.Out.WriteLine("This will generate an exception.");
  6.      try
  7.      {
  8.          result = a / b; // generate an exception
  9.      }
  10.      catch (DivideByZeroException exc)
  11.      {
  12.          Console.Error.WriteLine(exc.Message);
  13.      }
  14.      Console.ReadLine();
  15.  }

a) This will generate an exception
b) 0
c) Compile time error
d)

This will generate an exception
Attempted to Divide by Zero
View Answer
Answer: d
Explanation: None.

11. Choose the methods provided by Console.Out and Console.Error?
a) Write
b) WriteLine
c) WriteKey
d) Write & WriteLine
View Answer

Answer: d
Explanation: None.

12. What will be the output of the following C# code?

  1.  static void Main(string[] args)
  2.  {
  3.      Console.WriteLine("This is a Console Application:");
  4.      Console.Write("Please enter your lucky number:");
  5.      string val1 = Console.ReadLine();
  6.      int val2 = System.Convert.ToInt32(val1, 10);
  7.      val2 = val2 * val2;
  8.      Console.WriteLine("square of number is:" +val2);
  9.      Console.Read();
  10.  }

a) Compile time error
b) Runs successfully does not print anything
c) Runs successfully, ask for input and hence displays the result
d) Syntax Error
View Answer

Answer: c
Explanation: None.
Output : This is a Console Application:
Please enter your lucky number: 3
Square of number is : 9

Sanfoundry Global Education & Learning Series – C# Programming Language.

Here’s the list of Best Books in C# Programming Language.

To practice all areas of C#, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.

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.