Users Online
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Latest Articles
Articles Hierarchy
C# Questions & Answers ? String Formatting ? 1
C# Questions & Answers – String Formatting – 1
This section of our 1000+ C# MCQs focuses on string formatting operations in C# Programming Language.
1. What will be the output of the following C# 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
Unequal Equal
c)
Equal Equal
d)
Unequal Unequal
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 following C# 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
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 following C# 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
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?
-
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
Explanation: None.
Output :
4 3
5. What will be the output of the following C# 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
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
Explanation: None.
7. Which of these method of class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) Lengthof()
d) Length()
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
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 following C# 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
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
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 the following C# 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
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()
Explanation: The StartsWith() determines whether a substring exists at the beginning of the string.