This set of C# Questions and Answers for Campus interviews 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
Answer: d
Clarification: 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
Answer: b
Clarification: 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
Answer: c
Clarification: 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.
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
Answer: b
Clarification: 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()
Answer: c
Clarification: 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
Answer: c
Clarification: 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
Answer: c
Clarification: Read() returns –1 on error. This method also throws an IOException on failure.
8. Choose the object of TextReader class.
a) Console.In
b) Console.Out
c) Console.Error
d) None of the mentioned
Answer: a
Clarification: 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
Answer: c
Clarification: Console.Out and Console.Error are objects of type TextWriter class.
10. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{ -
int a = 10, b = 0;
-
int result;
-
Console.Out.WriteLine("This will generate an exception.");
-
try -
{ -
result = a / b; // generate an exception
-
} -
catch (DivideByZeroException exc)
-
{ -
Console.Error.WriteLine(exc.Message);
-
} -
Console.ReadLine();
-
}
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
Clarification: None.
11. Choose the methods provided by Console.Out and Console.Error?
a) Write
b) WriteLine
c) WriteKey
d) Write & WriteLine
Answer: d
Clarification: None.
12. What will be the output of the following C# code?
-
static void Main(string[] args)
-
{ -
Console.WriteLine("This is a Console Application:");
-
Console.Write("Please enter your lucky number:");
-
string val1 = Console.ReadLine();
-
int val2 = System.Convert.ToInt32(val1, 10);
-
val2 = val2 * val2;
-
Console.WriteLine("square of number is:" +val2);
-
Console.Read();
-
}
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
Answer: c
Clarification: None.
Output : This is a Console Application:
Please enter your lucky number: 3
Square of number is : 9
C# for Campus Interviews, .
