Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Basic Input/output Aptitude Questions and Answers
This section contains aptitude questions and answers on C# Basic Input/output Aptitude Questions and Answers.
1) What will be the output of following program?
static void Main(string[] args)
{
int a = 10, b = 20;
Console.WriteLine("{0},{0}", a, b);
}
- 10,10
- 10,20
- 20,20
- 20,10
Correct answer: 1
10,10
There are two variables a and b, but while printing the values of a and b, we are using the same placeholder {0}, that will print the value of the first variable. Thus the output is 10,10.
2) What will be the output of following program?
static void Main(string[] args)
{
int a = 10, b = 20;
Console.WriteLine("{0}+{1}", a, b);
}
- 30
- 20+20
- 10+10
- 10+20
Correct answer: 4
10+20
In the statement Console.WriteLine("{0}+{1}", a, b); {0} is the placeholder for variable a and {1} is the placeholder for variable b, {0}+{1} will not perform any operation, values of a and b will be printed at the place of {0} and {1}. Thus, the output will be 10+20.
3) What will be the output of following program?
static void Main(string[] args)
{
Console.WriteLine(Console.Write("Hello"));
}
- Error
- 6Hello
- Hello6
- Hello5
Correct answer: 1
Error
Console.Write() and Console.WriteLine() methods do not return any value, their return type is void. Thus, the statement will not be executed and an error "cannot convert void to bool" will return.
4) What will be the output of following program?
static void Main(string[] args)
{
int a = 10, b = 10;
Console.WriteLine(a == b);
}
- 1
- a==b
- True
- true
Correct answer: 3
True
a==b is a Boolean expression, values of a and b are the same. Thus, the output will be True.
5) What will be the output of the following program, if the input is "Hello world!"?
static void Main(string[] args)
{
string text = "";
text = Console.ReadLine();
Console.WriteLine(text);
}
- Hello
- Hello world!
- Helloworld!
- None
Correct answer: 2
Hello world!
Here, text is a string variable, and Console.ReadLine() method is used to read the string with spaces. Thus, there is no issue in this code and output will be "Hello world!".