Home »
Aptitude Questions and Answers »
C# Aptitude Questions and Answers
C# Strings Aptitude Questions and Answers | Set 1
C# Strings Aptitude Questions | Set 1: This section contains aptitude questions and answers on C# Strings.
Submitted by Nidhi, on April 05, 2020
1) There are following statements are given below, which of them are correct about "String" in C#.NET?
- In C#.NET, we used the character array to represent a string just like C language.
- In C#.NET, String is mutable.
- A String is a predefined class in C#, which is used to create strings.
- We need to use the System.Array class to create a string.
Options:
- Only A
- Only D
- A and D
- Only C
Correct answer: 4
Only C
In the given statements, the C statement is correct about "String" in C#.
2) In C#, String is a value type?
- Yes
- No
Correct answer: 2
No
In C#.NET, String is a reference type.
3) If we have two references for strings like STR1 and STR2. And we want to compare them, and then what is the correct way?
- STR1 is STR2
- STR1 = STR2
- STR1 equal to STR2
- STR1.Equals(STR2)
Correct answer: 4
STR1.Equals(STR2)
In C#.NET, Equals() method is used to compare two string.
4) What is the correct output of given code snippets?
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");
}
- S1 and S2 are equal
- S1 and S2 are not equal
- Syntax Error
- Runtime Exception
Correct answer: 1
S1 and S2 are equal
The above code will print "S1 and S2 are equal" on the console screen.
Only B is the correct statement about given code snippets.
5) 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);
Console.WriteLine(STR1 + " " + STR2 + " "+STR3);
}
- ABC ABC
- ABC ABC ABC
- Syntax Error
- Runtime Exception
Correct answer: 2
ABC ABC ABC
In the above code, we are coping string, STR1 to STR2 and STR3, but copy string to another, Copy() method is more reliable compared to assignment.
Advertisement
Advertisement