🧩 C# Programming MCQs and Exam Questions

Practice 151 approved 🧩 C# Programming MCQs and exam questions from the 💻 Programming subject.

151 solved MCQs

Which one of the following classes are present System.Collections.Generic namespace? 1. Stack 2. Tree 3. SortedDictionary 4. SortedArrayAnswer: 1 and 3 only For the code snippet shown below, which of the following statements are valid? public class Generic<T> { public T Field; public void TestSub() { T i = Field + 1; } } class MyProgram { static void Main(string[] args) { Generic<int> gen = new Generic<int>(); gen.TestSub(); } }Answer: Compiler will report an error: Operator '+' is not defined for types T and int. Which of the following statements are valid about generics in .NET Framework? 1.Generics is a language feature. 2. We can create a generic class, however, we cannot create a generic interface in C#.NET. 3. Generics delegates are not allowed in C#.NET. 4. Generics are useful in collection classes in .NET framework. 5. None of the aboveAnswer: 1 and 4 Only Which of the following statements is valid about generic procedures in C#.NET?Answer: Generic procedures must take at least one type parameter. For the code snippet shown below, which of the following statements are valid? public class TestIndiaBix { public void TestSub<M> (M arg) { Console.Write(arg); } } class MyProgram { static void Main(string[] args) { TestIndiaBix bix = new TestIndiaBix(); bix.TestSub("IndiaBIX "); bix.TestSub(4.2f); } }Answer: Program will compile and on execution will print: IndiaBIX 4.2 For the code snippet given below, which of the following statements is valid? public class Generic<T> { public T Field; } class Program { static void Main(string[ ] args) { Generic<String> g = new Generic<String>(); g.Field = "Hello"; Console.WriteLine(g.Field); } }Answer: It will print string "Hello" on the console. For the code snippet given below, which of the following statements are valid? public class MyContainer<T> where T: IComparabte { // Insert code here } 1. Class MyContainer requires that it's type argument must implement IComparabte interface. 2. Type argument of class MyContainer must be IComparabte. 3. Compiler will report an error for this block of code. 4. This requirement on type argument is called as constraint.Answer: 1 and 4 Only For the code snippet given below, which of the following statements are valid? public class MyContainer<T> where T: class, IComparable { //Insert code here } 1. Class MyContainer requires that it's type argument must implement IComparable interface. 2.Compiler will report an error for this block of code. 3. There are multiple constraints on type argument to MyContainer class. 4. Class MyContainer requires that its type argument must be a reference type and it must implement IComparable interface.Answer: 3 and 4 Only Which of the following statements is valid about advantages of generics?Answer: Generics provide type safety without the overhead of multiple implementations. The [Serializable()] attribute gets inspected atAnswer: Run-time Which of the following are correct ways to specify the targets for a custom attribute?Answer: By applying AttributeUsageAttribute to the custom attribute's class definition. Which of the following are correct ways to pass a parameter to an attribute? 1. By value 2. By reference 3. By address 4. By position 5. By nameAnswer: 4, 5 Which of the following statements are correct about inspecting an attribute in C#.NET? 1. An attribute can be inspected at link-time. 2. An attribute can be inspected at compile-time. 3. An attribute can be inspected at run-time. 4. An attribute can be inspected at design-time.Answer: 1, 2 Which of the following is correct ways of applying an attribute?Answer: [WebService (Name = "IndiaBIX", Description = "BIX WebService")] class AuthenticationService: WebService { /* .... */} Which of the following statements are correct about Attributes used in C#.NET?Answer: The CLR can change the behaviour of the code depending upon the attributes applied to it. Which of the following forms of applying an attribute is correct?Answer: [ Serializable() ] class sample { /* ... */ } Which of the following statements are correct about Attributes in C#.NET? 1. On compiling a C#.NET program the attibutes applied are recorded in the metadata of the assembly. 2. On compilation all the attribute's tags are deleted from the program. 3. It is not possible to create custom attributes.. 4. The attributes applied can be read from an assembly using Reflection class. 5. An attribute can have parameters.Answer: 1, 4 and 5 only Which of the following correctly describes the contents of the filename AssemblyInfo.cs?Answer: It contains assembly-level attributes. It possible to create a custom attribute that can be applied only to specific programming element(s) like ____ .Answer: Classes and Methods It possible to create a custom attribute that can be applied only to specific programming element(s) like ____ .Answer: Classes and Methods Which of the following CANNOT be a target for a custom attribute?Answer: Namespace Once applied which of the following CANNOT inspect the applied attribute?Answer: Linker Which of the following is the correct way to apply an attribute to an Assembly?Answer: [ assembly : AssemblyDescription("DCube Component Library") ] Which of the following is the correct way of applying the custom attribute called Tested which receives two-arguments - name of the tester and the testgrade? 1. Custom attribute cannot be applied to an assembly. 2. [assembly: Tested("Sachin", testgrade.Good)] 3. [Tested("Virat", testgrade.Excellent)] class customer { /* .... */ } 4. Custom attribute cannot be applied to a method. 5. Custom attribute cannot be applied to a class.Answer: 2, 3 Attributes can be applied to 1. Method 2. Class 3. Assembly 4. Namespace 5. EnumAnswer: 1, 2 and 3 Which of the following statements is incorrect about delegate?Answer: Only one method can be called using a delegate. In which of the following areas are delegates commonly used? 1. Remoting 2. Serialization 3. File Input/Output 4. Multithreading 5. Event handlingAnswer: 4 and 5 only Which of the following is the necessary condition for implementing delegates?Answer: Class declaration Which of the following statements are correct about the delegate declaration given below? delegate void del(int i); 1. On declaring the delegate a class called del will get created. 2. The signature of del need not be same as the signature of the method that we intend to call using it. 3. The del class will be derived from the MulticastDelegate class. 4. The method that can be called using del should not be a static method. 5. The del class will contain a one-argument constructor and an lnvoke() method.Answer: 1, 3 and 5 only Which of the following is the correct way to call the function MyFun() of the Sample class given below? class Sample { public int MyFun(int i) { Console.WriteLine("Welcome to IndiaBIX.com !" ); return 0; } }Answer: delegate int del(int i); del d; Sample s = new Sample(); d = new del(ref s.MyFun); d(10);