Skip to content
QuiznExam

Constructors in C#

Test your knowledge of constructors in C#, including default constructors, parameterized constructors, static constructors, constructor overloading, inheritance, and object initialization.

15
Questions
12 min
Time limit
Medium
Difficulty

No sign-up needed · Last updated:

Save your score

At least 8 characters.

Forgot password?

Related quizzes

All 15 questions and answers Spoilers: this shows the questions and answers. Try the quiz first! Open
  1. 1. What is the primary purpose of a constructor in C#?

    • A. To initialize an object when it is created
    • B. To destroy an object
    • C. To define a namespace
    • D. To handle exceptions
    Show answer

    A. To initialize an object when it is created

    A constructor is called when an object is created and is commonly used to initialize the object's state.

  2. 2. Which characteristic distinguishes a constructor from a regular method in C#?

    • A. A constructor has the same name as its class and has no return type
    • B. A constructor must always return void
    • C. A constructor must have a different name from its class
    • D. A constructor must be declared static
    Show answer

    A. A constructor has the same name as its class and has no return type

    A constructor uses the class name and does not declare a return type, including void.

  3. 3. When is an instance constructor normally called?

    • A. When an instance of the class is created
    • B. When the class is compiled
    • C. When the application is installed
    • D. When the namespace is imported
    Show answer

    A. When an instance of the class is created

    Instance constructors execute as part of creating an instance of the class.

  4. 4. What happens if a class has no instance constructor declared and no constructor with parameters?

    • A. The compiler can provide a parameterless instance constructor
    • B. The class cannot be instantiated
    • C. A static constructor is automatically created instead
    • D. The class becomes abstract
    Show answer

    A. The compiler can provide a parameterless instance constructor

    If no instance constructors are declared, the compiler can provide a public parameterless constructor for a class.

  5. 5. Which of the following is a parameterized constructor?

    • A. public Student() { }
    • B. public Student(int id) { }
    • C. public void Student() { }
    • D. static Student() { }
    Show answer

    B. public Student(int id) { }

    A parameterized constructor accepts one or more parameters that can be used to initialize an object.

  6. 6. Can a C# class have multiple constructors with different parameter lists?

    • A. Yes
    • B. No
    • C. Only if all are static
    • D. Only in abstract classes
    Show answer

    A. Yes

    C# supports constructor overloading, allowing multiple constructors with different parameter lists.

  7. 7. What is constructor overloading?

    • A. Defining multiple constructors with different parameter lists
    • B. Calling the same constructor repeatedly
    • C. Creating constructors in different namespaces
    • D. Using a constructor as a method
    Show answer

    A. Defining multiple constructors with different parameter lists

    Constructor overloading allows a class to provide multiple ways to initialize objects by defining constructors with different parameter lists.

  8. 8. Which keyword is used to call another constructor in the same C# class?

    • A. base
    • B. this
    • C. self
    • D. parent
    Show answer

    B. this

    A constructor initializer such as : this(...) calls another constructor in the same class.

  9. 9. Which keyword is used in a constructor initializer to call a constructor of the base class?

    • A. this
    • B. base
    • C. parent
    • D. super
    Show answer

    B. base

    The base keyword can be used in a constructor initializer to invoke a constructor of the base class.

  10. 10. What is a static constructor used for in C#?

    • A. Initializing static data or performing one-time initialization for a type
    • B. Initializing every object instance separately
    • C. Destroying static objects
    • D. Overriding instance methods
    Show answer

    A. Initializing static data or performing one-time initialization for a type

    A static constructor performs initialization for a type and is executed automatically at most once per application domain or relevant type initialization context.

  11. 11. Which statement about a static constructor is correct?

    • A. It cannot have parameters
    • B. It must always be public
    • C. It can be called directly like an instance constructor
    • D. It must return void
    Show answer

    A. It cannot have parameters

    A static constructor cannot have parameters and cannot have an access modifier.

  12. 12. Can a constructor be inherited by a derived class?

    • A. Yes, all constructors are inherited
    • B. No, constructors are not inherited
    • C. Only public constructors are inherited
    • D. Only static constructors are inherited
    Show answer

    B. No, constructors are not inherited

    Constructors are not inherited by derived classes. A derived class can explicitly invoke an accessible base constructor.

  13. 13. What is the purpose of the `base(...)` constructor initializer in a derived class?

    • A. To invoke a constructor of the base class
    • B. To create a new namespace
    • C. To prevent inheritance
    • D. To call a static method
    Show answer

    A. To invoke a constructor of the base class

    The base constructor initializer allows a derived class constructor to explicitly select and pass arguments to an accessible base class constructor.

  14. 14. Which statement about constructors is correct?

    • A. Constructors can have return types
    • B. Constructors can be overloaded
    • C. Constructors must always be private
    • D. Constructors can be overridden
    Show answer

    B. Constructors can be overloaded

    C# supports constructor overloading, but constructors do not have return types and cannot be overridden.

  15. 15. What is the main difference between an instance constructor and a static constructor?

    • A. An instance constructor initializes an object instance, while a static constructor initializes type-level static state
    • B. A static constructor is inherited while an instance constructor is not
    • C. An instance constructor must always have parameters
    • D. A static constructor returns an object
    Show answer

    A. An instance constructor initializes an object instance, while a static constructor initializes type-level static state

    Instance constructors are associated with individual objects, while static constructors perform one-time initialization associated with the type itself.