Blog
Programming Fundamentals BSIt 1st Term 2016 Past Paper UOS
Transcript of the Paper
University of Sargodha
BS 1st Term Examination 2016
Subject: I.T
Paper: Programming Fundamentals (CMP-2122)
Time Allowed: 2:30 Hours
Maximum Marks: 60
Objective Part Compulsory (12*2=24)
Note: Attempt all questions. All questions carry equal marks.
- Why break statement is used in switch statement?
- Why it is important to close a file. How is it closed?
- What is the scope of Local Variable?
- What is meant by Case Sensitivity in C language?
- What is built-in function? Give example.
- What is Logical Error and how it can be detected?
- What is the purpose of goto statement and why it is not preferable?
- Differentiate between function prototype and function definition?
- Which function is used to find the length of a string?
- Define recursive function?
- What is Dangling Memory?
- What is the difference between pre-increment and post-increment?
Subjective Part (4*9=36)
Note: Attempt any four questions. All questions carry equal marks.
Q2. Write down a program that reads 10 elements in an array from user. Pass the array to a method that finds and print the largest number of given numbers.
Q3. Write down a program that accepts an integer number from user. Calculate and display its factorial by using a recursive function.
Q4. Write down a C program that prints the following pattern?
Q5.
a) Write down a routine that reads contents of a file and print these contents on screen.
b) Write down a method that swaps two values passed by reference.
Q6. Write a program that declares a structure Book to store bookno(int), name(string) and authorname(string). The program defines an array to store record of five students. It inputs five students and then displays the record of each student.
Q7. Write a function Is Prime that has an input parameter num and returns a value of 1 if it is prime. Otherwise it returns the value of 0.
Solved answers
All programs are in C.
Q1 – Short questions
(i) Why break is used in switch: to exit the switch after the matching case runs. Without break, execution “falls through” into the following cases.
(ii) Why close a file and how: closing flushes buffered data to the disk, releases the file handle and system resources, and prevents data loss or corruption. It is closed with fclose(fp);.
(iii) Scope of a local variable: a local variable is declared inside a function or block and can be used only within that block; it is created when the block starts and destroyed when it ends.
(iv) Case sensitivity in C: C treats uppercase and lowercase letters as different, so sum, Sum and SUM are three different identifiers, and keywords must be written in lowercase (int, not Int).
(v) Built-in function: a predefined library function provided with the compiler, used by including its header file, e.g. printf(), scanf(), sqrt(), strlen().
(vi) Logical error: an error in the program’s logic that makes it produce wrong output although it compiles and runs (e.g. using + instead of *). It is detected by testing with known inputs and comparing results, tracing/dry-running and using a debugger.
(vii) goto statement: transfers control unconditionally to a labelled statement. It is not preferred because it makes programs hard to read, understand, debug and maintain (“spaghetti code”); structured loops and break/continue are better.
(viii) Function prototype vs definition: a prototype (declaration) tells the compiler the function’s name, return type and parameter types, ending with a semicolon: int add(int, int);. A definition contains the function header and the body with the actual code.
(ix) Function to find string length: strlen() from <string.h>; it returns the number of characters before the null character.
(x) Recursive function: a function that calls itself, with a base case to stop the recursion, e.g. factorial: fact(n) = n * fact(n - 1), fact(0) = 1.
(xi) Dangling memory (dangling pointer): a pointer that still points to a memory location that has been freed or has gone out of scope. Using it causes undefined behaviour; set pointers to NULL after free().
(xii) Pre-increment vs post-increment: ++x increments x first and then uses the new value; x++ uses the current value first and then increments. If x = 5: y = ++x; gives y = 6; y = x++; gives y = 5 (x becomes 6 in both).
Q2 – Largest of 10 numbers using a function
#include <stdio.h>
void printLargest(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
printf("Largest number = %d\n", max);
}
int main() {
int a[10];
printf("Enter 10 numbers:\n");
for (int i = 0; i < 10; i++)
scanf("%d", &a[i]);
printLargest(a, 10);
return 0;
}Q3 – Factorial using a recursive function
#include <stdio.h>
long long factorial(int n) {
if (n <= 1) /* base case */
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0)
printf("Factorial of a negative number is not defined.\n");
else
printf("Factorial of %d = %lld\n", num, factorial(num));
return 0;
}Q4 – Printing a pattern
The pattern was printed as a figure in the paper. The common star pyramid asked in this course is shown below; the same nested-loop method works for other patterns (outer loop = rows, inner loops = spaces and stars).
*
***
*****
*******
*********
#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 j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}Q5 (a) – Read a file and print its contents
#include <stdio.h>
void printFile(const char *fileName) {
FILE *fp = fopen(fileName, "r");
if (fp == NULL) {
printf("Cannot open file %s\n", fileName);
return;
}
int ch;
while ((ch = fgetc(fp)) != EOF)
putchar(ch);
fclose(fp);
}
int main() {
printFile("data.txt");
return 0;
}Q5 (b) – Swap two values passed by reference
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 10, y = 20;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y); /* x = 20, y = 10 */
return 0;
}Q6 – Structure Book with an array of five records
#include <stdio.h>
struct Book {
int bookno;
char name[50];
char authorname[50];
};
int main() {
struct Book b[5];
for (int i = 0; i < 5; i++) {
printf("\nBook %d\n", i + 1);
printf("Enter book number: ");
scanf("%d", &b[i].bookno);
printf("Enter book name: ");
scanf(" %49[^\n]", b[i].name);
printf("Enter author name: ");
scanf(" %49[^\n]", b[i].authorname);
}
printf("\nBook records:\n");
for (int i = 0; i < 5; i++)
printf("%d %s by %s\n", b[i].bookno, b[i].name, b[i].authorname);
return 0;
}Q7 – Function IsPrime
#include <stdio.h>
int IsPrime(int num) {
if (num < 2)
return 0;
for (int i = 2; i * i <= num; i++)
if (num % i == 0)
return 0;
return 1;
}
int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (IsPrime(n))
printf("%d is prime\n", n);
else
printf("%d is not prime\n", n);
return 0;
}Answers prepared by Shining Study for revision. Check them against your class notes and teacher’s guidance.
