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
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
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
What is the keyword used to declare a class in C#?
a) object
b) class
c) type
d) new
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();
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
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
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
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
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
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#
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
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
What is the correct syntax for a destructor in C#?
a) void DestructorName() { }
b) ~ClassName() { }
c) ClassName() { }
d) ~DestructorName() { }
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
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
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
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
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; }
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
What keyword is used to declare a property in C#?
a) private
b) field
c) property
d) get/set
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
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
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
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
Which of the following is a valid example of a read-only property in C#?