C# Strings MCQ

1. How do you concatenate strings in C#?

a) Using the '+' operator
b) Using the '&' operator
c) Using the concat() method
d) Using the append() method

Answer:

a) Using the '+' operator

Explanation:

In C#, strings are commonly concatenated using the '+' operator.

2. What method is used to compare two strings for equality in C#?

a) equals()
b) isEqual()
c) CompareTo()
d) Compare()

Answer:

a) equals()

Explanation:

The equals() method is used to compare two strings for equality in C#.

3. Which method in the String class returns the length of a string in C#?

a) Length
b) Count
c) Size
d) GetLength

Answer:

a) Length

Explanation:

The Length property of the String class is used to get the length of a string in C#.

4. How can you convert a string to uppercase in C#?

a) toUpperCase()
b) ToUpper()
c) Uppercase()
d) MakeUpper()

Answer:

b) ToUpper()

Explanation:

The ToUpper() method is used to convert a string to uppercase in C#.

5. What is the output of the following code in C#?

a) Hello,
b) World!
c) ello, World!
d) , World!

Answer:

b) World!

Explanation:

The Substring(7) method returns a new string starting from the 7th index of the original string.

6. What is the character used to denote a verbatim string in C#?

a) @
b) #
c) $
d) %

Answer:

a) @

Explanation:

In C#, the '@' character is used before a string literal to denote a verbatim string.

7. How do you split a string on specific characters in C#?

a) Using the Split() method
b) Using the Divide() method
c) Using the Break() method
d) Using the Cut() method

Answer:

a) Using the Split() method

Explanation:

The Split() method is used to split a string into an array of substrings based on specific characters.

8. What does the Trim() method do in C#?

a) Removes all whitespace
b) Removes whitespace from the start and end of a string
c) Trims a string to a specified length
d) Removes specified characters from a string

Answer:

b) Removes whitespace from the start and end of a string

Explanation:

The Trim() method is used to remove all leading and trailing white-space characters from a string.

9. How do you determine if a string starts with a specific substring in C#?

a) StartsWith()
b) BeginsWith()
c) HasPrefix()
d) LeadingString()

Answer:

a) StartsWith()

Explanation:

The StartsWith() method is used to check if a string starts with a specific substring.

10. What is the result of string concatenation using the following code in C#?

a) HelloWorld
b) Hello World
c) Hello+World
d) Hello_World

Answer:

b) Hello World

Explanation:

The '+' operator is used to concatenate strings with a space in between.

11. How do you find the position of a substring within a string in C#?

a) Index()
b) FindIndex()
c) Locate()
d) IndexOf()

Answer:

d) IndexOf()

Explanation:

The IndexOf() method is used to find the position of a substring within a string.

12. Which method is used to replace a specified substring with another substring in a string in C#?

a) Change()
b) Switch()
c) Replace()
d) Modify()

Answer:

c) Replace()

Explanation:

The Replace() method is used to replace all occurrences of a specified substring with another substring in a string.

13. What is the output of the following code in C#?

a) S
b) h
c) a
d) C

Answer:

b) h

Explanation:

String indexing in C# is zero-based, so text[2] refers to the third character, which is 'h'.

14. How do you convert a string to a number in C#?

a) int.Parse()
b) Convert.ToInt32()
c) StringToNumber()
d) Both a and b

Answer:

d) Both a and b

Explanation:

Both int.Parse() and Convert.ToInt32() methods can be used to convert a string to a number.

15. What does the IsNullOrEmpty method do in C#?

a) Checks if a string is empty or consists of only whitespace characters
b) Checks if a string is null or has a length of zero
c) Checks if a string is null or consists of only whitespace characters
d) Checks if a string is null or empty array

Answer:

b) Checks if a string is null or has a length of zero

Explanation:

The IsNullOrEmpty method checks whether a string is null or its length is zero.

16. How do you format a string in C# using placeholders?

a) Using the Format() method
b) Using string interpolation
c) Using the Concat() method
d) Both a and b

Answer:

d) Both a and b

Explanation:

Both the String.Format() method and string interpolation can be used to format strings using placeholders in C#.

17. What is the escape sequence used for a new line in C#?

a) \\n
b) \n
c) /n
d) //n

Answer:

b) \n

Explanation:

The '\n' escape sequence is used to represent a new line in C#.

18. How can you check if a string contains a specific substring in C#?

a) Contains()
b) Has()
c) Includes()
d) Find()

Answer:

a) Contains()

Explanation:

The Contains() method is used to check if a string contains a specific substring.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top