🧩 C# Programming MCQs and Exam Questions

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

151 solved MCQs

Which of the following statements is correct about an interface used in C#.NET?Answer: If a class implements an interface partially, then it should be an abstract class. Which of the following statements is correct about an interface?Answer: A class that implements an interface can explicitly implement members of that interface. Which of the following statements are correct about an interface in C#.NET? 1. A class can implement multiple interfaces. 2. Structures cannot inherit a class but can implement an interface. 3. In C#.NET, : is used to signify that a class member implements a specific interface. 4. An interface can implement multiple classes. 5. The static attribute can be used with a method that implements an interface declaration.Answer: 1, 2, 3 Which of the following is the correct implementation of the interface given below? interface IMyInterface { double MyFun(Single i); }Answer: class MyClass: IMyInterface { double IMyInterface.MyFun(Single i) { // Some code } } Which of the following statements is correct?Answer: Interfaces members are automatically public. Which of the following statements are correct about an interface used in C#.NET? 1. An interface can contain properties, methods and events. 2. The keyword must implement forces implementation of an interface. 3. Interfaces can be overloaded. 4. Interfaces can be implemented by a class or a struct. 5. Enhanced implementations of an interface can be developed without breaking existing code.Answer: 1, 4, 5 Which of the following can implement an interface? 1. Data 2. Class 3. Enum 4. Structure 5. NamespaceAnswer: 2, 4 Which of the following statements is correct about the C#.NET code snippet given below? interface IMyInterface { void fun1(); void fun2(); } class MyClass: IMyInterface { private int i; void IMyInterface.fun1() { // Some code } }Answer: The compiler will report an error since the interface IMyInterface is only partially implemented. Which of the following statements is correct about the C#.NET code snippet given below? interface IPerson { String FirstName { get; set; } String LastName { get; set; } void Print(); void Stock(); int Fun(); }Answer: This is a perfectly workable interface. Which of the following is the correct way to implement the interface given below? interface IPerson { String FirstName { get; set; } }Answer: class Employee : IPerson { private String str; public String FirstName { get { return str; } set { str = value; } } } Which of the following unary operators can be overloaded? 1. true 2. false 3. + 4. new 5. isAnswer: 1, 2, 3 A derived class can stop virtual inheritance by declaring an override asAnswer: sealed Which of the following keyword is used to change the data and behavior of a base class by replacing a member of a base class with a new derived member?Answer: new Which of the following statements is correct?Answer: When used as a modifier, the new keyword explicitly hides a member inherited from a base class. Which of the following keyword is used to overload user-defined types by defining static member functions?Answer: operator Which of the following is the correct way to overload + operator?Answer: public static sample operator + ( sample a, sample b ) Which of the following statements is correct?Answer: When overriding a method, the names and type signatures of the override method must be the same as the virtual method that is being overriden. Which of the following statements are correct? 1. All operators in C#.NET can be overloaded. 2. We can use the new modifier to modify a nested type if the nested type is hiding another type. 3. In case of operator overloading all parameters must be of the different type than the class or struct that declares the operator. 4. Method overloading is used to create several methods with the same name that performs similar tasks on similar data types. 5. Operator overloading permits the use of symbols to represent computations for a type.Answer: 2, 5 Which of the following statement is correct about the C#.NET code snippet given below? public class Sample { public int x; public virtual void fun() { } } public class DerivedSample : Sample { new public void fun() { } }Answer: DerivedSample class hides the fun() method of base class. Which of the following statements is correct?Answer: The conditional logical operators cannot be overloaded. Which of the following operators cannot be overloaded? 1. true 2. false 3. new 4. ~ 5. sizeofAnswer: 3, 5 Which of the following modifier is used when a virtual method is redefined by a derived class?Answer: override In order for an instance of a derived class to completely take over a class member from a base class, the base class has to declare that member asAnswer: virtual Which of the following can be declared as a virtual in a class? 1. Methods 2. Properties 3. Events 4. Fields 5. Static fieldsAnswer: 1, 2, 3 Which of the following statements is correct?Answer: If a derived class does not provide its own version of virtual method then the one in the base class is used. Which of the following are necessary for Run-time Polymorphism? 1. The overridden base method must be virtual, abstract or override. 2. Both the override method and the virtual method must have the same access level modifier. 3. An override declaration can change the accessibility of the virtual method. 4. An abstract inherited property cannot be overridden in a derived class. 5. An abstract method is implicitly a virtual method.Answer: 1, 2, 5 Which of the following is NOT a .NET Exception class?Answer: StackMemoryException Which of the following statements is correct about an Exception?Answer: It occurs at run-time. In C#.NET if we do not catch the exception thrown at runtime then which of the following will catch it?Answer: CLR Which of the following statements is correct about the C#.NET program given below? using System; namespace IndiabixConsoleApplication { class MyProgram { static void Main(string[] args) { int index = 6; int val = 44; int[] a = new int[5]; try { a[index] = val ; } catch(IndexOutOfRangeException e) { Console.Write("Index out of bounds "); } Console.Write("Remaining program"); } } }Answer: It will output: Index out of bounds Remaining program