1) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str1 = "ABC";
String str2 = "ABC";
if (str1.CompareTo(str2)==0)
Console.WriteLine("Both are equal");
else
Console.WriteLine("Both are not equal");
}
- Both are equal
- Both are not equal
- Runtime Exception
- Syntax Error
Correct answer: 1
Both are equal
The above code will print "Both are equal" on the console screen.
2) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str = "This is a string";
int i1 = 0;
int i2 = 0;
i1 = str.IndexOf('s');
i2 = str.IndexOf('s', i1 + 1);
Console.WriteLine(i1 + " " + i2);
}
- 3 6
- Syntax Error
- Runtime Exception
- 3 3
Correct answer: 1
3 6
The above code will print "3 6" on the console screen.
4) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str1 = "Hello ";
String str2 = "World!";
String str3;
str3 = str1.Concat(str2);
Console.WriteLine(str3);
}
- Hello World!
- HelloWorld!
- Runtime Exception
- Syntax Error
Correct answer: 4
Syntax Error
The above code will generate syntax error, because String class does not have Concat() method.
5) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str1 = "Hello ";
String str2 = "World!";
String str3;
str3 = str1+str2;
Console.WriteLine(str3);
}
- Hello World!
- HelloWorld!
- Runtime Exception
- Syntax Error
Correct answer: 1
Hello World!
In C#.NET, + operator is used to concatenate two strings.