Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Strings Aptitude Questions and Answers | Set 3
C# Strings Aptitude Questions | Set 3: This section contains aptitude questions and answers on C# Strings.
Submitted by Nidhi, on April 05, 2020
1) Can we create a string object without a new operator?
- Yes
- No
Correct answer: 1
Yes
Yes, we can create an object of string without using the new operator.
2) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str = "sample";
Console.Write(str.IndexOf('p'));
Console.Write(str.Length);
}
- 46
- 36
- 37
- 47
Correct answer: 2
36
The Index of string starts from 0, and then the index of 'p' will be 3, and length of the string will be 6. Thus, the output will be "36".
3) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str = "sample sample";
Console.Write(str.IndexOf('p'));
Console.Write(str.Length);
}
- 313
- 312
- 1012
- 1013
Correct answer: 1
313
The Index of string starts from 0, and then the index of 'p' will be 3 (String.IndexOf() method returns the index of first occurrence of character), and length of the string will be 13. Thus, the output will be "313".
4) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str = "sample sample";
Console.Write(str.LastIndexOf('p'));
Console.Write(str.Length);
}
- 313
- 312
- 1012
- 1013
Correct answer: 4
1013
The Index of string starts from 0, and then the index of 'p' will be 10 (String.LastIndexOf() method returns the index of first occurrence of character), and length of the string will be 13. Thus, the output will be "1013".
5) What is the correct output of given code snippets?
static void Main(string[] args)
static void Main(string[] args)
{
String str1 = "ABC";
String str2 = "ABC";
if (str1.CompareTo(str2))
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: 4
Syntax Error
The above code generates syntax error, because of String.CompareTo() method returns an integer value, whereas if condition required boolean value.