C# Questions & Answers on String Formatting for Freshers

1. What will be the output of the given code snippet?
  1.  static void Main(string[] args)
  2.  {
  3.      string s1 = "olleH";
  4.      string s2 = "olleh";
  5.      if (s1 == s2)
  6.      Console.WriteLine("Equal");
  7.      else
  8.      Console.WriteLine("Unequal");
  9.      if (s1.Equals(s2))
  10.      Console.WriteLine("Equal");
  11.      else
  12.      Console.WriteLine("Unequal");
  13.      Console.ReadLine();
  14.  }
a) Equal
Unequal
b) Unequal
Equal
c) Equal
Equal
d) Unequal
Unequal
Answer: d

Explanation: In the first comparison it is being checked if two strings are equal or not, but in the second comparison it is checked if two string references are equal or not. Also the length of the string and characters match is tested for the equality of strings.
Output : Unequal
Unequal
2. What will be the output of the given code snippet?
  1. static void Main(string[] args)
  2. {
  3.     string s1 = " Ixg";
  4.     string s2 = s1.Insert(3,"i");
  5.     string s3 = s2.Insert(5, "o");
  6.     for (int i = 0; i < s3.Length; i++)
  7.     Console.WriteLine(s3[i]);
  8.     Console.ReadLine();
  9. }
a) Ixgo
b) Ixig
c) Ixigo
d) Ixg
Answer: c

Explanation: Insert() the built in method inserts characters at specified position mentioned with index positions.
Output :Ixigo
3. What will be the output of the given code snippet?
  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          char []chars = {'a', 'b', 'c'};
  6.          String s = new String(chars);
  7.          Console.WriteLine(s);
  8.          Console.ReadLine();
  9.      }
  10.  }
a) a
b) b
c) c
d) abc
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
4. What will be the output of given code snippet?
  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          char []chars = {'a', 'b', 'c'};
  6.          String s = new String(chars);
  7.          String s1 = "abcd";
  8.          int len1 = s1.Length;
  9.          int len2 = s.Length;
  10.          Console.WriteLine(len1 + " " + len2);
  11.          Console.ReadLine();
  12.      }
  13.  }
a) 4 0
b) 3 0
c) 3 4
d) 4 3
Answer: d
Output : 4 3
5. What will be the output of given code snippet?
  1.  class A
  2.  {
  3.      int i;
  4.      int j;
  5.      public A()
  6.      {
  7.          i = 1;
  8.          j = 2;
  9.      }
  10.  }
  11.  class Program
  12.  {
  13.      static void Main(string[] args)
  14.      {
  15.          A obj1 = new A();
  16. 	 Console.WriteLine(obj1.ToString());
  17.          Console.ReadLine();
  18.      }
  19.  }
a) True
b) False
c) String associated with obj1
d) Compile time error
Answer: c

Explanation: ToString() is the method of class Object, since it is the superclass of every class, every object has this method. ToString() returns the string associated with the calling object.
Output : ConsoleApplication19.A
6. 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
Answer: a
7. Which of these method of class String is used to obtain length of String object?

a) get()
b) Sizeof()
c) Lengthof()
d) Length()
Answer: d

Explanation: Method Length() of string class is used to get the length of the object which invoked the method Length().
8. Choose the base class for string() method :

a) System.Array
b) System.char
c) System.String
d) None of the mentioned
Answer: c

Explanation: String is an alias for the predefined “System.string” class from which most of the string() methods are derived.
9. What will be the output of the given code snippet?
  1.  class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          String c = "Hello i love Csharp";
  6.          Boolean var;
  7. 	 var = c.StartsWith("hello");
  8.          Console.WriteLine(var);
  9.          Console.ReadLine();
  10.      }
  11.  }
a) True
b) False
c) 1
d) Run time error
Answer: b

Explanation: StartsWith() method is case sensitive “hello” and “Hello” are treated differently, hence false is stored in var.
10. What is the value returned by the 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
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.
11. What will be the output of give code snippet?
  1. class Program
  2. {
  3.     static void Main(string[] args)
  4.     {
  5.         String s1 = "Hello i love Csharp";
  6.         StringBuilder s2 = new  StringBuilder(s1);
  7.         Console.WriteLine(s1.Equals(s2));
  8.         Console.ReadLine();
  9.     }
  10. }
a) True
b) False
c) 0
d) Compile time error
Answer: b

Explanation: Equals() compares the content of two strings. StringBuilder class supports many methods which are useful for manipulating dynamic strings.
Output :False
12. Which of these methods 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()
Answer: a
Explanation: The StartsWith() determines whether a substring exists at the beginning of the string.
13. 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
Answer: b

14. What is the output for the given 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
Answer: c
Explanation: Two strings can be concatenated using Concat() method.
Output: one two
15. Which of these methods of class String is used to remove leading and trailing whitespaces?
a) startsWith()
b) TrimEnd()
c) Trim()
d) TrimStart()
Answer: c
Explanation: Removes white space from the string.
16. What will be the output of the given code snippet?
  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”
Answer: c
Explanation: Trim() method is used to remove leading and trailing whitespaces in a string.
Output: “Hello World”
17. What will be the output of the 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
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
18. What will be the output of the 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
Answer: b
Output: Hell
19. What will be the output of the given 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
Answer: b
Output: 4 9
20. What will be the output of the given 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
Answer: b
Explanation: StartsWith() method is case sensitive “i” and “I” are treated differently, hence false is stored in a.
Output: false
21. What will be the output of the given 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
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: z
y
22. What will be the output of the 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
Answer = d
Output: abc
23. What will be the output of the given 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
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

Related

Multiple Choice Questions 1116931984615004091

Post a Comment

emo-but-icon

item