C# Programming Questions and Answers Part-16

1. What will be the output of the following C# code?
static void 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();
}
a) Cshar
b) CsharP
c) Csharp
d) Cshrap

Answer: c
Explanation: Insertion of character ‘a’ at position ‘3’ using insert() which returns a new string with a substring inserted at a specified location.
Output:
Csharp

2. Which of the following statement is correct about a string in C#.NET?
a) The System.Array class is used to represent a string
b) A string has a zero-based index
c) A number cannot be represented in the form of a string
d) A string is mutable because it can be modified once it has been created

Answer: b
Explanation: A string has a zero-based index

3. What will be the output of the following C# code?
static void 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();
}
a) Equal
Unequal
b) Unequal
Equal
c) Equal
Equal
d) Unequal
Unequal

Answer: d
Explanation: 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

4. What will be the output of the following C# code?
static void Main(string[] args)
{
string s1 = "Hello" + " I " + "Love" + " ComputerScience ";
Console.WriteLine(s1);
Console.ReadLine();
}
a) HelloILoveComputerScience
b) Hello I Love ComputerScience
c) Compile time error
d) Hello

Answer: b
Explanation: Here ‘+’ defined operator works as concatenation for strings.
Output :
Hello I Love ComputerScience

5. Correct way to find if contents of two strings are equal?
a) if (s1 = s2)
b) if (s1 != s2)
c) if (strcmp (s1 ,s2))
d) if ( s1 is s2)

Answer: c
Explanation: “==” operator used to compare length of two strings and strcmp() is the inbuilt method derived from string class.

6. Which of the following statements are correct?
a) String is value type
b) String literals can contain any character literal including escape sequences
c) The equality operators are defined to compare values of string objects as well as references
d) All of the mentioned

Answer: b
Explanation: String literals can contain any character literal including escape sequences

7. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||

Answer: a
Explanation:
string s1 = "Hello"+ " I " + "Love" + " ComputerScience ";
Console.WriteLine(s1);
Hello I Love ComputerScience.

8. The Method use to remove white space from a string?
a) Split()
b) Substring()
c) Trim()
d) TrimStart()

Answer: c
Explanation: Perfectly removes whitespace from string whereas TrimStart() removes a string of characters from the end of the string.

9. What is the String in C# meant for?
a) Variable
b) Character Array
c) Object
d) Class

Answer: c
Explanation: C# Supports a predefined reference type known as string. When we declare a string using string type we are declaring the object to be of type “System.String”.

10. What does the term ‘immutable’ means in term of string objects?
a) We can modify characters included in the string
b) We cannot modify characters contained in the string
c) We cannot perform various operation of comparison, inserting, appending etc
d) None of the mentioned

Answer: b
Explanation: String objects are ‘immutable’ means we cannot modify the characters contained in string. Also operation on string produce a modified version of string rather then modifying characters of string.