2015-02-11



Strings in C# | String Formatting | String Operations

These are the MCQs & Code Problems related to Strings in C#. These contains questions about String theoretical questions, String Operations & String Operations. These practice questions focuses on Strings in C Sharp Programming Language.

String Operations

1. Which of the following method are used to compare two strings with each other?

a) CopyTo()

b) Compare(stringA, StringB)

c) Copy()

d) CompareTo()

Click to show answer

Answer: b
Explanation: Compares two specified String objects and returns an integer that indicates their relative position in the sort order.

2. Choose the base class for String methods :

a) System.Array

b) System.char

c) System.String

d) None of the mentioned

Click to show answer

Answer: c
Explanation: String is an alias for the predefined “System.String” class from which most of the string() methods are derived.

Arithmetic Operators Questions In C#
www.csharpens.com/questions-arithmetic-operators-in-c-sharp

3. What is output of for following set of code:

static void Main(string[] args)

{

string s1 = "Cshr";

string s2 = s1.Insert(3 , "a");

string s3 = s2.Insert(5 , "p");

for (int i = 0;i < s3.Length; i++)

Console.Write(s3[i]);

Console.ReadLine();

}

a) Cshar

b) CsharP

c) Csharp

d) Cshrap

Click to show answer

Answer: c
Explanation: Insertion of character ‘a’ at postion ’3′ using insert() which returns a new string with a substring inserted at a specified location.
Output: Csharp

4. Which of the following statement is correct about a string in C#.NET?

a) The System.Array class is used to represent a string

b) A string has a zero-based index

c) A number cannot be represented in the form of a string

d) A string is mutable because it can be modified once it has been created

Click to show answer

Answer: b
Explanation: None.

5. What is output of for following set of code?

static void Main(string[] args)

{

string s1 = "Hello";

string s2 = "hello";

if (s1 == s2)

Console.WriteLine("Equal");

else

Console.WriteLine("Unequal");

if (s1.Equals (s2))

Console.WriteLine("Equal");

else

Console.WriteLine("Unequal");

Console.ReadLine();

}

a) Equal

Unequal

b) Unequal

Equal

c) Equal

Equal

d) Unequal

Unequal

Click to show answer

Answer: d
Explanation:In first comparison it is being checked either two strings are equal or not but in second comparison it is checked whether two references are equal or not.
Output: Unequal
Unequal

6. Choose Output for following set of code :

static void Main(string[] args)

{

string s1 = "Hello" + " I " + "Love" + " ComputerScience ";

Console.WriteLine(s1);

Console.ReadLine();

}

a) HelloILoveComputerScience

b) Hello I Love ComputerScience

c) Compile time error

d) Hello

Click to show answer

Answer: b
Explanation: Here ‘+’ defined operator works as concatenation for strings.
Output : Hello I Love ComputerScience.

7. Correct way to find if contents of two strings are equal or not?

a) if (s1 = s2)

b) if (s1 == s2)

c) if (String.Compare(s1 ,s2))

d) if ( s1 is s2)

Click to show answer

Answer: b , c
Explanation: “==” operator used to compare length of two strings and String.Compare method derived from string class.

8. Which of the following statements are correct?

a) String is value type

b) String literals can contain any character literal including escape sequences

c) The equality operators are defined to compare values of string objects as well as references

d) All of the mentioned

Click to show answer

Answer: b
Explanation: None

9. Which of these operators can be used to concatenate two or more String objects?

a) +

b) +=

c) &

d) ||

Click to show answer

Answer: a
Explanation: string s1 = “Hello”+ ” I ” + “Love” + ” ComputerScience “;
Console.WriteLine(s1);
Hello I Love ComputerScience.

Type Conversion Questions In C#
www.csharpens.com/questions-of-type-conversion-in-C-sharp

10. Method use to remove white space from string?

a) Split()

b) Substring()

c) Trim()

d) TrimStart()

Click to show answer

Answer: c
Explanation: Trim() method removes all leading and trailing white-space characters from the current String object. MSDN

Comparison of Strings
11. Which of these method of class String is used to compare two String objects for their equality?
a) equals()
b) Equals()
c) isequal()
d) Isequal()

Click to show answer

Answer: b
Explanation : None.
12. Which of these method is used to compare two string such that after comparison output returns different integer values as ( 0 for false, 1 for true)?
a) Equals ()
b) == operator
c) Compare()
d) None of the mentioned

Click to show answer

Answer: c
Explanation: The comparison is case sensitive in nature and hence different integer values are return for different conditions as under :
i. zero integer (0), if string s1 equal to string s2.
ii. positive integer(+1) , if string s1 greater than s2.
iii. Negative integer(-1) , if string s1 is less than s2.
13. Which of these method of class String is used to check weather a substring exists at beginning of the particular string?
a) StartsWith()
b) EndsWith()
c) Starts()
d) ends()

Click to show answer

Answer: a
Explanation : Method startswith() of string class used to check that whether a substring exists in the beginning of string or not.
14. Which method returns the string such that some characters which are specified to be removed from the end of strings are removed from string by mentioning number of characters to be removed?
a) Trim()
b) Remove()
c) TrimEnd()
d) Split()

Click to show answer

Answer: a
Explanation : Remove a string of characters from the end of string by mentioning number of characters to be removed from the string. 'The string that remains after all occurrences of the characters in the trimChars parameter are removed from the start and end of the current string. If trimChars is null or an empty array, white-space characters are removed instead. MSDN'
15. What is the value returned by function compareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) None of the mentioned

Click to show answer

Answer: b
Explanation : compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
16. Which of these data type value is returned by equals() method of String class?
a) char
b) int
c) boolean
d) All of the mentioned

Click to show answer

Answer: c
Explanation : equals() method of string class returns boolean value true if both the string are equal and false if they are unequal.
17. What is output of the given set of Code?

class Program
{
static void Main(string[] args)
{
String c = "i love Csharp";
bool a;
a = c.StartsWith("I");
Console.WriteLine(a);
Console.ReadLine();
}
}
a) true
b) false
c) 0
d) 1

Click to show answer

Answer: b
Explanation: StartsWith() method is case sensitive “i” and “I” are treated differently, hence false is stored in a.
Output: false

If else Questions In C#
www.csharpens.com/questions-of-if-else-in-c-sharp

18. What is output of the given set of Code?

class Program
{
static void Main(string[] args)
{
String s1 = "I love You";
String s2 = s1;
Console.WriteLine((s1 == s2) + " " + s1.Equals(s2));
Console.ReadLine();
}
}
a) true true
b) false false
c) true false
d) false true

Click to show answer

Answer: a
Explanation : The ‘==’ operator tests the equality of strings and since s1 = “I love You” and also s2 = s1 .So, true is returned Similarly,Equals() returns true
as content of both s1 and s2 are equal in nature.
Output : true true
19. What is output of the given set of Code?

class Program
{
static void Main(string[] args)
{
String []chars = {"z", "x", "y", "z", "y"};
for (int i = 0; i < chars.Length; ++i)
for (int j = i + 1; j < chars.Length; ++j)
if(chars[i].CompareTo(chars[j]) == 0)
Console.WriteLine(chars[j]);
Console.ReadLine();
}
}
a) zx
b) xy
c) zy
d) yz

Click to show answer

Answer: c
Explanation : compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared To.4
Output : z
y
20. What will be the output for given set of code ?
String a = "Csharp";
String b = "CSHARP";
int c;
c = a.CompareTo(b);
Console.WriteLine(c);
a) 0
b) 1
c) -2
d) -1

Click to show answer

Answer: d
Explanation : Negative integer -1 returned as ‘a’ is less than ‘b’ by CompareTo() method.
Output : -1

String Formatting
21. 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

Click to show answer

Answer: c, d
Explanation: Generally, a string is defined as a sequence of characters but it’s different in C# for c++ string is array of characters.In case of C# strings are objects of the built-in string data type. Thus, string is a reference type
22. Select the namespace in which string class is built?
a) System.String
b) System.Net
c) System.IO
d) None of the mentioned

Click to show answer

Answer: a
Explanation: None.
23. Select the interfaces defined by the string class?
a) IComparable
b) IComparable
c) ICloneable
d) IEnumerable

Click to show answer

Answer: a, b, c, d
Explanation: None.
24. Choose the constructor type used to built strings from character array:
a) public String(char[ ] value)
b) public String(char[ ] value, int startIndex, int length)
c) public String(char[ ])
d) All of the mentioned

Click to show answer

Answer: a, 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
25. Select the operators used for checking the equality in strings:
a) !=
b) ==
c) <
d) >=

Click to show answer

Answer: a, b
Explanation: None.
26. What does the given code set specifies?
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 for (A < B) else '0' for(A=B)
d) All of the mentioned

Click to show answer

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
27. Select the output for given set of code:

static void Main(string[] args)
{
string s1 = "Hello" + "c" + "Sharp";
Console.WriteLine(s1);
Console.ReadLine();
}
a) Hello c Sharp
b) HellocSharp
c) Compile time error
d) Hello

Click to show answer

Answer: b
Explanation: Here ‘+’ defined operator works as concatenation for strings.
Output : Hello c Sharp
28. Which of these operators can be used to concatenate two or more String objects?
a) +
b) +=
c) &
d) ||

Click to show answer

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

Click to show answer

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.
30. Which string operation does the below mentioned method defines?
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) None of the mentioned

Click to show answer

Answer: a, b, c
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.
31. Choose the base class for string() method :
a) System.Array
b) System.char
c) System.String
d) None of the mentioned

Click to show answer

Answer: c
Explanation: String is an alias for the predefined “System.string” class from which most of the string() methods are derived.

Questions about Variables Lifetime In C#
www.csharpens.com/variables-lifetime-in-c-sharp

32. Method use to remove white space from string?
a) Split()
b) Substring()
c) Trim()
d) TrimStart()

Click to show answer

Answer: c
Explanation: Perfectly removes a whitespace from string whereas TrimStart() removes a string of characters from the end of the string.
33. What will be the output of given code snippet?

static void Main(string[] args)
{
string s1 = "olleH";
string s2 = "olleh";
if (s1 == s2)
Console.WriteLine("Equal");
else
Console.WriteLine("Unequal");
if (s1.Equals(s2))
Console.WriteLine("Equal");
else
Console.WriteLine("Unequal");
Console.ReadLine();
}
a) Equal
Unequal
b) Unequal
Equal
c) Equal
Equal
d) Unequal
Unequal

Click to show answer

Answer: d
Explanation: In first comparison it is being checked either two strings are equal or not but in second comparison it is checked whether two string references are equal or not also length of string and characters match is tested for checking the equality of strings.
Output : Unequal
Unequal
34. What will be the output of given code snippet?

static void Main(string[] args)
{
string s1 = " Ixg";
string s2 = s1.Insert(3,"i");
string s3 = s2.Insert(5, "o");
for (int i = 0; i < s3.Length; i++)
Console.WriteLine(s3[i]);
Console.ReadLine();
}
a) Ixgo
b) Ixig
c) Ixigo
d) Ixg

Click to show answer

Answer: c
Explanation: Insert() the built in method insert characters at specified position mentioned with index positions.
Output :Ixigo
35. What will be the output of given code snippet?

class Program
{
static void Main(string[] args)
{
char []chars = {'a', 'b', 'c'};
String s = new String(chars);
Console.WriteLine(s);
Console.ReadLine();
}
}
a) a
b) b
c) c
d) abc

Click to show answer

Answer: d
Explanation: String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars,So s contains “abc”.
Output :abc
36. What will be the output of given code snippet?

class Program
{
static void Main(string[] args)
{
char []chars = {'a', 'b', 'c'};
String s = new String(chars);
String s1 = "abcd";
int len1 = s1.Length;
int len2 = s.Length;
Console.WriteLine(len1 + " " + len2);
Console.ReadLine();
}
}
a) 4 0
b) 3 0
c) 3 4
d) 4 3

Click to show answer

Answer: d
Explanation: None.
Output : 4 3

Variable Initialization In C#
www.csharpens.com/c-sharp-variable-initialization-questions

37. What will be the output of given code snippet?

class A
{
int i;
int j;
public A()
{
i = 1;
j = 2;
}
}
class Program
{
static void Main(string[] args)
{
A obj1 = new A();
Console.WriteLine(obj1.ToString());
Console.ReadLine();
}
}
a) true
b) false
c) String associated with obj1
d) Compile time error

Click to show answer

Answer: c
Explanation: ToString() is method of class Object, since it is superclass of every class, every object has this method. ToString() returns the string associated with the calling object.
Output : ConsoleApplication19.A
38. Which of these constructors is used to create an empty String object?
a) String()
b) String(void)
c) String(0)
d) None of the mentioned

Click to show answer

Answer: a
Explanation: None.
39. Which of these method of class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) Lengthof()
d) Length()

Click to show answer

Answer: d
Explanation: Method Length() of string class is used to get the length of the object which invoked method Length().
40. Choose the base class for string() method :
a) System.Array
b) System.char
c) System.String
d) None of the mentioned

Click to show answer

Answer: c
Explanation: String is an alias for the predefined “System.string” class from which most of the string() methods are derived.
41. What will be the output of given code snippet?

class Program
{
static void Main(string[] args)
{
String c = "Hello i love Csharp";
Boolean var;
var = c.StartsWith("hello");
Console.WriteLine(var);
Console.ReadLine();
}
}
a) True
b) False
c) 1
d) Run time error

Click to show answer

Answer: b
Explanation: StartsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
42. What is the value returned by function CompareTo() if the invoking string is less than the string compared?
a) zero
b) value less than zero
c) value greater than zero
d) None of the mentioned

Click to show answer

Answer: b
Explanation: CompareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared to.
43. What will be the output of give code snippet?

class Program
{
static void Main(string[] args)
{
String s1 = "Hello i love Csharp";
StringBuilder s2 = new  StringBuilder(s1);
Console.WriteLine(s1.Equals(s2));
Console.ReadLine();
}
}
a) True
b) False
c) 0
d) Compile time error

Click to show answer

Answer: b
Explanation: Equals() compares the content of two strings. StringBuilder class supports many methods which are useful for manipulating dynamic strings.
Output :False
44. Which of these method of class String is used to check whether a given string starts with a particular substring or not?
a) StartsWith()
b) EndsWith()
c) Starts()
d) Ends()

Click to show answer

Answer: a
Explanation: The StartsWith() determines whether a substring exists at the beginning of the string.
45. Which of these method of class String is used to extract a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned

Click to show answer

Answer: b
Explanation: None.
46. Write the output for given code snippet?

class Program
{
static void Main(string[] args)
{
String s1 = "one";
String s2 = string.Concat(s1 + " " + "two");
Console.WriteLine(s2);
Console.ReadLine();
}
}
a) one
b) two
c) one two
d) two one

Click to show answer

Answer: c
Explanation: Two strings can be concatenated by using Concat() method.
Output: one two
47. Which of these method of class String is used to remove leading and trailing white spaces?
a) startsWith()
b) TrimEnd()
c) Trim()
d) TrimStart()

Click to show answer

Answer: c
Explanation: Removes white space from the string

Char Datatypes In C#
www.csharpens.com/char-data-types-in-c-sharp-questions

48. What will be the output of given code snippet?

class Program
{
static void Main(string[] args)
{
String c = "  Hello World  ";
String s = c.Trim();
Console.WriteLine("\""+s+"\"");
Console.ReadLine();
}
}
a) ” Hello World ”
b) “HelloWorld”
c) “Hello World”
d) “Hello”

Click to show answer

Answer: c
Explanation: Trim() method is used to remove leading and trailing whitespaces in a string. MSDN
Output: “Hello World”
49. What will be the output of code snippet?

class Program
{
static void Main(string[] args)
{
String s1 = "CSHARP";
String s2 = s1.Replace('H','L');
Console.WriteLine(s2);
Console.ReadLine();
}
}
a) CSHAP
b) CSHP
c) CSLARP
d) CSHP

Click to show answer

Answer: c
Explanation: Replace() method replaces all occurrences of one character in invoking string with another character. s1.Replace(‘H’,'L’) replaces every occurrence of ‘H’ in CSHARP by ‘L’, giving CSLARP.
Output: CSLARP
50. What will be the output of code snippet?

class Program
{
static void Main(string[] args)
{
String s1 = "Hello World";
String s2 = s1.Substring(0, 4);
Console.WriteLine(s2);
Console.ReadLine();
}
}
a) Hello
b) Hell
c) H
d) Hello World

Click to show answer

Answer: b
Explanation: None.
Output: Hell
51. What will be the output of given code snippet?

class Program
{
static void Main(string[] args)
{
String s = "Hello World";
int i = s.IndexOf('o');
int j = s.LastIndexOf('l');
Console.WriteLine(i + " " + j);
Console.ReadLine();
}
}
a) 9 5
b) 4 9
c) 9 0
d) 9 4

Click to show answer

Answer: b
Explanation: None.
Output: 4 9
52. What will be the output of given code snippet?

class Program
{
static void Main(string[] args)
{
String c = "i love Csharp";
bool a;
a = c.StartsWith("I");
Console.WriteLine(a);
Console.ReadLine();
}
}
a) true
b) false
c) 0
d) 1

Click to show answer

Answer: b
Explanation: StartsWith() method is case sensitive “i” and “I” are treated differently, hence false is stored in a.
Output: false
53. What will be the output of code snippet?

class Program
{
static void Main(string[] args)
{
String []chars = {"z", "x", "y", "z", "y"};
for (int i = 0; i < chars.Length; ++i)
for (int j = i + 1; j < chars.Length; ++j)
if(chars[i].CompareTo(chars[j]) == 0)
Console.WriteLine(chars[j]);
Console.ReadLine();
}
}
a) zx
b) xy
c) zy
d) yz

Click to show answer

Answer: c
Explanation: compareTo() function returns zero when both the strings are equal, it returns a value less than zero if the invoking string is less than the other string being compared and value greater than zero when invoking string is greater than the string compared To 4
Output: z
y

Questions about Switch Cases in C#
www.csharpens.com/questions-about-switch-case-in-c-sharp

54. What will be the output of code snippet?

static void main(String args[])
{
char chars[] = {'a', 'b', 'c'};
String s = new String(chars);
Console.WriteLine(s);
}
a) a
b) b
c) ab
d) abc

Click to show answer

Answer = d
Explanation: None.
Output: abc
55. What will be the output of given code snippet?

static void Main(string[] args)
{
string s = " i love you";
Console.WriteLine(s.IndexOf('l') + "  " + s.lastIndexOf('o') + "  " + s.IndexOf('e'));
Console.ReadLine();
}
a) 3 5 7
b) 4 5 6
c) 3 9 6
d) 2 4 6

Click to show answer

Answer: c
Explanation: indexof(‘l’) and lastIndexof(‘o’) are pre defined function which are used to get the index of first and last occurrence of the character pointed by l and c respectively in the given array.
Output: 3, 9, 6

Show more