1.what is a Constructor ?
Constructor is a method that is called when an instance of an object is created.
They have the same name as a class.
eg-
class Test
{
public Test()
{
}
}
Constructor is a method that is called when an instance of an object is created.
They have the same name as a class.
eg-
class Test
{
public Test()
{
}
}
2.Can a constructor have access modifier ?
Yes .The foll are access modifiers allowed -
1.Public --This is called whenever a class is initialized.
2.Private- This will prevent the class from being instantiated(creating objects)
3.Protected-
4.Internal--cannot be instantiated outside the assembly.
Yes .The foll are access modifiers allowed -
1.Public --This is called whenever a class is initialized.
2.Private- This will prevent the class from being instantiated(creating objects)
3.Protected-
4.Internal--cannot be instantiated outside the assembly.
3.If a class has parametrized constructor do we need to have default constructor?
c# provides a default constructor with no parameters if there are no explicit ones. But if we
have a parameterized constructor we need to specify a defualt one with no parameters.
c# provides a default constructor with no parameters if there are no explicit ones. But if we
have a parameterized constructor we need to specify a defualt one with no parameters.
4.what is static constructor?
5.Can constructors be inherited ?
No.
No.
6.How can you call a base class constructor from a child class ?
Child class can call base class constructor by using the keyword base.
Child class can call base class constructor by using the keyword base.
7.In which order constructor is called in inheritance ?
If Class B inherits Class A ,when you create object of Class B then Class B contructor is called first then Class A construcotr is called.
eg-
class ClassA
{
public ClassA()
{
Console.WriteLine("i am class A");
}
}
class ClassB:ClassA
{
public ClassB()
{
Console.WriteLine("i am class B");
}
}
output- i am class B
i am class A
If Class B inherits Class A ,when you create object of Class B then Class B contructor is called first then Class A construcotr is called.
eg-
class ClassA
{
public ClassA()
{
Console.WriteLine("i am class A");
}
}
class ClassB:ClassA
{
public ClassB()
{
Console.WriteLine("i am class B");
}
}
output- i am class B
i am class A
8.What is Copy construtor . Do you have this in C#?
c# does not provide copy constructor.
9.Can constructors be overloaded ?
Yes.
c# does not provide copy constructor.
9.Can constructors be overloaded ?
Yes.
10.How do you overload a constructor?
Overload a constructor by specifying diff parameters.
eg-
class numbers
{
public numbers() {
}
public numbers(int a){
}
}
11.How do you call one constructor from another in the same class ?
By using this keyword. eg-
class numbers
{
public numbers() {
}
public numbers(int a):this(){
}
}
12.When you create an object of the above class which constructor is called
first ?
When you instantiate the above class first overloaded constructor is called and then the
default one is called.
13.Can a constructor call itself by using this keyword ?
N0 it will through an compile time error.
12.Does memory gets allocated when constructor is called ?
Yes using new operator.
13. what happens when you instantiate an object ?
When you create instance of a class using new operator foll happens-
New memory is allocated
instance is created on heap
reference is created on stack and passed on the the instantiated object.
14.What happens if "new" keyword fails while instantiating ?
it throws OutOfMemoryException.
14.What is default access modifier of constructor ?
Private
15.Is it Ok to throw an exception from a constructor ?
No its not preferred. Constructors are used mainly for initialization purpose.but there are
certain .Net Framewrok classes like DateTime,FileStream et which throw exception from
constructor.
Overload a constructor by specifying diff parameters.
eg-
class numbers
{
public numbers() {
}
public numbers(int a){
}
}
11.How do you call one constructor from another in the same class ?
By using this keyword. eg-
class numbers
{
public numbers() {
}
public numbers(int a):this(){
}
}
12.When you create an object of the above class which constructor is called
first ?
When you instantiate the above class first overloaded constructor is called and then the
default one is called.
13.Can a constructor call itself by using this keyword ?
N0 it will through an compile time error.
12.Does memory gets allocated when constructor is called ?
Yes using new operator.
13. what happens when you instantiate an object ?
When you create instance of a class using new operator foll happens-
New memory is allocated
instance is created on heap
reference is created on stack and passed on the the instantiated object.
14.What happens if "new" keyword fails while instantiating ?
it throws OutOfMemoryException.
14.What is default access modifier of constructor ?
Private
15.Is it Ok to throw an exception from a constructor ?
No its not preferred. Constructors are used mainly for initialization purpose.but there are
certain .Net Framewrok classes like DateTime,FileStream et which throw exception from
constructor.
7.In which order constructor is called in inheritance ?
ReplyDeleteThis Answer is wrong - Please verify.
yes IN Q7 first Constructor A is Called then B constructor
ReplyDeleteQ4 static constructor loaded during loading of class in CLR loader,before instantiation of class object.for example when you goes to college the college name is visible before becoming part of (instance of or before entry )college
ReplyDeleteQus: 14 Default access modifier for constructor is Public. check your answer
ReplyDeleteDefault access modifier for constructor is Private as per MSDN.
DeleteBy default constructor is private, have written a sample code and checked
DeleteThis comment has been removed by the author.
DeleteAll the answers provided here are not exact and correct answers. People preparing for interview don't follow this site.
ReplyDeleteIn Question 7,First Class A is called then second
ReplyDelete7.In which order constructor is called in inheritance ?
ReplyDeleteAnswer is wrong .Answer will be i am class A,i am class B
Question 7 have wrong answer the correct output would be
ReplyDeleteoutput- i am class A
i am class B
I agree with Gopal Singh. The output mentioned in the Question 7 will be possible when the constructors are declared as Static.
DeleteAllitTechnolgies Interview Question and Answers :
ReplyDeleteDotnet Interview Question and Answers
ans for Question 7 is wrong Parent class constructor called first : Refer Video 21 kudvenkat of c# tuts
ReplyDeleteeg.
class ClassA
{
static void Main()
{
ClassB objB = new ClassB();
}
public ClassA()
{
Console.WriteLine("i am class A");
}
}
class ClassB : ClassA
{
public ClassB()
{
Console.WriteLine("i am class B");
}
}
Ans 12 is incorrect
ReplyDeleteFirst the constructor that is called using this() operator will be called then the calling/parametrized constructor will be called
Please don't follow this blog
ReplyDeleteQ7 , Q12: answers are incorrect. Here are the correct answers verified by executing code:
ReplyDeleteQ7. Ans: First Class A is called then second
Q12. Ans: 1st constructor that is called using this() operator will be called then the calling/parametrized constructor will be called
Answer for Que. 7 is wrong. Correct answer is
ReplyDeletei am class A
i am class B
Inheritance always follow parent to child.
ReplyDeleteAnswer for que.12 is wrong.
ReplyDeleteclass numbers
{
public numbers() {
}
public numbers(int a):this(){
}
}
Answer for this is it is called first default and then overloaded constructor will be called
Please don't follow this blog
ReplyDeleteMostly answers are wrong
ReplyDelete