School Of Technology
Programming Principles and Practice with C and C++
Total Marks: 50
Instructions
1. All 28 questions are compulsory - 25 MCQ and 3 Programming
2. The Multiple Choice Question (MCQ) section carries a total of 25 marks, with
each question allotted 1 mark.
3. The Programming section carries a total of 25 marks, distributed as 5 marks, 8
marks, and 12 marks respectively.
4. No negative marking will be applied in any section.
SECTION-A (Q1 – Q10) (1 mark each)
Question 1
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int s = 0, n = 8;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
s += i + j;
printf("%d\n", s);
return 0;
}
A. 512
B. 576
C. 640
D. 592
Question 2
What is the output of the following code snippet?
#include <stdio.h>
int main() {
char c = 'd';
int x = 70;
printf("%d", c + x);
}
A. 170
B. 168
C. 169
D. 171
Question 3
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
signed char c = 259;
printf("%d\n", (int)c);
return 0;
}
A. 3
B. 1
C. 259
D. Implementation-defined / varies
Question 4
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
char a = 20, b = 30;
char d = a * b;
short x = a * b - d / 100 - 500;
printf("%d\n", x);
return 0;
}
A. -450
B. -470
C. 100
D. 600
Question 5
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
signed char a = 80;
signed char b = 100;
signed int c = a + b;
printf("%d\n", (int)c);
return 0;
}
A. 180
B. -76
C. 9216
D. 44
Question 6
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
unsigned short a = 50;
signed short b = -10;
printf("%d\n", a > b + 30);
return 0;
}
A. 1
B. 0
C. -1
D. Compile error
Question 7
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a = 5, b = 7, c = 2, d = 6, z = 0;
z = a+++ ++b*c--- --d;
printf("%d\n", z);
return 0;
}
A. 41
B. 39
C. 37
D. 43
Question 8
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a, b, c, d;
a = b = c = d = 3;
a *= b + 2;
c += d *= 2;
printf("%d\n", a + c);
return 0;
}
A. 23
B. 21
C. 19
D. 25
Question 9
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a = 7;
a = a++;
a = a++ * a--;
int c = a;
int d = a++ * a++;
int x = (c + d) / 100;
printf("%d\n", x);
return 0;
}
A. 0
B. 1
C. 2
D. 3
Question 10
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a, b, c, p = 15;
p += (a = 6, b = a + 3, c = a + b);
printf("%d\n", p);
return 0;
}
A. 30
B. 31
C. 29
D. 32
SECTION-B (Q11 – Q20) (1 mark each)
Question 11
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int i = -2;
unsigned int j = 1;
int x = 0;
if (i < j) x = 10;
else x = 20;
printf("%d\n", x);
return 0;
}
A. 10
B. 20
C. Compile error
D. Undefined behavior
Question 12
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a = 3, y = 10;
switch (a) {
y = 30;
case 2:
y++;
break;
case 3:
y--;
break;
default:
y += 5;
}
printf("%d\n", y + 1);
return 0;
}
A. 10
B. 31
C. 29
D. 32
Question 13
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int v = 3, a = 1, b = 3;
switch (v) {
case a:
a++;
break;
case b:
b++;
break;
}
printf("%d\n", a + b);
return 0;
}
A. 4
B. 5
C. 3
D. 2
Question 14
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int g = 9, h = 0;
if ((g / 2) == 4) h = h + 1;
if ((g % 4) == 1) h = h + 2;
if ((g / 2) != 4) h = h + 5;
printf("%d\n", h);
return 0;
}
A. 1
B. 3
C. 8
D. 7
Question 15
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int i, j, k = 0, c = 0;
j = 2 * 4 / 5 + 3.0 / 6 + 9 / 4;
k -= --j;
for (i = 0; i < 4; i++) {
switch (i + k) {
case 0: case 1: c += 1;
case 2: ++c;
default: c++;
}
}
printf("%d\n", c);
return 0;
}
A. 10
B. 13
C. 11
D. 9
Question 16
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int i, j, count = 0;
i = 0;
for (j = -2; j <= 2; j++) {
if ((j >= 0) && (i++)) count += j;
}
count += i;
printf("%d\n", count + i);
return 0;
}
A. 7
B. 5
C. 6
D. 4
Question 17
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a = 3, b = -2, c = 4, d = -1, x = 0;
if (a = b - c - d) x = (a++, b--, c++);
else x = (c--, ++a, ++b);
printf("%d\n", x);
return 0;
}
A. 0
B. 1
C. 2
D. Compile-time error
Question 18
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
unsigned char i = 254;
while (i++ != 0);
printf("%d\n", i);
return 0;
}
A. 0
B. 1
C. 255
D. 2
Question 19
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int i = 1, j = 0, s = 0, n = 8;
for (i = 1; i <= n; i++)
for (j = i; j <= i; j++) s = s + j;
printf("%d\n", s);
return 0;
}
A. 36
B. 32
C. 28
D. 45
Question 20
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int i = 0, k = 0, j = 3;
i = j < k ? k : j--;
if (j < i) j = j + k - 1;
if (j == i) j = j - i;
else j = j + --k;
printf("%d\n", j + k - i + 7);
return 0;
}
A. 4
B. 5
C. 3
D. 6
SECTION-C (Q21 – Q25) (1 mark each)
Question 21
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
unsigned long long c = 0, n = 1ULL << 20; // 2^20
while (n) {
c++; n >>= 1;
}
printf("%llu\n", c);
return 0;
}
A. 20
B. 21
C. 22
D. 19
Question 22
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int i, j, k = 0;
j = 3 * 4 / 5 + 2.0 / 4 + 7 / 3;
k -= --j;
int c = 0;
for (i = 0; i < 4; i++) {
switch (i + k) {
case 0:
case 1: ++c;
case 2: ++c;
default: ++c;
}
}
printf("%d\n", c);
return 0;
}
A. 6
B. 8
C. 10
D. 12
Question 23
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a = 15, b = 27, x = 0;
a = b++ + a++;
b = ++b + ++a;
int sum = a + b - 19;
for (int i = sum; i >= 1; i /= 2) x += 1;
printf("%d\n", x);
return 0;
}
A. 3
B. 4
C. 5
D. 6
Question 24
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a = 0, i, j, k, n = 7;
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
for (k = 1; k <= j; k++)
a = a + 1;
printf("%d\n", a);
return 0;
}
A. 140
B. 120
C. 84
D. 56
Question 25
What is the output of the following code snippet?
#include <stdio.h>
int main(void) {
int a = 0, i, j, k, n = 8;
for (i = 1; i <= n; i++)
for (j = 1; j <= i; j++)
for (k = 1; k <= j; k++)
a = a + 1;
printf("%d\n", a);
return 0;
}
A. 120
B. 165
C. 220
D. 84
Section-D (Q26-Q28) Programming Questions
Question 26 (5 marks)
Write a C program to determine if a year is a leap year using an if-else-if
ladder.
Input Example
2024
Expected Output
Leap Year
Question 27 (8 marks)
Problem Name: Product Minus Sum of Digits (variant)
Problem Statement:
Given a positive integer n, compute and return the value (product of digits of
n) − (sum of digits of n).
Constraints: 1 <= n <= 10^6
Input / Output format:
First line contains TC. Each of the following TC lines contains a single integer n.
For each test case print the result on a new line.
Example Input 4
123
405
999
560
Example Output
-2
0
524
-11
Question 28 Attempt any one of the following : either option A or option B.
Option A (12 marks)
Factorial Trailing Zeroes
Given n, print count of trailing zeros in n!.
Example
Input: 4
4
10
50
100000
Output:
0
2
12
24999
Option B: (12 marks)
Number of Common Factors
Given integers a and b, count numbers dividing both.
Example
Input: 4
10 20
16 24
7 49
13 17
Output:
4
4
2
1