Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Strings Aptitude Questions and Answers | Set 2
C# Strings Aptitude Questions | Set 2: This section contains aptitude questions and answers on C# Strings.
Submitted by Nidhi, on April 05, 2020
1) What is the correct output of given code snippets?
static void Main(string[] args)
{
String STR1 = "ABC";
String STR2;
String STR3;
STR2 = STR1;
STR3 = String.Copy(STR1);
STR1.Insert(1, "XYZ");
Console.WriteLine(STR1 + " " + STR2 + " "+STR3);
}
- AXYZBC ABC ABC
- ABC ABC ABC
- AXYZBC AXYZBC ABC
- Syntax Error
Correct answer: 2
ABC ABC ABC
The above code will print "ABC ABC ABC" on console screen.
2) What is the correct output of given code snippets?
static void Main(string[] args)
{
String STR1 = "ABC";
String STR2;
String STR3;
STR2 = STR1;
STR3 = String.Copy(STR1);
STR1 = STR1.Insert(1, "XYZ");
Console.WriteLine(STR1 + " " + STR2 + " "+STR3);
}
- AXYZBC ABC ABC
- ABC ABC ABC
- AXYZBC AXYZBC ABC
- Syntax Error
Correct answer: 1
AXYZBC ABC ABC
In the above source code, we have used String.Insert() method of String class. It inserts a string into another string and returns the updated string.
3) If we will access a character from a string that is outside the string size, then which exception will generate?
- ArrayIndexOutOfBoundExcepion
- IndexOutOfRangeException
- OutOfBoundException
- StringException
Correct answer: 2
IndexOutOfRangeException
If we access character outside the range of string, it will generate "IndexOutOfRangeException".
4) What is the correct way to get a character from string in C#.NET?
static void Main(string[] args)
{
String s1 = "ABC";
string s2 = "ABC";
if(s1==s2)
Console.WriteLine("S1 and S2 are equal");
else
Console.WriteLine("S1 and S2 are not equal");
}
- String str="sample"; char ch = str[0];
- String str="sample"; char ch = str.getChar(0);
- String str="sample"; char ch = str.CharAt(0);
- String str="sample"; char ch = str.Char(0);
Correct answer: 1
String str="sample"; char ch = str[0];
In the above code, the first option is correct to get a character from string.
5) What is the correct output of given code snippets?
static void Main(string[] args)
{
String str = "SAMPLE";
char ch = str[6];
Console.WriteLine(ch);
}
- E
- 'E'
- Runtime Exception
- Syntax Error
Correct answer: 3
Runtime Exception
The above code will generate runtime exception (IndexOutOfRangeException).