Programming Fundamentals, Past Papers

Programming Fundamentals BSCS 1st Term 2016 Past Paper UOS

A clear scanned exam paper from University of Sargodha dated 2016 for BS 1st Term Computer Science students.

University of Sargodha
BS 1st Term Examination 2016

Subject: Computer Science
Paper: Programming Fundamental (CMP-2122)

Time Allowed: 2:30 Hours
Maximum Marks: 60

Objective Part (Compulsory)

Q.1. Write short answers of the following questions in 2-3 lines only (12*2=24):

i. Evaluate the expression 50 / (6 + (8 – 4) * 2) + 15
ii. Write the name of two escape sequences and their functions.
iii. Give one example of implicit and explicit type casting?
iv. Is the following statement valid or invalid? Give reason whatever the case may be.

v. What does the following code display?

vi. What is scope and life time of a static variable?
vii. Differentiate between while and do while loop?
viii. If ptr is a pointer to int, and i and j are variables of int, what is wrong in the statement?

ix. What is the output?

x. Is statement valid? If it is wrong, write the correct statement.
xi. Declare an int variable and pointer to int in a single statement.
xii. Write a prototype of a function that passes an int value, an int reference and returns a double value.

Subjective Part (4 * 9 = 36)

Q # 2: Write a program that accepts 10 integer values from user in an array and passes array to a function. The function finds the greatest value of the array and returns it to main() that displays the value.

Q # 3: Write a program declares a structure Student with data member rollno and age. Create two variables of structure, accept values and find the average age of these two students and display the average age.

Q # 4: Write a program that displays the sum of first four terms of the series.

Q # 5: Write a program to display the following pattern?

Q # 6: Write a program that accepts a number from user and passes it to a function that determines whether the number is prime or composite. The result is displayed by main function.

Q # 7: Write a program that inputs obtained marks from a student, calculates the percentage (assuming total marks are 1100) and displays the grade. The grade should be calculated according to the following criterion.

Solved answers

Q1 – Short questions

(i) 50 / (6 + (8 − 4) * 2) + 15 = 50 / (6 + 8) + 15 = 50 / 14 + 15 = 3 + 15 = 18 (integer division; with floating-point values it would be 18.57).

(ii) \n – new line (moves the cursor to the next line); \t – horizontal tab. (Others: \a alert/beep, \\ backslash, \" double quote.)

(iii) Implicit (automatic) casting: int a = 5; double d = a; (int converted to double by the compiler). Explicit casting: double x = 7.8; int n = (int)x; (n becomes 7).

(iv) int 2abc = 6; is invalid – an identifier cannot start with a digit; it must begin with a letter or underscore (e.g. int abc2 = 6;).

(v) p + q = 13 < 14 is true, and r < q − 3 means −2 < 0, also true, so the if-part runs and prints r + 3 = 1.

(vi) A static local variable has local (block) scope – it is visible only inside the function/block where it is declared – but its lifetime is the whole program: it is created once, initialised once, and keeps its value between function calls. (A static global variable has file scope.)

(vii) while is an entry-controlled loop: the condition is checked first, so the body may run zero times. do…while is exit-controlled: the body runs first and the condition is checked afterwards, so it always runs at least once; it ends with a semicolon.

(viii) ptr = &(i + j); is wrong because the address operator & can only be applied to an lvalue (a variable with a memory location); i + j is a temporary value with no address. Correct: int k = i + j; ptr = &k;

(ix) As printed, the loop uses capital I, which is undeclared (C/C++ are case-sensitive), so it is a compile-time error. Even with lowercase i, the loop for(i = 10; i >= 2; i = i + 2) increases i, so the condition is always true and it becomes an infinite loop (until overflow) – nothing useful is printed. (With i = i − 2 the sum would be 10 + 8 + 6 + 4 + 2 = 30.)

(x) Invalid – an array cannot be assigned with an initializer list after declaration. Correct: int arr[5] = {1, 2, 3, 4, 5};

(xi) int a, *p; (optionally int a, *p = &a;).

(xii) double compute(int value, int &ref);

Q2 – Greatest value using a function

#include <iostream>
using namespace std;

int findMax(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 arr[10];
    cout << "Enter 10 integers: ";
    for (int i = 0; i < 10; i++) cin >> arr[i];
    cout << "Greatest value = " << findMax(arr, 10) << endl;
    return 0;
}

Q3 – Structure Student and average age

#include <iostream>
using namespace std;

struct Student {
    int rollno;
    int age;
};

int main() {
    Student s1, s2;
    cout << "Enter roll no and age of student 1: ";
    cin >> s1.rollno >> s1.age;
    cout << "Enter roll no and age of student 2: ";
    cin >> s2.rollno >> s2.age;
    float avg = (s1.age + s2.age) / 2.0;
    cout << "Average age = " << avg << endl;
    return 0;
}

Q4 – Sum of 1/2! + 2/3! + 3/4! + 4/5!

#include <iostream>
using namespace std;

int main() {
    double sum = 0, fact = 1;          // fact will hold (n+1)!
    for (int n = 1; n <= 4; n++) {
        fact = fact * (n + 1);         // 2!, 3!, 4!, 5!
        sum = sum + n / fact;
    }
    cout << "Sum = " << sum << endl;   // 0.991667
    return 0;
}

Check: 0.5 + 0.3333 + 0.125 + 0.0333 = 0.991667.

Q5 – Pattern

#include <iostream>
using namespace std;

int main() {
    for (int row = 1; row <= 5; row++) {
        for (int k = 5; k >= row; k--)
            cout << k << " ";
        cout << endl;
    }
    return 0;
}
/* Output:
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5          */

Q6 – Prime or composite using a function

#include <iostream>
using namespace std;

bool isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0) return false;
    return true;
}

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    if (num < 2)
        cout << num << " is neither prime nor composite" << endl;
    else if (isPrime(num))
        cout << num << " is a prime number" << endl;
    else
        cout << num << " is a composite number" << endl;
    return 0;
}

Q7 – Percentage and grade (total 1100)

#include <iostream>
#include <string>
using namespace std;

int main() {
    float obtained;
    cout << "Enter obtained marks (out of 1100): ";
    cin >> obtained;
    float per = obtained / 1100 * 100;
    string grade;
    if (per >= 80)      grade = "A+";
    else if (per >= 70) grade = "A";
    else if (per >= 60) grade = "B";
    else if (per >= 50) grade = "C";
    else if (per >= 40) grade = "D";
    else if (per >= 33) grade = "E";
    else                grade = "F";
    cout << "Percentage = " << per << "%  Grade = " << grade << endl;
    return 0;
}

All programs were compiled and tested (e.g. 880 marks → 80% A+; 350 marks → 31.8% F; 91 → composite; 97 → prime).

Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.

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.