This section of our 1000+ C# multiple choice questions focuses on operation of characters in C# Programming Language.
1. Which of these methods of the class String is used to obtain length of String object?
a) get()
b) Sizeof()
c) lengthof()
d) length()
View Answer
Explanation: Method length() of string class is used to get the length of the object as string. Length and hence invokes the length() method.
2. Which of these methods is an alternative to getChars() that stores the characters in an array of bytes?
a) getBytes()
b) GetByte()
c) giveByte()
d) Give Bytes()
View Answer
Explanation: getBytes() stores the character in an array of bytes. It uses default character to byte conversions.
3. Which of these methods can be used to convert all characters in a String into a character array?
a) CharAt()
b) getChars()
c) TocharArray()
d) All of the mentioned
View Answer
Explanation: None.
4. What will be the output of the following C# code snippet?
static void main(String args[])
{
char chars[] = {'x', 'y', 'z'};
String s = new String(chars);
Console.WriteLine(s);
}
a) x
b) xy
c) z
d) xyz
View Answer
Explanation: String(chars) is a constructor of class string, it initializes string s with the values stored in character array chars, therefore s contains “xyz”.
Output :
xyz
5. Choose the effective stringBuilder method which helps in producing output for the following C# code?
static void Main(string[] args)
{
StringBuilder s = new StringBuilder("object");
s./*______*/("Oriented Language");
Console.WriteLine(s);
Console.ReadLine();
}
Output : objectOriented Language
a) Insert()
b) Add()
c) Append()
d) Join()
View Answer
Explanation:
static void Main(string[] args) { StringBuilder s = new StringBuilder("object"); s.Append("Oriented Language"); Console.WriteLine(s); Console.ReadLine(); } Output : objectOriented Language
6. What will be the output of the following C# code?
static void Main(string[] args)
{
string s = " i love you";
Console.WriteLine(s.IndexOf('l') + " " + s.lastIndexOf('o') + " " + s.IndexOf('e'));
Console.ReadLine();
}
a) 3 5 7
b) 4 5 6
c) 3 9 6
d) 2 4 6
View Answer
Explanation: indexof(‘l’) and lastIndexof(‘o’) are pre defined functions 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
7. Which of these methods of class String is used to extract all the characters from a String object?
a) CHARAT()
b) Remove()
c) charAt()
d) Replace()
View Answer
Explanation: Replace() replaces all instances of a character with a new character while Remove extracts characters from the string.
8. What will be the output of the following C# code snippet?
static void Main(string[] args)
{
string c = "hello";
string c1 = c.Remove(1);
Console.WriteLine(c1);
Console.ReadLine();
}
a) ello
b) h
c) hell
d) none of the mentioned
View Answer
Explanation: The remove() deletes characters from the string except the character which is specified with its given position.
Output :
h
9. How is a string typically processed?
a) On a character by character basis
b) On a string by string basis
c) Both On a character by character basis & On a string by string basis
d) None of the mentioned
View Answer
Explanation: None.
10. How to print \\ on the screen?
a) Console.WriteLine(“\\”);
b) Console.WriteLine(“\\\”);
c) Console.WriteLine(“\\\\”);
d) Console.WriteLine(“\\\\\\”);
View Answer
Explanation: Console.WriteLine(“\\\\”);
Output :
\\
Sanfoundry Global Education & Learning Series – C# Programming Language.
To practice all areas of C# language, here is complete set of 1000+ Multiple Choice Questions and Answers.
If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]
- Check Computer Science Books
- Apply for C# Internship
- Check MCA Books
- Apply for Computer Science Internship
- Practice MCA MCQs