Object Oriented in C#-Interview Questions

No.of Views2228
By Pankaj Kumar Gupta  On 10 Mar 2011
emailbookmarkadd commentsprint

1. What do you mean by Object Oriented Programming?

It is a problem solving technique to develop software systems. It’s a technique to think real world in terms of objects. Object maps the software model to real world concept. These objects have responsibilities and provide services to application or other objects.

2. What is a Class?

A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It is a comprehensive data type which represents a blue print of objects. It’s a template of object.

3. What is an Object?

It’s a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Objects are members of a class. Attributes and behavior of an object are defined by the class definition.

4. What are different properties provided by Object Oriented Systems?

Following are characteristics of Object Oriented Systems-

1). Abstraction

It allows complex real world to be represented in simplified manner. Example color is abstracted to RGB.By just making the combination of these three colors we can achieve any color in world. It’s a model of real world or concept.

2). Encapsulation

The process of hiding all the internal details of an object from the outside world.

3). Polymorphism

When inheritance is used to extend a generalized class to a more specialized class, it includes behavior of the top class (Generalized class).The inheriting class often implement a behavior that can be somewhat different than the generalized class, but the name of the behavior can be same. It is important that a given instance of an object use the correct behavior, and the property of polymorphism allows this to happen automatically.

4). Inheritance

Hierarchy is used to define more specialized classes based on a preexisting generalized class. Example we have Vehicle class and we can inherit this class make more specialized class like Car, which will add new attributes and use some existing qualities of the parent class. It’s a kind of parent child relationship.

5. What are Interface and/or Abstract Class?

Following are features of a abstract class:-

  • You cannot create a object of abstract class
  • Abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built.
  • Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited.
  • In C# .NET abstract classes are created using “Abstract” keyword.
  • Abstract classes can have implementation or pure abstract methods which should be implemented in the child class.

Interface is a contract that defines the signature of the functionality. So if a class is implementing a interface it says to the outer world, that it provides specific behavior. Example if a class is implementing IDisposable interface that means it has a functionality to release unmanaged resources. Now an external object using this class knows that it has contract by which it can dispose unused unmanaged objects.

  • Single Class can implement multiple interfaces.
  • If a class implements a interface then it has to provide implementation to all its methods.

6. What are the difference between Interface & abstract classes?

  • Abstract classes can have concrete methods while interfaces have no methods implemented.
  • Interfaces do not come in inheriting chain, while abstract classes come in inheritance
  • Refer next slide for all the differences

Can we define variables in interfaces?
–    No

Can we define static members in interfaces?
–    No

Can we apply access modifiers in the methods of interface?
–    No

Can we implement more than one interface in a class?
–    Yes

7. In which Scenario you will go for Interface or Abstract Class?

  • Interfaces, like classes, define a set of properties, methods, and events. But unlike classes, interfaces do not provide implementation. They are implemented by classes, and defined as separate entities from classes. Even though class inheritance allows your classes to inherit implementation from a base class, it also forces you to make most of your design decisions when the class is first published.
  • Abstract classes are useful when creating components because they allow you specify an invariant level of functionality in some methods, but leave the implementation of other methods until a specific implementation of that class is needed. They also version well, because if additional functionality is needed in derived classes, it can be added to the base class without breaking code.

8. Why an error is thrown while a method is written without a return type. What is the expected error?

Not all code paths return a value.

9. What is Overloading and/or Overriding?

Overloading is a feature found in object oriented programming languages that allows the creation of several functions with the same name which differ from each other in terms of the type of the input and the type of the output of the function.

An example of this would be a square function which takes a number and returns the square of that number. In this case, it is often necessary to create different functions for integer and floating point numbers.

Overriding, in object oriented programming, is a language feature that allows a subclass to provide a specific implementation of a method that is already provided by one of its superclasses. The implementation in the subclass overrides (replaces) the implementation in the superclass.

A Subclass can give its own definition of methods which also happen to have the same signature as the method in its Superclass. This means that the subclass's method has the same name and parameter list as the Superclass's overridden method. Constraints on the similarity of return type vary from language to language, as some languages support covariance on return types.

10. What is Operator Overloading in .Net?

It provides a way to define and use operators such as +, -, and / for user-defined classes or structures. It allows us to define/redefine the way operators work with our classes and structures.

This allows programmers to make their custom types look and feel like simple types such as int and string. VB.NET till now does not support operator overloading. Operator overloading is done by using the “Operator” keyword.

11. How we can call members of Superclass in Subclass?

Using “base” keyword

12. Where we use the “virtual” and “new” keywords?

Virtual functions implement the concept of polymorphism, except that you use the override keyword with the virtual function implementation in the child class. The parent class uses the same virtual keyword. Every class that overrides the virtual method will use the override keyword.

Use the new modifier to explicitly hide a member inherited from a base class. To hide an inherited member, declare it in the derived class using the same name, and modify it with the new modifier. You can define in a child class a new version of a function, hiding the one which is in base class. A new keyword is used to define a new version.

13. What does virtual keyword mean?

That method and property can be overridden.

14. 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.

15. What do you mean by Value-Type and Ref-Type?

Value types directly contain their data are either allocated on the stack or allocated in-line in a structure. Reference types store a reference to the value's memory address, and are allocated on the heap. Reference types can be self-describing types, pointer types, or interface types. Variables that are value types each have their own copy of the data, and therefore operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable. All types derive from the System. Object base type.

16. What is Boxing and Unboxing?

Boxing permits any value type to be implicitly converted to type object or to any interface type implemented by value type. Boxing is process in which a object instances created and copying value types value in to that instance.

Unboxing is vice versa of boxing operation where the value is copied from the instance in to appropriate storage location.

Below is sample code of boxing and unboxing where integer data type is converted in to object and then vice versa.

Dim x As Integer 
	Dim y As Object 
	x = 10 ‘ boxing process 
	y = x ‘ unboxing process x = y

17. What is StringBuilder Class?

This class represents a mutable string of characters. This class cannot be inherited. This class represents a string-like object whose value is a mutable sequence of characters. The value is said to be mutable because it can be modified once it has been created by appending, removing, replacing, or inserting characters

18. What is the difference between System.String and System.Stringbuilder Classes?

System.String is immutable; System.StringBuilder was designed with the purpose of having a mutable string where a variety of operations can be performed.

19. What are Generics?

Generics are a new feature in version 2.0 of the C# language and the common language runtime (CLR). Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.

Generics Overview:-

  • Use generic types to maximize code reuse, type safety, and performance.
  • The most common use of generics is to create collection classes.
  • The .NET Framework class library contains several new generic collection classes in the System.Collections.Generic namespace. These should be used whenever possible in place of classes such as ArrayList in the System.Collections namespace.
  • You can create your own generic interfaces, classes, methods, events and delegates.
  • Generic classes may be constrained to enable access to methods on particular data types.
  • Information on the types used in a generic data type may be obtained at run-time by means of reflection.

20. What are Generic Type Parameters?

In a generic type or method definition, a type parameters is a placeholder for a specific type that a client specifies when they instantiate a variable of the generic type.

GenericList<float> list1 = new GenericList<float>(); 
GenericList<ExampleClass> list2 = new GenericList<ExampleClass>(); 
GenericList<ExampleStruct> list3 = new GenericList<ExampleStruct>(); 

In each of these instances of GenericList<T>, every occurrence of T in the class will be substituted at run time with the type argument. By means of this substitution, we have created three separate type-safe and efficient objects using a single class definition.

21. What are Generic Classes?

Generic classes encapsulate operations that are not specific to a particular data type. The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees and so on where operations such as adding and removing items from the collection are performed in much the same way regardless of the type of data being stored.

22. What are Unsafe, Managed and Unmanaged Code in .NET?

Unsafe: The unsafe keyword denotes an unsafe context, which is required for any operation involving pointers.You can use the unsafe modifier in the declaration of a type or a member. The entire textual extent of the type or member is therefore considered an unsafe context.

Managed/Unmanaged Code: Managed code runs inside the environment of CLR i.e. .NET runtime. In short all IL are managed 7code. But if you are using some third party software example VB6 or VC++ component they are unmanaged code as .NET runtime (CLR) does not have control over the source code execution of the language.

Managed Code: is what Visual Basic .NET and C# compilers create, It compiles to Intermediate Language (IL), not to machine code that could run directly on your computer. The IL is kept in a file called an assembly, along with metadata that describes the classes, methods, and attributes (such as security requirements) of the code you've created. This assembly is the one-stop-shopping unit of deployment in the .NET world. You copy it to another server to deploy the assembly there—and often that copying is the only step required in the deployment.

Managed code runs in the Common Language Runtime. The runtime offers a wide variety of services to your running code. In the usual course of events, it first loads and verifies the assembly to make sure the IL is okay. Then, just in time, as methods are called, the runtime arranges for them to be compiled to machine code suitable for the machine the assembly is running on, and caches this machine code to be used the next time the method is called. (This is called Just In Time, or JIT compiling)

As the assembly runs, the runtime continues to provide services such as security, memory management, threading, and the like. The application is managed by the runtime.

Visual Basic .NET and C# can produce only managed code. If you're working with those applications, you are making managed code. Visual C++ .NET can produce managed code if you like: When you create a project, select one of the application types whose name starts with .Managed. such as Managed C++ application.

Unmanaged Code: is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6, heck, even that 15-year old C compiler you may still have kicking around on your hard drive all produced unmanaged code. It compiled directly to machine code that ran on the machine where you compiled it—and on other machines as long as they had the same chip, or nearly the same. It didn't get services such as security or memory management from an invisible runtime; it got them from the operating system. And importantly, it got them from the operating system explicitly, by asking for them, usually by calling an API provided in the Windows SDK. More recent unmanaged applications got operating system services through COM calls.

Unlike the other Microsoft languages in Visual Studio, Visual C++ can create unmanaged applications. When you create a project and select an application type whose name starts with MFC, ATL, or Win32, you're creating an unmanaged application.

This can lead to some confusion: When you create a .Managed C++ application., the build product is an assembly of IL with an .exe extension. When you create an MFC application, the build product is a Windows executable file of native code, also with an .exe extension. The internal layout of the two files is utterly different. You can use the Intermediate Language Disassembler, ildasm, to look inside an assembly and see the metadata and IL. Try pointing ILDASM at an unmanaged exe and you'll be told it has no valid CLR (Common Language Runtime) header and can't be disassembled—Same extension, completely different files.

23. How .Net Flow works like MSIL?

When compiling to managed code, the compiler translates your source code into Microsoft intermediate language (MSIL), which is a CPU-independent set of instructions that can be efficiently converted to native code. MSIL includes instructions for loading, storing, initializing, and calling methods on objects, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception handling, and other operations. Microsoft intermediate language (MSIL) is a language used as the output of a number of compilers and as the input to a just-in-time (JIT) compiler. The common language runtime includes a JIT compiler for converting MSIL to native code

24. What is Managed Heap?

The .NET framework includes a managed heap that all .NET languages use when allocating reference type objects. Lightweight objects known as value types are always allocated on the stack, but all instances of classes and arrays are created from a pool of memory known as the managed heap. 
Of all of the technologies to be found in .NET, the most controversial, seems to be garbage collection. A key part of the .NET framework, the managed heap and the garbage collection mechanism are foreign ideas to many of us.

25. What are the steps that GC follows for collection?

The basic algorithm used by the garbage collector is quite simple:-

  • Mark all managed memory as garbage
  • Look for used memory blocks, and mark them as valid
  • Discard all unused memory blocks
  • Compact the heap
About Author
Pankaj Kumar Gupta
Occupation-Software Engineer
Company-Miri Infotech (P) Ltd, India
Member Type-Junior
Joined date-27 Oct 2010
Home Page-www.codegain.com
Blog Page-codegain.com
- I am working with Miri Infotech (P) Ltd, India as Senior Software Engineer. - Having 5+ years experience in .NET Technologies (ASP.NET, C#.net, VB.net, SQL Server, XML, Web Services, MS Dynamics CRM, SharePoint). - I have masters M.Sc-Computer Science, M.Tech-Information Technology with Honours. - I have completed MCP, MCSD.NET, MCAD, MCDBA SQL Server, MCTS, MCPD-EAD - Microsoft Certified Professional & Technology Specialist - Obsessed in OOP, MVC, MVP style design and programming. - Designing and developing the client/server applications for a number of doimains. - Designing and implementing Business Planning Tools & Applications. - Good understanding of formal software engineering tools & technologies.
Other popular Interview Questions On CSharp
^ Scroll to Top