MCQs on Object-Oriented Programming (OOP) Basics | C#

Learn about Object-Oriented Programming (OOP) basics in C# with these 30 multiple-choice questions. Topics include classes, objects, constructors, destructors, properties, fields, and access modifiers.


Chapter 7: Object-Oriented Programming (OOP) Basics for C#

Classes and Objects

  1. What is a class in C#?
    • a) A collection of variables
    • b) A template for creating objects
    • c) A method that performs a task
    • d) A special type of array
  2. How do you create an object from a class in C#?
    • a) By using the new keyword
    • b) By defining a constructor
    • c) By creating a static method
    • d) By declaring a variable
  3. What is the keyword used to declare a class in C#?
    • a) object
    • b) class
    • c) type
    • d) new
  4. Which of the following is used to create an instance of a class in C#?
    • a) new ClassName();
    • b) ClassName();
    • c) create ClassName();
    • d) ClassName new();
  5. What is the main purpose of an object in C#?
    • a) To store data
    • b) To define methods
    • c) To represent real-world entities
    • d) To perform arithmetic operations
  6. What type of data does a class represent in C#?
    • a) Integer data
    • b) String data
    • c) Structured data (methods, properties, etc.)
    • d) Boolean data
  7. How do you access an object’s property in C#?
    • a) Using dot notation
    • b) Using square brackets
    • c) Using parentheses
    • d) Using curly braces
  8. What does the new keyword do when creating an object in C#?
    • a) Initializes the object
    • b) Declares the object
    • c) Assigns the object to a variable
    • d) All of the above

Constructors and Destructors

  1. What is the purpose of a constructor in C#?
    • a) To initialize an object when it is created
    • b) To destroy an object when it is no longer needed
    • c) To define the class properties
    • d) To modify the object’s properties
  2. Which of the following is true about constructors in C#?
  • a) A constructor has the same name as the class
  • b) A constructor can be called explicitly
  • c) A constructor must return a value
  • d) A constructor is not required in C#
  1. What happens if no constructor is defined in a class in C#?
  • a) The compiler automatically provides a default constructor
  • b) An error occurs
  • c) The class is incomplete
  • d) The class cannot be instantiated
  1. Which of the following is the purpose of a destructor in C#?
  • a) To initialize objects
  • b) To clean up resources when an object is destroyed
  • c) To define properties
  • d) To call methods on the class
  1. What is the correct syntax for a destructor in C#?
  • a) void DestructorName() { }
  • b) ~ClassName() { }
  • c) ClassName() { }
  • d) ~DestructorName() { }
  1. How is a destructor called in C#?
  • a) Automatically when the object is no longer referenced
  • b) Explicitly by the developer
  • c) Using the Dispose method
  • d) By invoking the Finalize method
  1. What is the difference between a constructor and a destructor in C#?
  • a) Constructors are used for initialization, destructors are used for cleanup
  • b) Constructors are optional, destructors are mandatory
  • c) Constructors are for classes, destructors are for objects
  • d) There is no difference
  1. How is a destructor related to the garbage collection process in C#?
  • a) It explicitly frees up memory
  • b) It helps to reclaim unused memory
  • c) It is automatically called during garbage collection
  • d) It does not interact with garbage collection

Properties and Fields

  1. What is the main difference between a field and a property in C#?
  • a) A field is a variable, while a property is a method that gets or sets a value
  • b) A field is a constant, while a property is a variable
  • c) A field cannot have a value, while a property can
  • d) A field is always public, while a property is always private
  1. Which of the following is the correct syntax for defining a property in C#?
  • a) public int Age { get; set; }
  • b) public int Age = 20;
  • c) int Age { get; set; }
  • d) public Age { int get; set; }
  1. How are fields in C# typically declared?
  • a) With a public access modifier
  • b) Inside methods
  • c) Inside constructors
  • d) Inside classes with an access modifier
  1. What keyword is used to declare a property in C#?
  • a) private
  • b) field
  • c) property
  • d) get/set
  1. What is the default value of a field in C# if not initialized?
  • a) 0 for numeric types, null for reference types
  • b) 0 for all types
  • c) null for all types
  • d) false for boolean types
  1. What is the advantage of using properties instead of fields in C#?
  • a) Properties provide more control over how values are assigned or retrieved
  • b) Fields allow better performance than properties
  • c) Properties are easier to use than fields
  • d) There is no advantage
  1. Can a field in C# be made read-only?
  • a) Yes, by using the readonly keyword
  • b) No, fields are always read-write
  • c) Yes, by using the private keyword
  • d) No, fields cannot be read-only
  1. Which of the following is true about properties in C#?
  • a) A property can have different access levels for get and set
  • b) A property must always have both get and set accessors
  • c) Properties cannot be used to validate data
  • d) Properties are not allowed in structs
  1. Which of the following is a valid example of a read-only property in C#?
  • a) public int Age { get; }
  • b) public int Age { set; }
  • c) public int Age { get; set; }
  • d) public int Age { readonly get; }

Access Modifiers (public, private, protected, internal)

  1. Which access modifier makes a member accessible only within its class?
  • a) private
  • b) public
  • c) protected
  • d) internal
  1. What is the default access modifier for class members if none is specified?
  • a) private
  • b) protected
  • c) public
  • d) internal
  1. Which access modifier makes a member accessible within its class and by derived classes?
  • a) protected
  • b) private
  • c) internal
  • d) public
  1. What is the purpose of the internal access modifier in C#?
  • a) It allows access to members within the same assembly
  • b) It allows access to members from any class
  • c) It restricts access to the class itself
  • d) It restricts access to derived classes
  1. Which access modifier allows a class member to be accessible from any code in the same assembly or outside it?
  • a) public
  • b) private
  • c) protected
  • d) internal

Answer Key

QnoAnswer
1b) A template for creating objects
2a) By using the new keyword
3b) class
4a) new ClassName();
5c) To represent real-world entities
6c) Structured data (methods, properties, etc.)
7a) Using dot notation
8d) All of the above
9a) To initialize an object when it is created
10a) A constructor has the same name as the class
11a) The compiler automatically provides a default constructor
12b) To clean up resources when an object is destroyed
13b) ~ClassName() { }
14a) Automatically when the object is no longer referenced
15a) Constructors are used for initialization, destructors are used for cleanup
16c) It is automatically called during garbage collection
17a) A field is a variable, while a property is a method that gets or sets a value
18a) public int Age { get; set; }
19d) Inside classes with an access modifier
20d) get/set
21a) 0 for numeric types, null for reference types
22a) Properties provide more control over how values are assigned or retrieved
23a) Yes, by using the readonly keyword
24a) A property can have different access levels for get and set
25a) public int Age { get; }
26a) private
27a) private
28a) protected
29a) It allows access to members within the same assembly
30a) public

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top