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

C# & .NET Interview Questions 2024 Edition

C# & .NET Interview Questions 2024 Edition

Uploaded by

Nikola Perišić
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
355 views

C# & .NET Interview Questions 2024 Edition

C# & .NET Interview Questions 2024 Edition

Uploaded by

Nikola Perišić
Copyright
© © All Rights Reserved
You are on page 1/ 14

C# & .

NET
Interview Questions
First Edition Handbook

LinkedIn
For free mock interview contact me at

12th September, 2024


Foreword
Author's note: I've aimed to include the most relevant questions.

While I can't guarantee that knowing all the answers will ensure you pass your interview, I can
promise that it will significantly boost your confidence in C# and .NET development.

Please, leave a review here.

How to contact me?

You can contact me on LinkedIn or by emailing me at [email protected].

1
Questions
1. Does .NET support multiple inheritance of interfaces?

Yes, .NET allows a class to implement multiple interfaces. This means a class can inherit
from multiple interface contracts, providing flexibility in defining behaviors.

2. What are the three states of virtual memory?


- Free: Memory that is available for allocation.
- Reserved: Memory that is set aside but not yet used.
- Committed: Memory that has been allocated and is actively being used.
3. What does the “sealed” keyword do to a class?

The sealed keyword prevents a class from being inherited.

4. Can abstract classes be instantiated?

No, abstract classes cannot be instantiated. They are meant to be inherited.

5. Does .NET support multiple inheritance of base classes?

No, .NET does not support multiple inheritance of base classes.

6. Can the static method be called without instantiating the class where it is defined?

Yes, static methods belong to the class itself rather than an instance.

7. Can the static method be called by instantiating a class?

No, static methods should be called on the class rather than on an instance.

8. What is a “read-only string”?

A read-only string is a variable whose value cannot be changed after initializing.

9. What is “virtual function”?

A virtual function is a method defined in a base class that derived classes can override.

10. What is the “abstract method”?

A method defined in an abstract class with no implementation. The body is provided by


the derived class.

2
11. What is the size limit of the Garbage Collector’s small object heap?

The size limit for the Garbage Collector’s (GC) small object heap is 85KB.

12. Which generations exist in Garbage Collector(GC)?

The GC has three generations: Generation 0, Generation 1, and Generation 2.

13. Where are stored the most recent objects? In which generation?

The most recent objects are stored in Generation 0.

14. Does the “using” block automatically do GC cleaning?

Yes if a certain condition is done. The answer to that is in the following question answer.

15. Is “using” the only way to perform a cleaning object from memory? Do you know the
pattern for doing garbage collection?

The “using” statement is not the only way. The Disposable pattern and implementation of
the IDisposable interface are also used to manage and clean up unmanaged resources.

16. Small object heap is <85kB or >85kB?

< 85kB

17. What do the acronyms “SOH” and “LOH” stand for?

SOH stands for Small Object Heap, and LOH stands for Large Object Heap.

18. If GC runs and deletes 1th generation, does it mean it also deletes 0th generation?

Yes, when the GC collects Generation 1, it also cleans up Generation 0 objects as part of
the process.

19. Which objects are cleaned? How does GC know which object should be deleted?

The GC cleans up objects that are no longer referenced.

20. What are the two main methods of GC working?

The two main methods of garbage collection are sweeping (removing unreachable
objects) and compacting (rearranging memory to reduce fragmentation).

3
21. What are the three phases of GC's way of working?
- Marking
- Relocating
- Compacting
22. Where are stored referenced types(classes, objects, constants)? On stack or the heap?

On the heap.

23. var car = new Car(“BMW”); Variable “car” is value or reference type?

“car” is a reference type. It holds a reference to the “Car” object on the heap.

24. Int x = 2; Variable “x” is value or a reference type?

x is a value type. It directly holds the integer value.

25. Does “Heap” memory contain one “layer” for static variables?

No, static variables are stored in a separate area called the High-Level Object (HLO)
space, not in the heap.

26. Do you know some more reference types other than classes and objects?

Arrays, lists, collections, and strings.

27. What is “Boxing” and “Unboxing”?

Boxing is converting a value type to a reference type by wrapping it in an object.


Unboxing is converting it back to a value type from a reference type.

28. What types of Garbage Collector exists? Hint: There are two of them.
- Server(Default in .NET)
- Workstation
29. Can you use the “using” statement if the class does not implement the “dispose” method?

No, the using statement requires the class to implement the IDisposable interface with a
Dispose method.

30. Is “finalize” destructor?

Yes, in .NET, Finalize is the term used for destructors, which are used to clean up
unmanaged resources before an object is collected by the GC.

4
31. What is method overloading?

Method overloading allows multiple methods with the same name but different signatures
(different parameters or return types) to be defined in the same class.

32. What is the Global Assembly Cache (GAC)?

The GAC is a machine-wide cache that stores assemblies shared among multiple
applications on a computer, allowing for versioning and centralized management.

33. What is method overriding?

When we override the already existing body of the method.

34. What is a keyword that we use to mark the method so it can be overridden?

Virtual.

35. Can you name some of the access modifiers?


- public
- internal
- protected
- private
36. What is the difference between “parameter” and “argument”?

A parameter is a variable defined in the method signature, while an argument is the actual
value passed to the method when it is called.

37. What does “step over” do while debugging? Does it skip the method?

“Step over” executes the current line of code, including method calls, without stepping
into the method's code. It does not skip the method but proceeds to the next line.

38. What are “guard clauses”?

Guard clauses are conditional statements used to handle edge cases.

39. What is the difference between “throw ex” and “throw”?

“throw ex” resets the call stack, losing the original error context, while “throw” preserves
the original stack trace, providing better debugging information.

40. For what do we use the “void” keyword?

The “void” keyword indicates that a method does not return any value.

5
41. What is the difference between the “Debug” and “Trace” classes?

The “Debug” class is used for logging and diagnostics only in debug builds, while the
“Trace” class is used for logging in both debug and release builds.

42. If a class does not inherit anything, what does it inherit by default?

System.Object

43. Does the “ref” parameter variable need to be initialized before or after calling the
method?

A “ref” parameter must be initialized before it is passed to the method.

44. What are “tuple” types?

Tuples are a data structure that allows grouping multiple values of different types into a
single unit, providing a way to return multiple values from a method.

45. What are “local” functions?

Local functions are functions defined within another method.

46. Is this considered good practice for naming a private field? -> _favoritePrimaryColor

Yes.

47. What does “indexer” give to the instances of classes?

An indexer allows instances of a class to be indexed like arrays.

48. Does the number underline separator exist in C#? -> 1_419

Yes. 1_419 is equivalent to 1419.

49. What does the “800M” value stand for in C#?

The “800M” value represents the number 800 as a decimal type (decimal), where M
denotes a decimal literal.

50. What types of “pattern matching” do you know in C#?


- Null checks
- Type checks

6
51. What does “init;” do to a property?

The init; accessor allows the property to be set only during object initialization.

52. Can you instantiate a record using the “while” keyword?

Yes.

53. What is this syntax called?

public record Person(string FirstName, string LastName); -> Positional syntax

54. Can you instantiate a class using the “with” keyword?

No.

55. What you can use on strings and arrays to make them saved on stack memory?

You can use the Span<T> type and manual stack allocation with the “stackalloc” keyword.

56. Why would we use the “Span<T>” type and “stackalloc” while allocating an array or string?

Using Span<T> and stackalloc for temporary data can improve performance by avoiding
heap allocations and reducing garbage collection overhead.

57. Do we need to use an “unsafe” block to use “Span<T>”?

Not anymore.

58. What types of “Span<T>” exist?


- Span<T>
- ReadOnlySpan<T> -> does not allow operations on that string/array
59. If “ReadOnlySpan<T>” is only for read-only does it mean we have some difference in
memory allocation of “Span<T>” and “ReadOnlySpan<T>”?

No. The only difference is the permission.

60. Can we inject a “scoped” service into a “singleton” service?

No, you cannot inject a scoped service into a singleton service because the singleton's
lifetime exceeds that of the scoped service, leading to potential issues with service
resolution.

7
61. Which types of service lifetimes exist?
- Singleton
- Scoped
- Transient
62. What types of .NET configurations exist?
- IConfiguration
- IOptions
- IOptionsSnapshot
- IOptionsMonitor
63. Does “asynchronous” resolve concurrency?

No, asynchronous programming does not inherently resolve concurrency; it improves


efficiency by not blocking threads, but concurrency issues still need to be managed.

64. On which types we can use the “size of” operator?

We can use it for the following types: int, float, and char.

65. Does “size of” operator require “unsafe block”?

No, the “sizeof” operator does not require an unsafe block for primitive types, but it does
for certain scenarios involving unmanaged types.

66. What is “serialization”?

Serialization is the process of converting an object into a stream of bytes for storage or
transmission.

67. What type of comments exist rather than single and multi-line comments?

XML comments.

68. How C# code is compiled and executed. Define the stages of that process.

C# code is first compiled into Intermediate Language (IL) by the C# compiler. At runtime,
the Just-In-Time (JIT) compiler converts IL into native machine code for execution.

69. What extensions do assemblies have?

.exe or .dll

70. Accronym “ILC” stands for?

ILC stands for Intermediate Language Code.

8
71. What’s the difference between “Array” and “ArrayList”?
- The array can not be null.
- The array needs to have a defined size.
- The array needs to have only one type of data stored in it.
72. What’s the difference between “String” and “StringBuilder”?

The string is immutable, meaning a new memory allocation is made for every modification.

73. What is the “Hash table” class?

The hash table is a collection class that stores key-value pairs.

74. How many types of constructors does a “Hash table” provide?

16.

75. When occurs “Race condition (deadlock)” in C#?

Occurs when two or more threads attempt to access and modify shared data
simultaneously, leading to conflicts and unpredictable behavior.

76. Will these threads be run chronologically one by one?

New Thread()

New Thread() answer -> OS determines the order

77. What is “Thread Pool”?

The “ThreadPool” is a collection of pre-created threads used to perform background


tasks.

78. Can “structs” be inherited?

No.

79. What are the ways that the method can be overloaded?
- Different data types of parameters
- Different number of passed parameters
- Different order of passed parameters
80. What techniques of serialization are supported in .NET?
- JSON serialization
- XML & SOAP serialization
- Binary serialization

9
81. What do we use for binary serialization in .NET?

The “BinaryFormatter” class.

82. Is it safe to do binary serialization? If yes, why?

It is not considered safe due to vulnerabilities in “BinaryFormatter”, which can lead to


security risks.

83. What is wrong here?

public inject UserRepository; answer -> Repositories should be injected


as private fields to follow encapsulation principles.

84. What types of inheritance exist in C#?


- Single inheritance
- Multiple inheritance(through interfaces)
- Multilevel inheritance
- Hybrid inheritance
85. What is the difference between monolith and microservices?

A monolith is a single, unified application where all components are tightly coupled.
Microservices are architecture where an application is broken down into smaller, loosely
coupled services that communicate over a network.

86. Are “records” immutable or mutable types?

Immutable.

87. What is the “singleton” pattern?

The “Singleton” pattern ensures that a class has only one instance in the application.

88. What does the “decorator” pattern allow us?

Allows adding new behavior to objects dynamically without altering their structure.

89. What categories of design patterns exist?

Design patterns are categorized into creational, structural, and behavioral patterns.

90. Is there a “Null reference exception” in C#? If it exists, why?

Yes, a “NullReferenceException” occurs when you try to access a member on a variable


that is null, meaning it has not been initialized or assigned a valid object reference.

10
91. What is the difference between IEnumerable, IQueryable, List, and Array in .NET?

“IEnumerable” provides a way to iterate over a collection. “IQueryable” allows querying


against a data source. The list is a dynamic array, and Array is a fixed-size collection.

92. How do you handle exceptions in .NET?

Use try, catch, and finally blocks to handle exceptions. try contains code that might throw
an exception, catch handles the exception, and finally executes code regardless of
whether an exception occurred.

93. What are extension methods in C#?

Extension methods allow adding new methods to existing types without modifying their
source code. They are defined as static methods in static classes.

94. What is Entity Framework, and how does it differ from .NET?

Entity Framework (EF) is an ORM that provides an abstraction layer for database
interactions, whereas .NET involves writing raw SQL queries and managing data
connections manually.

95. What is the difference between abstract and interface in C#?

An abstract class can provide default behavior and maintain state, while an interface
defines a contract without implementation. A class can implement multiple interfaces but
inherit from only one abstract class.

96. What is the difference between a Task and a Thread in .NET?

Task is used for asynchronous programming and provides higher-level abstractions for
managing concurrent operations, while Thread represents a lower-level thread of
execution.

97. Explain the concept of a delegate in C#.

A delegate is a type that represents references to methods with a specific signature. It


allows methods to be passed as parameters and called dynamically.

11
98. How do you implement logging in a .NET application?

Logging can be implemented using built-in frameworks like ILogger in ASP.NET Core or
third-party libraries such as NLog or Serilog.

99. What are attributes in C#?

Attributes are metadata added to code elements (classes, methods, properties) that
provide additional information and can be retrieved using reflection.

Prepare, the last question is here :)

___________________________________________________________________________

100. Did you learn something new? 🙂


Congratulations! 🎉
You came to the end of this. Hope you enjoyed and learned something new :)

___________________________________________________________________________

Whats next?

I have a document that contains 250+ interview code questions called “Cracking the C#
.NET technical interview easy way”.

And that’s not all! If you want to do an online live interview with me 1 on 1 you can contact
me at [email protected].

___________________________________________________________________________

Thanks for reading and see you in the next chapter!

12
Useful links

13

You might also like