C#

Top 40 C# Interview Questions And Answers

1. What is C#?

C# is a high-level object-oriented programming language. It is used for building secure and robust applications.


2. What are the benefits of using C#?

  • Easy to learn
  • Fast development time
  • High scalability
  • Compiled on multiple computer platforms
  • Modularity for easier troubleshooting allows developers to work on multiple objects simultaneously

3. Can you name the types of comments in C#?

There are two types of comments in C#

Single line:

//contains only 1 line of code

Multiple line (/* */):

/*Line 1

Line 2

Last line*/


4. Can we execute multiple catch blocks in C# program for one exception?

No. You can’t use multiple catch blocks for same exception in C# because a catch block is preceded by a try block.


5. What is the difference between C# and C programming language?

C# supports object-oriented programming whereas C supports procedural programming.

6. Can you name the types of classes in C#?

There are mainly four types of classes in C#:
  • Abstract class
  • Partial class
  • Sealed class
  • Static class

7. Is C++ the same as C#?

No. C# is a high level programming language whereas C++ is a low level programming language. Another difference is C# compiles to CLR whereas C++ compiles to machine code.


8. Define Operators

Operators are a set of symbols that tells the compiler to perform an action.

9. What do you mean by throw statement in C#?

The throw statement allows you to manually throw an exception during the execution of a program.

10. Is it possible to get the array index using the for each loop?

No, it is not possible to get the array index using the for each loop. To access the array index, you need to use a standard for loop.

11. Can you inherit a class into another class?

Yes, it is possible to inherit a class into another.
It is of two types:
  • Derived class-child
  • Base class-parent

12. Can you tell which access specifier in C# should be used for the Main() method?

Public. As the Main() method is called by the runtime, it should be defined as public.


13. Define a variable in C#.

Variables are containers used to store data values. We can change the value or reuse the variable as many times as we like.

14. How do you do Exception Handling in C#?

The following four keywords are used for Exception Handling in C#:

Try - The try block recognizes which block of code has particular exceptions activated.

Catch - The catch keyword signifies a program for catching an exception using an exception handler.

Finally - The finally block executes a given block of code whether or not an exception is caught.

Throw - Using the throw keyword, the program throws an exception in the event of a problem.

15. What’s Recursion in C#?

Recursion refers to the process of making a function call itself.


16. What is a multicasting delegate?

Multicasting delegates allow users to invoke multiple callbacks. It can refer to multiple methods and functions having the same signature at one time.

17. What are Namespaces in C#?

Namespaces are used for differentiating one set of names different from another. They are used to organize code in distinct groups so that one group can be differentiated from another.

18. List the steps of code compilation in C#.

Here are the four steps:

  • Pre-processing
  • Compiling
  • Assembling Linking

19. Can you name some access modifiers available in C#?

  • Public
  • Private 
  • Protected
  • Internal
  • Protected Internal

20. Which parameter can be used to return multiple values from a function?

Reference or output parameters can be used to return multiple values from a function.

21. What are the constructors?

Constructor is a method that gets executed when a new class object is created. It can be public or private.


22. What are the features of read-only variables?

The features of read-only variable are as follows:
  • Initialized at runtime
  • Can be used with static modifiers
  • Only declared at the class level

23. What is method overloading?

Method overloading is a method of having two or more methods with the same name but different parameter lists.

24. Can you tell us the difference between a constant from a read-only?

Read-only is a runtime constant. Const is a compile-time constant.

25. Define dynamic type variables in C#.

Dynamic type variable was introduced in C# 4.0. It is used to skip type checking at compile-time. It is created using dynamic keywords. You can store any type of value in a dynamic variable.

26. Define nullable types in C#

Nullable types allow you to assign a normal range to null values. You can also assign true or false to null types
The syntax is:
< data_type> ? <variable_name> = null;

27. Can you tell us something about the stream reader and stream writer class in C#?

Stream reader and stream writer classes are used for reading and writing actions to a file. Both are inherited from the abstract base class stream.

28. Can you tell the difference between overloading and overriding?

Overloading - When you have two or more methods in the same scope with the same name but different parameters.

Overriding- It allows you to change the behavior of a method in a subclass or child class.

29. Define file handling in C#

File handling refers to the management of files. It consists of different actions like creating the file, writing to the file, reading from the file, etc. Read and write are the two operations used in file handling.

30. Differentiate between managed and unmanaged code

The difference between managed and unmanaged code is as follows:


Managed Code - Managed Code is developed within the .NET framework. CLR directly executes such code by using managed code execution. Any language written in the .NET framework is considered to be managed code.

Unmanaged Code - Unmanaged code is any code developed outside the .NET framework. Unmanaged applications are not executed by CLR. Some languages like C++ can write unmanaged applications such as an application for accessing the low-level functions of the operating system. Some examples of unmanaged code include background compatibility with the code of VB, ASP, and COM.

31. Differentiate between Struct and Class in C#

Class and struct both are user-defined data types. However, they have some important differences:

Struct

Struct is a value type in C# that inherits values from System.Value

It is mostly used for small quantities of data It cannot be inherited to any other type A Struct cannot have abstract values.

Class

Class is a reference type in C#. Since it refers to objects, it inherits from System.Object

Classes are mostly used for large quantities of data Classes can be inherited to other classes Classes can have abstract values.

A default constructor can be created for classes.

32. For methods inside the interface, why can you not specify the accessibility modifier?

Virtual methods in an interface have no method definition. The methods here are written to be overridden in the derived class and hence, they are publicly available.


33. How is encapsulation done in C#?

Access specifiers help implement Encapsulation, in C#, is implemented by using access specifiers. A class member’s scope and visibility are defined by these access specifiers. With public access specifiers, a class can expose its member variables and functions to other objects and functions. Once a member is public, it can be reached from outside the class. With private access specifiers, a class can hide its member variables and functions from other objects and functions. The private members of a class can be accessed only by functions of the same class. Even instances of the same class do not have access to its private members. Protected access specifiers are similar to private access specifiers because they cannot be accessed outside the class. However, protected class members can be accessed by any subclass of that class as well. This enables implementing inheritance.

34. Why is finally block used in C#?

The finally block always gets executed if there is an exception or not. When the code is executed in the try block and an exception occurs, control returns to the catch block, and in the end, the finally block gets executed. The finally block therefore can contain closing connections to the database and the release of file handlers.


35. Define a local variable in C#

Local variables are referred to as variables that are defined in a code block. They are only visible in the code block they’re declared in.

36. What is the difference between object type variables and dynamic type variables in C#?

Dynamic and object type variables are similar in function. Object type variables type checks during the compile time, whereas dynamic type variables at run time.


37. Write a program in C# to reverse a string?

Code public class Program

{ static void Main(string[] args)

{ string str;
Console.Write("Enter a string : "); str = Console.ReadLine();
char arr = str. ToCharArray();
Array.Reverse(arr);
Console.Write("Reversed string is: "); foreach (char ch in arr)
{
Console.Write(ch);
}
}
}

38. What is a read only variable?

Read only variables are created using the readonly keyword. Its value can be modified only within a constructor.

39. Write a program in C# to find if a given string is palindrome or not?

Code

internal static void chkPalindrome(string str)

{

bool flag = false; for (int a = 0, b = str.Length - 1; a < str.Length / 2; a++, b--)

{ if (str[a] != str[b])

{

flag = false; break;

}

else flag = true;

} if (flag) {

Console.WriteLine("The entered string is palindrome");

}

else

Console.WriteLine("The entered string is not palindrome");


40. What will be the output of the following code?

Code int a = 4 int b = 7

int sum = a + b;


Console.WriteLine(sum);

Output:11
line

Copyrights © 2024 letsupdateskills All rights reserved