C# Questions & Answers on Introduction of String Formatting for Freshers

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
Answer: c

Explanation: Generally, a string is defined as a sequence of characters but it is different in C#. In c++, string is an array of characters.In case of C#, strings are objects of the built-in string data type. Thus, 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
Answer: a
3. Select the interfaces defined by the string class?

a) IComparable
b) IComparable
c) ICloneable
d) All of the mentioned
Answer: d
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
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) >=
Answer: a
6. What does the given 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 b="" br="" else="" for="">d) All of the mentioned
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.
7. Select the output for given set of code:
  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
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) ||
Answer: a

Explanation: string s1 = “Hello”+ ” I ” + “Love” + ” ComputerScience “;
Console.WriteLine(s1);
Output :Hello I Love ComputerScience.
9. What does the given 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
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
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
Answer: c

Explanation: String is an alias for the predefined “System.string” class from which most of the string() methods are derived.
12. Method used to remove white space from string?

a) Split()
b) Substring()
c) Trim()
d) TrimStart()
Answer: c

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

Related

Multiple Choice Questions 5395489546835978010

Post a Comment

emo-but-icon

item