Top 30 Interview Questions and Answers on Constructors
(Simplified)
1. What is a
Constructor?
A constructor is a special method that gets
called when you create an object from a class. It helps set up the object with
default or specific values.
2. Types of
Constructors?
1. Default Constructor – No parameters.
2. Parameterized Constructor – Accepts values.
3. Copy Constructor – Copies another object.
4. Static Constructor – Sets static values. Runs once.
5. Private Constructor – Used to stop outside object creation.
3. What is a
Default Constructor?
A constructor without any parameters. It
gives default values to object properties.
4. What is a
Parameterized Constructor?
This constructor accepts arguments so you
can set specific values when creating an object.
5. Can you
overload constructors?
Yes. You can create multiple constructors
with different parameter lists. This is called constructor overloading.
6. What is a
Copy Constructor?
It creates a new object by copying the
values of an existing object.
7. What is a
Static Constructor?
A special constructor that runs only once
to initialize static data. You can't call it manually.
8. Can
constructors be private?
Yes. Private constructors are used in
Singleton patterns to restrict object creation.
9. Difference
between Constructor and Method?
Constructor: Same name as class, no return
type, runs automatically.
Method: Can have any name, has return type, runs when called.
10. Can
constructors be inherited?
No, but you can call the base class
constructor using 'base()' from the child class.
11. Why use
constructor chaining?
To avoid repeating code. You can call one
constructor from another using 'this()'.
12. What is
constructor initializer in C#?
It lets you call another constructor of the
same or base class using 'this()' or 'base()'.
13. Can you
call a constructor from another constructor?
Yes, using 'this()' keyword to reuse
initialization code.
14. What
happens if no constructor is defined?
The compiler automatically creates a
default constructor.
15. Can a
constructor be abstract?
No, constructors can’t be abstract because
they are never overridden.
16. Can a
constructor be virtual?
No, constructors can't be virtual. You
can’t override them.
17. What is
constructor overloading?
It means having more than one constructor
with different parameters.
18. Why are
static constructors parameterless?
Because they are called automatically and
can’t take any arguments.
19. Can a
constructor throw exceptions?
Yes, you can use 'throw' inside a
constructor if something goes wrong.
20. Is
constructor required in every class?
No. If you don’t define one, a default
constructor is provided by the compiler.
21. What is
the use of 'base()' in constructors?
It is used in a child class to call the
parent class constructor.
22. What
happens when an object is created?
Memory is reserved and the constructor runs
to set initial values.
23. Can we
use access modifiers in constructors?
Yes, you can use 'public', 'private', or
'protected' with constructors.
24. What is
the role of constructor in Dependency Injection?
It helps pass required objects
(dependencies) to a class using parameters.
25. Can we
use 'base' and 'this' together in a constructor?
No, you can use only one at a time in a
constructor initializer.
26. What is
the order of constructor calls in inheritance?
First the base class constructor runs, then
the child class constructor.
27. Can we
define logic in constructors?
Yes, but only simple setup logic. Avoid
putting complex operations in constructors.
28. What are
common uses for private constructors?
Used in Singleton pattern or static classes
to prevent object creation.
29. What’s
the benefit of constructor injection?
It makes sure that required data is
provided and makes the code easier to test.
30. What is
the Telescoping Constructor Pattern?
A pattern where one constructor calls another with more parameters, making step-by-step object creation.
Comments
Post a Comment