أسئلة القسم
تدرب على أسئلة المقابلات في هذا القسم. اكتب إجابتك، قم بتقييمها، أو اضغط على "عرض الإجابة" بعد التفكير.
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).