C# Multiple Choice Questions – String Formatting

This section of our 1000+ C# MCQs focuses on basics of string formatting in C# Programming Language.

1. What are strings in C#?
a) a sequence of characters
b) array of characters
c) objects of built-in data type
d) a reference type
View Answer

Answer: c
Explanation: Generally, a string is defined as a sequence of characters but it is different in C#. In c++, the string is an array of characters. In case of C#, strings are objects of the built-in string data type. Thus, a string is a reference type.

2. Select the namespace in which string class is built?
a) System.Text
b) System.Net
c) System.IO
d) None of the mentioned
View Answer

Answer: a
Explanation: None.

3. Select the interfaces defined by the string class?
a) IComparable
b) IComparable<string>
c) ICloneable
d) All of the mentioned
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

4. Choose the constructor type used to build strings from character array.
a) public String(value)
b) public String(char[ ] value, int startIndex, int length)
c) public String(char[ ])
d) all of the mentioned
View Answer

Answer: b
Explanation: public String(char[ ] value) – This form of constructor constructs a string that contains characters in value
public String(char[ ] value, int startIndex, int length) -The second form uses length characters from value, beginning at the index specified by startIndex.

5. Select the operators used for checking the equality in strings:
a) !=
b) >
c) <
d) >=
View Answer

Answer: a
Explanation: None.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

6. What does the following C# code set specifies?

  1. public static int Compare(string strA, string strB)

a) Comparison is case and culture sensitive
b) Two strings A and B are compared with each other
c) Output is : >0 for (A > B), <0 for (A < B) else ‘0’ for(A=B)
d) All of the mentioned
View Answer

Answer: d
Explanation: Compares the string referred to by strA with strB. Returns greater than zero if strA is greater than strB, less than zero if strA is less than strB, and zero if strA and strB are equal. The comparison is case and culture-sensitive.
advertisement

7. What will be the output of the following C# code snippet?

advertisement
  1. static void Main(string[] args)
  2. {
  3.     string s1 = "Hello" + "c" + "Sharp";
  4.     Console.WriteLine(s1);
  5.     Console.ReadLine();
  6. }

a) Hello c Sharp
b) HellocSharp
c) Compile time error
d) Hello
View Answer

Answer: a
Explanation: Here ‘+’ operator works as concatenation for strings.
Output :

Hello c Sharp

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

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

Hello I Love ComputerScience.

9. What does the following C# code set specify?

  1. public static int Compare(string strA, int indexA, string strB, int indexB, int length, bool ignoreCase)

a) Comparison begins at strA[indexA] and strB[indexB] and runs for length of characters
b) Returns output > 0 for for strA > strB else < 0 for strA < strB else if strA = str B output is 0
c) Comparison is culture sensitive and if ignore case is true, comparison ignores case differences
d) All of the mentioned
View Answer

Answer: d
Explanation: Compares portions of the strings referred to by strA and strB. The comparison begins at strA[indexA] and strB[indexB] and runs for length characters. Returns greater than zero if strA is greater than strB, less than zero if strA is less than strB, and zero if strA and strB are equal. If ignoreCase is true, the comparison ignores case differences. Otherwise, case differences matter. The comparison is culture-sensitive.

10. Which string operation does the below-mentioned method define?

  1. public static string Concat(string str0, string str1)

a) method returns a string
b) string str1 is concatenated to the end of str0
c) can be used to concatenate any number of strings
d) all of the mentioned
View Answer

Answer: d
Explanation: This method returns a string that contains str1 concatenated to the end of str0. Another form of Concat(), shown here, concatenates three strings:
public static string Concat(string str0, string str1, string str2). Hence, any number of strings can be concatenated using this method.

11. Choose the base class for string() method.
a) System.Array
b) System.char
c) System.String
d) None of the mentioned
View Answer

Answer: c
Explanation: String is an alias for the predefined “System.string” class from which most of the string() methods are derived.

12. Did method use to remove whitespace from the string?
a) Split()
b) Substring()
c) Trim()
d) TrimStart()
View Answer

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

More MCQs on C# String Formatting:

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.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.