Sample questions
Question 1Medium
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();
}
- EqualUnequal
- UnequalEqual
- EqualEqual
- UnequalUnequal
Answer: D. UnequalUnequal
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: UnequalUnequal
Step-by-step solution
- The == operator on strings compares the text exactly and is case-sensitive, so Hello and hello are not equal: prints Unequal.
- string.Equals also compares ordinal text and is case-sensitive, so it returns false: prints Unequal.
- The output is UnequalUnequal, which is option D.
Why the other options are wrong
- A. Both comparisons are case-sensitive, so the first one is not Equal.
- B. The second check is also false, so it cannot print Equal.
- C. Both checks are false, so neither prints Equal.
Tip: C# string == and Equals are case-sensitive; use string.Equals(a, b, StringComparison.OrdinalIgnoreCase) to ignore case.
Question 2Medium
Which of the following will be the correct output for the C#.NET code snippet given below?
String s1 = "Nagpur";
String s2;
s2 = s1.Insert(6, "Mumbai");
Console.WriteLine(s2);
- NagpuMumbair
- Nagpur Mumbai
- Mumbai
- NagpurMumbai
Answer: D. NagpurMumbai
Explanation
If we see a String as an array of the characters then its indexing will start from 0 as it is known & it can also be considered as dynamic array. In the rule of array 0 is the first index.
So if NAGPUR is stored in a string it will store following value at this index:
0 - N
1 - A
2 - G
3 - P
4 - U
5 - R
So if we look at line below:
s2 = s1. Insert (6, "Mumbai") ;.
It says to put the mumbai at index 6 which is a free unused space &
hence instead of replacing the 'R' it stores the 'MUMBAI' after
'NAGPUR' is completed.
Step-by-step solution
- S1 is 'Nagpur', which has 6 characters at indexes 0 to 5.
- Insert(6, 'Mumbai') inserts at index 6, the position just after the last character.
- Insert adds text and replaces nothing, so 'Nagpur' is kept and 'Mumbai' is placed after it.
- The result is NagpurMumbai, so the answer is option D.
Why the other options are wrong
- A. Insert does not replace the R or shift letters; the R stays in place.
- B. Insert adds no space, so there is no space between the two words.
- C. Insert does not replace the whole string; 'Nagpur' stays in the result.
Tip: String.Insert adds text at an index without replacing characters; an index equal to the length appends at the end.
Question 3Medium
Which method is use to remove white space from string?
Split()Substring()Trim()TrimStart()
Answer: C. Trim()
Explanation
Perfectly removes a whitespace from string whereas TrimStart() removes a string of characters from the end of the string
Step-by-step solution
- Trim() removes whitespace from both ends of a string.
- Split breaks text into parts, Substring cuts out a section, and TrimStart removes only from the start.
- So the correct answer, Trim(), is correct.
Why the other options are wrong
- A. Split breaks the string into parts; it does not remove spaces.
- B. Substring cuts out a section; it does not remove spaces.
- D. TrimStart removes whitespace from the start only.
Tip: Trim removes from both ends; TrimStart and TrimEnd remove from one side.
Question 4Medium
Which of the following statements are correct?
- String is value type
- String literals can contain any character literal including escape sequences
- The equality operators are defined to compare values of string objects as well as references
- All of the mentioned
Answer: B. String literals can contain any character literal including escape sequences
Explanation
a variety of string literals. The last string literal, j, is a verbatim string literal that spans multiple lines.
The characters between the quotation marks, including white space such as new line characters, are preserved verbatim.
Since a hexadecimal escape sequence can have a variable number of hex digits, the string literal "\x123" contains a single character with hex value 123. To create a string containing the character with hex value 12 followed by the character 3, you could write "\x00123" or "\x12" + "3" instead.
The type of a string-literal is string.
Step-by-step solution
- String is reference type, not value type. So option A is wrong.
- String literals can include any character and escape sequences. So option B is correct.
- Equality operators compare values, not references. So option C is wrong.
- Only option B is correct. So answer is B.
Why the other options are wrong
- A. String is a reference type in C#.
- C. Equality operators compare values, not references.
Tip: Remember: strings are reference types and equality compares value.
Question 5Medium
A String has a zero-based index so index 9 will point to C and taking 6
characters starting from C will yield COLOUR.
Consider the following C# program:
using System;
namespacecomp
{
class Strings
{
public static void Main(String[] args)
{
String s1 = "THAT_DOG_COLOUR_IS_BROWN";
Console.WriteLine(s1.Length);
}
}
}
Do you agree that output of this program will yield correct length of string s1 which is 24?
- Yes
- No
Answer: A. Yes
Explanation
Counting the characters in THAT_DOG_COLOUR_IS_BROWN gives 24, and Length returns the number of characters in the string. So the answer is Yes.
Step-by-step solution
- Count THAT (4), underscore (5), DOG (8), underscore (9).
- COLOUR takes 6 more (15), underscore (16), IS (18), underscore (19).
- BROWN adds 5, giving 24.
- So option A is correct, which is Yes.
Why the other options are wrong
- B. The length really is 24.
Tip: Length counts every character, including underscores.
Question 6Medium
string in C# is a Reference Type.
- True
- False
Answer: A. True
Explanation
In C#, strings are indeed reference types, not value types. This means that when you assign a string to a variable or pass it as an argument, you're working with a reference to the actual string data, rather than a copy of the data itself. Therefore, the statement 'string in C# is a Reference Type' is correct.
Step-by-step solution
- In C#, strings are objects.
- Objects are reference types by default.
- So string is a reference type.
- Therefore, the answer is True.
Why the other options are wrong
- B. False because strings are reference types in C#.
Question 7Medium
What is the output of the following C# program?
using System;
namespace comp
{
class Strings
{
public static void Main(String[] args)
{
string str1 = "456.123";
double double1 = Convert.ToDouble(str1);
double double2 = double.Parse(str1);
Console.Write(double1 + " " + double2);
}
}
}
- 456.123 456.123
- 456 456
- 456.123 456
- 456 456.123
Answer: A. 456.123 456.123
Explanation
Convert.ToDouble(string value): Converts string representation of a number("456.123") to the equivalent double (456.123)
double.Parse(string value): Converts string representation of a number("456.123") to the equivalent double (456.123)
Step-by-step solution
- Convert.ToDouble and double.Parse both parse 456.123.
- Both give 456.123.
- So option A is correct, which is 456.123 456.123.
Why the other options are wrong
- B. Neither method drops the decimals.
- C. double.Parse keeps decimals.
- D. Convert.ToDouble keeps decimals.
Tip: Both parse methods keep the full double value.
Question 8Medium
What is the output of the following C# program?
using System;
namespacecomp
{
class Strings
{
public static void Main(String[] args)
{
String s1 = "THAT_DOG_COLOUR_IS_BROWN";
String s2;
s2 = s1.Substring(9, 6);
Console.WriteLine(s2);
}
}
}
- _COLOU
- COLOUR
- OLOUR_
- None of these
Answer: B. COLOUR
Explanation
Syntax of Substring is: Substring(int startIndex, int length)
Step-by-step solution
- Indexes start at 0: T0 H1 A2 T3 _4 D5 O6 G7 _8 C9 O10 L11 O12 U13 R14.
- Substring(9, 6) takes 6 characters starting at index 9.
- Those characters are C, O, L, O, U, R.
- Console.WriteLine prints COLOUR.
Why the other options are wrong
- A. _COLOU starts at index 8, not index 9.
- C. OLOUR_ starts at index 10, not index 9.
- D. COLOUR is in the output, so 'None of these' is wrong.
Tip: Substring(start, length) counts the start from zero and takes exactly length characters.
Practise all 8 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →