Week 10: Programming, OOP & Computer Graphics

Unit III combines three areas the exam treats as a single unit: procedural programming fundamentals (as embodied by C), the object-oriented paradigm that generalises past it, and computer graphics basics. The programming and OOP questions are usually concept-checks on well-known gotchas (pointer arithmetic, virtual functions, storage classes) rather than requiring you to trace long programs.

Module 2 of 3 Week 10 of 19 ~3 Hours Exam-Style Practice Included

By the end of this week, you'll be able to

  • Explain C storage classes, pointer arithmetic and how arrays relate to pointers
  • Define encapsulation, inheritance, polymorphism and abstraction with a concrete example of each
  • Describe basic 2D transformation matrices and line/polygon clipping at a conceptual level

1. Programming Fundamentals in C

C's four storage classes are: auto (default, local scope, exists only during the function call), static (retains its value between function calls, initialised only once), extern (declares a variable defined elsewhere, often another file), and register (a hint to store the variable in a CPU register for faster access — a mere hint the compiler may ignore). A common exam trap: a static local variable inside a function keeps its value across repeated calls, unlike an ordinary local variable which resets every call.

An array name, in most expression contexts, decays to a pointer to its first element — this is why arr[i] and *(arr + i) are equivalent, and why pointer arithmetic (ptr + 1 advances by sizeof(*ptr) bytes, not 1 byte) is central to understanding both arrays and dynamic memory in C. Passing an array to a function actually passes a pointer, which is why sizeof on a function parameter that "looks like" an array gives the pointer's size, not the array's.

static vs. auto local variable
void counter_auto(void)  { int c = 0; c++; printf("%d\n", c); }  // always prints 1
void counter_static(void){ static int c = 0; c++; printf("%d\n", c); } // prints 1,2,3,...

Calling counter_auto() three times prints: 1 1 1
Calling counter_static() three times prints: 1 2 3

(static's initializer "= 0" runs only ONCE, the first time control reaches it)
The pointer-arithmetic rule to never forget

ptr + 1 moves forward by sizeof(*ptr) bytes, not by 1 byte — for an int* this is typically 4 bytes, for a char* it's 1 byte. This single fact resolves most "what does this pointer expression print" exam questions.

2. Object-Oriented Programming Concepts

The four pillars: Encapsulation bundles data and the methods operating on it into one unit (a class), restricting direct access via access modifiers. Abstraction exposes only essential behaviour while hiding implementation detail (an interface or abstract class defines what, not how). Inheritance lets a derived class reuse and extend a base class's members. Polymorphism lets the same interface behave differently depending on the actual object — compile-time (function/operator overloading, resolved by the compiler using argument types) versus run-time (virtual functions/method overriding, resolved at execution using the actual object type, enabling a base-class pointer to correctly call a derived class's overridden method).

Run-time polymorphism specifically requires a virtual function mechanism (implemented via a vtable in languages like C++): without it, calling a method through a base-class pointer would always invoke the base class's version, even if the pointer actually points to a derived object — this exact scenario is one of the most frequently tested OOP questions on this paper.

3. Computer Graphics Basics

Basic graphics primitives are generated algorithmically: Bresenham's line algorithm draws lines using only integer arithmetic (no floating-point division), making it fast and exact for raster displays; Bresenham's/midpoint circle algorithm extends similar integer-only logic to circles, using 8-way symmetry to compute only one octant and mirror the rest.

2D transformations are represented as matrices applied to a point's coordinates: translation, scaling and rotation each have a standard matrix form, and homogeneous coordinates (adding a third coordinate) let translation be expressed as matrix multiplication too, so multiple transformations can be composed into a single combined matrix. Clipping (removing parts of a scene outside a viewing window) uses algorithms like Cohen-Sutherland for lines, which assigns a 4-bit region code to each endpoint to quickly accept/reject/subdivide a line against the clip window.

Why integer-only algorithms matter here

Bresenham's algorithms exist specifically because early raster hardware had no fast floating-point unit — using only integer addition/subtraction to decide the next pixel made real-time line and circle drawing possible, and the same integer-decision technique is still used today for performance.

4. Hands-on Exercise

Hands-on

Trace pointer arithmetic and derive a transformation matrix

Concept checks on this unit are best cemented by tracing exact values, not reading definitions.

Part 1 — C tracing:

  1. Given int arr[5] = {10,20,30,40,50}; int *p = arr;, write out the value of *(p+2), *p+2, and *(p+2)+1 separately.
  2. Write a 3-call trace of a function with a static counter, listing exactly what each call prints.

Part 2 — Graphics:

  1. Write the 2x2 rotation matrix for rotating a point by angle θ about the origin.
  2. Apply it by hand to rotate the point (1, 0) by 90° and verify the result is (0, 1).
  3. Explain in one sentence why homogeneous coordinates are needed to combine this rotation with a translation into one matrix.

5. Exam-Style Practice (UGC NET Pattern)

Five NTA-pattern questions on C internals, OOP and graphics fundamentals.

Q1

A local variable declared as `static int count = 0;` inside a function that is called multiple times will:

A) Reset to 0 at the start of every call
B) Retain its value from the previous call, since it is initialised only once
C) Cause a compilation error
D) Be automatically shared across all functions in the program

Correct answer: B) Retain its value from the previous call, since it is initialised only once. A static local variable is initialised exactly once and retains its value between successive calls to the function, unlike an ordinary (auto) local variable which is recreated and reset every call.

Q2

If `int *p` points to the start of an `int` array on a system where `sizeof(int) == 4`, what does the expression `p + 1` represent?

A) The address one byte after p
B) The address 4 bytes after p (the next int element)
C) A syntax error, since integers can't be added to pointers
D) The value stored at p, incremented by 1

Correct answer: B) The address 4 bytes after p (the next int element). Pointer arithmetic scales by the pointed-to type's size — for an int* with sizeof(int)==4, p+1 advances the address by 4 bytes, landing on the next array element.

Q3

Which OOP mechanism specifically enables a base-class pointer to correctly invoke a derived class's overridden method at run time?

A) Function overloading
B) Operator overloading
C) Virtual functions
D) Static binding

Correct answer: C) Virtual functions. Virtual functions enable dynamic (run-time) dispatch, so a call through a base-class pointer or reference resolves to the actual derived object's overridden implementation, not the base class's version.

Q4

Bresenham's line-drawing algorithm is specifically valued for which property?

A) It uses only floating-point trigonometric functions for precision
B) It draws lines using only integer arithmetic, making it fast on raster hardware
C) It only works for perfectly horizontal or vertical lines
D) It requires a graphics processing unit (GPU) to function

Correct answer: B) It draws lines using only integer arithmetic, making it fast on raster hardware. Bresenham's algorithm decides each next pixel using only integer addition/subtraction and comparison, avoiding slow floating-point/division operations — this is exactly why it was suited to early raster hardware and is still used for performance today.

Q5

Why are homogeneous coordinates used in 2D computer graphics transformations?

A) To represent colour information alongside position
B) To allow translation to be expressed as matrix multiplication, so multiple transformations can be combined into a single matrix
C) To reduce the number of bits needed to store a coordinate
D) To enable 3D rendering exclusively, with no use in 2D

Correct answer: B) To allow translation to be expressed as matrix multiplication, so multiple transformations can be combined into a single matrix. Ordinary 2D transformation matrices can represent scaling and rotation via multiplication, but not translation. Adding a third homogeneous coordinate lets translation also be expressed as matrix multiplication, so a sequence of transformations can be composed into one combined matrix.