C# Object-Oriented Programming: Classes and Objects Essentials Quiz Quiz

Explore fundamental concepts of classes and objects in C# with this quiz, covering constructors, methods, properties, object instantiation, and class members. Perfect for beginners looking to reinforce their understanding of object-oriented programming principles within the C# language.

  1. Class Definition Basics

    Which of the following defines a basic class named Car in C#?

    1. def class Car { }
    2. class Car { }
    3. public void Car( )
    4. Car class { }

    Explanation: The correct syntax to define a class named Car in C# is 'class Car { }'. The other options are incorrect: 'Car class { }' reverses the order, 'def class Car { }' incorrectly uses the Python keyword 'def', and 'public void Car( )' is the syntax for a method, not a class.

  2. Object Instantiation

    How do you create a new instance of the Student class?

    1. Student student = new Student{};
    2. Student student = Student();
    3. Student student = new Student();
    4. student = create Student();

    Explanation: In C#, 'Student student = new Student();' is the correct way to instantiate an object of the Student class. 'Student student = Student();' omits 'new', 'student = create Student();' uses an invalid keyword, and 'Student student = new Student{};' lacks parentheses required for object instantiation.

  3. Access Modifiers

    Which access modifier allows fields or methods to be accessible only within the same class?

    1. protected
    2. private
    3. internal
    4. public

    Explanation: 'private' restricts access so the field or method is only accessible within its own class. 'public' makes members accessible from anywhere, 'protected' allows access within the class and derived classes, while 'internal' permits access within the same assembly but not necessarily the same class.

  4. Properties

    Given 'public int Age { get; set; }', which of the following best describes 'Age'?

    1. Auto-implemented property
    2. Method
    3. Class field
    4. Static variable

    Explanation: 'Age' is an auto-implemented property that provides get and set accessors automatically. It is not a field, which would lack accessors. 'Method' refers to a function, and 'Static variable' is unrelated because no static keyword is present.

  5. Using Constructors

    What is the primary purpose of a constructor in a C# class?

    1. To return a value
    2. To delete the object
    3. To expose static members
    4. To initialize object data when an instance is created

    Explanation: A constructor is used to set up initial values or perform setup tasks when an object is instantiated. It does not delete objects (that’s the job of the garbage collector), does not return values, and is unrelated to exposing static members.

  6. Class vs Object

    Which statement is true about the relationship between a class and an object in C#?

    1. An object contains the definition of a class.
    2. Classes and objects are the same in C#.
    3. A class is a blueprint, and an object is an instance of that blueprint.
    4. A class is created from an object.

    Explanation: A class defines the structure and behaviors, serving as a blueprint, while an object is a specific instance of that class. An object does not contain the definition, classes are not made from objects, and classes and objects are not the same.

  7. Static Members

    What is the effect of declaring a method in a class as static?

    1. It can be called without creating an instance of the class.
    2. It can only access non-static members.
    3. It can be overridden by child classes by default.
    4. It can only be used inside the constructor.

    Explanation: A static method belongs to the class itself and can be called without an instance. It cannot access non-static members unless those are also static, it is not restricted to the constructor, and it cannot be overridden unless specifically allowed (and static methods cannot be overridden).

  8. Field Declaration

    Which of the following correctly declares a private integer field named 'count' in a C# class?

    1. private count int;
    2. int private count;
    3. private int count()
    4. private int count;

    Explanation: 'private int count;' is the correct way to declare a private integer field. 'int private count;' has the order of the keywords reversed, 'private count int;' is not valid C# syntax, and 'private int count()' is the syntax for a method, not a field.

  9. Method Declaration

    Which of the following correctly defines a public method named Display that returns no value in C#?

    1. void Display(public) { }
    2. public int Display() { }
    3. public void Display() { }
    4. public Display void() { }

    Explanation: 'public void Display() { }' is correct because 'void' specifies no return value and the syntax matches C# conventions. 'public int Display() { }' returns an integer rather than void, 'void Display(public) { }' misplaces the access modifier, and 'public Display void() { }' does not follow valid method definition syntax.

  10. Accessing Properties

    Assuming you have an object 'person' with a property 'Name', how would you access the Name property in C#?

    1. person.Name
    2. person[Name]
    3. person-u003EName
    4. person:Name

    Explanation: 'person.Name' is the correct way to access the Name property in C#. 'person-u003EName' uses pointer syntax from C++, 'person:Name' is not C# syntax, and 'person[Name]' uses bracket notation, which is used for indexers or arrays, not properties.