Saturday, January 26, 2008

OOPS Interview Questions:

1. What is Class?
A user-defined data structure that groups properties and methods. Class doesn’t occupy memory.

2. What is Object?
Instance of Class is called object. An object is created in memory using keyword “new”.

3. Difference between Struct and Class?
i) Struct are Value type and are stored on stack, while Class are Reference type and are stored on heap
ii) Struct “do not support” inheritance, while class supports inheritance. However struct can implements interface
iii) Struct should be used when you want to use a small data structure, while Class is better choice for complex data structure.

4. What is Encapsulation?
Wrapping up of data and function into a single unit is known as Encapsulation.

5. What is Properties?
Attribute of object is called properties. Eg1:- A car has color as property.
eg.: private string m_Color;
public string Color
{
get
{
return m_Color;
}
set
{
m_Color = value;
}
}
Car Maruti = new Car();
Maruti.Color= “White”;
Console.Write(Maruti.Color);

6. Use of "this" Keyword?
Each object has a reference “this” which points to itself. Two uses of this keyword are 1)Can be used to refer to the current object, 2)It can also be used by one constructor to explicitly invoke another constructor of the same class.

7. What is Constructor?
- A constructor is a special method whose task is to initialize the object of its class
- It is special because its name is the same as the class name.
- They do not have return types, not even void and therefore they cannot return values.
- They cannot be inherited, though a derived class can call the base class constructor
- Constructor is invoked whenever an object of its associated class is created
Note: There is always atleast one constructor in every class. If you do not write a constructor, C# automatically provides one for you, this is called default constructor. Eg: class A, default constructor is A().

8. What is Static Members of the class?
-Static members belong to the whole class rather than to individual object
-Static members are accessed with the name of class rather than reference to objects.
Eg:
class Test
{
public int rollNo;
public int mathsMarks;
public static int totalMathMarks;
}
class TestDemo
{
public static void main()
{
Test stud1 = new Test();
stud1.rollNo = 1;
stud1.mathsMarks = 40;
stud2.rollNo = 2;
stud2.mathsMarks = 43;
Test.totalMathsMarks = stud1.mathsMarks + stud2.mathsMarks;
}
}

9. What is Static Method of the class?
Methods that you can call directly without first creating an instance of a class.
Eg: Main() Method, Console.WriteLine()

10. what is Destructor?
A destructor is just opposite to constructor. It has same as the class name, but with prefix ~ (tilde). They do not have return types, not even void and therefore they cannot return values.

11. What is Garbage Collection?
Garbage collection is the mechanism that reclaims the memory resources of an object when it is no longer referenced by a variable.
.Net Runtime performs automatically performs garbage collection, however you can force the garbage collection to run at a certain point in your code by calling System.GC.Collect()

12. Use of Enumeration?
Enumeration improves code readability. It also helps in avoiding typing mistake.

13. Concept of Heap and Stack:
The Program Instruction and Global and Static variables are stored in a region known as permanent storage area and the local variables are stored in another area called stack. The memory space located between these two regions is available for dynamic memory allocation during execution of program. This free memory region is called heap. The size of heap keeps on changing when program is executed due to creation and death of variables that are local to functions and blocks. Therefore, it is possible to encounter memory “overflow” during dynamic allocation process.

14. What is Value Type and Reference Type?
A variable is value type or reference type is solely determined by its data type.
Eg: int, float, char, decimal, bool, decimal, struct, etc are value types,
object type such as class, String, Array, etc are reference type.

15. what is Boxing and Un-Boxing?
Boxing: means converting value-type to reference-type.
Eg:
int I = 20;
string s = I.ToSting();
UnBoxing: means converting reference-type to value-type.
Eg:
int I = 20;
string s = I.ToString(); //Box the int
int J = Convert.ToInt32(s); //UnBox it back to an int.
Note: Performance Overheads due to boxing and unboxing as the boxing makes a copy of value type from stack and place it inside an object of type System.Object in the heap.

16. What is Inheritance?
The process of sub-classing a class to extend its functionality is called Inheritance.It provides idea of reusability.

17. What are Sealed Classes in C#?
The sealed modifier is used to prevent derivation from a class. A compile-time error occurs if a sealed class is specified as the base class of another class. (A sealed class cannot also be an abstract class)

18. Can you prevent your class from being inherited by another class?
Yes. The keyword “sealed” will prevent the class from being inherited.

19. Can you allow a class to be inherited, but prevent the method from being over-ridden?
Yes. Just leave the class public and make the method sealed.

20. List of Facts in Inheritance?
- Multiple inheritance of classes is not allowed in C#.
- In C# you can implements more than one interface, thus multiple inheritance is achieved through interface.
- The Object class defined in the System namespace is implicitly the ultimate base class of all the classes in C# (and the .NET framework)
- Structures (struct) in C# does not support inheritance, it can only implements interfaces.

21. What is Polymorphism?
Polymorphism means same operation may behave differently on different classes.
Eg:
Method Overloading is an example of Compile Time Polymorphism.
Method Overriding is an example of Run Time Polymorphism

22. How many types of Access Modifiers?
1) Public – Allows the members to be globally accessible
2) Private – Limits the member’s access to only the containing
3) Protected – Limits the member’s access to the containing type and all classes derived from the containing type
4) Internal – Limits the member’s access to within the current project

23. What is Method Overloading?
Method with same name but with different arguments is called method overloading. Method Overloading forms compile-time polymorphism.
Eg. class A1
{
void hello()
{
Console.WriteLine(“Hello”);
}
void hello(string s)
{
Console.WriteLine(“Hello {0}”,s);
}
}

24. What is Method Overriding?
Method overriding occurs when child class declares a method that has the same type arguments as a method declared by one of its superclass. Method overriding forms Run-time polymorphism.
Eg.
Class parent
{
virtual void hello()
{
Console.WriteLine(“Hello from Parent”);
}
}
Class child : parent
{

override void hello()
{
Console.WriteLine(“Hello from Child”);
}
}
static void main()

{
parent objParent = new child();
objParent.hello();
}
//Output

Hello from Child.

25. What is Virtual Method?
By declaring base class function as virtual, we allow the function to be overridden in any of derived class.
Eg:
Class parent
{
virtual void hello()
{
Console.WriteLine(“Hello from Parent”);
}
}
Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”);
}
}
static void main()
{
parent objParent = new child();
objParent.hello();
}
//Output
Hello from Child.

26. What is Interface?
- Interface states “what” to do, rather than “how” to do.
- An interface defines only the members that will be made available by an implementing object. The definition of the interface states nothing about the implementation of the members, only the parameters they take and the types of values they will return. Implementation of an interface is left entirely to the implementing class. It is possible, therefore, for different objects to provide dramatically different implementations of the same members.

Eg.
public interface IDrivable
{
void GoForward(int Speed);
}
public class Truck : IDrivable
{
public void GoForward(int Speed)
{
// Implementation omitted
}
}
public class Aircraft : IDrivable
{
public void GoForward(int Speed)
{
// Implementation omitted
}
}
public class Train : IDrivable
{
public void GoForward(int Speed)
{
// Implementation omitted
}
}

27. Difference between Interface and Abstract Class?
Multiple inheritance:
A class may implement several interfaces.
A class may extend only one abstract class.
Default implementation:
An interface cannot provide any code at all, much less default code.
An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.
Third party convenience:
An interface implementation may be added to any existing third party class.
A third party class must be rewritten to extend only from the abstract class.

No comments: