Programming Fundamentals, Past Papers, UOS Main Campus

Programming Fundamentals Past Paper 2017 – BSCS 1st Term UOS (CMP-2122)

ProgramBSCS
Term1st
Year2017
Course codeCMP-2122
AnswersSolved ✓

University of Sargodha Programming Fundamentals (CMP-2122) paper for BS Computer Science 1st term, 2017 exam (60 marks, 2:30 hours). The full paper is below, followed by solved answers for all 12 short questions and complete C programs for every long question.

Solved answers

Q1 – Short answers (12 × 2)

a) Evaluate 25 * (6 + (50 − 48) / 2) + 15: (50 − 48)/2 = 1 → 6 + 1 = 7 → 25 × 7 = 175 → 175 + 15 = 190.

b) Output of the code: var = 6; var += 2 makes 8 → prints “The value of variable is = 8”. var %= 2 makes 0 → prints “The value of variable is = 0” (on the same line, since there is no \n).

c) Syntax errors: Main() should be int main(); n is not declared; while (c < 100); has an extra semicolon (empty loop, and c never changes – infinite loop); printf(welcome) needs quotes: printf("welcome"); printf("%d" c) is missing a comma: printf("%d", c).

d) Rewrite as a while loop:

i = 0;
while (i < num) {
    printf("%d", a);
    printf("Enter a value: ");
    scanf("%d", &val);
    printf("you entered value = %d", val);
    i++;
}

e) Is float’ = 3.14 * r; valid? Invalid – no variable name is given (and ‘ cannot be part of an identifier). Correct: float area = 3.14 * r;

f) Output with p = 10, q = 3, r = −2: (p + q) = 13 < 14 is true and r < q − 3 → −2 < 0 is true, so it prints r + 3 = 1.

g) Scope and lifetime of local vs global variables: A local variable is visible only inside its block/function and exists only while the function runs. A global variable is declared outside all functions, is visible to every function after it and exists for the whole program.

h) Pointer with example: A variable that stores the address of another variable: int x = 5; int *p = &x; printf("%d", *p); prints 5.

i) Output of the loop: As printed, Int and I are compile errors (C is case-sensitive). If corrected to i = i + 2, i goes 11, 13, 15 … and never becomes < 1, so the loop does not end. (If the intended update is i = i − 2, sum = 11 + 9 + 7 + 5 + 3 + 1 = 36.)

j) Escape sequences: Character combinations starting with \ that represent special characters: \n new line, \t tab, \\ backslash, \” quote, \a bell.

k) Function overloading: Defining several functions with the same name but different parameter lists (C++ feature, not available in C), e.g. int add(int, int) and float add(float, float).

l) Declare and display a string: char name[20] = "Sargodha"; printf("%s", name);

Q2 – Largest value in an array using a function (9)

#include <stdio.h>
int largest(int a[], int n) {
    int max = a[0];
    for (int i = 1; i < n; i++)
        if (a[i] > max) max = a[i];
    return max;
}
int main() {
    int a[10];
    for (int i = 0; i < 10; i++) {
        printf("Enter value %d: ", i + 1);
        scanf("%d", &a[i]);
    }
    printf("Greatest value = %d\n", largest(a, 10));
    return 0;
}

Q3 – Pyramid pattern (9)

#include <stdio.h>
int main() {
    int rows = 5;
    for (int i = 1; i <= rows; i++) {
        for (int s = 1; s <= rows - i; s++) printf(" ");
        for (int k = 1; k <= 2 * i - 1; k++) printf("*");
        printf("\n");
    }
    return 0;
}

Output: 1, 3, 5, 7, 9 stars centred on 5 lines.

Q4 – Factorial through a function (9)

#include <stdio.h>
long factorial(int n) {
    long f = 1;
    for (int i = 2; i <= n; i++) f *= i;
    return f;
}
int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial = %ld\n", factorial(n));
    return 0;
}

Q5 – Sum of 1/1! + 2/2! + 3/3! + 4/4! (9)

#include <stdio.h>
int main() {
    double sum = 0, fact = 1;
    for (int i = 1; i <= 4; i++) {
        fact *= i;            /* i! */
        sum += i / fact;
    }
    printf("Sum = %.4f\n", sum);   /* 1 + 1 + 0.5 + 0.1667 = 2.6667 */
    return 0;
}

Q6 – Day of the week with switch (9)

#include <stdio.h>
int main() {
    int d;
    printf("Enter a number (1-7): ");
    scanf("%d", &d);
    switch (d) {
        case 1: printf("Monday"); break;
        case 2: printf("Tuesday"); break;
        case 3: printf("Wednesday"); break;
        case 4: printf("Thursday"); break;
        case 5: printf("Friday"); break;
        case 6: printf("Saturday"); break;
        case 7: printf("Sunday"); break;
        default: printf("Invalid number");
    }
    return 0;
}

Q7 – Structure Person and average age (9)

#include <stdio.h>
struct Person {
    char name[30];
    int age;
};
int main() {
    struct Person p1, p2;
    printf("Enter name and age of person 1: ");
    scanf("%s %d", p1.name, &p1.age);
    printf("Enter name and age of person 2: ");
    scanf("%s %d", p2.name, &p2.age);
    printf("Average age = %.1f\n", (p1.age + p2.age) / 2.0);
    return 0;
}
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.