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
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
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
Explanation: None.
4. What will be the output of the following C# code?
static void Main(string[] args)
{
string s1 = "Hello I Love Csharp ";
Console.WriteLine(Convert.ToChar( (s1.IndexOf('I') - s1.IndexOf('l')) * s1.IndexOf('p'));
Console.ReadLine();
}
a) I
b) Hello I
c) Love
d) H
View Answer
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
View Answer
Explanation: 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?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
Console.WriteLine (obj.Equals(obj2) + " " + obj2.CompareTo(obj) );
Console.ReadLine();
}
a) True True
b) False False
c) True 0
d) False 1
View Answer
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 will be the output of the following C# code?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
Console.WriteLine(obj + " " + obj1);
string s = obj + " " + obj1;
Console.WriteLine(s.Length);
Console.ReadLine();
}
a)
hello world 10
b)
hello world 6
c)
hello world 11
d)
hello world 5View Answer
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?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
string s = obj+" "+obj1;
Console.WriteLine(s.IndexOf('r'));
Console.ReadLine();
}
a) 7
b) 8
c) 9
d) 10
View Answer
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?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "world";
String obj2 = obj;
string s = obj + " " + obj1;
Console.WriteLine(s.Substring(6 ,5));
Console.ReadLine();
}
a) hello
b) orld
c) world
d) o world
View Answer
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?
static void Main(string[] args)
{
String obj = "hello";
String obj1 = "worn";
String obj2 = obj;
Console.WriteLine(obj + " " + (obj1.Replace('w' ,'c')));
Console.ReadLine();
}
a) hello hello
b) hello worn
c) hello corn
d) hello
View Answer
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#.
- Practice MCA MCQs
- Apply for C# Internship
- Check Computer Science Books
- Apply for Computer Science Internship
- Practice Computer Science MCQs