Past Papers, Programming Fundamentals, UOS Main Campus

Programming Fundamentals Past Paper 2013 – BS IT 1st Term UOS (CMP-140)

ProgramBS IT
Term1st
Year2013
Course codeCMP-140
AnswersSolved ✓

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).

Stepabc
Start121023
abc(a, b, c): t = 24, n1 = a = 24 + 10 = 34, n2 = 23 (copy only), n3 = c = 34341034
b = a; c = a + b343468
abc(a, b, c): t = 68, a = 68 + 34 = 102, c = 10210234102

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 34102

Note 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

LineErrorCorrection
1Header name wrong (stdio); also the program uses cout#include <iostream> and #include <cstring> (C++), using namespace std;
3Prototype says void string1(char) but the definition returns int and takes an arrayint string1(char abc[]);
4MAIN must be main and should return intint main()
7An array cannot be assigned with =; curly quotesstrcpy(ch, “PAPER”);
9Wrong operator for outputcout << ch;
10main should return a valuereturn 0; }
13No function strlengthint a = strlen(abc);
14b is not declaredfloat b = float(a);
15–16int function returns nothingreturn 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.

Save this paper for offline studyDownload the question paper, or save the paper together with all solved answers as a PDF.
↓ Download Paper (JPG)

About Saif R

An educator and technology enthusiast passionate about empowering students through quality education and digital learning. Dedicated to making academic resources and opportunities accessible to everyone.