Classes and Objects in C#
15 questions · 12 min · Medium
Test your knowledge of constructors in C#, including default constructors, parameterized constructors, static constructors, constructor overloading, inheritance, and object initialization.
No sign-up needed · Last updated:
Score: 0 00:00
Create a free account to keep your scores and track your progress. It is optional.
15 questions · 12 min · Medium
15 questions · 12 min · Medium
20 questions · 15 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
15 questions · 12 min · Medium
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.
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.
A. When an instance of the class is created
Instance constructors execute as part of creating an instance of the class.
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.
B. public Student(int id) { }
A parameterized constructor accepts one or more parameters that can be used to initialize an object.
A. Yes
C# supports constructor overloading, allowing multiple constructors with different parameter lists.
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.
B. this
A constructor initializer such as : this(...) calls another constructor in the same class.
B. base
The base keyword can be used in a constructor initializer to invoke a constructor of the base class.
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.
A. It cannot have parameters
A static constructor cannot have parameters and cannot have an access modifier.
B. No, constructors are not inherited
Constructors are not inherited by derived classes. A derived class can explicitly invoke an accessible base constructor.
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.
B. Constructors can be overloaded
C# supports constructor overloading, but constructors do not have return types and cannot be overridden.
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.