C# OOP MCQ

1. What is encapsulation in C#?

a) Breaking a program into multiple parts
b) Hiding the internal state of an object and requiring all interaction to be performed through an object's methods
c) The ability of a class to extend the functionality of another class
d) A class that can be used to create objects

Answer:

b) Hiding the internal state of an object and requiring all interaction to be performed through an object's methods

Explanation:

Encapsulation involves hiding the internal state of an object and exposing only necessary methods for interaction, enhancing security and ease of maintenance.

2. What does the 'public' keyword denote in C#?

a) The method can only be accessed within its class
b) The variable is a constant
c) The method or property is accessible from any class
d) The method is static

Answer:

c) The method or property is accessible from any class

Explanation:

The 'public' keyword is an access modifier in C# that makes a method or property accessible from any class.

3. What is a class in C#?

a) A blueprint from which objects are created
b) A specific instance of an object
c) A method in object-oriented programming
d) A variable type

Answer:

a) A blueprint from which objects are created

Explanation:

A class in C# defines a blueprint for objects, specifying what data and behavior they have.

4. What is polymorphism in C#?

a) The ability of different classes to use methods of the same name in different ways
b) Splitting a class into multiple subclasses
c) Changing the data type of a variable
d) Protecting data from access by unauthorized code

Answer:

a) The ability of different classes to use methods of the same name in different ways

Explanation:

Polymorphism allows methods with the same name to be used differently in different classes, supporting concepts like method overriding and method overloading.

5. What is inheritance in C#?

a) Copying properties from one class to another
b) The process where one class acquires the properties and behaviors of another class
c) Using the same method in multiple classes
d) Storing data within a class

Answer:

b) The process where one class acquires the properties and behaviors of another class

Explanation:

Inheritance is an OOP feature where a new class (derived class) inherits properties and methods from an existing class (base class).

6. What is the purpose of constructors in C#?

a) To initialize objects of a class
b) To perform calculations
c) To connect to a database
d) To destroy objects of a class

Answer:

a) To initialize objects of a class

Explanation:

Constructors are special methods used to initialize objects of a class with a valid state.

7. What is an object in C#?

a) A variable in a class
b) An instance of a class
c) A method in a class
d) A property in a class

Answer:

b) An instance of a class

Explanation:

An object is an instance of a class that encapsulates data and behavior related to that data.

8. What is an abstract class in C#?

a) A class that cannot be instantiated and must be inherited
b) A class designed for creating objects
c) A class without any methods
d) A class used for data storage only

Answer:

a) A class that cannot be instantiated and must be inherited

Explanation:

An abstract class cannot be instantiated and is designed to be a base class for other classes.

9. What is the purpose of the 'this' keyword in C#?

a) To reference the current instance of a class
b) To destroy the current instance of a class
c) To create a new instance of a class
d) To exit the current method

Answer:

a) To reference the current instance of a class

Explanation:

The 'this' keyword in C# is used within an instance method or constructor to refer to the current instance of the class.

10. What does the term 'interface' refer to in C#?

a) A class that provides a default implementation of methods
b) A protocol that defines a set of methods and properties without implementing them
c) A method that connects two classes
d) A built-in data type

Answer:

b) A protocol that defines a set of methods and properties without implementing them

Explanation:

An interface in C# is a contract that defines a set of methods and properties, which implementing classes must define.

11. What is method overloading in C#?

a) Changing the return type of a method
b) Creating multiple methods with the same name but different parameters in the same class
c) Using the same method name in different classes
d) Creating a method that loads data

Answer:

b) Creating multiple methods with the same name but different parameters in the same class

Explanation:

Method overloading is defining methods with the same name but different parameter lists within the same class.

12. What is the use of the 'virtual' keyword in C#?

a) To declare a variable that can change types
b) To mark a method or property as overridable in a derived class
c) To create a virtual class
d) To define a method that must be implemented in a derived class

Answer:

b) To mark a method or property as overridable in a derived class

Explanation:

The 'virtual' keyword is used in a base class to indicate that a method or property can be overridden in a derived class.

13. What is method overriding in C#?

a) Renaming a method in a derived class
b) Providing a new implementation of a method in a derived class that already exists in the base class
c) Overloading a method in the same class
d) Changing the return type of a method in a derived class

Answer:

b) Providing a new implementation of a method in a derived class that already exists in the base class

Explanation:

Method overriding involves providing a new implementation in a derived class for a method that was defined in the base class.

14. What does the 'sealed' keyword do in C#?

a) Seals a method so it cannot be overridden
b) Seals a class so it cannot be extended
c) Protects a variable from being modified
d) Encrypts data within a class

Answer:

b) Seals a class so it cannot be extended

Explanation:

The 'sealed' keyword is used to prevent a class from being inherited or a virtual method from being overridden in derived classes.

15. What is a destructor in C#?

a) A method for destroying classes
b) A special method used to clean up before an object is garbage collected
c) A method used to delete variables
d) An overloaded version of a constructor

Answer:

b) A special method used to clean up before an object is garbage collected

Explanation:

Destructors in C# are special methods invoked automatically when an object is about to be destroyed or garbage collected, used for cleanup.

16. What is a property in C#?

a) A global variable in a class
b) A special type of class
c) A method that does not take any parameters
d) A member that provides a flexible mechanism to read, write, or compute the value of a private field

Answer:

d) A member that provides a flexible mechanism to read, write, or compute the value of a private field

Explanation:

Properties in C# are used to control access to class members, providing a way to get or set values while encapsulating implementation details.

17. What is the difference between a class and a struct in C#?

a) A class is a reference type, while a struct is a value type
b) There is no difference
c) Structs can have methods, while classes cannot
d) Classes are used for small objects, while structs are used for large objects

Answer:

a) A class is a reference type, while a struct is a value type

Explanation:

The key difference is that classes are reference types and structs are value types, affecting how they are stored and passed in memory.

18. What is an 'interface' in C#?

a) A collection of abstract methods
b) A class that cannot be instantiated
c) A protocol that defines a set of methods and properties without implementing them
d) A built-in data type

Answer:

c) A protocol that defines a set of methods and properties without implementing them

Explanation:

An interface in C# is a contract that specifies a set of methods and properties. Classes or structs that implement the interface agree to implement the specified members.

19. What is the purpose of the 'override' keyword in C#?

a) To replace a method in a base class with a new implementation in a derived class
b) To prevent a method from being overridden
c) To make a method available for overloading
d) To indicate that a method is optional to implement

Answer:

a) To replace a method in a base class with a new implementation in a derived class

Explanation:

The 'override' keyword is used in a derived class to provide a new implementation of a virtual method defined in a base class.

20. What does 'encapsulation' achieve in C# OOP?

a) It ensures that only necessary aspects of a class are exposed to other parts of the program
b) It allows a class to inherit from multiple base classes
c) It provides a mechanism to copy objects
d) It allows a method to change its behavior based on the input

Answer:

a) It ensures that only necessary aspects of a class are exposed to other parts of the program

Explanation:

In an Object Oriented programming language, encapsulation is one of the key language features. Encapsulation is the procedure of encapsulating data and functions into a single unit.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top