C# INTERVIEW QUESTIONS (OOP) FOR JOB

1. What is C#? 

C# is the best language for writing Microsoft .NET applications. C# provides the rapid application development found in Visual Basic with the power of C++. Its syntax is similar to C++ syntax and meets 100% of the requirements of OOPs like the following:

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance

2. What is an Object?

According to MSDN, "a class or struct definition is like a blueprint that specifies what the type can do. An object is basically a block of memory that has been allocated and configured according to the blueprint. A program may create many objects of the same class. Objects are also called instances, and they can be stored in either a named variable or in an array or collection. The client code is the code that uses these variables to call the methods and access the public properties of the object. In an object-oriented language such as C#, a typical program consists of multiple objects interacting dynamically".

3. What is Managed or Unmanaged Code?

Managed Code

“The code, which is developed in the .NET framework is known as managed code. This code is directly executed by CLR with the help of managed code execution. Any language that is written in .NET Framework is managed code”.

Unmanaged Code

The code, which is developed outside .NET framework is known as unmanaged code.

“Applications that do not run under the control of the CLR are said to be unmanaged, and certain languages such as C++ can be used to write such applications, which, for example, access low - level functions of the operating system. Background compatibility with the code of VB, ASP, and COM are examples of unmanaged code”.

Unmanaged code can be unmanaged source code and unmanaged compile code. Unmanaged code is executed with the help of wrapper classes.

Wrapper classes are of two types:

CCW (COM Callable Wrapper).
RCW (Runtime Callable Wrapper).

CLR - CLR is the heart of the .NET framework and it does 4 primary important things:

  1. Garbage collection
  2. CAS (Code Access Security)
  3. CV (Code Verification)
  4. IL to Native translation

4. What are Boxing and Unboxing?

Answer: Boxing and Unboxing both are used for type conversion but have some difference:

Boxing:

Boxing is the process of converting a value type data type to the object or to any interface data type which is implemented by this value type. When the CLR boxes a value means when CLR is converting a value type to Object Type, it wraps the value inside a System.object and stores it on the heap area in the application domain.

Example:



Unboxing:

Unboxing is also a process which is used to extract the value type from the object or any implemented interface type. Boxing may be done implicitly, but unboxing has to be explicit by code.

Example:



The concept of boxing and unboxing underlines the C# unified view of the type system in which a value of any type can be treated as an object.

5. What is the difference between a struct and a class in C#?

Answer:
Class and struct both are the user-defined data type but have some major difference:

Struct


  • The struct is value type in C# and it inherits from System.value Type.
  • The struct is usually used for smaller amounts of data.
  • Struct can’t be inherited to other types.
  • A structure can't be abstract.
  • No need to create an object by new keyword.
  • Do not have permission to create any default constructor.

Class


  • The class is a reference type in C# and it inherits from the System.object Type.
  • Classes are usually used for large amounts of data.
  • Classes can be inherited to other class.
  • A class can be an abstract type.
  • The object of a class created using the new keyword.
  • We can create a default constructor.


6. What is the difference between Interface and Abstract Class?

Answer:
Theoretically, there are some differences between Abstract Class and Interface which are listed below:

Interface

  • A class can implement any number of (multiple) interfaces 
  • In case of interface, all the methods have to be abstract. All methods have just declaration and child-class has to provide the definition of all methods.
  • An interface is not allowed to declare the variable.
  • In interface, all member or functions are public by default.
  • In an interface, we don’t need to use abstract keyword.
  • An interface can be used as multiple inheritance.
  • An interface we don’t have any type of constructor.

Abstract Class

  • a subclass can at most use only one abstract class
  • An abstract class can have non-abstract methods (concrete methods)
  • An abstract class can declare or use any variables 
  • In an abstract class, all data member or functions are private by default 
  • In an abstract class, we need to use the abstract keyword to declare abstract methods
  • An abstract class can’t be used for multiple inheritances
  • An abstract class use constructor

7. What is enum in C#?

Answer:
An enum is a value type with a set of related named constants often referred to as an enumerator list. The enum keyword is used to declare an enumeration. It is a primitive data type, which is user-defined.

An enum type can be an integer (float, int, byte, double etc.). But if you used beside int it has to be cast.

An enum is used to create numeric constants in .NET framework. All the members of the enum are of the enum type. There must be a numeric value for each enum type.

The default underlying type of the enumeration element is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

  1. enum Dow {Sat, Sun, Mon, Tue, Wed, Thu, Fri};  


Some points about enum

  • Enums are enumerated data type in c#.
  • Enums are not for end-users, they are meant for developers.
  • Enums are strongly typed constant. They are strongly typed, i.e. an enum of one type may not be implicitly assigned to an enum of another type even though the underlying value of their members are the same.
  • Enumerations (enums) make your code much more readable and understandable.
  • Enum values are fixed. Enum can be displayed as a string and processed as an integer.
  • The default type is int, and the approved types are byte, sbyte, short, ushort, uint, long, and ulong.
  • Every enum type automatically derives from System.enum and thus we can use System.enum methods on enums.
  • Enums are value types and are created on the stack and not on the heap.

8. What is the difference between “continue” and “break” statements in C#?

Answer: 
Using break statement, you can 'jump out of a loop' whereas by using continue statement, you can 'jump over one iteration' and then resume your loop execution.

Break Statement 

  1. using System;  
  2. using System.Collections;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace break_example   
  7.     {  
  8.         Class brk_stmt {  
  9.             public static void main(String[] args) {  
  10.                 for (int i = 0; i <= 5; i++) {  
  11.                     if (i == 4) {  
  12.                         break;  
  13.                     }  
  14.                     Console.WriteLine("The number is " + i);  
  15.                     Console.ReadLine();
  16.   
  17.                 }  
  18.             }  
  19.         }  
  20.   
  21.     } 

Output 

The number is 0; The number is 1; The number is 2; The number is 3;

Continue Statement

  1. using System;  
  2. using System.Collections;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace continue_example  
  7. {  
  8.     Class cntnu_stmt   
  9.     {  
  10.         public static void main(String[]   
  11.         {  
  12.             for (int i = 0; i <= 5; i++)   
  13.             {  
  14.                 if (i == 4)   
  15.                 {  
  16.                     continue;  
  17.                 }  
  18.                 Console.WriteLine(“The number is "+ i); 
  19.                 Console.ReadLine();
  20.                   
  21.             }  
  22.         }  
  23.     }  
  24.       
  25. }  
  26.   


Output



The number is 1;

The number is 2;

The number is 3;

The number is 5; 



9. What is the difference between constant and read-only in c#?




Answer: 



Constant (const) and Read-only (read-only) both look like same as per the uses but they have some differences: 


Constant is known as “const” keyword in C# which is also known immutable values which are known at compile time and do not change their values at runtime like in any function or constructor for the life of application till the application is running.

Read-only is known as “read-only” keyword in C# which is also known immutable values and are known at compile and runtime and do not change their values at runtime like in any function for the life of application till the application is running. You can assay their value by constructor when we call the constructor with “new” keyword.

  1. class Test {  
  2.     readonly int read = 10;  
  3.     const int cons = 10;  
  4.     public Test() {  
  5.         read = 100;  
  6.         cons = 100;  
  7.     }  
  8.     public void Check() {  
  9.         Console.WriteLine("Read only : {0}", read);  
  10.         Console.WriteLine("const : {0}", cons);  
  11.     }  
  12. }  

Here I was trying to change the value of both the variables in the constructor but when I am trying to change the constant it gives an error to change their value in that block which has to call at runtime.

So finally remove that line of code from class and call this Check() function like the following code snippet:
  1. class Program {  
  2.     static void Main(string[] args) {  
  3.         Test obj = new Test();  
  4.         obj.Check();  
  5.         Console.ReadLine();  
  6.     }  
  7. }  
  8. class Test {  
  9.     readonly int read = 10;  
  10.     const int cons = 10;  
  11.     public Test() {  
  12.         read = 100;  
  13.     }  
  14.     public void Check() {  
  15.         Console.WriteLine("Read only : {0}", read);  
  16.         Console.WriteLine("const : {0}", cons);  
  17.     }  
  18. }  

Output:


10. What is the difference between ref and out keywords?


Answer: 

In C Sharp (C#) we can have three types of parameters in a function. The parameters can be in parameter (which is not returned back to the caller of the function), out parameter and ref parameter. We have lots of differences in both of them.




Reference






Comments

Post a Comment