Web API Interview Questions and Answers
... (existing content remains unchanged)
🔹 C# Difference-Based Interview Questions (with Detailed
Answers)
...(difference section unchanged)
🔹 C# Interview Questions (with Detailed Answers and Code
Examples)
🔸 Object-Oriented Programming
1. What is a class and how is it different from an object?
• A class is a blueprint; an object is an instance of the class.
public class Car {
public string Brand;
}
Car myCar = new Car();
[Link] = "Toyota";
2. What is encapsulation and how is it implemented?
• Encapsulation hides data using access modifiers.
public class Person {
private string name;
public string GetName() => name;
public void SetName(string value) => name = value;
}
3. What is inheritance? Give an example.
• Inheritance allows a class to acquire properties of another class.
1
class Animal { public void Eat() => [Link]("Eating"); }
class Dog : Animal { public void Bark() => [Link]("Barking"); }
4. What is polymorphism? Explain method overloading and overriding.
• Overloading: same method name, different parameters.
• Overriding: derived class modifies base class method.
class Shape {
public virtual void Draw() => [Link]("Drawing Shape");
}
class Circle : Shape {
public override void Draw() => [Link]("Drawing Circle");
}
5. What is an abstract class vs interface?
• Abstract class: can have implementations.
• Interface: defines only contracts.
abstract class Animal {
public abstract void MakeSound();
}
interface IAnimal {
void MakeSound();
}
🔸 Interfaces & Inheritance
6. What is an interface and how is it different from an abstract class?
• Interface: no implementation.
• Abstract class: can have methods implemented.
7. How does C# support multiple inheritance?
• Through interfaces, not classes.
interface IOne { void A(); }
interface ITwo { void B(); }
class Demo : IOne, ITwo {
public void A() {}
public void B() {}
}
2
8. What are default interface methods (C# 8.0)?
• Methods with default implementation in interfaces.
interface IMy {
void Run();
void Walk() => [Link]("Walking");
}
🔸 Constructors & Access Modifiers
9. What is a constructor and types of constructors?
• Constructor initializes object.
• Types: Default, Parameterized, Static, Copy.
public class User {
public string Name;
public User() => Name = "Default";
public User(string name) => Name = name;
}
10. What are access modifiers in C#?
• public , private , protected , internal , protected internal , private protected
11. What is a static constructor?
• Initializes static members; runs once.
class MyClass {
static MyClass() { [Link]("Static ctor"); }
}
🔸 Properties, Indexers, Object Initializers
12. What is a property? What are auto-implemented properties?
public class Product {
public int Id { get; set; }
}
13. What is the use of indexers?
3
public string this[int i] => names[i];
14. How are object initializers used?
var user = new User { Name = "Raj" };
🔸 Delegates, Events, Lambdas
15. What is a delegate?
• Holds reference to a method.
delegate void Print();
Print p = () => [Link]("Hello");
p();
16. What is an event?
• Encapsulated delegate.
public event Print OnPrint;
17. Delegate vs Event?
• Events restrict access to delegate invocation.
18. What is an anonymous method and lambda expression?
Print p = delegate { [Link]("Hi"); };
Print q = () => [Link]("Hello");
🔸 Collections & Generics
19. List, Dictionary, Array, HashSet, Queue?
List<int> list = new();
Dictionary<string, int> dict = new();
HashSet<int> set = new();
Queue<string> queue = new();
20. What are generics in C#?
4
public class Box<T> { public T Value; }
21. IEnumerable vs IQueryable?
• IEnumerable : in-memory
• IQueryable : LINQ to SQL
🔸 LINQ
22. What is LINQ? Benefits?
• Query syntax for collections.
var result = [Link](x => x > 10).ToList();
23. Deferred vs Immediate Execution?
• ToList() forces immediate execution.
24. Select vs SelectMany?
Select: returns collection of collections
SelectMany: flattens into one sequence
25. Filter, Sort, Group using LINQ?
var sorted = [Link](x => x);
var grouped = [Link](x => x % 2);
🔸 Exception Handling
26. How does exception handling work?
try { ... } catch (Exception ex) { ... } finally { ... }
27. Difference: throw vs throw ex?
• throw preserves stack trace.
28. What are custom exceptions?
5
class MyEx : Exception { public MyEx(string msg): base(msg) {} }
🔸 Async & Await
29. What is async/await?
• For non-blocking code.
async Task Run() {
await [Link](1000);
}
30. What is Task and Task?
• Task: void-returning async work
• Task: returns value
31. Synchronous vs Asynchronous?
• Sync: blocking
• Async: non-blocking
🔸 Memory & Static
32. Static vs Instance Members?
• Static: one per class
• Instance: per object
33. What is garbage collection?
• Automatic memory management
34. Value Types vs Reference Types?
• Value types: stack
• Reference: heap
35. Boxing/Unboxing?
int x = 10;
object o = x; // boxing
int y = (int)o; // unboxing
6
Would you like to:
• 📄 Export all this into a C# + Web API PDF?
• ➕ Add threading, async streams, or code optimization topics next?