0% found this document useful (0 votes)
8 views

Interview question

The document provides an overview of key concepts in C# programming, including class access modifiers, inheritance, interfaces, constructors, destructors, polymorphism, method overloading, and method overriding. It explains the default behaviors and rules associated with these concepts, such as the inability to create instances of interfaces and abstract classes, and the use of keywords like 'sealed' and 'params'. Additionally, it covers the distinctions between different types of constructors and methods, including their accessibility and usage in object-oriented programming.

Uploaded by

Suraj Javarat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Interview question

The document provides an overview of key concepts in C# programming, including class access modifiers, inheritance, interfaces, constructors, destructors, polymorphism, method overloading, and method overriding. It explains the default behaviors and rules associated with these concepts, such as the inability to create instances of interfaces and abstract classes, and the use of keywords like 'sealed' and 'params'. Additionally, it covers the distinctions between different types of constructors and methods, including their accessibility and usage in object-oriented programming.

Uploaded by

Suraj Javarat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

CLASS

1. By default class follows which access modifier?


-Internal access modifier.

2. Can we declare class as private/protected/protected internal?


-No. we can declare class as public and internal.

3. By default class member and property which access modifier?


- Private.

ACCESS MODIFIER

1. Public
- Public member can be accessible in any project.

2. Private
- Members can be accessed within the same class only.

3. Protected
- Members can be accessed only derived class.

4. Internale
- Member can be accessed in same project and same assembly.

MAIN
- Main static mean it is entry point of console application.

OOPS

INHERTANCE

1. What is inheritance?
- One of the most important concepts in object oriented programming is inheritance.
Inheritance allows us to define a class in term of another class.
Inheritance is use to provide the code resuability.

2. How to achieve multiple inheritance?


- Interface

3. Why c# does not support multiple inheritance?


- Diamond and ambiguity - When 2 classes have same method then derived class will
be confused which method to refered.

4. In circular inheritance possible ? A:B , B:A


- No

5. How do you prevent a class from being inherited ?


- we can use sealed class.

6. Upcasting and Downcasting?


- Upcasting - Assigning the derived class object to base class this is impliciti.
Base b=new Derived();
- Downcasting - Assigning the base class object to derived class this is explicit
and it throw run time error. Base b=new Base(); c=(Derived)b;

SEALED

1. Can we create the object of Sealed class?


- we can create the object of sealed class.

2. Can derive class have public modifier when there are no modifiers specified on
the base class?
- No

3. Can we mark methods as Sealed?


- No

INTERFACE

1. What is the interface?


Interface just like a class but it can have signiture of method, properties, events
and indexers. its dose not containt the implementation on method.

2. By default interface is?


- Internal

3. Can we have access modifier in interface?


- No

4. By default interface member is?


- All methods are by default public in interface.

5. Can we have variables in interface?


- No

6. Can we create instance of interface?


- No

7. Can we create constructor in interface?


- No

8. Can we declare properties in interface?


- Yes

9. Can interface inherit another interface?


- Yes

10. Can interface inherit class?


- No

11. If 2 interface has same method then how to implement the method?
- Explicit implimentation.

12. What is implicit implementation and when to use in interface?


- Direct implimentation.

13. What is explicit implementation and when to use?


- When you having same method name in 2 different interface then use explicit
interface. Explicit interface use is "interface.method name".

14. Is it compulsory to implement all methods of interface?


- Yes

15. How to call interface implemented methods?


- we can call direct method throw object.

16. How to call explicit implemented methods of interface?


- we can call method throw interface. IEmployee e = New Employee();

CONSTRUCTOR

1. What is constructor?
- Constructor is special method of class which will invoke automatically whenever
instance or object of is created.
Constructor name should match with class and constructor dose not have any return
type.

2. Type constructor?
-
1. Default Constructor - A Constructor without having any parameters called default
constructor.
2. Parameteries Constructor - A Constructor with atleast one parameter called
parameteries constructor.
3. Copy Constructor - A Constructor is parameteries that contains a parameter as
same type of class called copy constructor. - Porpose (is to initialize new
instance to the values of exsting instance.)
4. Private Constructor - A Constructor with private access modifer called private
Constructor - Porpose (static member) - we can not create object of this class
5. Static Constracturor - A Constructor with static key word called static
constructor - Porpose (static constructor it is use to initialize the static field
of the class)

3. Use of parameterized constructor?


- construcator is that we can initialize the value at the time of creation of
object class.

4. What is constructor overloading?


- we can create two constructor with different parameter.

5. Is it mandatory to have default and parameterized constructor in class?


- No

6. If we have only parameterized constructor, can we create the object of class?


- Yes

7. Can we have default and private constructor both in class?


- No

8. Can we create the object of class if we have only private constructor in class?
- No

9. Can we inherit the class if we have only private constructor in class?


- No

10. Can we have parameters in private constructor?


- Yes

11. Can we have access modifier in static constructor?


- No

12. Can we pass parameters in static constructor?


- No

13. How to call static constructor or when static constructor is called?


- when we create the first instance of class automatically static constructor.
14. How many times we can call static constructor?
- Only one static constructor call the excution.

15. When we create instance of class, which constructor is called first?


- Static

16. How can we call one constructor from another in the same class ?
- : This keyword

17. How to call explicitly base class constructor from child class?
- : Base() keyword

18. Default access modifier/specifier for constructor ?


- Private

DESTRUCTOR

1. What is destructor?
- A destructor runs after a class becomes unreachable it has the special "~'
characters in its name.

2. Can we have access modifier in destructor?


- No we can not have access modifier.

3. Can we have parameters in destructor?


- No

4. How many destructor we can have in one class?


- Only one.

5. Can we define destructor in struct data type?


- No

6. Do we have any control when destructor will be called?


- No. GC.Collect.

7. Do we always need to implement the destructor?


- No

8. When we define destructor, which method of garbage collector is called?


- Finalize();

POLYMORPHISM

1. What is polymorphism?
- Polymorphism mean one name many form.
Same method name for different purpose called polymorphism.

METHOD OVERLOADING

1. Overloading
- Having two or more different with same name but different parameter in single
class.
Examples,
1.
public class Employee // example its working
{
public int Sum(int a, int b)
{
return a+b;
}

public int Sum(int a, int b, int c)


{
return a+b+c;
}
}

2.
public class Employee // confuse question - answer is not working.
{
public int Sum(int a, int b)
{
return a+b;
}

public string Sum(int a, int b) // different return type only


{
return a+b;
}
}

3.
public class Employee // example its working but it not method overloading - c# is
case sencitive.
{
public int Sum(int a, int b)
{
return a+b;
}

public int sum(int a, int b)


{
return a+b;
}
}

2. When we should use method overloading?


- When you need the couple of methods to take different parameters but do the same
method name.
ex,
searchEmployee(string firstname)
searchEmployee(string firstname, string lastname)

3. What are different names used for method overloading?


- Early binding, compiler time and static

METHOD OVERRIDING

1. What is Overriding?
- A overriding method provide a new implementation of a member that is inherited
from a base class.
A overriding is same name and same parameter but different implementation.

2. Is it mandatory to implement virtual method of base class in child class?


- No

3. Can we override non-virtual method?


- No

4. Can we use the virtual modifier with the static, abstract, private or override
modifiers.
- No

5. Can we use virtual for internal, protected and protected internal?


- Yes

6. What are different names/terms used for overriding:


- Late Binding , runtime , dyanamic

7. If parent class virtual method and child have same method without override
keyword, Will code be compiled?
- Yes

8. What is �new� keyword in method overriding?: method hiding


- Method hiding - we can predefine the method of parent class and child class by
implementing the concept of method hiding.

9. Can we allow a class to be inherited but prevent method to be overridden?


- we can use sealed keyword.

PARAMS

1. What is Params keyword?


- By using params keyword, you can specify a method parameter that takes a
variable number of argument.
params parameter should be last.

2. Order of params parameters?


- It must be placed at last in method

ABSTRACT

1. What is abstract class?


- Abstract class can have abstract as well as non abstract method. Abstract class
behaves like interface as well as class.

2. What is the abstract method?


- A abstract method is only singniture of a method. abstract method can not have
any implementation but it is posible in derived class.

3. Can we declare abstract method as private?


- NO

5. Can we create instance of abstract class? : No


6. Can abstract class inherit another class? : Yes
7. Can we declare Abstract class as static? : No
8. Can we declare Static methods in abstract class? Yes

7. Can abstract class inherit interface? : Yes


8. Can abstract class have constructor? : Yes

You might also like