C# Programming Questions and Answers
Which of the following string() method are used to compare two strings with each other?
Creates a new string by copying one string to another.
Which of the following statement is correct about a string in C#.NET?
Answer: Option (B)
Choose Output for the following set of code :
staticvoid Main(string[] args)
{
string s1 ="Hello"+" I "+"Love"+" ComputerScience ";
Console.WriteLine(s1);
Console.ReadLine();
}
Here ‘+’ defined operator works as concatenation for strings.
Output : Hello I Love ComputerScience.
Which of the following statements are correct?
Answer: Option (B)
Which of these operators can be used to concatenate two or more String objects?
string s1 = “Hello”+ ” I ” + “Love” + ” ComputerScience “;
Console.WriteLine(s1);
Hello I Love ComputerScience.
The Method use to remove white space from string?
Perfectly removes a whitespace from string whereas TrimStart() removes a string of characters from the end of the string.
What is output for the following set of code:
staticvoid Main(string[] args)
{
string s1 =" Cshr ";
string s2 = s1.Insert(3 , " a ");
string s3 = s2.Insert(5 , " p ");
for(int i =0;i < s3.Length; i++)
Console.WriteLine(s3[i]);
Console.ReadLine();
}
Insertion of character ‘a’ at postion ‘3’ using insert() which returns a new string with a substring inserted at a specified location.
Output: Csharp
What is output for the following set of code?
staticvoid Main(string[] args)
{
string s1 ="Hello";
string s2 ="hello";
if(s1 == s2)
Console.WriteLine("Equal");
else
Console.WriteLine("Unequal");
if(s1.Equals(s2))
Console.WriteLine("Equal");
else
Console.WriteLine("Unequal");
Console.ReadLine();
}
In first comparison it is being checked either two strings are equal or not but in second comparison it is checked whether two references are equal or not.
Output: Unequal
Unequal
Correct way to find if contents of two strings are equal ?
“==” operator used to compare length of two strings and strcmp() is the inbuilt method derived from string class.