MCQs on Working with Strings | C#

Working with strings is fundamental to programming in C#. Understanding string manipulation, string interpolation, StringBuilder, and regular expressions allows for efficient data handling and cleaner code in your applications.


Working with Strings in C# – 30 Multiple Choice Questions

1. String Manipulation

  1. Which of the following is used to concatenate strings in C#?
    • A) + operator
    • B) Concat() method
    • C) Append() method
    • D) Both A and B
  2. What will be the result of the following C# code?csharpCopy codestring str = "CSharp"; str = str.Substring(1, 3); Console.WriteLine(str);
    • A) Shar
    • B) Sharp
    • C) Sha
    • D) Har
  3. How can you remove all white spaces from a string in C#?
    • A) Trim()
    • B) Replace(" ", "")
    • C) Remove()
    • D) Split()
  4. What does the ToUpper() method do in C#?
    • A) Converts a string to lowercase
    • B) Converts a string to uppercase
    • C) Trims spaces from a string
    • D) Returns the string length
  5. Which of the following methods returns the index of the first occurrence of a character in a string?
    • A) IndexOf()
    • B) LastIndexOf()
    • C) Substring()
    • D) FindIndex()
  6. How do you check if a string contains a substring in C#?
    • A) Contains()
    • B) HasSubstring()
    • C) Substring()
    • D) IsSubstring()
  7. What does the Split() method do in C#?
    • A) Splits the string into multiple parts based on a delimiter
    • B) Converts the string into an array of characters
    • C) Joins an array of strings into one string
    • D) Concatenates two strings
  8. Which method is used to check if a string starts with a specific substring?
    • A) StartsWith()
    • B) Contains()
    • C) Substring()
    • D) IndexOf()
  9. What is the purpose of the Trim() method in C#?
    • A) Removes leading and trailing whitespace from a string
    • B) Converts all characters to uppercase
    • C) Replaces whitespace characters with a specified character
    • D) Trims a string to a specific length
  10. What is the return type of the Replace() method in C#?
    • A) void
    • B) int
    • C) string
    • D) char

2. String Interpolation

  1. What is string interpolation in C#?
    • A) A method to create a new string from multiple values
    • B) A process to convert a string into an array
    • C) A way to compare strings
    • D) A method to extract a substring
  2. How do you use string interpolation to include a variable in a string in C#?
    • A) "Hello " + name + "!"
    • B) "Hello {name}!"
    • C) "Hello $name!"
    • D) "Hello ${name}!"
  3. What is the correct syntax to embed expressions inside a string using interpolation in C#?
    • A) $"Hello, {name}!"
    • B) "Hello, {name}"$
    • C) "Hello, (name)"$
    • D) "Hello, [name]!"
  4. Can string interpolation be used for formatting numbers in C#?
    • A) Yes
    • B) No
    • C) Only for decimals
    • D) Only for integers
  5. How do you format a date using string interpolation in C#?
    • A) $"{date:MM/dd/yyyy}"
    • B) "{date:MM/dd/yyyy}"
    • C) "MM/dd/yyyy{date}"
    • D) "{date}"
  6. Which method does string interpolation use under the hood in C#?
    • A) String.Concat()
    • B) String.Format()
    • C) StringBuilder.Append()
    • D) String.Join()
  7. What happens if a variable inside a string interpolation expression is null in C#?
    • A) It results in a compilation error
    • B) It will display null in the output
    • C) The program will crash
    • D) It will be ignored silently
  8. Can you use string interpolation with complex expressions in C#?
    • A) Yes
    • B) No
    • C) Only simple variables
    • D) Only numeric expressions
  9. What is the correct output for the following code in C#?csharpCopy codestring name = "Alice"; Console.WriteLine($"Hello, {name.ToUpper()}!");
    • A) Hello, Alice!
    • B) Hello, ALICE!
    • C) Hello, {name.ToUpper()}!
    • D) Hello, !
  10. What is the difference between string concatenation and string interpolation in C#?
    • A) String concatenation is slower than string interpolation
    • B) String interpolation requires more memory than concatenation
    • C) Both are equally efficient
    • D) String concatenation cannot handle variables

3. StringBuilder Class

  1. What is the primary advantage of using the StringBuilder class over regular strings in C#?
    • A) StringBuilder is faster and more memory efficient when modifying strings
    • B) StringBuilder supports string formatting
    • C) StringBuilder can handle only fixed-length strings
    • D) StringBuilder is only useful for concatenation
  2. How do you append text to an existing StringBuilder object in C#?
    • A) StringBuilder.Append()
    • B) StringBuilder.Add()
    • C) StringBuilder.Insert()
    • D) StringBuilder.Join()
  3. Which method is used to convert a StringBuilder object back into a string in C#?
    • A) ToString()
    • B) Convert()
    • C) Parse()
    • D) GetString()
  4. Can you insert a string at a specific position in a StringBuilder object in C#?
    • A) Yes, using Insert() method
    • B) No, StringBuilder can only append
    • C) Yes, but only for single characters
    • D) Yes, but only at the end of the string
  5. Which of the following is a method of the StringBuilder class?
    • A) AppendLine()
    • B) Add()
    • C) InsertAt()
    • D) AddText()
  6. What will the following C# code do?csharpCopy codeStringBuilder sb = new StringBuilder("Hello"); sb.Append(" World"); Console.WriteLine(sb.ToString());
    • A) Hello World
    • B) HelloWorld
    • C) Hello
    • D) World
  7. Which method is used to remove all characters from a StringBuilder object in C#?
    • A) Clear()
    • B) Delete()
    • C) Remove()
    • D) Trim()
  8. What is the default capacity of a StringBuilder object in C#?
    • A) 16
    • B) 32
    • C) 64
    • D) 128
  9. Which method of StringBuilder is used to replace part of a string with another string in C#?
    • A) Replace()
    • B) Change()
    • C) Swap()
    • D) Modify()
  10. When should you use StringBuilder over regular strings in C#?
    • A) When the string is being modified multiple times
    • B) When the string will be used only once
    • C) When working with large static strings
    • D) When working with null strings

Answer Key

QnoAnswer
1D) Both A and B
2C) Sha
3B) Replace(" ", "")
4B) Converts a string to uppercase
5A) IndexOf()
6A) Contains()
7A) Splits the string into multiple parts based on a delimiter
8A) StartsWith()
9A) Removes leading and trailing whitespace from a string
10C) string
11A) A method to create a new string from multiple values
12D) "Hello ${name}!"
13A) $"Hello, {name}!"
14A) Yes
15A) $"{date:MM/dd/yyyy}"
16B) String.Format()
17B) It will display null in the output
18A) Yes
19B) Hello, ALICE!
20A) String concatenation is slower than string interpolation
21A) StringBuilder is faster and more memory efficient when modifying strings
22A) StringBuilder.Append()
23A) ToString()
24A) Yes, using Insert() method
25A) AppendLine()
26A) Hello World
27A) Clear()
28A) 16
29A) Replace()
30A) When the string is being modified multiple times

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top