Paper A — Questions and Answers
1. Explain .NET Framework with its main components.
-
Definition
- .NET Framework is a software development platform by Microsoft that provides an execution environment (CLR), a large class library (FCL), language interoperability and services for building Windows applications, web apps, and services.
-
Main components
-
Common Language Runtime (CLR)
- Execution engine: loads and runs programs, performs JIT compilation (IL → native), handles memory management (Garbage Collector), exception handling, security enforcement and thread management.
-
Framework Class Library (FCL) / Base Class Library (BCL)
- Large reusable library of types for I/O, collections, networking, data access (ADO.NET), XML, UI (WinForms, WPF), web (ASP.NET) etc.
-
Common Type System (CTS)
- Defines data types and how they are represented in memory; enables cross-language type compatibility.
-
Common Language Specification (CLS)
- A set of rules that language compilers follow so code in different languages can interoperate.
-
Assemblies & Metadata
- Units of deployment (DLL/EXE) containing MSIL, metadata and manifest. Manifest includes versioning and dependency info.
-
Just-In-Time (JIT) compiler
- Converts MSIL to native code when methods are first called.
-
Security and Versioning
- Code Access Security (CAS) (older .NET), strong-named assemblies, GAC (Global Assembly Cache) for shared assemblies.
-
-
How it works (high-level)
- Source code → compiler (e.g., csc) → MSIL + metadata → assembly → CLR loads assembly → JIT compiles IL to native code → executes managed code under CLR services.
-
Use-cases
- Desktop apps, Web apps, enterprise systems, services, and libraries.
2. Write difference between overloading and overriding.
-
Method Overloading
-
Definition: Multiple methods in the same class with the same name but different parameter lists (signature).
-
Compile-time polymorphism (resolved at compile time).
-
Same class (or derived class can overload base methods).
-
Return type may differ but cannot be sole differentiator.
-
Example:
void Print(int x),void Print(string s).
-
-
Method Overriding
-
Definition: Derived class provides a new implementation for a method declared
virtualorabstractin base class usingoverride. -
Run-time polymorphism (dynamic dispatch).
-
Requires inheritance; method signature must match base method.
-
Access modifiers and return type must be compatible.
-
Example:
public virtual void Show()in base andpublic override void Show()in derived.
-
-
Tabular differences
-
Time: Overloading → compile-time; Overriding → runtime.
-
Scope: Overloading → within same class (or derived); Overriding → base & derived across inheritance hierarchy.
-
Keywords: Overloading → none (or optional
new); Overriding →virtual(base) andoverride(derived). -
Purpose: Overloading → convenience/variations; Overriding → change behaviour in subclass.
-
3. What is inheritance? Explain.
-
Definition
- Inheritance is an OOP mechanism where a class (derived/subclass) acquires fields and methods of another class (base/superclass). It enables code reuse and hierarchical classification.
-
Types
-
Single inheritance: one base class.
-
Multilevel inheritance: chain of inheritance (A → B → C).
-
Hierarchical inheritance: multiple derived classes from one base.
-
(C# does not support multiple class inheritance — supports multiple interface inheritance.)
-
-
Advantages
- Reusability, extensibility, polymorphism, cleaner code, easier maintenance.
-
Example (C#)
class Animal { public void Eat() { Console.WriteLine("Eating"); } } class Dog : Animal { public void Bark() { Console.WriteLine("Bark"); } } // Usage: Dog d = new Dog(); d.Eat(); // inherited d.Bark(); -
When to use
- Use inheritance for “is-a” relationships (Dog is an Animal). Prefer composition for “has-a” when behaviour reuse without tight coupling is needed.
4. What is delegate in C#? Explain with example.
-
Definition
- A delegate is a type-safe object that references a method (or multiple methods). It encapsulates a method signature and allows methods to be passed as parameters, stored and invoked.
-
Key points
-
Type-safe: signature must match.
-
Supports multicast (combining delegates with
+=). -
Useful for callbacks, event handling, and implementing observer patterns.
-
-
Syntax & example
// Delegate type declaration public delegate int MathOp(int a, int b); // Methods matching the signature public class Calculator { public static int Add(int x, int y) => x + y; public static int Multiply(int x, int y) => x * y; } // Usage MathOp op = Calculator.Add; int result = op(3, 4); // 7 // Multicast (only last return value returned for non-void delegates) op += Calculator.Multiply; foreach (MathOp d in op.GetInvocationList()) { Console.WriteLine(d(3,4)); } -
Events
- Delegates are the basis of events:
public event EventHandler MyEvent;
- Delegates are the basis of events:
5. Explain the concept of operator overloading with example.
-
Definition
- Operator overloading allows user-defined types (classes/structs) to provide custom implementations for standard operators (
+,-,*, etc.) by definingoperatormethods.
- Operator overloading allows user-defined types (classes/structs) to provide custom implementations for standard operators (
-
Rules
-
Overload must be declared
public static. -
At least one operand must be a user-defined type.
-
Not all operators can be overloaded (e.g.,
?:cannot).
-
-
Example: Complex number + and -
public class Complex { public double Real { get; } public double Imag { get; } public Complex(double r, double i) { Real = r; Imag = i; } public static Complex operator +(Complex a, Complex b) { return new Complex(a.Real + b.Real, a.Imag + b.Imag); } public static Complex operator -(Complex a, Complex b) { return new Complex(a.Real - b.Real, a.Imag - b.Imag); } public override string ToString() => $"{Real} + {Imag}i"; } // Usage: var c1 = new Complex(1, 2), c2 = new Complex(3, 4); var sum = c1 + c2; // uses overloaded + -
Use-cases
- Mathematical types, domain-specific DSLs, simplifying client code.
6. Explain exception handling with example.
-
Purpose
- Exception handling provides structured mechanism to detect and respond to runtime errors, enabling graceful recovery or controlled termination.
-
Key constructs (C#)
-
tryblock: code that may throw exceptions. -
catchblock(s): handle specific exceptions. -
finallyblock: executes regardless of exception (cleanup). -
throw: rethrow or throw new exceptions. -
Custom exceptions: derive from
Exception.
-
-
Example
try { int a = 10, b = 0; int c = a / b; // throws DivideByZeroException Console.WriteLine(c); } catch (DivideByZeroException ex) { Console.WriteLine("Cannot divide by zero: " + ex.Message); } catch (Exception ex) { Console.WriteLine("Some error: " + ex.Message); } finally { Console.WriteLine("Cleanup code runs here."); } -
Guidelines
-
Catch specific exceptions first, then general
Exception. -
Avoid empty catches.
-
Use
finallyfor releasing resources or useusingstatement forIDisposable.
-
7. Write a short note on any TWO: (choose any two)
I’ll answer all three briefly; exam asks for any two.
a) Static keyword
-
Meaning
staticmarks members (fields, methods, constructors, classes) that belong to the type itself rather than an instance.
-
Usage
-
static int Counter;shared across all instances. -
static void Main(string[] args)program entry. -
static class Utilities { ... }cannot be instantiated; contains only static members.
-
-
Static constructor
- Executes once before first access, used to initialize static state.
b) out parameters
-
Purpose
outallows a method to return additional values by reference; the called method must assign a value before returning.
-
Example
bool TryParseInt(string s, out int value) { return int.TryParse(s, out value); } if (TryParseInt("123", out int v)) { Console.WriteLine(v); // 123 } -
Difference from
ref-
ref: variable must be initialized before call. -
out: variable need not be initialized; callee must assign.
-
c) DataAdapter (ADO.NET)
-
Definition
DataAdapteracts as a bridge between aDataSet(in-memory) and a data source (e.g., database). It uses commands (SelectCommand,InsertCommand,UpdateCommand,DeleteCommand).
-
Usage
-
Fill a
DataTableorDataSet:adapter.Fill(dataSet). -
Persist changes from
DataSet:adapter.Update(dataSet).
-
-
Advantage
- Disconnected data access model: work offline and sync changes later.
8. Create a delegate that can call two different methods: first is subtraction and second is division.
-
Delegate & methods
using System; public delegate void MathAction(double a, double b); class Program { static void Subtract(double x, double y) { Console.WriteLine($"Subtraction: {x} - {y} = {x - y}"); } static void Divide(double x, double y) { if (y == 0) Console.WriteLine("Division: Cannot divide by zero"); else Console.WriteLine($"Division: {x} / {y} = {x / y}"); } static void Main() { MathAction actions = Subtract; actions += Divide; // multicast delegate actions(10, 2); // Output: // Subtraction: 10 - 2 = 8 // Division: 10 / 2 = 5 } } -
Notes
-
Multicast delegates invoke methods in order they were added.
-
For non-void delegates, only the last method’s return value is received by the caller — prefer void for multicast results or use separate calls.
-
9. Write a C# code to create and use custom exception.
-
Custom exception class
using System; // Custom exception public class InvalidAgeException : Exception { public InvalidAgeException() { } public InvalidAgeException(string message) : base(message) { } public InvalidAgeException(string message, Exception inner) : base(message, inner) { } } -
Throwing and catching
class Program { static void ValidateAge(int age) { if (age < 0 || age > 120) { throw new InvalidAgeException($"Invalid age: {age}"); } } static void Main() { try { ValidateAge(-5); } catch (InvalidAgeException ex) { Console.WriteLine("Custom exception caught: " + ex.Message); } catch (Exception ex) { Console.WriteLine("General exception: " + ex.Message); } finally { Console.WriteLine("End of validation."); } } } -
Guidelines
-
Derive from
Exception(orApplicationExceptionhistorically). -
Provide appropriate constructors and messages.
-
Use custom exceptions when you need to represent domain-specific errors.
-