C# Questions & Answers on Searching and Modifying Strings for Freshers

1. Which of these methods of class String is used to separate a substring from a String object?

a) substring()
b) Substring()
c) SubString()
d) None of the mentioned
Answer: b
2. What will be the output for the given set of code?
  1.  static void Main(string[] args)
  2.  {
  3.      String a = "Ilove";
  4.      String b = "CSHARP";
  5.      b = string.Concat(a, '  ', b);
  6.      Console.WriteLine(b);
  7.      Console.ReadLine();
  8.  }
a) IloveCSHARP
b) I loveCSHARP
c) Ilove
d) Ilove CSHARP
Answer: d

Explanation: Concat() method is used to join two strings without the use of ‘+’ operator .
Output : Ilove CSHARP
3. Which of these methods of class are used to remove the leading and backward whitespaces?

a) startsWith()
b) trim()
c) Trim()
d) doTrim()
Answer: c
4. What will be the output for the given set of code?
  1.   static void Main(string[] args)
  2.   {
  3.       String a = "Ilove";
  4.       String b = "CSHARP";
  5.       b = string.Concat(a,' ',b);
  6.       string d = b.TrimStart('I', 'l', 'o', 'H');
  7.       Console.WriteLine(d);
  8.       Console.ReadLine();
  9.   }
a) Ilove CSHARP
b) love CSHARP
c) ve CSHARP
d) ve CSARP
Answer: c

Explanation: trimstart() removes character mentioned consecutively in front positions not characters in mentioned in between positions.
Output : ve CSHARP
5. What will be the output for the given set of code?
  1.  static void Main(string[] args)
  2.  {
  3.      String c = "  Hello Computer ";
  4.      String a = c.Trim();
  5.      Console.WriteLine("\"" + s + "\"");
  6.  }
a) ” Hello Computer ”
b) “HelloComputer”
c) “Hello Computer”
d) Hello Computer
Answer: c

Explanation: Trim() method is used to remove forward and backward spaces in strings.
Output : “Hello Computer”
6. What will be the output for the given set of code?
  1.  static void Main(string[] args)
  2.  {
  3.      String c = "Hello";
  4.      String a = c + "Bye";
  5.      Console.WriteLine(a);
  6.      Console.ReadLine();
  7.  }
a) “Hello Bye”
b) “HelloBye”
c) Hello Bye
d) HelloBye
Answer: d

Explanation: ‘+’ operator method works in the form of concatenate method() and hence is used to join two strings together.
Output : HelloBye
7. What will be the output for the given set of code?
  1. static void Main(string[] args)
  2. {
  3.     String c = "Hello";
  4.     String a ;
  5.     a = c.Replace('l', 'w');
  6.     Console.WriteLine(a);
  7.     Console.ReadLine();
  8. }
a) Helloll
b) Hewlo
c) Helwo
d) Hewwo
Answer: d
Output : Hewwo
8. Which of the following statements is correct?

a) replace() replace() method replaces last occurrence of a character in invoking strings with another character
b) replace() method replaces only first occurrence of a character in invoking strings with another character
c) replace() method replaces all occurrences of one character in invoking strings with another character
d) none of the mentioned
Answer: c

Explanation: By definition.
9. What will be the output of the given code snippet?
  1.   static void Main(string[] args)
  2.   {
  3.       String c = "Hello i love you";
  4.       String a ;
  5.       a = c.Substring(12, 3);
  6.       Console.WriteLine(a);
  7.       Console.ReadLine();
  8.   }
a) ove
b) you
c) yo
d) love you
Answer: c
Output : yo
10. Which among the following is the correct way to find out the index of second ‘s’ in the string “She sold her beauty in one night to someone else”?

  1. a) String a = "She sold her beauty in one night to someone else";
  2.    int i;
  3.    i = a.SecondIndexOf("s");
  4. b) String a = "She sold her beauty in one night to someone else";
  5.    int i, j;
  6.    i = a.FirstIndexOf("s");
  7.    j = a.IndexOf("s", i + 1);
  8. c) String a = "She sold her beauty in one night to someone else";
  9.    int i, j;
  10.    i = a.IndexOf("s");
  11.    j = a.IndexOf("s", i + 1);
  12. d) None of the mentioned
Answer: c

Explanation: static void Main(string[] args)
{
String c = “She sold her beauty in one night to someone else”;
int i,j;
i = c.IndexOf(“s”);
j = c.IndexOf(“s”, i + 1);
Console.WriteLine(i + ” ” + j);
Console.ReadLine();
}
Output : 4, 36

Related

Linux Questions & Answers on Process Management for Freshers

1. If a program executing in background attempts to read from STDIN a) It is terminatedb) It’s execution is suspendedc) STDIN is made available to itd) None of the mentioned Answer: b 2. Which co...

C# Questions & Answers on Attributes for Freshers

1. Which of the following cannot further inspect the attribute that is once applied? a) Linkerb) ASP.NET Runtimec) Language compilersd) CLR Answer: a 2. To apply an attribute to an Assembly, the ...

C#Questions & Answers on Try & Catch in Detail for Freshers

1. What is the use of try & catch? a) It is used to manually handle the exceptionb) It helps to fix the errorsc) It prevents automatic terminating of the program in cases when an exception occu...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item