C# Questions & Answers ? Writing Console Output Operations
Posted by Superadmin on August 24 2022 09:02:54

C# Questions & Answers – Writing Console Output Operations

 

 

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

Answer: c
Explanation: TextWriter is a class with objects as write() and writeline().

 

 

 

2. What will be the output of the following C# code snippet?

  1.  static void Main(string[] args)
  2.  {
  3.      Console.Write("c");
  4.      Console.Write("sharp" );
  5.      Console.ReadLine();
  6.  }

a) sharp
b)

c
sharp
 c) c

d) sharp c

Answer: b
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

Answer: d
Explanation: By the definition of writeline().

 

 

 

 

4. What will be the output of the following C# code snippet?

  1.  static void Main(string[] args)
  2.  {
  3.      String a ="i love iostream";
  4.      Console.WriteLine(a.IndexOf('i') + " " + a.IndexOf('e') + " " + a.LastIndexOf('i') + " " + a.LastIndexOf('e'));
  5.      Console.ReadLine();
  6.  }

a) 0 6 7 8
b) 0 5 7 9
c) 5 9 0 7
d) 0 5 7 12

Answer: d
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?

  1.  static void Main(string[] args)
  2.  {
  3.      StringBuilder sb = new StringBuilder("hello world");
  4.      sb.Insert(6, "good");
  5.      Console.WriteLine(sb);
  6.      Console.ReadLine();
  7.  }

a) hello 6world
b) hello good world
c) hello goodworld
d) hello good world

Answer: c
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?

  1.  static void Main(string[] args)
  2.  {
  3.      string h = "i lovelife";
  4.      string h1 = new string(h.Reverse().ToArray());
  5.      Console.WriteLine(h1);
  6.      Console.ReadLine();
  7.  }

a) efil evoli
b) lifelove i
c) efilevol i
d) efil evol i

Answer: c
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

Answer: c
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

Answer: b
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

Answer: c
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

Answer: c
Explanation: To write a byte to a file, the WriteByte( ) method is used. Its simplest form is shown here:

 

   void WriteByte(byte value)