C# Questions & Answers – String Class with Description

This set of C# Multiple Choice Questions & Answers (MCQs) focuses on “String Class with Description”.

1. What is the String in C# meant for?
a) Variable
b) Character Array
c) Object
d) Class
View Answer

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

Answer: b
Explanation: 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
View Answer

Answer: d
Explanation: None.
advertisement
advertisement

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
View Answer

Answer: d
Explanation: ‘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
Note: Join free Sanfoundry classes at Telegram or Youtube

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
View Answer

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

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
View Answer

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

advertisement
True 0

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
Explanation: Length() method calculates number of characters in a string. ‘Obj2’ assumes the value of object ‘obj’ in itself.
Output:

        hello world
        11
 
 

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
View Answer

Answer: b
Explanation: IndexOf() used to find absolute position of a character of substring.
Output:

8

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
View Answer

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

world

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
View Answer

Answer: c
Explanation: Replace() method provided by string builder class is used to replace characters.
Output:

hello corn

Sanfoundry Global Education & Learning Series – C# Programming Language.

Here’s the list of Best Books in C# Programming Language.

To practice all areas of C#, here is complete set on 1000+ Multiple Choice Questions and Answers on C#.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

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.