CS U111 · Mid-sem prep · Past papers
Three past mid-sems, every question worked
CS F111 mid-semester papers from 2024-25 (Sem 1, two parts), 2025-26 Sem 1 and 2025-26 Sem 2. Every question is quoted as set, answered, and traced step by step. Every answer was compiled and run, and five mistakes in the official keys are flagged where they occur.
What these papers are. They come from CS F111, the course CS U111 replaced. The C content matches the first half of your syllabus: operators, input/output, selection, loops, arrays. But U111 is a redesign that adds computational thinking and flowcharts, so treat the format below as likely, not certain. This year's date isn't announced yet (last year's Sem 1 mid-sem was 9 Oct 2025). It's 90 minutes, closed book, 30%.
Nothing here is ahead of you. Not one question in three papers touches functions, strings, pointers or recursion. Every question uses what the loops, arrays and operators pages already cover. The tricks (the comma operator, & vs &&, scanf field widths) are new to everyone in the hall, JEE or not; no school syllabus teaches them.
What the papers look like
| Paper | Shape | Write-a-program marks |
|---|---|---|
| 2024-25 Sem 1 | Part 1: 15 multiple-choice questions on an OMR sheet, 40 marks, 40 min, −25% per wrong answer. Part 2: 50 marks, 50 min: rewrite code 20, fill in blanks 20, one program 10 | 10 / 90 |
| 2025-26 Sem 1 (9 Oct 2025) | 90 marks, 90 min, all written: syntax one-liners 10, predict the output 20, number systems 8, rewrite/complete 24, two programs 14 + 14 | 28 / 90 |
| 2025-26 Sem 2 (13 Mar 2026) | Part A: 10 multiple-choice questions, 10 marks, −0.5 per wrong answer. Part B: five 16-mark programs | 80 / 90 |
The trend is the main message: writing a complete program on paper went from 10 marks to 80. The last paper was a second-semester sitting, likely set by different examiners, so don't expect exactly 80. But practise writing programs by hand, not only predicting output. The write-a-program practice page builds each of these programs one step at a time.
1 · Negative marking: do the arithmetic once. A blind guess among 4 options is right with probability ¼. At −25% (2024), its average value is ¼ × 1 − ¾ × 0.25 = +0.06 of the question's marks, so guessing pays slightly. At −0.5 per 1 mark (2026), it's ¼ − ¾ × 0.5 = −0.125, so a blind guess loses marks on average. Rule out one option and it's exactly 0. Rule out two and it's ½ − ½ × 0.5 = +0.25. So under heavy negative marking, guess only after eliminating two options.
2 · Never leave a program blank. The published rubrics pay for structure: 2–6 marks for headers, declarations and scanf/printf, before any logic. A correct skeleton with the input and output lines is worth real marks.
3 · You may state an assumption. Two papers say so explicitly ("If you feel that something is missing … make your own assumption, and state it clearly"). Write it in one line above the code, e.g. "Assume a ≠ 0."
Topic map: every question, by skill
Use this to practise by topic instead of by paper. Each link goes to the question.
| Skill | 2024-25 | 2025-26 Sem 1 | 2025-26 Sem 2 |
|---|---|---|---|
Operators & expressions: ++/--, &&/|| stopping early, comma operator, integer division, precedence, & vs && | P1 Q1 · Q3 · Q5 | Q2b · Q2d · Q2h · Q4c · Q1e | A3 · A5 · A6 · A10 |
Formatted I/O: scanf widths and literals, %Lf/%lf/%u | P1 Q2 · Q15 · P2 Q6 | Q1b · Q4d | A9 |
| Number systems: bases, fractions, 2's complement | P1 Q6 | Q3 (8 marks) | A4 · A10 |
Selection: nested if, switch fallthrough, ?:, rewriting between them | P1 Q7 · Q12 · P2 Q1 · Q2 · Q3 · Q5 | Q1g · Q2i · Q2j · Q4f | A6 · B2 · B5 |
| Loops: tracing, break/continue, patterns, for ↔ do-while | P1 Q4 · Q9 · Q10 · Q13 · Q14 · P2 Q4 | Q2a · Q2c · Q2e · Q2f · Q4a · Q4b · Q4e · Q4g · Q4h | A7 · A8 |
Arrays: size by sizeof, min/count, reverse, bubble sort, two arrays | P1 Q8 · Q11 · P2 Q7 · Q8 · Q9 · Q10 | Q1c,d,f · Q2g | A2 |
| Write a whole program | P2 Q11 | Q5 · Q6 | B1 · B2 · B3 · B4 · B5 |
All five were confirmed by compiling the code. 2025 Q2b: the key says 14, the program prints 16. 2025 Q2h: the key says 348, the program prints 012348. Both come from reading a single & as &&. 2026 B5: the model solution's -b/(2*a) is integer division and gives wrong roots for most inputs. 2025 Q4d and 2024 P2 Q4: the model answers leave out a declaration the program needs. And 2024 P2 Q8 accepts || where only && is safe. If your answer disagreed with a key on one of these, you were right.
2024-25 Sem 1 · Part 1: multiple choice 40 marks · 40 min · −25%
Part 1 · Q1 · 2 marks · operators
Q1 · ||, && and the side effects that never happen
What is the output of the following program?
#include <stdio.h>
int main()
{
int a = 1, b = 1;
int c = a || --b;
int d = a-- && --b;
printf("a = %d, b = %d, c = %d, d = %d", a, b, c, d);
return 0;
}- (a) a = 0, b = 1, c = 1, d = 0
- (b) a = 0, b = 0, c = 1, d = 0
- (c) a = 1, b = 1, c = 1, d = 1
- (d) a = 0, b = 0, c = 0, d = 0
Answer: (b) a = 0, b = 0, c = 1, d = 0
The rule. || stops as soon as its left side is true, and && stops as soon as its left side is false. The right side is then never evaluated, so any ++/-- inside it never happens.
| line | what runs | a | b | result |
|---|---|---|---|---|
| c = a || --b | a is 1 (true) → stop; --b skipped | 1 | 1 | c = 1 |
| d = a-- && --b | a-- gives the OLD a (1, true), then a becomes 0 | 0 | 1 | — |
| left was true, so --b runs: b becomes 0 (false) | 0 | 0 | d = 0 |
Option (a) is the trap: it assumes --b never runs anywhere. It is skipped only in the first line. More on this in the operators & selection notes.
Part 1 · Q2 · 3 marks · formatted input
Q2 · scanf field widths chop the input
Consider the following C program:
#include<stdio.h>
int main () {
int n, m;
float x;
scanf("%2d %5f %2d", &n,&x,&m);
printf("n= %d,x=%5.2f,m = %d",n
,x,m);
return 0;
}Find the correct output of the program if the input entered is 1234.5678 99.88 777
- (a) n = 1234, x = 0.5678, m = 99
- (b) n = 12, x = 34.56, m = 78
- (c) n = 1234, x = 0.56, m = 78
- (d) None of the above.
Answer: (b) n = 12, x = 34.56, m = 78
A number between % and the letter is a maximum number of characters to read. scanf keeps reading where the previous item stopped, and spaces in the input only matter where they happen to fall.
| format item | reads at most | characters taken | value |
|---|---|---|---|
| %2d | 2 chars | 12 | n = 12 |
| %5f | 5 chars | 34.56 | x = 34.56 |
| %2d | 2 chars | 78 | m = 78 |
The rest of the input (99.88 777) is simply never read. Then %5.2f prints 34.56 with two decimals in a 5-character field.
Part 1 · Q3 · 2 marks · operators
Q3 · The comma operator keeps only the last value
What is the output of the following program?
#include<stdio.h>
int main()
{
int a = 7;
int c= (a>=7)?a++:++a;
int b=(a+1, c+1, 100, a+c);
printf("%d",b);
return 0;
}- (a) 16
- (b) 18
- (c) 15
- (d) 100
Answer: (c) 15
(a>=7)is true, so the?:evaluatesa++. That gives the old value, 7, soc = 7, and thenabecomes 8. (++ais never evaluated.)- Inside the brackets,
,is the comma operator: it evaluates each part left to right and the whole expression takes the value of the last part.a+1,c+1and100are computed and thrown away (none of them changes a variable). - The value is
a+c = 8+7 = 15.
Option (d) is the trap for anyone who thinks the brackets hold a list and b gets some middle item.
Part 1 · Q4 · 3 marks · loops / flowcharts
Q4 · Tracing a flowchart (subtractive GCD)
What is the value that the following flowchart prints?
- (a) 12
- (b) 4
- (c) 16
- (d) 36
Answer: (a) 12
| pass | m | n | m != n? | m > n? | update |
|---|---|---|---|---|---|
| 1 | 48 | 36 | yes | yes | m = 48 − 36 = 12 |
| 2 | 12 | 36 | yes | no | n = 36 − 12 = 24 |
| 3 | 12 | 24 | yes | no | n = 24 − 12 = 12 |
| 4 | 12 | 12 | no → Print(n) | — | prints 12 |
Repeatedly subtracting the smaller number from the larger one is Euclid's method, so the flowchart prints the GCD of 48 and 36. Recognising that is a fine check, but it is the trace table that earns the marks.
Part 1 · Q5 · 2 marks · operators
Q5 · Integer division happens before you expect
What is the output of the following C program?
#include<stdio.h>
int main(){
printf("%d", 2*3/4-3/4*2 );
return 0;
}- (a) 0
- (b) 1
- (c) 1.5
- (d) 2
Answer: (b) 1
* and / have equal precedence and run left to right. Every number here is an int, so every / throws the remainder away.
2*3/4→6/4→ 1 (not 1.5).3/4*2→0*2→ 0 (3/4 is already 0 before the ×2).- 1 − 0 = 1. Option (c) is impossible anyway:
%dcan only print an integer.
Part 1 · Q6 · 2 marks · number systems
Q6 · An unknown base
Determine x when (10)8 = (12)x.
- (a) 6
- (b) 7
- (c) 8
- (d) 9
Answer: (a) 6
Expand each side in its own base. (10)8 = 1×8 + 0 = 8. (12)x = 1×x + 2. Setting them equal gives x + 2 = 8, so x = 6. Check: (12)6 = 6 + 2 = 8 ✓. The digit 2 must also be smaller than the base, which it is. Practice: the number-systems drill.
Part 1 · Q7 · 2 marks · selection
Q7 · switch jumps straight to the matching case
What is the output of the following program?
#include<stdio.h>
int main()
{
int a =50;
switch(a)
{ case 49: a++; break;
default: a=45; break;
case 50: a--; break;
case 51: a =a+1; break;
}
printf("%d",a);
return 0;
}- (a) 45
- (b) 50
- (c) 49
- (d) 51
Answer: (c) 49
switch does not test the cases in the order they are written. It jumps to the label that matches (case 50) and uses default only if no label matches, wherever default is written. So a-- gives 49 and break leaves. Option (a) is the trap for anyone who reads top to bottom and stops at default.
Part 1 · Q8 · 2 marks · arrays
Q8 · Reversing an array: stop at the middle
Let A be an array with n elements. Choose the correct values of exp1 and exp2 so that the following code reverses the array elements in A. For example, if A = {1, 2, 3, 4, 5} then the code segment coverts it into A = {5, 4, 3, 2, 1}.
for(int j=0; exp1; j++)
{
int temp = A[j];
A[j] = A[exp2];
A[exp2] = temp;
}- (a) exp1 : j < n, exp2 : n − j − 1
- (b) exp1 : j <= n/2, exp2 : n − j
- (c) exp1 : j < n/2, exp2 : n − j − 1
- (d) None of these
Answer: (c)
- exp2.
A[j]must swap with its mirror. The first element (j = 0) pairs with the last, indexn−1. In generaln−1−j. Option (b)'sn−jgivesA[n]when j = 0, which is one past the end. - exp1. Each swap fixes two elements, so only half as many swaps are needed. With
j < n(option a), the second half swaps everything back: compiled,{1,2,3,4,5}comes out as1 2 3 4 5, unchanged. Withj < n/2, n = 5 does swaps j = 0, 1 (the middle element stays put) and n = 6 does j = 0, 1, 2. Both reverse correctly.
Part 1 · Q9 · 4 marks · nested loops
Q9 · A staircase pattern: count the spaces first
Which of the following is the program's correct output?
#include<stdio.h>
int main() {
int i,j,x;
x=4;
for(i=0;i<4;i++){
for(j=0;j<i;j++){
printf(" ");
}
for(j=0;j<=i;j++){
printf("%d",i+j);
}
printf("\n");
}
return 0;
}0
12
234
34560
12
234
34560
12
345
67890
12
345
6789Answer: (a)
| i | spaces (j < i) | numbers printed: i+j for j = 0…i | line |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1, 2 | 12 |
| 2 | 2 | 2, 3, 4 | 234 |
| 3 | 3 | 3, 4, 5, 6 | 3456 |
Decide the two features one at a time. Indentation: the first inner loop prints i spaces, which rules out (b) and (c). Digits: each row starts at i (because j starts at 0), not where the previous row ended, which rules out (d). The unused x=4 is a distraction.
Part 1 · Q10 · 4 marks · loops
Q10 · break vs continue in the inner loop
Prof. Forget gave two C programs to you. He forgot the outputs of these programs. He asked you to identify which of the output lines is printed by code 2 but not by code 1.
Code 1:
#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i + j == 3) {
break;
}
printf("i=%d, j=%d\n", i, j);
}
}
return 0;
}Code 2:
#include <stdio.h>
int main() {
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (i + j == 3) {
continue;
}
printf("i=%d, j=%d\n", i, j);
}
}
return 0;
}- (a) i=2, j=1
- (b) i=1, j=2
- (c) i=2, j=2
- (d) i=1, j=1
Answer: (c) i=2, j=2
Only pairs with i + j == 3 behave differently: (1, 2) and (2, 1). break abandons the rest of the inner loop for that i, while continue skips only that one pass.
| i | Code 1 (break) prints j = | Code 2 (continue) prints j = |
|---|---|---|
| 0 | 0, 1, 2 | 0, 1, 2 |
| 1 | 0, 1 → j=2 breaks | 0, 1 → j=2 skipped |
| 2 | 0 → j=1 breaks (j=2 never reached) | 0, 2 (only j=1 skipped) |
Neither program prints (1, 2) or (2, 1). Only Code 2 reaches (2, 2).
Part 1 · Q11 · 4 marks · arrays / sorting
Q11 · Counting bubble-sort swaps
What is the output of the following code?
#include <stdio.h>
int main ()
{
int A[] = {5, 7, 4, 8, 12, 2, 10};
int count =0;
int n=sizeof(A)/sizeof(A[0]);
for (int i=0; i<n; i++) {
for (int j=0; j<n-1-i; j++)
{
if (A[j] > A[j + 1]) {
int temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp ;
count ++;
}
}
}
printf ("%d", count);
return 0;
}- (a) 7
- (b) 8
- (c) 10
- (d) None of these
Answer: (b) 8
| pass (i) | swaps made in this pass | array after the pass | count |
|---|---|---|---|
| 0 | 7↔4, 12↔2, 12↔10 | 5 4 7 8 2 10 12 | 3 |
| 1 | 5↔4, 8↔2 | 4 5 7 2 8 10 12 | 5 |
| 2 | 7↔2 | 4 5 2 7 8 10 12 | 6 |
| 3 | 5↔2 | 4 2 5 7 8 10 12 | 7 |
| 4 | 4↔2 | 2 4 5 7 8 10 12 | 8 |
| 5, 6 | none (already sorted) | — | 8 |
Exam shortcut, with its reason. Call a pair of positions an inversion if the larger number comes first. Each bubble swap exchanges one adjacent out-of-order pair, which removes exactly one inversion and creates none. The sorted array has zero inversions. So swaps = inversions. Count, for each number, how many smaller numbers appear after it: 5 → 2 (4, 2), 7 → 2, 4 → 1, 8 → 1, 12 → 2, 2 → 0, total 8.
Part 1 · Q12 · 3 marks · nested if-else
Q12 · Which condition reaches s2?
Consider the following code and determine the condition which prints "s2" for any given values of a, b, and c.
#include <stdio.h>
int main() {
int a,b,c;
scanf("%d,%d,%d",&a,&b,&c);
if(a>b)
if(b>c)
printf("s1");
else
printf("s2");
else
if(a>c)
printf("s3");
else
printf("s4");
return 0;
}- (a) when a <= b and c >= a
- (b) when b >= a and c < a
- (c) when b < a and b <= c
- (d) when b < a and b > c
Answer: (c) when b < a and b <= c
Follow the path to s2: the outer test must be true (a > b, i.e. b < a), and then the inner test must be false. The opposite of b > c is b <= c, not b < c. Compiled with inputs 5,3,4 and 5,3,3, both print s2. Option (d) is the path to s1.
Part 1 · Q13 · 2 marks · loops
Q13 · do…while(i++) tests the old value
Output of the following code is
#include<stdio.h>
int main() {
int i=0;
do{
if(i==3)
break;
printf("%d ",i);
}while(i++);
return 0;
}- (a) 0
- (b) 0 1 2
- (c) 0 1 2 3
- (d) Compilation error
Answer: (a) 0
- A
dobody always runs once:iis 0, not 3, so it prints0. - The test
i++uses the old value 0 (false), and only then doesibecome 1. - A false test ends the loop. It never gets near
i == 3, so thebreakis a decoy.
Part 1 · Q14 · 3 marks · loops / operators
Q14 · A ?: as a whole statement
What is the output of the following code? Assume that the warnings are ignored
#include<stdio.h>
int main() {
int k;
for(k=0;k<20;k=k+2)
(k%3==1)?printf("%d",k):printf("");
return 0;
}- (a) 36912
- (b) 51015
- (c) 41016
- (d) 61218
Answer: (c) 41016
The ?: expression is the loop's whole body, and each branch is a printf call, which is legal. k runs 0, 2, 4, …, 18. Keep only even k with remainder 1 when divided by 3: 4 (= 3 + 1), 10 (= 9 + 1), 16 (= 15 + 1). They print with no separators: 41016.
Part 1 · Q15 · 2 marks · formatted input
Q15 · A literal character in scanf's format
If the user enters "10 20" as input, what will the following code print?
#include<stdio.h>
int main() {
int n=0,m=0;
scanf("%d-%d",&n,&m) ;
printf("%d-%d",n,m);
return 0;
}- (a) 10-20
- (b) 10 20
- (c) 10-0
- (d) 10 10
Answer: (c) 10-0
Any ordinary character in the format (here the -) must appear in the input exactly. scanf reads 10, then expects - but finds a space, so it stops. m keeps its starting value 0, and the output is 10-0. (Had the user typed 10-20, it would print 10-20.)
2024-25 Sem 1 · Part 2: rewrite, complete, write 50 marks · 50 min
Part 2 · Q1 · 4 marks · selection trick
A · Q1 · Print "HelloWorld" from an if-else
Rewrite the code given below in the box with appropriate "condition" expression to print "Helloworld"
# include <stdio.h>
void main( )
{
if(Condition)
printf("Hello");
else
printf("World");
}Answer: if(!printf("Hello"))
if(!printf("Hello"))
printf("Hello");
else
printf("World");HelloWorldNormally only one branch of an if-else runs, so both words can only appear if the condition itself prints "Hello". printf returns the number of characters it printed (here 5). !5 is 0 (false), so the else branch prints "World". This is a puzzle, not a pattern to reuse; what it tests is knowing that printf returns a value.
Part 2 · Q2 · 4 marks · selection rewrite
A · Q2 · switch → nested if-else (no else if)
You are building a vending machine simulation where a user selects a drink option, and the machine displays the price based on the drink type. Enter 1 for "Soda" – 20 INR · Enter 2 for "Juice" – 25 INR · Enter 3 for "Water" – 10 INR. If the user enters an invalid option, display "Invalid selection." Rewrite the following switch statements in the box using nested if-else statements (do not use else if).
switch (drink) {
case 1:
printf("Soda: 20 INR\n");
break;
case 2:
printf("Juice: 25 INR\n");
break;
case 3:
printf("Water: 10 INR\n");
break;
default:
printf("Invalid selection\n");
}Answer: each else holds the next if inside braces
if (drink == 1) {
printf("Soda: 20 INR\n");
} else {
if (drink == 2) {
printf("Juice: 25 INR\n");
} else {
if (drink == 3) {
printf("Water: 10 INR\n");
} else {
printf("Invalid selection\n");
}
}
}Each case becomes an equality test, and default becomes the innermost else. "No else if" means the next if must sit inside the braces of the previous else, and the nesting is how the marker checks it. Compiled with inputs 1, 2, 3 and 7: all four messages are correct.
Part 2 · Q3 · 4 marks · selection rewrite
A · Q3 · ?: → if-else ladder
You are developing a discount program for a store. If a customer is a member and spends more than $100, they get a 20% discount. If they are a member but spend less than $100, they get a 10% discount. If they are not a member, they do not get any discount. Rewrite the following code in the box using if-else ladder (if, else if, else).
discount = is_member ? (amount_spent > 100 ? 0.20 : 0.10) : 0.0;Answer: a three-rung ladder
if (is_member && amount_spent > 100) {
discount = 0.20;
} else if (is_member) {
discount = 0.10;
} else {
discount = 0.0;
}Rewrite the code's behaviour, not the English. The prose says "less than $100", but the code gives exactly $100 the 10% rate (100 > 100 is false), and a rewrite must keep that. The official key writes the middle test as is_member && amount_spent <= 100. That is correct but longer than needed: to reach the else if, the first test has already failed. Compiled with (1, 150), (1, 100), (1, 50) and (0, 500): 20%, 10%, 10%, 0%.
Part 2 · Q4 · 4 marks · loop rewrite
A · Q4 · for → nested do-while
Rewrite the code in the box to create a simple number pattern using nested do-while loops only. The pattern should look like this: 1 2 3 / 4 5 6 / 7 8 9
int count = 1;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", count);
count++;
}
printf("\n");
}Answer: move each loop's three parts (start, test, update) to their new places
int count = 1;
int i = 0; /* the for's start, moved above */
do {
int j = 0;
do {
printf("%d ", count);
count++;
j++; /* the for's update, moved to the end */
} while (j < 3); /* the for's test, moved to the bottom */
printf("\n");
i++;
} while (i < 3);1 2 3
4 5 6
7 8 9The official answer starts at do { and never declares i. The original for declared it, so the rewrite must too: int i = 0; before the outer do. Without it the code does not compile, and that is an easy mark to lose.
A do-while is an exact replacement here only because each loop is guaranteed to run at least once (3 > 0). A do body always runs once, even when a for would have run zero times.
Part 2 · Q5 · 4 marks · selection rewrite
A · Q5 · if-else ladder → one chain of ?:
You are developing a grading system for a course. […] if the score is 90 or above, the student receives an "A"; if the score is 80 or above, they receive a "B"; if the score is 70 or above, they receive a "C"; if the score is 60 or above, they receive a "D"; and any score below 60 results in an "F". […] Rewrite the following if-else block in the box using the conditional (ternary) operators:
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}Answer: each else if becomes the next ? :
grade = (score >= 90) ? 'A' :
(score >= 80) ? 'B' :
(score >= 70) ? 'C' :
(score >= 60) ? 'D' : 'F';Read x ? p : q as "if x then p else q". The final else ('F') is the last thing after the final colon. Compiled with scores 95, 90, 85, 72, 61, 59: A A B C D F.
Part 2 · Q6 · 4 marks · formatted I/O
B · Q6 · Format specifiers for long double and double
The following C program computes the Gravitational Force between two particle masses. Fill in the four blanks with theoretically appropriate programming constructs and syntax.
double G = 6.67e-5;
long double m1, m2;
double x[] = {1.0, 2.5, 3.222323};
double y[] = {0.5, 1.5, 1.52};
double distance, force;
printf("Enter the mass of the first object (kg): ");
scanf("____", &m1); /* (1) */
printf("Enter the mass of the second object (kg): ");
scanf("%____", &m2); /* (2) */
distance = sqrt(pow((x[0] - y[0]), 2.0) + pow((x[1] - y[1]), 2.0) + pow((x[2] - y[2]), 2.0));
if (distance ____ ) /* (3) */
return 1;
else
{
force = (G * (m1 * m2)) / (distance * distance);
printf("Force is %____ Newtons\n",force); /* (4) */
}Answer: (1) %Lf · (2) Lf · (3) <= 0 or == 0 · (4) lf (or f)
Look at the variable's declared type, not what it stands for. m1, m2 are long double → %Lf (capital L) in scanf. Blank (1) includes the %; blank (2) already has it. Blank (3) guards the division, since dividing by a zero distance is meaningless. force is a double, and printf accepts %lf or %f for it.
Part 2 · Q7 · 4 marks · arrays
B · Q7 · Minimum of an array
The following C Program computes the minimum value in an integer array. Fill in the four blanks with appropriate programming constructs and syntax.
int numbers[] = {34, 71, 29, 88, 15};
int ______ = numbers[0]; /* (1) */
for (int i = 1; i < sizeof(numbers) / sizeof(______); i++) /* (2) */
{
if (numbers[i] < minimum) {
minimum = _______________; /* (3) */
}
}
printf("The minimum element is %d\n", ______); /* (4) */Answer: (1) minimum · (2) numbers[0] or int · (3) numbers[i] · (4) minimum
Blank (1) is the only place the name minimum could be declared, since later lines already use it. Blank (2): the total bytes ÷ the bytes in one element = the number of elements (5). Prints 15.
Part 2 · Q8 · 4 marks · arrays
B · Q8 · Matching characters in two arrays
The following C Program counts how many characters at the same position of two arrays match. Fill in the four blanks with appropriate programming constructs and syntax.
char array1[] = {'h', 'e', 'l', 'l', 'o', 'o', '\0'};
char array2[] = {'w', 'o', 'r', 'l', 'd', '\0'};
int count = 0;
int i = 0;
while (array1[i] != '\0' _______ array2[i] != '\0') { /* (1) */
if (________) { /* (2) */
count++;
}
____________; /* (3) — printed as "(4)" */
}
printf("No. of matching characters: %d", ______); /* (4) */Answer: (1) && · (2) array1[i] == array2[i] · (3) i++ · (4) count
The key accepts "&& or ||". Only && is safe. With || the loop continues while either array still has characters, so when array2 (length 5) ends, the loop keeps indexing it past its end. Compiled with AddressSanitizer, the || version stops with "stack-buffer-overflow … READ". Use &&: stop when either array ends. (Small print: the paper labels two blanks "(4)", and prints one 'o' with curly quotes, which would not compile.)
Position by position: h/w, e/o, l/r, l/l, o/d. Output: No. of matching characters: 1.
Part 2 · Q9 · 4 marks · arrays
B · Q9 · Negative even numbers in the last five
The following C program segment calculates and prints the number of negative even values found within the last five elements of an array. Fill in the four blanks with appropriate programming constructs and syntax.
int arr[] = {12, -3, 5, -8, -2, -4, 17, -22, 14, -16};
int n = 10;
int count = 0;
int i;
for (i = _________; i < n; i++) { /* (1) */
if (_________ == 0 && _______) /* (2,3) */
{ count++; }
}
printf("Number of negative even values in the last 5 elements: %d\n", ___________); /* (4) */Answer: (1) n - 5 · (2) arr[i] % 2 · (3) arr[i] < 0 · (4) count
The last five indices are 5–9, i.e. from n−5. Those values are −4, 17, −22, 14, −16. The negative even ones are −4, −22 and −16, so it prints 3. Why % 2 == 0 works for negatives: in C, -4 % 2 is 0. But -3 % 2 is −1, not 1, so an odd test written as x % 2 == 1 silently fails for negative numbers.
Part 2 · Q10 · 4 marks · arrays
B · Q10 · Elements of A that are not in B
The code reads two arrays A and B and insert the elements present in A but not in B into another Array, C. Fill in the blanks to complete the code.
int A[] = {1, 3, 4, 5, 7, 8, 10}, B[] = {2, 3, 5, 6, 9};
int a = sizeof(A)/sizeof(A[0]);
int b = ____________; /* (1) */
int C[a], c = 0;
for(int i=0; i<a; i++) {
int flag=0;
for (int j=0; j<b; j++) {
if( ________ ) { /* (2) */
flag=1;
break;
}
}
if(flag==0)
C[ ______ ] =A[i]; /* (3) */
}
for(int k=0; __________ ; k++) /* (4) */
printf("%d ", C[k]);Answer: (1) sizeof(B)/sizeof(B[0]) · (2) A[i] == B[j] · (3) c++ · (4) k < c
flag records "found in B". C[c++] = A[i] stores at position c, then moves c on, so c always equals how many have been stored. That is why blank (4) is k < c and not k < a. Prints 1 4 7 8 10.
Part 2 · Q11 · 10 marks · write the code
C · Q11 · Read characters until Enter: sum the digits, find the highest letter
Develop a program that reads a sequence of characters (comprising of digits and lowercase alphabets) from the console terminated by an enter character. The program displays the sum of the digits. The program saves only the alphabets in a character array. The program displays the highest alphabet present in the array. (Hint: ASCI values of digits range from 48 to 57 and enter character is 10)
Usecase1: Input Sequence: ab12cd34 → Sum of digits: 10, Highest alphabet: d. Usecase2: Input Sequence: a1z2g3f9 → Sum of digits: 15, Highest alphabet: z.
#include <stdio.h>
int main() {
char ch, letters[100];
int n = 0, sum = 0;
scanf("%c", &ch);
while (ch != '\n') { /* Enter has ASCII value 10 */
if (ch >= '0' && ch <= '9')
sum += ch - '0'; /* '7' - '0' is the number 7 */
else
letters[n++] = ch;
scanf("%c", &ch);
}
printf("Sum of digits: %d\n", sum);
if (n > 0) {
char best = letters[0];
for (int i = 1; i < n; i++)
if (letters[i] > best) best = letters[i];
printf("Highest alphabet: %c\n", best);
} else
printf("No alphabets\n");
return 0;
}ab12cd34 → Sum of digits: 10 / Highest alphabet: d
a1z2g3f9 → Sum of digits: 15 / Highest alphabet: z
123 → Sum of digits: 6 / No alphabetsThe published rubric (10 marks): declarations 1 · reading characters 1 + 1 · the Enter exit condition 1 · converting a character to its digit value 2 · storing only letters 2 · finding the largest letter 2. Note how much is earned before the "clever" part.
The idea behind ch - '0': characters are stored as numbers ('0' is 48, '1' is 49, …), so subtracting '0' turns the character '7' into the number 7. Letters compare the same way ('a' < 'z'), so "highest letter" is an ordinary maximum loop.
2025-26 Sem 1 · 9 Oct 2025 90 marks · 90 min · closed book
Q1 · 10 marks · syntax
Q1 · Seven one-line syntax questions
| Part (as set) | Answer | Why / watch out |
|---|---|---|
| a. Define a symbolic constant named TRUE with the value 1 using the #define directive. | #define TRUE 1 | No = and no ;. It is a text substitution, not an assignment. |
| b. Write a single scanf statement to read the values of int a, long double b, and char ch. | scanf("%d%Lf %c",&a,&b,&ch); | %Lf for long double. The space before %c skips the space/Enter left after the number. Without it, ch would read that space. |
| c. Declare a one-dimensional float array named marks (without specifying its size) and initialize it with the following values: 56.7, 76.34, 49, 678 | float marks[]={56.7,76.34,49,678}; | Empty brackets are allowed only because the list fixes the size (4). |
| d. Declare a two-dimensional integer array named sales to store the sales data of 5 products over the last two days. Each column should represent the sales values of a single product. | int sales[2][5]; | Rows come first: 2 rows (days) × 5 columns (products). |
| e. Write a valid C expression for d = (a + (b − c)/(b + c)) / (a + b·c) − a (printed as a stacked fraction) | d=((a+(b-c)/(b+c))/(a+b*c))-a | Every fraction bar becomes a pair of brackets around its top and its bottom. Checked with a = 2, b = 3, c = 1: −1.5 both ways. |
| f. Write a C code snippet to copy the elements of one integer array a[5] = {11, 12, 13, 14, 15} into another integer array b[5]. […] using a for loop. | for(i=0;i<5;i++) b[i]=a[i]; | b = a; is illegal: arrays cannot be assigned as a whole (see 2026 A2). |
g. Rewrite the following else-if ladder using a conditional (ternary) operator in a single line: int a = 5; if (a == 1) printf("One"); else if (a == 2) printf("Two"); else printf("Other"); | (a == 1) ? printf("One") : (a == 2) ? printf("Two") : printf("Other"); | Same chain shape as 2024 P2 Q5. Compiled for a = 1, 2, 3. |
Q2 · 20 marks · 2 each · tracing
Q2 · Ten predict-the-output snippets
For each of the following code snippets, write the answers in the space provided. Assume that all necessary header files (e.g., #include) and the main() function are already included.
Q2a · Write the output
int x=8,y=3;
do{
if(x>y)
x=x-y;
else
y=y-x;
}while(x!=y); printf("%d",y);Answer: 1
| pass | x | y | action |
|---|---|---|---|
| 1 | 8 | 3 | x = 5 |
| 2 | 5 | 3 | x = 2 |
| 3 | 2 | 3 | y = 1 |
| 4 | 2 | 1 | x = 1 |
| test | 1 | 1 | x == y → stop, print y |
This is the same subtractive GCD as 2024 Q4. The GCD of 8 and 3 is 1.
Q2b · Write the output
int x, y, z;
x=0;y=0;z=0;
for(y=-3;y<=3;++y){
if((y>=0)&(x=x+1))
z = z + x;
}
z = z + y;
printf("%d", z);Answer: 16 (the official key says 14)
Look closely: one &. That is the bitwise AND. It does not stop early, so x=x+1 runs on every pass, and it compares the two numbers bit by bit. (y>=0) is 0 or 1, so the result is 1 only when y >= 0 and x is odd.
| y | x after x=x+1 | y>=0 | 1 & x | z |
|---|---|---|---|---|
| −3 | 1 | 0 | 0 | 0 |
| −2 | 2 | 0 | 0 | 0 |
| −1 | 3 | 0 | 0 | 0 |
| 0 | 4 | 1 | 1 & 100₂ = 0 | 0 |
| 1 | 5 | 1 | 1 & 101₂ = 1 | 5 |
| 2 | 6 | 1 | 1 & 110₂ = 0 | 5 |
| 3 | 7 | 1 | 1 & 111₂ = 1 | 12 |
| 4 | — | loop ends | z = 12 + 4 = 16 |
The key's 14 is what you get by reading & as &&. Then x increases only when y >= 0, giving z = 1 + 2 + 3 + 4 = 10, plus y = 4, = 14. The program as printed outputs 16 (compiled and run). Exam habit: when you see a single & or | inside an if, slow down. It evaluates both sides and works bit by bit. See the operators & selection notes.
Q2c · Write the output
int i,j;
for(i=5;i>0;i-=3){
for(j=i;j>0;j-=3){
printf("%d ",i+j);
}
}Answer: 10 7 4
i = 5: j = 5 → prints 10; j = 2 → prints 7; j = −1 stops. i = 2: j = 2 → prints 4. i = −1 stops.
Q2d · Write the output
int i=-3, j=2, k=0, m;
m = !i && ++j || ++k;
printf("%d,%d,%d,%d", i, j, k, m);Answer: -3,2,1,1
&& binds tighter than ||, so this reads (!i && ++j) || ++k. !(-3) is 0 (any non-zero number counts as true, and ! flips it), so the && stops and ++j never runs: j stays 2. The left side of || is 0, so ++k runs: k = 1, and the whole expression is true, so m = 1.
Q2e · Write the output
float x;
int count = 0;
for(x=0.0;x!=1.0;x=x+0.2) {
count++;
if (count > 4) {
printf("\nLoop terminated!");
break;
}
}
printf("\nFinal x = %.2f", x);
printf("\nCount = %d", count);Answer: Loop terminated! / Final x = 0.80 / Count = 5
Passes run with x = 0.0, 0.2, 0.4, 0.6, 0.8. On the 5th pass count becomes 5 > 4, so it breaks with x still 0.8. The point of the question: x != 1.0 is a dangerous test. 0.2 is not stored exactly in binary, so adding it five times need not land exactly on 1.0. Without the break the loop could run forever. Prefer an integer counter, or x < 1.0.
Q2f · Final value of i, and how many times "BITS Hyderabad" is printed
int i;
for(i = 0; i < 5; i++);
printf("BITS Hyderabad");Answer: i = 5; printed 1 time
The ; at the end of the for line is the loop body: an empty statement. The loop counts i up to 5 doing nothing, and then the indented printf (not part of the loop) runs once. Indentation means nothing to the compiler.
Q2g · One expression to double every element
int arr[] = {1, 2, 3, 4, 5}, i;
for(i = 0; i < 5; i++)
LINEAnswer: a[i]=a[i]*2; (i.e. arr[i] = arr[i] * 2;, or arr[i] *= 2;)
Changes {1, 2, 3, 4, 5} to {2, 4, 6, 8, 10}. (The key writes a[i]; the array is named arr.)
Q2h · Write the output
int i,j,count;
i=0;
count=0;
for(j=-2; j<=2; j++){
if((j>=1)&(printf("%d",i)))
count+=j;
i++;
}
count+=i;
printf("%d",count);Answer: 012348 (the official key says 348)
Two things to spot. (1) Single & again, so printf("%d",i) runs on every pass and prints i. It returns 1 (one character printed). (2) No braces, so only count+=j; belongs to the if. i++ runs every pass, whatever the indentation suggests.
| j | prints | (j>=1) & 1 | count | i after i++ |
|---|---|---|---|---|
| −2 | 0 | 0 | 0 | 1 |
| −1 | 1 | 0 | 0 | 2 |
| 0 | 2 | 0 | 0 | 3 |
| 1 | 3 | 1 | 1 | 4 |
| 2 | 4 | 1 | 3 | 5 |
| end | count += i → 8, prints 8 | 8 |
The key's 348 again treats & as && (then printf runs only when j ≥ 1). The program as printed outputs 012348 (compiled and run).
Q2i · The value of j for i = 2 and for i = 1
switch (i) {
case 2: i = i * i;
case 4: i = i * i;
default: i = i * i;
break;
case 16: i = i * i;
}
j = i;Answer: i = 2 → j = 256; i = 1 → j = 1
No break after the first two cases, so execution falls through. i = 2 enters at case 2: 2 → 4 → 16 → 256, stopping at the break under default. Notice that the case 4 label is never re-checked once you are inside. i = 1 matches nothing, so it goes to default: 1 × 1 = 1.
Q2j · Write the output
float a = 5.5, b = 2.0;
float res = a / b;
printf("res = %.1f\n",res);
if (res > 2.5)
printf("X ");
if (res == 2.75)
printf("Y ");
else
printf("Z ");
if (a > b)
printf("W");Answer: res = 2.8 / X Y W
res = 2.75. %.1f rounds it to 2.8. The three ifs are independent, and the else pairs only with the if directly above it. res == 2.75 happens to be true because 2.75 = 2 + ½ + ¼ is exactly representable in binary. Don't generalise: comparing with a value like 0.1 using == usually fails (as in Q2e).
Q3 · 8 marks · number systems
Q3 · Four number-system conversions
| As set | Answer | Method |
|---|---|---|
| a. Convert the decimal number 9.125 into binary. | 1001.001 | Whole part: 9 = 8 + 1 = 1001. Fraction: keep multiplying by 2 and read off the whole-number parts: .125 → 0.25 (0) → 0.5 (0) → 1.0 (1), so .001. |
| b. Convert the hexadecimal number 5C.A3 into binary. | 01011100.10100011 | Replace each hex digit by 4 bits: 5 = 0101, C = 1100, A = 1010, 3 = 0011. It works on both sides of the point. |
| c. Convert the decimal number 209.375 into octal representation. | 321.3 | 209 = 3 × 64 + 2 × 8 + 1, so 321. Fraction: .375 × 8 = 3.0, so .3. |
| d. Write the 2's complement binary form of the decimal numbers 53 and −69 in 8-bit representation. | 00110101, 10111011 | 53 = 32 + 16 + 4 + 1. For −69: write 69 = 01000101, flip every bit (10111010), then add 1 (10111011). |
All four were re-computed by machine. Eight marks of pure method: drill these in the number-systems drill.
Q4 · 24 marks · 3 each
Q4 · Eight rewrite / complete / output parts
Q4a · Rewrite the following for loops as an equivalent do-while loops.
int i,j,n = 5, sum = 0;
for(i = 1; i <= n; i++) {
for(j = i; j <= n; j += 2) {
sum += i * j;
printf("(%d,%d) ", i, j);
}
printf("\n");
}
printf("Sum = %d", sum);Answer: both loops become do-while (the key's answer is correct)
int i = 1, j, n = 5, sum = 0;
do {
j = i;
do {
sum += i * j;
printf("(%d,%d) ", i, j);
j += 2;
} while (j <= n);
printf("\n");
i++;
} while (i <= n);
printf("Sum = %d", sum);Both versions print the same five lines and Sum = 86 (compiled side by side). They are equivalent only because every loop runs at least once: the inner loop starts at j = i ≤ n, and a do body always runs once.
Q4b · Complete Line1 and Line2 to print a centred star pyramid (5 rows)
int i,j,n = 5;
for(int i = 1; i <= n; i++) {
for(j = 1; j <= Line1; j++) {
printf(" ");
}
for(k = 1; k <= Line2; k++) {
printf("* ");
}
printf("\n");
}Answer: Line1 = n - i · Line2 = i
Make a table of row → spaces → stars: row 1 has 4 spaces and 1 star; row 5 has 0 spaces and 5 stars. So spaces = n − i and stars = i. Each star is printed with a trailing space ("* "), which is what centres the pyramid. (The paper never declares k; assume int k;.)
Q4c · Write the output
int w = 0, x = 2.5, y = 5, z = 3, r, s = 4, t = 5, u = -3;
double a = 2.36, b = 3.19, c = 3.0, d = 2.91726;
printf("Expr_1 = %d\n", (int)(c * y / z + y / z * c));
printf("Expr_2 = %lf\n", x - s * t * - c - u);
printf("Expr_3 = %f\n",(float)(x + y < z + w && a > b - 17 * x || ! x < 5));Answer: 8 · 65.000000 · 1.000000
- First trap:
int x = 2.5stores 2. The fraction is dropped at the declaration. - Expr_1:
c*y/z= 3.0 × 5 / 3 = 5.0 (a double, becausecis).y/z*c= (5/3) × 3.0 = 1 × 3.0 = 3.0, because5/3is int ÷ int. The total is 8.0, cast to int: 8. - Expr_2:
s*t*-c= 4 × 5 × (−3.0) = −60.0. Then 2 − (−60.0) − (−3) = 65.000000. - Expr_3:
x+y < z+wis 7 < 3, false, so the whole&&is 0. Then!x < 5:!binds tightest, so it is(!2) < 5= 0 < 5 = 1. 0 || 1 = 1 → 1.000000.
Q4d · Declarations and a mixed-type formula (recipe score)
1. Declare two int variables, item_count and calories_per_item, on the same line,
initialized to 8 and 65 respectively.
2. Declare a float variable sugar_content = 12.5 and a char variable grade = 'B'.
3. Calculate final_score as a double using the formula: (item_count * sugar_content) - grade
4. Print each variable on a new line, followed by final_score.
Sample Output
Item Count: 8
Calories Per Item: 65
Sugar (g): 12.5
Grade: B
Final Score: 34.000000Answer: four blocks, including the char
int item_count = 8, calories_per_item = 65;
float sugar_content = 12.5;
char grade = 'B';
double final_score = (item_count * sugar_content) - grade;
printf("Item Count: %d\n", item_count);
printf("Calories Per Item: %d\n", calories_per_item);
printf("Sugar (g): %.1f\n", sugar_content);
printf("Grade: %c\n", grade);
printf("Final Score: %f\n", final_score);Why 34? A char is stored as a number, and 'B' is 66. 8 × 12.5 − 66 = 100 − 66 = 34. The sample's "12.5" needs %.1f; a plain %f would print 12.500000.
Step 2 asks for two declarations, but the official answer writes only float sugar_content = 12.5;. Without char grade = 'B';, the formula and the printf use an undeclared variable and the program does not compile.
Q4e · The loop condition for an HCF search
int a, b, x;
x = a < b ? a : b;
while(COND){
if ( (a % x == 0) && (b % x == 0) ) {
printf("%d\n", x);
break;
}
x--;
}Answer: x >= 1 (or x > 0)
Start at the smaller number and count down; the first common divisor you meet is the highest. The condition must stop before x reaches 0, because a % 0 is a division by zero. Since 1 divides everything, the loop always breaks by then anyway. Checked: a = 12, b = 18 prints 6.
Q4f · Water bottles: refill if at least two are empty
int b1, b2, b3;
scanf("%d %d %d", &b1, &b2, &b3);
if (COND)
printf("Water filling time");
else
printf("Wait");Answer: (b1 + b2 + b3) <= 1
Each bottle is 0 (empty) or 1 (full), so the sum counts the full bottles. "At least two empty" = "at most one full" = sum ≤ 1. All 8 combinations were compiled and checked. A longer but equally valid answer lists the pairs: (!b1 && !b2) || (!b1 && !b3) || (!b2 && !b3).
Q4g · Complete Line1–Line3 for the number pyramid 1 / 232 / 34543 / 4567654 / 567898765
int n=5,i,j;
for(i=1;i<=n;i++){
//Code to print space...
for(j=0;Line1;j++)
printf(" ");
/*Code to print the first half of the numbers in each row...*/
for(j=i; Line2;j++)
printf("%d",j);
/*Code to print the first half of the numbers in each row...*/
for(j=(2*i-2);Line3;j--)
printf("%d",j);
printf("\n");
}Answer: Line1 j<n-i · Line2 j<=(2*i-1) · Line3 j>=i
Tabulate one row before writing any condition. Row i = 3 is 34543: 2 spaces (n − i), rising half 3, 4, 5 (from i up to 2i − 1), falling half 4, 3 (from 2i − 2 down to i). Now read the three conditions off the table. Compiled output matches all five rows.
Q4h · Complete Line1–Line3 for 1 / 121 / 12321 / 1234321 / 123454321 (each row indented one more)
int n=5,i,j;
for(i=1;i<=n;i++){
//Code to print space...
for(j=1;Line1;j++)
printf(" ");
for(j=1;Line2;j++)
printf("%d",j);
for(j=i-1;Line3;j--)
printf("%d",j);
printf("\n");
}Answer: Line1 j<i · Line2 j<=i · Line3 j>=1
The picture on the paper shifts each row one more space to the right, so the spaces grow: i − 1 of them, i.e. j < i starting from 1. The rising half is 1…i, and the falling half is i − 1 down to 1.
Q5 · 14 marks · write the logic
Q5 · Add 1 to a number stored as an array of digits
In a banking system, each customer has an account number that can be extremely large, and the system stores account numbers as array of digits for efficient processing. Each digit of the account number is stored in a list, with the most significant digit at the beginning of the list and the least significant at the end. Now, when a new customer joins, the system increments the last account number by one. However, the challenge is that the account number is stored in an array, and due to its large size, performing the increment operation requires handling potential carries. You are given an array digits representing a large integer, where each digits[i] is the ith digit of the integer. The digits are ordered from the most significant to the least significant, and there are no leading zeros in the integer. Your task is to increment the large integer by one and print the resulting array of digits. The result must reflect the correct account number after the increment. Assume that second most significant digit is not 9. Sample Input: {1,0,9,9} Sample Output: {1,1,0,0}
#include <stdio.h>
int main(){
int n; //Size of account number
scanf("%d",&n);
int last_accno[n]; //Scan last account number
for(int i=0;i<n;i++)
scanf("%d",&last_accno[i]);
int new_accno[n];
//Write logic for new account no.
int carry = 1; /* "+1" enters at the last digit */
for (int i = n - 1; i >= 0; i--) {
int s = last_accno[i] + carry;
new_accno[i] = s % 10; /* digit that stays */
carry = s / 10; /* 1 only if s was 10 */
}
for(int i=0;i<n;i++)
printf("%d",new_accno[i]);
return 0;
}4 1 0 9 9 → 1100
3 1 2 3 → 124
5 4 8 9 9 9 → 49000This is column addition from primary school, done right to left. Add the carry to the digit; the ones digit of the result stays, and the tens digit (0 or 1) becomes the next carry. The given assumption (second digit not 9) guarantees the carry dies out before the front, so the array never needs an extra digit. The official answer splits "last digit is 9" and "last digit is not 9" into two branches with a flag, which is also correct (compiled with the same inputs). The carry loop is shorter and harder to get wrong under time pressure.
Q6 · 14 marks · write the program
Q6 · Smallest number from the digits of N, without arrays
Write a C program that will read a 5-digit positive integer N and output the smallest integer possible by shuffling the digits of N. For example if N = 62371 then the output should be 12367. Assume all the digits of N are distinct from each other. You are not allowed to use arrays.
#include <stdio.h>
int main() {
int n, result = 0;
scanf("%d", &n);
for (int d = 0; d <= 9; d++) { /* try digits smallest first */
int k = n;
while (k > 0) { /* is digit d somewhere in n? */
if (k % 10 == d)
result = result * 10 + d; /* append d on the right */
k = k / 10;
}
}
printf("Output = %d\n", result);
return 0;
}62371 → Output = 12367
98765 → Output = 56789
50314 → Output = 1345The idea: the smallest arrangement lists the digits in increasing order. So try d = 0, 1, …, 9, and whenever d appears in N, append it to the answer. result*10 + d is "write d on the right" (12 → 123), and k % 10 / k / 10 peel off digits from the right, the loops-page routine. The official answer (about 30 lines: repeatedly find and remove the minimum digit, tracking powers of ten) gives the same outputs but is much easier to get wrong on paper.
If N contains a 0, as in 50314, both programs print 1345: the 0 lands at the front and disappears as a leading zero. The paper doesn't say whether "smallest integer" may start with 0. This is exactly the "state your assumption" case. Write "Assume a leading 0 is allowed (01345 = 1345)". If you instead want a 5-digit answer, put the smallest non-zero digit first: 10345.
2025-26 Sem 2 · 13 Mar 2026 90 marks · 90 min · closed book
Part A · Q1 · 1 mark
A1 · Which of the following is a standard identifier in C
- (a) return
- (b) printf
- (c) void
- (d) int
Answer: (b) printf
return, void and int are keywords, reserved words you cannot use as names. printf is an ordinary name (an identifier) that the standard library defines.
Part A · Q2 · 1 mark
A2 · What would be the error message when you run the below C program?
#include <stdio.h>
int main() {
int arr1[3] = {1,2,3};
int arr2[3];
arr2 = arr1;
printf("%d\n", arr2[1]);
return 0;
}- (a) error: Assignment to expression with array type
- (b) 4096
- (c) lvalue required as left operand of assignment
- (d) Array out of bound
Answer: (a)
An array name cannot be assigned to; copy element by element with a loop (2025 Q1f). Option (a) is gcc's wording. On a Mac, clang says error: array type 'int[3]' is not assignable, which is the same error in different words. It is a compile error: the program never runs.
Part A · Q3 · 1 mark
A3 · Which of the following operator has higher precedence than '%' operator in C ?
- (a) >>
- (b) %
- (c) &&
- (d) !
Answer: (d) !
Unary operators (!, unary -, ++) bind tighter than * / %, which bind tighter than + -, then shifts >>, then comparisons, then &&, then ||. % itself is equal, not higher. The full ladder is in the operators & selection notes.
Part A · Q4 · 1 mark
A4 · The binary representation of decimal value 143 is:
- (a) 10001111
- (b) 10001101
- (c) 01000111
- (d) 10010111
Answer: (a) 10001111
143 = 128 + 15 = 128 + 8 + 4 + 2 + 1, so the 128 bit and the four lowest bits are set: 1000 1111.
Part A · Q5 · 1 mark
A5 · a = a * sizeof(a) with a float
What will be printed if the following program is compiled and executed? Assume that float takes 4 bytes.
#include<stdio.h>
int main()
{ float a=2.0; a=a*sizeof(a);
printf("%f", a);
return 0; }- (a) 4.0
- (b) 4.000000
- (c) 8.000000
- (d) 8.0
Answer: (c) 8.000000
sizeof(a) is 4 (bytes), so a = 2.0 × 4 = 8.0. %f always prints six decimal places, which rules out (a) and (d).
Part A · Q6 · 1 mark
A6 · A nested ?:
int m, a=15, b=9, c=12;
m= (a< b ? (a < c ? a : c) : (b > c ? b : c));
printf("%d",m);- (a) 15
- (b) 12
- (c) 9
- (d) 1
Answer: (b) 12
a < b is 15 < 9, false, so take the part after the colon: b > c ? b : c. 9 > 12 is false, so m = c = 12. (It does not compute the largest of the three; that would be 15.)
Part A · Q7 · 1 mark
A7 · What will be the output if n = 12?
scanf("%d", &n);
while (n >= 0) {
printf("%2d", n % 10);
n = n / 10;
}- (a) 2 1
- (b) 1 2
- (c) Infinite Loop
- (d) Syntax Error
Answer: (c) Infinite Loop
n goes 12 → 1 → 0 → 0 → 0 …, because 0 / 10 is 0, and 0 >= 0 stays true forever. It prints " 2 1 0 0 0 …" without end. The digit-peeling loop must use n > 0.
Part A · Q8 · 1 mark
A8 · What problem can be solved by the above code?
if (x > y)
min = y;
else
min = x;
for (int g = min; g > 0; --g)
if (x % g == 0 && y % g == 0) {
printf("g = %d\n", g);
break;
}- (a) Syntax Error
- (b) Greatest common divisor (GCD)
- (c) Least common multiple (LCM)
- (d) None of these
Answer: (b) GCD
Count down from the smaller number; the first number that divides both is the greatest common divisor. This is the same loop as 2025 Q4e. Checked with 12 and 18: g = 6.
Part A · Q9 · 1 mark
A9 · What is the format specifier for unsigned int
- (a) %d
- (b) %u
- (c) %l
- (d) %ui
Answer: (b) %u
%u for unsigned int. %l alone is incomplete (it needs a letter after it, as in %ld), and %ui does not exist.
Part A · Q10 · 1 mark
A10 · Counting the 1-bits of 2273
What is the output of this code if the input given to it is 2273?
#include <stdio.h>
int main()
{
int n, count = 0, i = 1;
scanf("%d", &n);
for(int j=0; j < 32; j++){
if(n&i) count++;
i = i<<1;
}
printf("%d", count);
return 0;
}- (a) 1
- (b) 3
- (c) 5
- (d) 7
Answer: (c) 5
i starts at 1 and i<<1 doubles it each pass (1, 2, 4, 8, …), so each pass tests one bit of n. The loop counts the 1-bits. 2273 = 2048 + 128 + 64 + 32 + 1 = 100011100001₂, which has five 1s.
Part B · Q1 · 16 marks · write the program
B1 · Insert into an array, shifting right (one array only)
Write a program to insert a new element it to an array of integers at a given position (index), while doing so, all the elements from that position(index) must be moved to right by one position, and the original last element is lost as it moves beyond the boundary. Then print the resultant array. We assume that the initial array has n elements. You are permitted to declare and use only one array, not more than one. […] Declare an array of integers of size n, where n is given as input through scanf(). Similarly, accept an integer to be inserted and the position (index) where it is to be inserted. Also scan all n initial values for elements of the original array. Ex: Assume that the Original array where n=5, is {2 5 6 4 9}; the new element to be inserted is 10, at insert position (index)=2. After the execution of the program, the resulting array should be :{ 2 5 10 6 4}. […] Note: the last original element, that is 9 is lost.
#include <stdio.h>
int main() {
int n, pos, val;
scanf("%d %d %d", &n, &pos, &val);
int a[n]; /* the one array allowed */
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int i = n - 1; i > pos; i--) /* shift right, from the END */
a[i] = a[i - 1]; /* old a[n-1] is overwritten: lost */
a[pos] = val;
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}5 2 10 2 5 6 4 9 → 2 5 10 6 4
5 0 1 2 5 6 4 9 → 1 2 5 6 4
5 4 7 2 5 6 4 9 → 2 5 6 4 7Why shift from the end: copying left-to-right (a[3] = a[2], then a[4] = a[3], …) would copy the same value along the whole array. Going right-to-left, each value moves before it is overwritten. The official answer instead carries a temp forward left-to-right, which is also correct but harder to trace. Rubric: overall program, syntax, variables 3 · array declaration + scanf + printf 5 · core logic 8. Half the marks are for the parts you can write before thinking.
Part B · Q2 · 10 + 6 marks
B2 · (a) Largest of three without && · (b) the missing-break bug
(a) Write a C program that takes 3 distinct integers as input and determines which is the largest. Your program should not use the logical AND operator (i.e. &&) and should use at most three if-else statements and no temporary variables (i.e. other than the variables storing the three values). [10]
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three distinct integers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b) {
if (a > c) printf("Largest = %d\n", a);
else printf("Largest = %d\n", c);
} else {
if (b > c) printf("Largest = %d\n", b);
else printf("Largest = %d\n", c);
}
return 0;
}Nesting replaces &&: being inside if (a > b) already means "a beats b", so one more test decides it. Exactly three if-else statements. All six orderings of 1, 2, 3 were compiled; each prints Largest = 3. Rubric: set-up and I/O 2, logic meeting the constraints 8.
(b) Prof. Cursor gives the following program to determine if a given number is positive, negative or zero. Determine why the program does not work correctly, and provide a modified program that will work correctly without adding additional cases. [6]
#include <stdio.h>
int main() {
int n = 6;
switch(n > 0 ? 1 : n < 0 ? -1 : 2) {
case -1:
printf("Negative");
case 1:
printf("Positive");
default:
printf("Zero");
}
return 0;
}Why it fails: it prints PositiveZero. There is no break, so after case 1 runs, execution falls through into default. Fix: add break; after the Negative and Positive printfs. Compiled with n = 6, −4, 0: Positive, Negative, Zero. Rubric: identifying the mistake 3, fixing it 3.
Part B · Q3 · 16 marks · write the program + dry run
B3 · Every way to make change with three denominations (only for loops)
Consider the problem to find change of a given currency note having value as "price", where the available denominations are "deno1", "deno2" and "deno3". Assume that "deno1" > "deno2" > "deno3". You need to write a C program to solve this problem. Your program must take as input the value of "price", and the values of denomination for "deno1", "deno2" and "deno3". The program produces as output the all possible combinations of the number of denominations of the form <d11, d12, d13> <d21, d22, d23> … where <di1, di2, and di3> indicates the number of denominations of "deno1", "deno2" and "deno3" respectively, that results in a valid change for "price". The program also produces as output the total number of ways […]. //write your logic in the following using only for(; ;) loop //display in the following the number of ways; display "Not Possible" if there is no way to change. […] Finally, show the result of executing (using dry run) your program for "price" = 110, deno1 = 100, deno2 = 20, deno3 = 10.
#include <stdio.h>
int main(){
int price = 0; //for the currency note
int deno1 = 0, deno2 = 0, deno3 = 0; // for the denominations
int ways = 0; // for the number of ways
printf("Enter price: ");
scanf("%d", &price);
printf("Enter denominations (largest first): ");
scanf("%d %d %d", &deno1, &deno2, &deno3);
for (int d1 = 0; d1 <= price / deno1; d1++)
for (int d2 = 0; d2 <= price / deno2; d2++)
for (int d3 = 0; d3 <= price / deno3; d3++)
if (d1 * deno1 + d2 * deno2 + d3 * deno3 == price) {
printf("<%d, %d, %d>\n", d1, d2, d3);
ways++;
}
if (ways > 0)
printf("Number of ways = %d\n", ways);
else
printf("Not Possible\n");
return 0;
}<0, 0, 11>
<0, 1, 9>
<0, 2, 7>
<0, 3, 5>
<0, 4, 3>
<0, 5, 1>
<1, 0, 1>
Number of ways = 7The idea is brute force: try every possible count of each note (at most price / deno of each) and keep the combinations that add up exactly. The dry run the paper asks for: with one 100-note, the remaining 10 can only be one 10-note: <1, 0, 1>. With no 100-notes, 20-notes can number 0 to 5 and 10-notes fill the rest (11, 9, 7, 5, 3, 1). Total 7. Also tested with 15 from 10/4/2: Not Possible. Rubric: I/O 2, loops 7, ways/"Not Possible" 2, dry-run tuples 3, count 2.
Part B · Q4 · 16 marks · write the program
B4 · Cricket statistics with input validation
Assume that in the recently concluded cricket tournament, player X has played 10 matches. Write a C program that accepts the runs scored in all 10 matches into a 1-d array, where each score must be an integer between 0 and 200. The program should validate the input and display an error message and terminate if any score falls outside the valid range. After successfully storing the scores, the program must calculate and display the total runs scored, the average runs (printed with two decimal places), the highest score, and the lowest score. It should also count and display the number of centuries scored (100 or more runs). Additionally, the program should determine the player's consistency by checking the difference between the highest and the lowest scores: if the difference is less than or equal to 50, the player is a "Consistent Performer"; else he is an "Inconsistent Performer". The program must not use any user-defined functions or sorting techniques, and all logic should be written inside the main function. A valid test case is as below: Input: 120 35 0 88 150 60 45 100 10 75 · Output: Total Runs = 683 Average Runs = 68.30 Highest Score = 150 Lowest Score = 0 Number of Centuries = 3 Performer Status = Inconsistent Performer
#include <stdio.h>
int main() {
int runs[10], total = 0, highest, lowest, centuries = 0;
printf("Enter runs in 10 matches: ");
for (int i = 0; i < 10; i++) {
scanf("%d", &runs[i]);
if (runs[i] < 0 || runs[i] > 200) {
printf("Error: runs must be between 0 and 200\n");
return 1;
}
}
highest = lowest = runs[0];
for (int i = 0; i < 10; i++) {
total += runs[i];
if (runs[i] > highest) highest = runs[i];
if (runs[i] < lowest) lowest = runs[i];
if (runs[i] >= 100) centuries++;
}
printf("Total Runs = %d\n", total);
printf("Average Runs = %.2f\n", total / 10.0); /* 10.0, not 10 */
printf("Highest Score = %d\n", highest);
printf("Lowest Score = %d\n", lowest);
printf("Number of Centuries = %d\n", centuries);
if (highest - lowest <= 50)
printf("Performer Status = Consistent Performer\n");
else
printf("Performer Status = Inconsistent Performer\n");
return 0;
}Total Runs = 683
Average Runs = 68.30
Highest Score = 150
Lowest Score = 0
Number of Centuries = 3
Performer Status = Inconsistent PerformerOne traversal does four jobs: a sum, a max, a min and a counter, the standard patterns from the arrays notes. Two details carry marks. total / 10.0: total / 10 would be integer division, printing 68.00. And start max and min at runs[0], not at 0, or the minimum of all-positive scores comes out as 0. Also tested: a 250 stops with the error, and scores 50–95 give "Consistent Performer".
Part B · Q5 · 16 marks · write the program
B5 · Quadratic roots with switch, no if
Write a program to determine the roots of a quadratic polynomial ax2+bx+c where a, b, and c, are integers. The program should work for both real and complex roots. For example, for the polynomial x2+4x+5, it should print the roots as -2.000000+1.000000i and -2.000000-1.000000i. The program should use a switch statement to deal with the different cases (real roots and complex roots) and should not use if conditions.
#include <stdio.h>
#include <math.h>
int main() {
int a, b, c;
double disc, re, im;
printf("Enter coefficients of the polynomial: ");
scanf("%d %d %d", &a, &b, &c);
disc = b * b - 4.0 * a * c;
switch (disc >= 0) { /* 1 = real roots, 0 = complex */
case 1:
printf("Roots are %lf and %lf\n",
(-b + sqrt(disc)) / (2.0 * a),
(-b - sqrt(disc)) / (2.0 * a));
break;
case 0:
re = -b / (2.0 * a);
im = sqrt(-disc) / (2.0 * a);
printf("Roots are %lf+%lfi and %lf-%lfi\n", re, im, re, im);
break;
}
return 0;
}1 4 5 → Roots are -2.000000+1.000000i and -2.000000-1.000000i
1 3 2 → Roots are -1.000000 and -2.000000
1 1 1 → Roots are -0.500000+0.866025i and -0.500000-0.866025iThe trick that avoids if: a comparison like disc >= 0 is a number, 1 or 0, so switch can branch on it. Complex roots are (−b ± i√(4ac − b²)) / 2a. The real part and the imaginary part are printed separately. State the assumption a ≠ 0. Rubric: headers, declarations, scanf, syntax 6 · switch 4 · root expressions 4 · printf 2.
The model solution writes re=-b/(2*a); and r1=-b/(2*a)+d/(2*a); with int a, b. -b/(2*a) is then integer division, which throws the fraction away. It happens to work for the sample x² + 4x + 5 (−4/2 = −2 exactly). Compiled, it prints −0.5 and −1.5 for x² + 3x + 2 (true roots −1, −2), a real part of 0 for x² + x + 1 (true −0.5), and 0.25, −0.25 for 2x² + 3x + 1 (true −0.5, −1). The fix is one character: 2.0*a. Test your programs on an input other than the sample.
Round 1 (by topic): use the topic map. Cover the answer, write yours on paper, then compare. For every miss, write one line saying which rule you missed (e.g. "&& skips the right side").
Round 2 (as an exam): do one paper against the clock (the 2025 paper is the closest to a full 90 minutes). Write the programs by hand, then type them in and compile, so the compiler marks your paper version.
The gaps these papers expose have their own pages: the operators & selection notes (short-circuit, comma, & vs &&, scanf formats, switch fallthrough), the number-systems drill, and the write-a-program practice for the long questions.