أسئلة القسم
تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.
Can you explain what OOP is and what its main principles are? سهل
Object-Oriented Programming (OOP) is a paradigm that organizes software design around objects instead of functions and logic.
Main Principles (Pillars):
- Encapsulation: Hiding internal details, exposing only necessary behavior.
- Inheritance: Reusing and extending existing classes.
- Polymorphism: Ability for different classes to be treated through a common interface.
- Abstraction: Focusing on essential behavior, hiding complexity.
What is an object in OOP? سهل
An object is an instance of a class.
It represents a real-world entity with state (fields/properties) and behavior (methods).
What does encapsulation mean? Can you give an example in C#? سهل
Encapsulation = wrapping data + methods together and controlling access via access modifiers.
Protects internal state, only exposes what’s necessary.
Example:
public class BankAccount {
private decimal balance; // hidden from outside
public void Deposit(decimal amount) => balance += amount;
public decimal GetBalance() => balance;
}
Can you explain inheritance with a practical example? سهل
Inheritance allows one class (child) to reuse and extend another class (parent).
Promotes code reuse.
Example:
class Vehicle { public void Drive() => Console.WriteLine("Driving..."); }
class Car : Vehicle { public void Honk() => Console.WriteLine("Beep!"); }
Car car = new Car();
car.Drive(); // inherited
car.Honk(); // child’s own
What are the polymorphism types and explain each of them? سهل
Polymorphism = “many forms” → same method name can behave differently depending on the context.
Types:
- Compile-time Polymorphism (Static)
- Achieved using method overloading or operator overloading.
- Decided at compile time by the compiler.
- Runtime Polymorphism (Dynamic)
- Achieved using method overriding with virtual/override.
- Decided at runtime by the CLR.
How would you explain abstraction in simple terms? سهل
Abstraction = hiding implementation details, exposing only what an object does, not how.
Done using abstract classes or interfaces.
What is a constructor in C#? سهل
A constructor is a special method (with the same class name) that runs when an object is created.
Used to initialize fields/properties.
What types of constructors are available in C#? سهل
- Default constructor: No parameters.
- Parameterized constructor: Takes arguments.
- Copy constructor: Copies values from another object.
- Static constructor: Runs once, initializes static data.
- Private constructor: Prevents direct instantiation, often used in Singleton.
What is the difference between a constructor and a destructor? سهل
Constructor:
- Called when object is created.
- Used for initialization.
Destructor (~ClassName):
- Called by GC before object is destroyed.
- Used for cleanup (rare in C#, usually replaced by IDisposable).
Can a constructor be private? What’s the use case for that? سهل
Yes.
Common Use Cases:
- Singleton pattern: Restricts instantiation.
- Static classes: Prevents object creation.
What is a static class in C#? سهل
A class declared with static keyword:
- Cannot be instantiated.
- Can only contain static members.
- Often used for utility/helper methods.
Can a static class implement an interface? سهل
No, a static class cannot implement an interface because:
- Static classes cannot be instantiated.
- Interfaces define a contract meant to be implemented by instances of classes.
What is the difference between a class and an interface? سهل
Class | Interface |
---|---|
Defines both implementation and data (fields/properties). | Defines only a contract (methods/properties, no implementation). |
Can be instantiated. | Cannot be instantiated directly. |
How is an abstract class different from an interface? سهل
Feature | Abstract Class | Interface |
---|---|---|
Implementation Allowed | Yes (abstract + concrete) | No (only contracts, until C# 8 default methods) |
Fields | Can have fields | Cannot have fields |
Multiple Inheritance | Only one abstract class allowed | Can implement multiple |
Constructors | Yes | No |
What’s the difference between abstract methods and virtual methods? سهل
Abstract Method:
- No implementation in base class.
- Must be overridden in derived class.
Virtual Method:
- Has a default implementation.
- Can be overridden (optional).
What is the difference between a class and a struct in C#? سهل
Class | Struct |
---|---|
Reference type (stored on heap). | Value type (stored on stack). |
Supports inheritance. | No inheritance (but can implement interfaces). |
Can have destructors. | Used for lightweight objects (e.g., Point, DateTime). |
What are the different types of classes in C#? سهل
- Regular class: Normal type.
- Abstract class: Cannot be instantiated.
- Sealed class: Cannot be inherited.
- Static class: Contains only static members.
- Partial class: Split across multiple files.
- Nested class: Class inside another class.
What is a sealed class, and when would you use it? سهل
Sealed class: Cannot be inherited.
Used when:
- You want to prevent inheritance for security or design reasons.
- For performance (JIT optimizes sealed methods).
Can you explain partial classes and their limitations? متوسط
Partial class: Lets you split a class across multiple files using the partial
keyword.
Commonly used in auto-generated code (e.g., Entity Framework, WinForms).
Limitations:
- All parts must have
partial
. - Must be in the same assembly and namespace.
- Cannot span multiple assemblies.
What is an extension method in C#? When would you use one? متوسط
Extension method: A static method that appears as if it’s part of an existing type.
Declared in a static class with this
keyword on the first parameter.
Use case: Add methods to existing classes (e.g., LINQ adds extension methods to IEnumerable<T>
).
What is a virtual method, and why would you use it? متوسط
Virtual method: A method in a base class marked with virtual
.
- Can be overridden in a derived class using
override
. - Provides runtime polymorphism.
Can a struct have virtual methods in C#? متوسط
No.
Structs are value types and do not support inheritance, so virtual/abstract methods are not allowed.
However, structs can implement interfaces.
What’s the difference between using the new keyword and the override keyword? متوسط
- override: Used when the base method is marked
virtual
,abstract
, oroverride
. It replaces the base implementation. - new: Hides the base class method. The base method still exists but is hidden in the derived class.
Key difference: override
= polymorphism (runtime binding). new
= method hiding (compile-time).
Can a class inherit from multiple classes in C#? Why or why not? متوسط
No, C# does not allow multiple inheritance of classes.
Reason: To avoid diamond problem (ambiguity when two parents have the same method/property).
Instead: C# supports multiple interfaces.
Is it possible for a class to implement multiple interfaces? متوسط
Yes, a class can implement multiple interfaces.
This is how C# provides multiple inheritance of behavior safely.
What does the out keyword specifically mean? متوسط
The out
keyword means the called method is responsible for assigning a value before returning.
Commonly used for methods returning multiple values.
How does an object initializer work in C#? متوسط
Object initializer: Provides a clean way to initialize properties when creating an object.
Reduces need for multiple constructors.
What is the difference between early binding and late binding? متوسط
- Early Binding (compile-time): Type and method resolution happens at compile-time (e.g., method calls on normal objects).
- Late Binding (runtime): Resolution happens at runtime (e.g., reflection,
dynamic
).
What are generics in C#? Can you explain with an example? متوسط
Generics allow type-safe, reusable code without sacrificing performance.
Example: List<T>
, Dictionary<TKey, TValue>
.
Can you explain covariance and contravariance in .NET generics? متوسط
- Covariance (out): Allows a method to return a more derived type (e.g.,
IEnumerable<Derived> → IEnumerable<Base>
). - Contravariance (in): Allows a method to accept less derived type (e.g.,
Action<Base> → Action<Derived>
). - Invariance: No conversion allowed.
What is an indexer in C#? متوسط
Indexer: Special property that lets a class/struct be accessed like an array.
When is the Dispose method called on a class that implements IDisposable? صعب
Dispose() is not called automatically by the CLR.
It must be called explicitly by:
- The developer (
obj.Dispose()
), or - The
using
/using declaration
statement, which ensures automatic disposal.
It is typically called when you are done using an object that holds unmanaged resources (e.g., file handles, DB connections).
What is the difference between Dispose and Finalize? صعب
Dispose | Finalize |
---|---|
Called manually or via using . |
Called automatically by GC. |
Part of IDisposable interface. |
Implemented by overriding ~ClassName() (destructor). |
Deterministic cleanup. | Non-deterministic cleanup (timing depends on GC). |
Faster, avoids GC overhead. | Slower, requires GC to track finalizable objects. |
Are the standard .NET generic collection classes thread-safe by default? صعب
No, collections like List<T>
, Dictionary<TKey,TValue>
are not thread-safe.
For thread-safe alternatives, .NET provides Concurrent Collections (ConcurrentDictionary
, BlockingCollection
, etc.).
Can you explain how method dispatch works internally in C# (vtable)? صعب
CLR uses a vtable (virtual method table) for virtual/abstract/override methods.
Each type has a vtable mapping method slots → function pointers.
When calling a virtual method:
- CLR looks up the object’s type.
- Jumps to the appropriate method pointer in the vtable.
Non-virtual methods = direct calls (no vtable lookup).
What happens under the hood in the CLR when you call a virtual method? صعب
CLR performs a vtable lookup at runtime.
The exact implementation (base or derived) is resolved based on the object’s runtime type, not compile-time type.
This enables runtime polymorphism.
What is object slicing, and does it apply in C#? صعب
Object slicing: In C++ when assigning a derived object to a base object by value, extra fields are "sliced off".
In C#: This doesn’t apply because:
- Objects are reference types by default.
- Only value types (structs) could cause similar behavior, but C# disallows slicing scenarios directly.
How does the CLR handle multiple interface inheritance when the interfaces have the same method signatures? صعب
CLR creates separate interface method tables.
If two interfaces declare the same method signature:
- A single implementation can satisfy both.
- Or explicit interface implementation can disambiguate.
What’s the difference between IDisposable and IAsyncDisposable? صعب
IDisposable | IAsyncDisposable |
---|---|
Synchronous cleanup (Dispose() ). |
Asynchronous cleanup (ValueTask DisposeAsync() ), useful for async I/O (e.g., Stream.DisposeAsync() ). |
How does memory management in .NET (GC + IDisposable) affect object lifetime? صعب
GC: Manages memory for managed objects (allocates on heap, reclaims unreachable ones).
IDisposable: Allows deterministic cleanup of unmanaged resources before GC collects the object.
Together: GC frees managed memory, Dispose()
handles unmanaged resources (files, sockets, DB connections).
What is the diamond problem in OOP, and how does C# solve it? صعب
Diamond problem: In multiple inheritance, ambiguity arises when two base classes define the same method.
C# solution:
- No multiple class inheritance.
- Multiple interface inheritance is allowed, and ambiguity is solved via explicit interface implementation.