Paper B — Midterm Internal (C# Programming) — Questions and Answers
1. Explain difference between C# and Java.
-
Platform
-
Java: originally “write once, run anywhere” using JVM; broadly cross-platform.
-
C#: originally Windows-focused (.NET Framework); modern .NET (Core/.NET 5+) is cross-platform.
-
-
Language features
-
C#: properties, events, delegates, LINQ,
unsafepointers, indexers,ref/out,async/awaitbuilt-in earlier. -
Java: uses interfaces and classes; lambdas added later; no delegates or properties; method references and streams for functional style.
-
-
Memory & runtime
-
Java: JVM, garbage collector; bytecode.
-
C#: CLR, MSIL, and JIT. Both have GC, but implementations differ.
-
-
Multiple inheritance
-
Java: single class inheritance + multiple interfaces.
-
C#: single class inheritance + multiple interfaces;
defaultinterface methods added later in Java.
-
-
Generics
-
C#: Generics are reified (preserve type info at runtime) and support constraints.
-
Java: Generics implemented via type erasure; runtime type info not preserved.
-
-
Pointers
-
C#: allows
unsafecode with pointers. -
Java: no pointer arithmetic; references only.
-
-
Native integration & tooling
-
C#: tight integration with Windows, Visual Studio; P/Invoke for native calls.
-
Java: wide ecosystem, many JVM languages, many tools.
-
-
Conclusion
- Both are modern OOP languages; differences are in features, runtime, type system, and ecosystem.
2. Explain with neat labeled diagram an architecture of .NET Framework.
(Describe a neat labeled diagram — draw when studying. Here is structured description:)
-
Top: Application Layer
- User applications: WinForms/WPF, ASP.NET, Console apps, Services.
-
Middle: Framework Class Library (FCL)
- Namespaces:
System,System.IO,System.Data,System.Web,System.Xml,System.Threading, etc.
- Namespaces:
-
Core: Common Language Runtime (CLR)
-
Subcomponents:
-
Metadata & Type System (CTS, CLS)
-
JIT Compiler (MSIL → native)
-
Garbage Collector
-
Security (Code Access Security) & Evidence
-
Exception Handling
-
Threading and Interop Services (P/Invoke, COM)
-
-
-
Assemblies & Metadata
- Assemblies (EXE/DLL) containing MSIL, manifest & metadata.
-
Bottom: Operating System
- Underlying OS resources (Windows APIs). CLR calls OS services for I/O, network, etc.
-
Flow
- Source → Compiler (
csc) → MSIL + metadata → Assembly → CLR loads → JIT → Native code → OS.
- Source → Compiler (
3. What are assemblies in .NET? What are different parts of assemblies? What are types of assemblies?
-
Assembly definition
- An assembly is the basic unit of deployment, versioning and security in .NET. It is a physical file (.exe or .dll) containing MSIL code, metadata, and a manifest.
-
Parts of an assembly
-
Manifest: contains assembly identity (name, version, culture, public key token), referenced assemblies, and security permissions.
-
Metadata: descriptive information about types, methods, properties, attributes, signatures.
-
MSIL (Microsoft Intermediate Language): platform-independent intermediate code.
-
Resources: embedded files (images, strings, satellite resources).
-
Optional native images: via NGen for precompiled native code.
-
-
Types of assemblies
-
Private assembly: used only by a single application; stored in application directory.
-
Shared assembly (strong-named): installed in Global Assembly Cache (GAC) for reuse across apps; has strong name (public/private key).
-
Satellite assembly: contains localized resources (for internationalization), loaded per culture.
-
Dynamic assembly: generated at runtime via Reflection.Emit.
-
4. Explain with neat labeled diagram compilation model of .NET Framework technologies.
(Describe compilation model in steps:)
-
Source Code
- C#, VB.NET, F# source files.
-
Compilation
- Language-specific compiler (csc.exe for C#) compiles source → MSIL (also called CIL) + metadata.
-
Assembly
- Output is an assembly (.exe/.dll) containing MSIL + manifest + metadata.
-
Deployment
- Assembly deployed to file system or GAC.
-
Execution
-
CLR loads assembly.
-
JIT compiler compiles MSIL method-by-method → native machine code.
-
Native code executed by OS.
-
CLR services (GC, security, exception handling) manage runtime.
-
-
Optional: NGEN / Precompilation
- Native images created ahead-of-time (AOT) to improve startup performance; bypass some JIT.
-
Diagram notes
- Show arrows: Source → Compiler → Assembly (MSIL + metadata) → CLR Loader → JIT → Native Code → OS.
5. What is the difference between an interface and abstract class? Explain with suitable code.
-
Interface
-
Only method/property/event declarations (until newer C# versions which allow default implementations).
-
No instance fields.
-
A class can implement multiple interfaces.
-
Represents capabilities/contract.
-
-
Abstract class
-
Can have abstract methods (no body) and concrete methods (implemented).
-
Can contain fields, constructors and access modifiers.
-
A class can inherit from only one abstract class (single inheritance).
-
Use when there is a strong “is-a” relationship and shared implementation.
-
-
Comparison (short)
-
Multiple inheritance: interface = yes; abstract class = no.
-
State: interface = no fields (traditionally); abstract class = can have fields.
-
Use-case: interface for capabilities (e.g.,
IComparable); abstract class for base behavior plus default implementation.
-
-
Code example
public interface IShape { double Area(); } public abstract class ShapeBase { public string Name { get; set; } public ShapeBase(string name) { Name = name; } public abstract double Area(); // must be overridden public void PrintName() { Console.WriteLine(Name); } // concrete } public class Circle : ShapeBase, IShape { public double Radius { get; set; } public Circle(double r) : base("Circle") { Radius = r; } public override double Area() => Math.PI * Radius * Radius; }
6. What is concept of Method overriding in C#? Write a C# program to explain concept of method overriding.
-
Concept
- Method overriding allows a derived class to provide a specific implementation of a method that is declared
virtual(orabstract) in base class. The derived class usesoverridekeyword. At runtime, the override is chosen based on object type (polymorphism).
- Method overriding allows a derived class to provide a specific implementation of a method that is declared
-
C# program
using System; public class Animal { public virtual void Speak() { Console.WriteLine("Animal speaks"); } } public class Dog : Animal { public override void Speak() { Console.WriteLine("Dog barks"); } } public class Cat : Animal { public override void Speak() { Console.WriteLine("Cat meows"); } } class Program { static void Main() { Animal a1 = new Dog(); Animal a2 = new Cat(); a1.Speak(); // Dog barks (override) a2.Speak(); // Cat meows (override) Dog d = new Dog(); d.Speak(); // Dog barks } } -
Key points
-
virtualon base,overrideon derived. -
If base method not virtual but derived declares new method with same signature using
new, it hides but not override; call resolves by compile-time type, not runtime.
-
7. Write Short Notes on any two of the following (I’ll give concise notes for all four so you can pick):
a) Properties in C#
-
Definition
- High-level members that provide controlled access to fields via
getandsetaccessors.
- High-level members that provide controlled access to fields via
-
Syntax
private int _age; public int Age { get { return _age; } set { if (value >= 0) _age = value; } } -
Auto-implemented properties
public int Count { get; set; }
-
Benefits
- Encapsulation, validation on set, readable syntax.
b) Abstraction in C#
-
Definition
- Hiding implementation details and exposing essential features through abstract classes and interfaces.
-
Mechanisms
abstractclasses and methods; interfaces.
-
Purpose
- Define common contract/behaviour while leaving implementation to derived classes.
c) Virtual and pure virtual method
-
Virtual method (C#)
- Declared with
virtualin base class; can be overridden in derived classes.
- Declared with
-
Pure virtual (C++ term)
- In C# equivalent is
abstractmethod in abstract class — declared withabstractand has no body; must be overridden.
- In C# equivalent is
-
Example
public abstract class Base { public abstract void DoWork(); // C# "pure virtual" public virtual void OptionalWork() { /* default */ } }
d) Destructors
-
C# destructor
- Syntax:
~ClassName() { /* cleanup */ }. Called by GC, non-deterministic.
- Syntax:
-
Preferred pattern
- Implement
IDisposableandDispose()for deterministic resource cleanup; useusingstatement.
- Implement
-
Notes
- Avoid heavy work in destructor; finalizer only for unmanaged resource release fallback.