This section of our 1000+ C# multiple choice questions focuses on writing output operations on console in C# Programming Language.
1. Select the objects of the class TextWriter which is/are not used to perform the write operations to the console?
a) Write()
b) WriteLine()
c) WriteError()
d) All of the mentioned
View Answer
Explanation: TextWriter is a class with objects as write() and writeline().
2. What will be the output of the following C# code snippet?
static void Main(string[] args)
{
Console.Write("c");
Console.Write("sharp" );
Console.ReadLine();
}
a) sharp
b)
c sharp
c) c
d) sharp c
View Answer
Explanation: Write() is used here which outputs one or more values to the screen without a newline character.
Output :
c sharp
3. Choose the correct statement about the WriteLine()?
a) Can display one or more value to the screen
b) Adds a newline character at the end of the each new output
c) Allows to output data in as many different formats
d) All of the mentioned
View Answer
Explanation: By the definition of writeline().
4. What will be the output of the following C# code snippet?
static void Main(string[] args)
{
String a ="i love iostream";
Console.WriteLine(a.IndexOf('i') + " " + a.IndexOf('e') + " " + a.LastIndexOf('i') + " " + a.LastIndexOf('e'));
Console.ReadLine();
}
a) 0 6 7 8
b) 0 5 7 9
c) 5 9 0 7
d) 0 5 7 12
View Answer
Explanation: indexof(‘i’) and lastIndexof(‘i’) are pre defined functions which are used to get the index of first and last occurrence of the character pointed by i in the given array.
Output :
0 5 7 12
5. What will be the output of the following C# code snippet?
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder("hello world");
sb.Insert(6, "good");
Console.WriteLine(sb);
Console.ReadLine();
}
a) hello 6world
b) hello good world
c) hello goodworld
d) hello good world
View Answer
Explanation: The insert() method inserts one string into another. It is overloaded to accept values of all simple types, plus String and Objects. String is inserted into invoking object at specified position. “Good ” is inserted in “Hello World” index 6 giving “Hello Good World”.
6. What will be the output of the following C# code snippet?
static void Main(string[] args)
{
string h = "i lovelife";
string h1 = new string(h.Reverse().ToArray());
Console.WriteLine(h1);
Console.ReadLine();
}
a) efil evoli
b) lifelove i
c) efilevol i
d) efil evol i
View Answer
Explanation: Reverse() an inbuilt method reverses all the characters singly and hence embed them into the string completely.
Output :
efilevol i
7. Which of the following statement is correct?
a) reverse() method reverses some characters
b) reverseall() method reverses all characters
c) replace() method replaces all instances of a character with new character
d) replace() method replaces first occurrence of a character in invoking string with another character
View Answer
Explanation: reverse() and replace() are by definition.
8. Which of these classes is used to create an object whose character sequence is mutable?
a) String()
b) StringBuilder()
c) String() & StringBuilder()
d) None of the mentioned
View Answer
Explanation: Mutable strings are dynamic strings. They can grow dynamically as characters are added to them. stringbuilder class supports those methods that are useful for manipulating dynamic strings.
9. Select the namespace/namespaces which consists of methods like Length(), Indexer(), Append() for manipulating the strings.
a) System.Class
b) System.Array
c) System.Text
d) None of the mentioned
View Answer
Explanation: The system.text namespace contains the Stringbuilder class and hence must include using system.text for manipulating the mutable strings.
10. Select the method used to write single byte to a file?
a) Write()
b) Wrteline()
c) WriteByte()
d) All of the mentioned
View Answer
Explanation: To write a byte to a file, the WriteByte( ) method is used. Its simplest form is shown here:
void WriteByte(byte value)
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.
- Check Computer Science Books
- Check C# Books
- Apply for C# Internship
- Practice MCA MCQs
- Practice Computer Science MCQs