You may recognise a for loop, inheritance and the definition of a stack when each appears separately, yet lose the answer when one question combines several state changes. The remedy is one trace method that follows actual values from a C array into stack operations, then uses the same practice setting to distinguish OOP relationships. The goal is to reason from state, ownership and operation order, not memorise isolated definitions.
UP LT Grade C, OOP and data structures: three reasoning jobs
Start by asking three different questions: What is the value now? for C tracing, What is the relationship? for OOP, and What operation changes the structure? for data structures. This keeps one familiar keyword from deciding an answer without its surrounding lifecycle or operation order.
Lens | State to track | Typical distractor | Best scratch-work |
|---|---|---|---|
C variables and indices | Current variable values, index and expression order | Confusing pre-increment with post-increment | One row after each executed statement |
OOP relationships | Inheritance, association, aggregation, composition and object lifecycle | Treating every | Label |
Stack, queue and list |
| Ignoring which end inserts or removes | Draw the structure after each operation |
Run all three lenses on paper before you look at the options. The one you skip is usually the one the question is testing. The UP LT Grade Teacher Exam Preparation page gathers the Computer Science material for this route in one place.
C program tracing: make every expression a state-table row
In the code below, array indices run from 0 through 4, top begins at -1, and the stack has capacity five.
int a[5] = {6, 2, 9, 4, 7};
int stack[5], top = -1, sum = 0;
for (int i = 0; i < 5; i++) {
if (a[i] % 2 == 0)
stack[++top] = a[i] + i;
else
sum += a[i];
}
while (top >= 0)
sum += stack[top--];
printf("%d", sum);Turn each pass through the for loop into one row:
i | a[i] | branch | value pushed | top after step | stack bottom to top | sum |
|---|---|---|---|---|---|---|
0 | 6 | even |
| 0 |
| 0 |
1 | 2 | even |
| 1 |
| 0 |
2 | 9 | odd | none | 1 |
| 9 |
3 | 4 | even |
| 2 |
| 9 |
4 | 7 | odd | none | 2 |
| 16 |
The condition % 2 == 0 selects even values. Because ++top increments before the array access, the first push uses index 0. Also notice that the expression pushes a[i] + i, so the pushed values are 6, 3 and 7, not the original even values 6, 2 and 4.

OOP relationships: classify is-a, has-a and uses-a
Now build an OOP/C++ model from the earlier C trace. Let Question be an abstract base class with virtual int solve() = 0. CodeTraceQuestion is a derived class storing {6,2,9,4,7}. PracticeSet holds references to several Question objects that can exist independently. Attempt creates and owns its StateStack, while Evaluator receives an Attempt& only while checking a result.
CodeTraceQuestionis aQuestion, so the relation is inheritance or generalisation.PracticeSetgroups independently existing questions, so the relation is aggregation.Attemptcreates, owns and destroys itsStateStack, so their matching lifecycle makes this composition.Evaluatortemporarily uses anAttemptwithout owning it, so this is association or dependency.
For a dispatch check, Question *q = new CodeTraceQuestion({6,2,9,4,7}); calls the derived solve() through the virtual base interface and returns 32. The broader OOP for Teaching CS Exams: Classes and Inheritance guide develops these distinctions further.

Data-structure operations: stack, queue and list order
After the first loop, sum=16, top=2, and the stack is {6,3,7} from bottom to top. stack[top--] reads index 2 before decrementing, so the first pop returns 7, changes top to 1, and makes the sum 16 + 7 = 23. The next pops return 3 and 6, giving 23 + 3 = 26 and 26 + 6 = 32. The final post-decrement leaves top=-1, the loop stops, and the program prints 32.
A queue behaves differently with the same insertions. Enqueueing 6, 3, 7 and then dequeueing three times returns 6, 3, 7. The stack reverses insertion order to 7,3,6, while the queue preserves it. Adding either sequence to 16 still gives 32, so an output-only check can hide incorrect data-structure reasoning. Trace the order, not only the total.
For a linked-list mutation, start with 10 -> 20 -> 30 -> NULL. Inserting 15 after node 10 gives 10 -> 15 -> 20 -> 30 -> NULL. Deleting the first node containing 20 then gives 10 -> 15 -> 30 -> NULL. Changing the link after a known node is constant-time, but finding a node by value can require a linear scan.
Complexity and boundary checks
For n=5, the first loop inspects five elements, performs three pushes and two direct additions, while the second loop performs three pops. For general n, these loops are sequential, not nested, so the running time is O(n). The auxiliary stack could hold all n values, making worst-case extra space O(n).
With capacity five, valid stack indices are 0 to 4. At top == 4, another push would overflow; at top == -1, a pop would underflow. This trace reaches only top=2, and while (top >= 0) prevents a fourth pop.
Operation | Time |
|---|---|
Array access by known index |
|
Stack push or pop at top |
|
Queue enqueue or dequeue with correct front/rear maintenance |
|
Linked-list search by value |
|
Linked-list insertion after a known node |
|
An array queue that shifts every remaining element on deletion has O(n) dequeue for that implementation. A circular queue avoids the shift.
UP LT Grade question traps: replace shortcuts with evidence
Each trap below breaks one of the three lenses. It either reads a value at the wrong moment, mislabels ownership, or ignores which end of the structure an operation touches.
Treating
++topas post-increment -> it puts the write at the wrong index. Correction: increment first, then use index0for the first push.Adding the original even values -> the right-hand side also includes
i. Correction: record6+0,2+1and4+3.Reading
stack[top--]after decrement -> this skips the current top. Correction: read index2, then changetopto1.Treating stack and queue order as interchangeable -> addition happens to hide the order difference. Correction: preserve
7,3,6for the stack and6,3,7for the queue.Calling every
has-arelation composition -> that ignores independent lifecycles. Correction: use composition only when the owner controls the part's lifecycle.Calling every pointer or reference aggregation -> syntax alone does not prove whole-part ownership. Correction: inspect whether the object is merely used, shared, or owned.
Two micro-checks expose the same habits. If the push became stack[top++] = a[i] + i while top still started at -1, the first write would attempt index -1. That is undefined behaviour, not merely a different numeric answer. If Attempt instead stored a non-owning pointer to a shared StateStack that outlived it, the relationship would fail the same-lifecycle test for composition.
For the wider C and data-structure syllabus behind these traps, work through C Programming & Data Structures.
UP LT Grade Computer Science: the short method and next step
Use four passes on your scratch paper: mark the initial state top=-1, sum=0; record all five loop rows; write stack order {6,3,7} and pop order 7,3,6; then classify OOP arrows by lifecycle and ownership. Finish with the independent checks 9 + 7 + 7 + 3 + 6 = 32 and top=-1.
For the exact Computer Science syllabus, paper structure, dates, marks and question counts, check the current notification on the official UPPSC website.
The UP LT Grade Assistant Teacher 2025 Computer Science Course teaches the subject in this order, and the UP LT Grade Test Series puts the method under timed conditions. Before checking an answer, trace one program by state, justify one OOP relation by ownership, and execute one structure operation by order.




