This section of our 1000+ C# multiple choice questions focuses on searching and modification of strings in C# Programming Language.
1. Which of these methods of class String is used to separate a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
View Answer
Explanation: None.
2. What will be the output of the following C# code?
static void Main(string[] args)
{
String a = "Ilove";
String b = "CSHARP";
b = string.Concat(a, ' ', b);
Console.WriteLine(b);
Console.ReadLine();
}
a) IloveCSHARP
b) I loveCSHARP
c) Ilove
d) Ilove CSHARP
View Answer
Explanation: Concat() method is used to join two strings without the use of ‘+’ operator.
Output :
Ilove CSHARP
3. Which of these methods of class are used to remove the leading and backward whitespaces?
a) startsWith()
b) trim()
c) Trim()
d) doTrim()
View Answer
Explanation: None.
4. What will be the output of the following C# code?
static void Main(string[] args)
{
String a = "Ilove";
String b = "CSHARP";
b = string.Concat(a,' ',b);
string d = b.TrimStart('I', 'l', 'o', 'H');
Console.WriteLine(d);
Console.ReadLine();
}
a) Ilove CSHARP
b) love CSHARP
c) ve CSHARP
d) ve CSARP
View Answer
Explanation: trimstart() removes character mentioned consecutively in front positions not characters in mentioned in between positions.
Output :
ve CSHARP
5. What will be the output of the following C# code?
static void Main(string[] args)
{
String c = " Hello Computer ";
String a = c.Trim();
Console.WriteLine("\"" + s + "\"");
}
a) ” Hello Computer ”
b) “HelloComputer”
c) “Hello Computer”
d) Hello Computer
View Answer
Explanation: Trim() method is used to remove forward and backward spaces in strings.
Output :
"Hello Computer"
6. What will be the output of the following C# code?
static void Main(string[] args)
{
String c = "Hello";
String a = c + "Bye";
Console.WriteLine(a);
Console.ReadLine();
}
a) “Hello Bye”
b) “HelloBye”
c) Hello Bye
d) HelloBye
View Answer
Explanation: ‘+’ operator method works in the form of concatenate method() and hence is used to join two strings together.
Output :
HelloBye
7. What will be the output of the following C# code?
static void Main(string[] args)
{
String c = "Hello";
String a ;
a = c.Replace('l', 'w');
Console.WriteLine(a);
Console.ReadLine();
}
a) Helloll
b) Hewlo
c) Helwo
d) Hewwo
View Answer
Explanation: None.
Output :
Hewwo
8. Which of the following statements is correct?
a) replace() replace() method replaces last occurrence of a character in invoking strings with another character
b) replace() method replaces only first occurrence of a character in invoking strings with another character
c) replace() method replaces all occurrences of one character in invoking strings with another character
d) none of the mentioned
View Answer
Explanation: By definition.
9. What will be the output of the following C# code snippet?
static void Main(string[] args)
{
String c = "Hello i love you";
String a ;
a = c.Substring(12, 3);
Console.WriteLine(a);
Console.ReadLine();
}
a) ove
b) you
c) yo
d) love you
View Answer
Explanation: None.
Output :
yo
10. Which among the following is the correct way to find out the index of second ‘s’ in the string “She sold her beauty in one night to someone else”?
a)
String a = "She sold her beauty in one night to someone else"; int i; i = a.SecondIndexOf("s");
b)
String a = "She sold her beauty in one night to someone else"; int i, j; i = a.FirstIndexOf("s"); j = a.IndexOf("s", i + 1);
c)
String a = "She sold her beauty in one night to someone else"; int i, j; i = a.IndexOf("s"); j = a.IndexOf("s", i + 1);
d) None of the mentioned
View Answer
Explanation:
static void Main(string[] args) { String c = "She sold her beauty in one night to someone else"; int i,j; i = c.IndexOf("s"); j = c.IndexOf("s", i + 1); Console.WriteLine(i + " " + j); Console.ReadLine(); } Output : 4, 36
Sanfoundry Global Education & Learning Series – C# Programming Language.
To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Computer Science Internship
- Check MCA Books
- Practice Computer Science MCQs
- Check Computer Science Books
- Apply for C# Internship