250+ TOP MCQs on String Class with Description and Answers

This set of C# online quiz on “String Class with Description”.

1. What is the String in C# meant for?
a) Variable
b) Character Array
c) Object
d) Class
Answer: c
Clarification: C# Supports a predefined reference type known as string. When we declare a string using string type we are declaring the object to be of type “System.String”.

2. What does the term ‘immutable’ means in term of string objects?
a) We can modify characters included in the string
b) We cannot modify characters contained in the string
c) We cannot perform various operation of comparison, inserting, appending etc
d) None of the mentioned
Answer: b
Clarification: String objects are ‘immutable’ means we cannot modify the characters contained in string. Also operation on string produce a modified version of string rather then modifying characters of string.

3. To perform comparison operation on strings supported operations are ____________
a) Compare()
b) Equals()
c) Assignment ‘==’ operator
d) All of the mentioned
Answer: d
Clarification: None.

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

  1.  static void Main(string[] args)
  2.  {
  3.      string s1 = "Hello I Love Csharp ";
  4.      Console.WriteLine(Convert.ToChar( (s1.IndexOf('I') - s1.IndexOf('l')) * s1.IndexOf('p'));
  5.      Console.ReadLine();
  6.  }

a) I
b) Hello I
c) Love
d) H
Answer: d
Clarification: ‘I’ = index position[6], ‘l’ = index position[2]. So, I – l = 6-2 = 4*(index position of p = 18) = 72. Character with ASCII Value 72 = ‘H’.
Output : H

5. Correct way to convert a string to uppercase using string class method()?
a) Upper()
b) ToUpper()
c) Object.ToUpper()
d) None of the mentioned

Answer: c
Clarification: string s1 = “Hello I Love Csharp “;
Console.WriteLine(s1.ToUpper());
Output: HELLO I LOVE CSHARP.

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

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      Console.WriteLine (obj.Equals(obj2) + "  " + obj2.CompareTo(obj) );
  7.      Console.ReadLine();
  8.  }

a) True True
b) False False
c) True 0
d) False 1
Answer: c
Clarification: Equal() checks if two string objects ‘obj’ and ‘obj2’ are equal or not and hence returns true or false. Similarly, “CompareTo” operator check two objects and since string obj2 = obj, it returns bool value ‘0’. Hence, they are equal.
Output :

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

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      Console.WriteLine(obj + "  " + obj1);
  7.      string s = obj + "  " + obj1;
  8.      Console.WriteLine(s.Length);
  9.      Console.ReadLine();
  10.  }

a)

hello world
10

b)

hello world
6

c)

hello world
11

d)

hello world
5

View Answer

Answer: c
Clarification: Length() method calculates number of characters in a string. ‘Obj2’ assumes the value of object ‘obj’ in itself.
Output:

 
 

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

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      string s = obj+" "+obj1;
  7.      Console.WriteLine(s.IndexOf('r'));
  8.      Console.ReadLine();
  9.  }

a) 7
b) 8
c) 9
d) 10
Answer: b
Clarification: IndexOf() used to find absolute position of a character of substring.
Output:

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

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "world";   
  5.      String obj2 = obj;
  6.      string s = obj + "  " + obj1;
  7.      Console.WriteLine(s.Substring(6 ,5));
  8.      Console.ReadLine();
  9.  }

a) hello
b) orld
c) world
d) o world

Answer: c
Clarification: ‘Substring()’ extract substrings from a given string using overloaded substring() method provided by string class.
Output:

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

  1.  static void Main(string[] args)
  2.  {
  3.      String obj = "hello";
  4.      String obj1 = "worn";   
  5.      String obj2 = obj;
  6.      Console.WriteLine(obj + "  " + (obj1.Replace('w' ,'c')));
  7.      Console.ReadLine();
  8.  }

a) hello hello
b) hello worn
c) hello corn
d) hello
Answer: c
Clarification: Replace() method provided by string builder class is used to replace characters.
Output:

To practice all areas of C# for online Quizzes,

Leave a Comment

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

Scroll to Top