What will be the output for following input from the console as a character? [code] static void Main(string[] args) { Console.WriteLine("

What will be the output for following input from the console as a character?
Code:
 static void Main(string[] args)
 {
     Console.WriteLine("what is your name?");
     char s;
     s = Convert.ToChar(Console.ReadLine());
     Console.WriteLine("how are you: "+s);
     Console.Read();
 }
A. Compile time error
B. Code run successfully prints nothing on console
C. Code runs successfully prints input on console
D. Run time error
 
Answer: d
Explanation: Since only a single character is required to be entered on console when a string is entered, a run time exception is being generated as we had not used Read() which reads single character but used readLine() which reads string and is converted into the char using convert.tochar().
 

Tìm kiếm

Back
Top