Blog
Programming Fundamentals Past Paper 2013 – BS IT 1st Term UOS (CMP-140)
University of Sargodha BS IT 1st term Programming Fundamentals (CMP-140) paper, 2013 exam (60 marks, 2:30 hours). The paper is retyped below without the handwritten number on the original, followed by solved answers for all 12 short questions and all six programs: a series sum, program output tracing, syntax-error correction, array maximum, swapping, factorial and a product function. Every program was compiled and run.
Solved answers
Q1 – Short answers (12 × 2)
i. 25 * (6 + (50 − 48) / 2) + 15. (50 − 48)/2 = 1; 6 + 1 = 7; 25 × 7 = 175; 175 + 15 = 190.
ii. Type casting. Converting a value from one data type to another. Implicit (automatic, e.g. int to float in 5 + 2.5) or explicit: (float)a / b.
iii. Syntax error. A violation of the grammar rules of the language, found by the compiler, e.g. a missing semicolon: int x = 5 or printf("Hi".
iv. Compiler vs interpreter. A compiler translates the whole program into machine code before running (fast execution, errors listed together, e.g. C). An interpreter translates and runs one statement at a time (slower, stops at the first error, e.g. Python, BASIC).
v. Comments. Notes for the reader that the compiler ignores. Types: single-line // comment and multi-line /* comment */.
vi. a + b / c − 5 / d + 7 / e − 37 % 3. Integer arithmetic: b/c = 20/15 = 1; 5/d = 5/8 = 0; 7/e = 7/40 = 0; 37 % 3 = 1. So 10 + 1 − 0 + 0 − 1 = 10. (Tested.)
vii. C expression. x = (x + 32) / (m - (32.0 / 2) * n);
viii. .h files. Header files contain declarations shared between source files: function prototypes, #define macros, constants, structure/typedef declarations and extern variable declarations (not function bodies). They are included with #include, e.g. stdio.h.
ix. int i = 3; i = i++; This is undefined behaviour in C: i is modified twice without a sequence point (by = and by ++), so the result is not defined (compilers may give 3 or 4). It should be written as i++;.
x. Boolean type in C. Classic C uses int (0 = false, non-zero = true). Since C99 the type _Bool exists, and bool, true, false come from <stdbool.h>.
xi. Static variable. A local static variable keeps its value between function calls and is initialised only once; a global static variable/function is visible only within its own file.
xii. Preprocessor. Before compilation it processes # directives: includes header files, expands macros (#define), handles conditional compilation (#ifdef) and removes comments.
Q2 – Series G = 1 + x³/3! + x⁵/5! + … (n terms)
#include <stdio.h>
#include <math.h>
int main(void)
{
int n, i;
double x, term, G = 1.0, fact = 1.0;
printf("Enter x and n: ");
scanf("%lf %d", &x, &n);
/* G = 1 + x^3/3! + x^5/5! + x^7/7! + ... (n terms) */
for (i = 1; i < n; i++) {
int p = 2 * i + 1; /* powers 3, 5, 7, ... */
fact = fact * (p - 1) * p; /* builds 3!, 5!, 7!, ... */
term = pow(x, p) / fact;
G = G + term;
}
printf("G = %lf\n", G);
return 0;
}Sample run: x = 1, n = 6 → G = 1.175201 (the same value was checked in Python).
Q3 – Output of the program
The program uses reference parameters (int &), so it is really C++ (it compiles with g++; in g++ main must return int).
| Step | a | b | c |
|---|---|---|---|
| Start | 12 | 10 | 23 |
| abc(a, b, c): t = 24, n1 = a = 24 + 10 = 34, n2 = 23 (copy only), n3 = c = 34 | 34 | 10 | 34 |
| b = a; c = a + b | 34 | 34 | 68 |
| abc(a, b, c): t = 68, a = 68 + 34 = 102, c = 102 | 102 | 34 | 102 |
Output (\t is a tab, \n a new line):
value of a and b and c
now value of a and b and c
102 34102Note the last format string “%d %d%d” prints b and c without a space between them: 34102 = 34 and 102.
Q4 – Syntax errors and corrections
| Line | Error | Correction |
|---|---|---|
| 1 | Header name wrong (stdio); also the program uses cout | #include <iostream> and #include <cstring> (C++), using namespace std; |
| 3 | Prototype says void string1(char) but the definition returns int and takes an array | int string1(char abc[]); |
| 4 | MAIN must be main and should return int | int main() |
| 7 | An array cannot be assigned with =; curly quotes | strcpy(ch, “PAPER”); |
| 9 | Wrong operator for output | cout << ch; |
| 10 | main should return a value | return 0; } |
| 13 | No function strlength | int a = strlen(abc); |
| 14 | b is not declared | float b = float(a); |
| 15–16 | int function returns nothing | return a; } |
Corrected program (compiles and prints PAPER):
#include <iostream>
#include <cstring>
using namespace std;
int string1(char abc[]);
int main()
{
char ch[10];
strcpy(ch, "PAPER");
string1(ch);
cout << ch;
return 0;
}
int string1(char abc[])
{
int a = strlen(abc);
float b = float(a);
return a;
}Q5 – Maximum of 10 floats using a function
#include <stdio.h>
float findMax(float arr[], int size)
{
float max = arr[0];
int i;
for (i = 1; i < size; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
int main(void)
{
float a[10];
int i;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++)
scanf("%f", &a[i]);
printf("Maximum value = %.2f\n", findMax(a, 10));
return 0;
}Run: 3 9.5 −2 7 1 0 4.25 8 6 2 → Maximum value = 9.50.
Q6 – Swap and factorial
#include <stdio.h>
int main(void)
{
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}Run: 4 9 → After swapping: a = 9, b = 4.
#include <stdio.h>
int main(void)
{
int n, i = 1;
long long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n) {
fact = fact * i;
i++;
}
printf("Factorial of %d = %lld\n", n, fact);
return 0;
}Run: 5 → Factorial of 5 = 120.
Q7 – Product using function add
#include <stdio.h>
float add(float x, float y, float z)
{
return x * y * z; /* product of the three numbers */
}
int main(void)
{
float a, b, c, result;
printf("Enter three numbers: ");
scanf("%f %f %f", &a, &b, &c);
result = add(a, b, c);
printf("Product = %.2f\n", result);
return 0;
}Run: 2 3 4 → Product = 24.00.
All programs below were compiled with gcc/g++ and run; the outputs shown are from those runs.
