C# Questions & Answers – String Formatting Operations – 2

This set of C# Interview Questions and Answers focuses on “String Formatting Operations – 2”.

1. Which of these methods of class String is used to extract a substring from a String object?
a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
View Answer

Answer: b
Explanation: None.

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         String s1 = "one";
  6.         String s2 = string.Concat(s1 + " " + "two");
  7.         Console.WriteLine(s2);
  8.         Console.ReadLine();
  9.     }
  10. }

a) one
b) two
c) one two
d) two one
View Answer

Answer: c
Explanation: Two strings can be concatenated using Concat() method.
Output:

advertisement
advertisement
one two

3. Which of these methods of class String is used to remove leading and trailing whitespaces?
a) startsWith()
b) TrimEnd()
c) Trim()
d) TrimStart()
View Answer

Answer: c
Explanation: Removes white space from the string.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         String c = "  Hello World  ";
  6.         String s = c.Trim();
  7.         Console.WriteLine("""+s+""");
  8.         Console.ReadLine();
  9.     }
  10. }

a) ” Hello World ”
b) “HelloWorld”
c) “Hello World”
d) “Hello”
View Answer

Answer: c
Explanation: Trim() method is used to remove leading and trailing whitespaces in a string.
Output:

advertisement
"Hello World"

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          String s1 = "CSHARP";
  6.          String s2 = s1.Replace('H','L');
  7.          Console.WriteLine(s2);
  8.          Console.ReadLine();
  9.      }
  10.  }

a) CSHAP
b) CSHP
c) CSHALP
d) CSHP
View Answer

Answer: c
Explanation: Replace() method replaces all occurrences of a single character in invoking strings with another character. s1.Replace(‘H’,’L’) replaces every occurrence of ‘H’ in CSHARP by ‘L’, giving CSHALP.
Output:

CSHALP

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          String s1 = "Hello World";
  6.          String s2 = s1.Substring(0, 4);
  7.          Console.WriteLine(s2);
  8.          Console.ReadLine();
  9.      }
  10.  }

a) Hello
b) Hell
c) H
d) Hello World
View Answer

Answer: b
Explanation: None.
Output:

Hell

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

  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          String s = "Hello World";
  6.          int i = s.IndexOf('o');
  7.          int j = s.LastIndexOf('l');
  8.          Console.WriteLine(i + " " + j);
  9.          Console.ReadLine();
  10.      }
  11.  }

a) 9 5
b) 4 9
c) 9 0
d) 9 4
View Answer

Answer: b
Explanation: None.
Output:

4 9

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     { 
  5.         String c = "i love Csharp";
  6.         bool a;
  7.         a = c.StartsWith("I");
  8.         Console.WriteLine(a);
  9.         Console.ReadLine();
  10.     }
  11. }

a) true
b) false
c) 0
d) 1
View Answer

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

false

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

  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     { 
  5.         String []chars = {"z", "x", "y", "z", "y"};
  6.         for (int i = 0; i < chars.Length; ++i)
  7.         for (int j = i + 1; j < chars.Length; ++j)
  8.         if(chars[i].CompareTo(chars[j]) == 0)
  9.         Console.WriteLine(chars[j]);
  10.         Console.ReadLine();
  11.     }
  12. }

a) zx
b) xy
c) zy
d) yz
View 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 a value greater than zero if the invoking string is greater than the string compared to 4
Output:

zy

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

  1.  static void main(String args[])
  2.  {
  3.      char chars[] = {'a', 'b', 'c'};
  4.      String s = new String(chars);
  5.      Console.WriteLine(s);
  6.  }

a) a
b) b
c) ab
d) abc
View Answer

Answer: d
Explanation: None.
Output:

abc

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

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

a) 3 5 7
b) 4 5 6
c) 3 9 6
d) 2 4 6
View 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

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# for Interviews, 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.