C# MCQ Number 00767

C# MCQ Set 1

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

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

2. What will be the output of the given 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

Answer: b [Reason:] 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

Answer: d [Reason:] By the definition of writeline().

4. What would be the output of given 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

Answer: d [Reason:] 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 given 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

Answer: c [Reason:] 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 given 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

Answer: c [Reason:] 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

Answer: c [Reason:] 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

Answer: b [Reason:] 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

Answer: c [Reason:] 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

Answer: c [Reason:] To write a byte to a file, the WriteByte( ) method is used. Its simplest form is shown here:
void WriteByte(byte value)

C# MCQ Set 2

1. Which feature enables to obtain information about the use and capabilities of types at runtime?
a) Runtime type ID
b) Reflection
c) Attributes
d) None of the mentioned

Answer

Answer: b [Reason:] Reflection is the feature that enables you to obtain information about a type. The term reflection comes from the way the process works: A Type object mirrors the underlying type that it represents. Reflection is a powerful mechanism because it allows us to learn and use the capabilities of types that are known
only at runtime.

2. Choose the namespace which consists of classes that are part of .NET Reflection API:
a) System.Text
b) System.Name
c) System.Reflection
d) None of the mentioned

Answer

Answer: c [Reason:] Many of the classes that support reflection are part of the .NET Reflection API, which is in the System.Reflection namespace.
eg : using System.Reflection;

3. Choose the correct statement about System.Type namespace:
a) Core of the reflection subsystem as it encapsulates a type
b) Consists of many methods and properties that can be used to obtain information about a type at runtime
c) Both a & b
d) Only b

Answer

Answer: c [Reason:] System.Type is at the core of the reflection subsystem because it encapsulates a type. It contains many properties and methods that you will use to obtain information about a type at runtime.

4. Choose the class from which the namespace ‘System.Type’ is derived:
a) System.Reflection
b) System.Reflection.MemberInfo
c) Both a & b
d) None of the mentioned

Answer

Answer: b [Reason:] Type is derived from an abstract class called System.Reflection.MemberInfo

5. What does the following property signify?
MemberTypes MemberType
a) Helps in distinguishing kinds of members
b) Property helps in determining if member is a field, method, property or event
c) Both a & b
d) None of the mentioned

Answer

Answer: c [Reason:] This property obtains the kind of the member. This value indicates if the member is a field, method, property, event, or constructor among others.

6. The property signifies “Obtains a Module object that represents the module (an executable file) in which the reflected type resides”. Choose the property which
specifies the following statement:
a) Type DeclaringType
b) int MetadataToken
c) Module Module
d) Type ReflectedType

Answer

Answer: c [Reason:] By definition.

7. Choose the method defined by MemberInfo:
a) GetCustomAttributes()
b) IsDefined()
c) GetCustomeAttributesData()
d) All of the mentioned

Answer

Answer: d [Reason:] MemberInfo includes two abstract methods: GetCustomAttributes( ) and IsDefined( ). These both relate to attributes. The first obtains a list of the custom attributes associated with the invoking object. The second determines if an attribute is defined for the invoking object. The .NET Framework Version 4.0 adds a method called GetCustomAttributesData(), which returns information about custom attributes

8. What does the following declaration specify?
MethodInfo[] GetMethods()
a) Returns an array of MethodInfo objects
b) Returns a list of the public methods supported by the type by using GetMethods()
c) Both a & b
d) None of the mentioned

Answer

Answer: c [Reason:] A list of the public methods supported by the type can be obtained by using GetMethods(). It returns an array of MethodInfo objects that describe the methods supported by the invoking type. MethodInfo is in the System.Reflection namespace.

9. What does the following code specify?
object Invoke(object obj, object[] parameters)
a) Calling a type using invoke()
b) Any arguments that need to be passed to the method are specified in the array parameters
c) The value returned by the invoked method is returned by Invoke()
d) All of the mentioned

Answer

Answer: d [Reason:] Here, obj is a reference to the object on which the method is invoked. (For static methods, you can pass null to obj.) Any arguments that need to be passed to the method are specified in the array parameters. If no arguments are needed, parameters must be null. Also, parameters must contain exactly the same number of elements as the number of arguments.

10. What does the following method specify?
Type[] GetGenericArguments()
a) A property defined by MemberInfo
b) Obtains a list of the type arguments bound to a closed constructed generic type
c) The list may contain both type arguments and type parameters
d) All of the mentioned

Answer

Answer: d [Reason:] The following method Obtains a list of the type arguments bound to a closed constructed generic type or the type parameters if the specified type is a generic type definition. For an open constructed type, the list may contain both type arguments and type parameters.

C# MCQ Set 3

1. From which of these classes, the character based output stream class Stream Writer is derived?
a) TextWriter
b) TextReader
c) Character Stream
d) All of the mentioned

Answer

Answer: a [Reason:] StreamWriter is derived from TextWriter. To create a character-based output stream, wrap a Stream object (such as a FileStream) inside a StreamWriter

2. The advantages of using character stream based file handling are?
a) they operate directly on unicode characters
b) they operate directly on bits
c) they store unicode text
d) all of the mentioned

Answer

Answer: a [Reason:] Although byte-oriented file handling is quite common, it is possible to use character-based streams for this purpose. The advantage of the character streams is that they operate directly on Unicode characters. Thus, if you want to store Unicode text, the character streams are certainly the best option.

3. Which among the following classes are used to perform the character based file operations?
a) StreamReader
b) InputStream
c) OutputStream
d) All of the mentioned

Answer

Answer: a, b [Reason:] In general, to perform character-based file operations, wrap a FileStream inside a StreamReader or a StreamWriter. These classes automatically convert a byte stream into a character stream, and vice versa.

4. Which of these is a method used to clear all the data present in output buffers?
a) clear()
b) flush()
c) fflush()
d) close()

Answer

Answer: b [Reason:] None.

5. Which method of the character stream class returns the numbers of characters successfully read starting at index?
a) int Read()
b) int Read(char[] buffer, int index, int count)
c) int ReadBlock(char[ ] buffer, int index, int count)
d) none of the mentioned

Answer

Answer: c [Reason:] Attempts to read the count characters into buffer starting at buffer[index], returning the number of characters successfully read.

6. Which method of character stream class returns the numbers of characters successfully read starting at count?
a) int Read()
b) int Read(char[] buffer, int index, int count)
c) int ReadBlock(char[ ] buffer, int index, int count)
d) none of the mentioned

Answer

Answer: b [Reason:] Attempts to read the count characters into buffer starting at buffer[count], returning the number of characters successfully read.

7. Which method among the following returns the integer if no character is available?
a) int peek()
b) int read()
c) string ReadLine()
d) none of the mentioned

Answer

Answer: a [Reason:] Obtains the next character from the input stream, but does not remove that character. Returns –1 if no character is available.

8. Which of the following is used to perform all input & output operations in C#?
a) streams
b) Variables
c) classes
d) Methods

Answer

Answer: a [Reason:] Streams are used for input and output operations in any programming language.

9. Which of the following is a type of stream in C#?
a) Integer stream
b) Character stream
c) Float stream
d) Long stream

Answer

Answer: b [Reason:] Two types of streams – Byte stream and character stream are defined in C#.

10. In the given constructor declaration for character based file operation what does ‘path’ and ‘bool specifies?
StreamWriter(string path, bool append)
a) the name of the file to open
b) specifies the full path of file
c) if append is true, the file is appended to the end of the existing file
d) all of the mentioned

Answer

Answer: d [Reason:] StreamWriter(string path, bool append) .Here, path specifies the name of the file to be opened, which can include a full path specifier. In the second form, if append is true, then the output is appended to the end of an existing file. Otherwise, output overwrites the specified file.

C# MCQ Set 4

1. Which among the following is not the ordered collection class?
a) BitArray
b) Queue
c) Stack
d) None of the mentioned

Answer

Answer: a [Reason:] None.

2. Which among the following is not an interface declared in System.Collection namespace?
a) IDictionaryComparer
b) IEnumerable
c) IEnumerator
d) Icomparer

Answer

Answer: a [Reason:] None.

3. Which among the following is the correct way to find out the number of elements currently present in an ArrayListCollection called arr?
a) arr.Capacity
b) arr.Count
c) arr.MaxIndex
d) arr.UpperBound

Answer

Answer: b [Reason:] None.

4. Which statement is correct about the C#.NET code snippet given below?

  1. Stack st = new Stack();
  2. st.Push("Csharp");
  3. st.Push(7.3);
  4. st.Push(8);
  5. st.Push('b');
  6. st.Push(true);

a) Unsimilar elements like “Csharp”,7.3,8 cannot be stored in the same stack collection.
b) Boolean values can never be stored in Stack collection
c) Perfectly workable code
d) All of the mentioned

Answer

Answer: c [Reason:] None.

5. Which is the correct statement about an ArrayList collection that implements the IEnumerable interface?
a) To access members of ArrayList from the inner class, it is necessary to pass ArrayList class’s reference to it
b) The inner class of ArrayList can access ArrayList class’s members
c) The ArrayList class consist of inner class that implements the IEnumerator interface
d) All of the mentioned

Answer

Answer: d [Reason:] None.

6. Which among the following is the correct way to access all the elements of the Stack collection created using the C#.NET code snippet given below?

  1. Stack st = new Stack();
  2. st.Push(10);
  3. st.Push(20);
  4. st.Push(-5);
  5. st.Push(30);
  6. st.Push(6);

a) IEnumerable e;
e = st.GetEnumerator();
while (e.MoveNext())
Console.WriteLine(e.Current);
b) IEnumerator e;
e = st.GetEnumerator();
while(e.MoveNext())
Console.WriteLine(e.Current);
c) IEnumerable e;
e = st.GetEnumerable();
while(e.MoveNext())
Console.WriteLine(e.Current);
d) None of the mentioned

Answer

Answer: b [Reason:] None.

7. The correct code to access all the elements of the queue collection created using the C#.NET code snippets given below is?

  1. Queue q = new Queue();
  2. q.Enqueue("Harsh");
  3. q.Enqueue('a');
  4. q.Enqueue(false);
  5. q.Enqueue(70);
  6. q.Enqueue(8.5);

a) IEnumerator e;
e = q.GetEnumerator();
while(e.MoveNext())
Console.WriteLine(e.Current);
b) IEnumerable e;
e = q.GetEnumerator();
while(e.MoveNext())
c) IEnumerable e
e = q.GetEnumerable();
while(e.MoveNext())
Console.WriteLine(e.Current);
d) All of the mentioned

Answer

Answer: a [Reason:] None.

8. Which statements among the following are correct about the Collection Classes available in Framework Class Library?
a) Elements of a collection cannot be transmitted over a network
b) Elements stored in a collection can be modified only if all the elements are of similar types
c) Elements stored in a Collection can be retrieved but cannot be modified
d) Collection classes make use of efficient algorithms to manage the collection,hence improving performance of the program

Answer

Answer: d [Reason:] None.

9. Among the given collections which one is I/O index based?
a) ArrayList
b) List
c) Stack
d) Queue

Answer

Answer: a [Reason:] None.

10. Which among the given statements are correct about the Stack collection?
a) It can be used for evaluation of expressions
b) It is used to maintain a FIFO list
c) Top most element of the Stack collection can be accessed using the Peek()
d) All of the mentioned

Answer

Answer: d [Reason:] None.

11. A HashTable t maintains a collection of names of states and capital city of each state.Which among the following finds out whether “New delhi” state is present in the collection or not?
a) t.HasValue(“New delhi”);
b) t.ContainsKey(“New delhi”);
c) t.HasKey(“New delhi”);
d) t.ContainsValue(“New delhi”);

Answer

Answer: b [Reason:] None.

12. In which of the following collections is the I/O based on a key?
a) BitArray
b) SortedList
c) Queue
d) Stack

Answer

Answer: b [Reason:] None.

13. The wrong statements about a HashTable collection are?
a) It is a keyed collection
b) It is a ordered collection
c) It’s not an indexed collection
d) It implements a IDictionaryEnumerator interface in its inner class

Answer

Answer: b [Reason:] None.

C# MCQ Set 5

1. What is meant by the term generics?
a) parameterized types
b) class
c) structure
d) interface

Answer

Answer: a [Reason:] The term generics means parameterized types. Parameterized types are important because they enable us to create classes, structures, interfaces, methods, and delegates in which, the type of data upon which they operate is specified as a parameter.

2. Are generics in C# are same as the generics in java and templates in C++?
a) Yes
b) No
c) May be
d) None of the mentioned

Answer

Answer: b [Reason:] Although C# generics are similar to templates in C++ and generics in Java, they are not the same as either. In fact, there are some fundamental differences among these three approaches to generics

3. Choose the advantages of using generics?
a) Generics facilitate type safety
b) Generics facilitate improved performance and reduced code
c) Generics promote the usage of parameterized types
d) All of the mentioned

Answer

Answer: d [Reason:] By definition of generics.

4. What does the following code block defines?

  1.  class Gen<T> 
  2.  {  
  3.       T ob;    
  4.  }

a) Generics class declaration
b) Generic constructor declaration
c) A simple class declaration
d) All of the mentioned

Answer

Answer: a [Reason:] class Gen This defines the generics declaration where ‘T’ is the name of type parameter. This parameter is used as a placeholder for the actual type that will be specified when a Gen object is created. Gen is a generic class. T is used to declare a variable called ‘ob’.

5. What does the following code set defines?

  1.  public Gen(T o) 
  2.  {
  3.       ob = o; 
  4.  }

a) Generics class decleration
b) Declaration of variable
c) Generic constructor declaration
d) All of the mentioned

Answer

Answer: c [Reason:] None.

6. Select the type argument of an open constructed type?
a) Gen<int>
b) Gen<T>
c) Gen<>
d) None of the mentioned

Answer

Answer: c [Reason:] A generic type, such as Gen, is an abstraction.In C# terminology, a construct such as Gen is called an open constructed type, because the type parameter T (rather than an actual type, such as int) is specified.

7. Which among the given classes is present in System.Collection.Generic.namespace?
a) Stack
b) Tree
c) Sorted Array
d) All of the mentioned

Answer

Answer: a [Reason:] None.

8. Which of these is a correct way of defining generic method?
a) name(T1, T2, …, Tn) { /* … */ }
b) public name { /* … */ }
c) class name[T1, T2, …, Tn] { /* … */ }
d) name{T1, T2, …, Tn} { /* … */ }

Answer

Answer: b [Reason:] The syntax for a generic method includes a type parameter, inside angle brackets, and appears before the method’s return type. For static generic methods, the type parameter section must appear before the method’s return type.

9. Which of these type parameters is used for generic methods to return and accept any type of object?
a) K
b) N
c) T
d) V

Answer

Answer: c [Reason:] T is used for type, A type variable can be any non-primitive type you specify: any class type, any interface type, any array type, or even another type variable.

10. Choose the correct way to call subroutine fun() of the sample class?

  1. class a
  2. {
  3.     public void x(int p, double k)
  4.     {
  5.         Console.WriteLine("k : csharp!");
  6.     }
  7. }

a) delegate void del(int i);
x s = new x();
del d = new del(ref s.x);
d(8, 2.2f);
b) delegate void del(int p, double k);
del d;
x s = new x();
d = new del(ref s.x);
d(8, 2.2f);
c) x s = new x();
delegate void d = new del(ref x);
d(8, 2.2f);
d) all of the mentioned

Answer

Answer: b [Reason:] None.

11. What will be the output of the given code snippet?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<string> g = new Generic<string>();
  19.         g.push(40);
  20.         Console.WriteLine(g.pop());
  21.         Console.ReadLine();
  22.     }
  23. }

a) 0
b) Runtime Error
c) 40
d) Compile time Error

Answer

Answer: c [Reason:] None.
Output : 40.

12. What will be the output of the given code snippet?

  1. public class Generic<T>
  2. {
  3.     Stack<T> stk = new Stack<T>();
  4.     public void push(T obj)
  5.     {
  6.         stk.Push(obj);
  7.     }
  8.     public T pop()
  9.     {
  10.         T obj = stk.Pop();
  11.         return obj;
  12.     }
  13. }
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         Generic<int> g = new Generic<int>();
  19.         g.push("Csharp");
  20.         Console.WriteLine(g.pop());
  21.         Console.ReadLine();
  22.     }
  23. }

a) Compile time error
b) Csharp
c) 0
d) Run time error

Answer

Answer: b [Reason:] None.
Output : Csharp

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.