C# Questions & Answers on String Class with Description for Freshers

1. What is the String in C# meant for?

a) Variable
b) Character Array
c) Object
d) Class
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
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
Answer: d
4. What will be output of the following set of 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

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

Explanation: string s1 = “Hello I Love Csharp “;
Console.WriteLine(s1.ToUpper());
Output: HELLO I LOVE CSHARP.
6. What would be the output for the following set of 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

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 : True 0
7. What would be the output for the code below?
  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
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 is output for the following set of 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

Explanation: IndexOf() used to find absolute position of a character of substring.
Output: 8
9. What is output for the set of 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

Explanation: ‘Substring()’ extract substrings from a given string using overloaded substring() method provided by string class.
Output: world
10. What will be the output for the set of given 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

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

Related

Multiple Choice Questions 1123773066718339144

Post a Comment

emo-but-icon

item