Blog
Programming Fundamentals BSCS 1st Term 2015 Past Paper UOS
University of Sargodha
BS 1st Term Examination 2015
Subject: CS
Paper: Programming Fundamentals (CMP-2122)
Time Allowed: 2:30 Hours
Maximum Marks: 60
Objective Part — Compulsory
Note: Attempt all questions each question carry equal marks (212=24)*
- What is preprocessor directive?
- Write three rules of declaring an identifier in C program.
- Differentiate while and do-while loop?
- What are primitive data types? Shortly discuss one of them.
- What is control structure?
- What are difference b/w pre-increment & post-increment operators?
- What is logical error and why is it the most difficult error to find?
- Difference b/w built-in and user defined function.
- What is the purpose of getch()?
- Differentiate 1-D array and 2-D array.
- What is a pointer?
- In file-processing why a programmer use fgets and fputs functions in C program.
Subjective Part (4*9=36)
Note: Attempt any four questions.
- Write a program that finds the sum of the following series using nested loop:
- Write a program that calculates four-function (+, -, , /) calculator. It asks the user to input two numbers and an operator from the prompt.
- Write a user-defined function to read the content from a text file OUT.txt, count and display the number of alphabets and digits present in it.
- Write a program that accepts an integer value from the user and passes that to a function by reference. If the number is positive, then function finds the square of the value. The updated value is displayed by main function.
- Write a program that accepts 10 elements of an integer array from the user. The array and its SIZE passes to user defined function that finds the sum and average of integer array.
- Write a program that declares a structure PERSON to store income (float), tax_rate (int) and tax_of_a_person (float). The program defines an array of structure to store the record of five persons. It inputs incomes and tax rate of five persons and then displays the tax payable (by each person).
Solved answers
All programs are in C.
Q1 – Short questions
(i) Preprocessor directive: an instruction beginning with # that is processed before compilation, e.g. #include <stdio.h> (insert a header file) and #define PI 3.14 (define a constant/macro).
(ii) Rules for identifiers: (1) may contain letters, digits and underscore only; (2) must begin with a letter or underscore, not a digit; (3) cannot be a keyword, and no spaces or special symbols are allowed (identifiers are case-sensitive).
(iii) while vs do-while: while checks the condition before the body (entry-controlled), so the body may run zero times. do-while checks the condition after the body (exit-controlled), so the body always runs at least once; it ends with a semicolon.
(iv) Primitive data types: basic built-in types — int, float, double, char (and void). Example: int stores whole numbers, usually 4 bytes, range about −2,147,483,648 to 2,147,483,647.
(v) Control structure: a statement that controls the flow of execution: sequence, selection (if, if-else, switch) and repetition (for, while, do-while).
(vi) Pre-increment vs post-increment: ++x increases x before its value is used; x++ uses the value first and then increases it. With x = 5, y = ++x gives y = 6, while y = x++ gives y = 5.
(vii) Logical error and why it is hardest to find: an error in the program’s logic that gives wrong results without any compiler or run-time message. It is hardest to find because the program runs normally; the programmer must test, trace the logic and compare outputs with expected results.
(viii) Built-in vs user-defined function: built-in (library) functions are predefined and come with the compiler (printf, sqrt); user-defined functions are written by the programmer for a specific task.
(ix) Purpose of getch(): reads a single character from the keyboard without echoing it and without waiting for Enter (from conio.h); often used to hold the output screen until a key is pressed.
(x) 1-D vs 2-D array: a 1-D array stores elements in a single row, accessed with one index: int a[5];. A 2-D array stores elements in rows and columns (a table/matrix), accessed with two indices: int m[3][4];.
(xi) Pointer: a variable that stores the memory address of another variable: int x = 5; int *p = &x; — *p gives the value 5.
(xii) fgets and fputs: they read and write whole strings (lines) instead of single characters: fgets(str, n, fp) reads up to n − 1 characters or a line from a file safely (prevents buffer overflow), and fputs(str, fp) writes a string to a file — faster and more convenient for text.
Q2 – Sum of a series using nested loops
The series was printed as a figure in the paper. A typical nested-loop series is S = 1! + 2! + 3! + … + n!, where the inner loop computes each factorial:
#include <stdio.h>
int main() {
int n;
long long sum = 0;
printf("Enter n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) { /* each term */
long long fact = 1;
for (int j = 1; j <= i; j++) /* inner loop: i! */
fact *= j;
sum += fact;
}
printf("Sum of series = %lld\n", sum); /* n = 4 gives 33 */
return 0;
}Q3 – Four-function calculator
#include <stdio.h>
int main() {
double a, b;
char op;
printf("Enter first number, operator (+ - * /) and second number: ");
scanf("%lf %c %lf", &a, &op, &b);
switch (op) {
case '+': printf("%.2f\n", a + b); break;
case '-': printf("%.2f\n", a - b); break;
case '*': printf("%.2f\n", a * b); break;
case '/':
if (b != 0) printf("%.2f\n", a / b);
else printf("Error: division by zero\n");
break;
default: printf("Invalid operator\n");
}
return 0;
}Q4 – Count alphabets and digits in OUT.txt
#include <stdio.h>
#include <ctype.h>
void countInFile(void) {
FILE *fp = fopen("OUT.txt", "r");
if (fp == NULL) {
printf("Cannot open OUT.txt\n");
return;
}
int ch, letters = 0, digits = 0;
while ((ch = fgetc(fp)) != EOF) {
if (isalpha(ch)) letters++;
else if (isdigit(ch)) digits++;
}
fclose(fp);
printf("Alphabets: %d\nDigits: %d\n", letters, digits);
}
int main() {
countInFile();
return 0;
}Q5 – Pass by reference: square a positive number
#include <stdio.h>
void squareIfPositive(int *n) {
if (*n > 0)
*n = (*n) * (*n);
}
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
squareIfPositive(&num);
printf("Updated value = %d\n", num);
return 0;
}Q6 – Sum and average of an array using a function
#include <stdio.h>
#define SIZE 10
void sumAverage(int arr[], int size) {
int sum = 0;
for (int i = 0; i < size; i++)
sum += arr[i];
printf("Sum = %d\nAverage = %.2f\n", sum, (float) sum / size);
}
int main() {
int a[SIZE];
printf("Enter %d integers:\n", SIZE);
for (int i = 0; i < SIZE; i++)
scanf("%d", &a[i]);
sumAverage(a, SIZE);
return 0;
}Q7 – Structure PERSON and tax payable
#include <stdio.h>
struct PERSON {
float income;
int tax_rate; /* percentage */
float tax_of_a_person;
};
int main() {
struct PERSON p[5];
for (int i = 0; i < 5; i++) {
printf("Person %d - enter income and tax rate (%%): ", i + 1);
scanf("%f %d", &p[i].income, &p[i].tax_rate);
p[i].tax_of_a_person = p[i].income * p[i].tax_rate / 100.0f;
}
printf("\nTax payable:\n");
for (int i = 0; i < 5; i++)
printf("Person %d: income %.2f, rate %d%%, tax %.2f\n",
i + 1, p[i].income, p[i].tax_rate, p[i].tax_of_a_person);
return 0;
}Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
