Past Paper 2022 Pf
Past Paper 2022 Pf
Question no.1:
a. We know that the program gcc is both a compiler and a linker. For a source code file
named myfile.c, write the statement that only compiles the source file
c. Assume float a = 1; and float b; (ignore everything else); what are the values of a and
b after b=a++; is executed?
int main(void)
char chr;
scanf("%c", &chr);
return 0; }
f. In a one dimensional array char arr[]= ('a', 'b', 'c', 'd', 'e'), how do you access the 5th
element using a pointer *k?
Solution:
a. To compile the source file myfile.c using gcc, you would use the following command:
gcc -c myfile.c
The `-c` flag tells gcc to compile the source file without linking it, producing an object file
(myfile.o) but not an executable.
b. The statement "C language is portable" means that C programs can be written in such a
way that they can be executed on different computer systems or platforms without
modification. A portable C program should behave consistently and correctly across various
hardware and operating system environments.
c. After the execution of `b = a++;`, the value of `a` will be 2, and the value of `b` will be 1.
The post-increment operator (`++`) increments the value of `a` but returns its original value
before the increment. So, `b` gets the original value of `a` before the increment.
d. The given code will read a character from the standard input and print it twice: once as a
character and once as an integer. Here's an example of how the code execution would look
like:
Input: X
This prints X
This prints 88
The first `printf` statement prints the character 'X', and the second `printf` statement prints its
value, which is 88.
e. An array is a data structure in C (and many other programming languages) used to store a
collection of elements of the same type. Two characteristics of arrays are:
2. **Fixed Size**: Arrays have a fixed size, determined at the time of declaration. Once an
array is created, its size cannot be changed during runtime. If you need to store a different
number of elements, you must create a new array with the desired size.
f. To access the 5th element in a one-dimensional array `char arr[] = {'a', 'b', 'c', 'd', 'e'}` using
a pointer `*k`, you can use the following syntax:
char *k = &arr[4]; // The pointer k points to the 5th element (index 4) of the array.
// Now you can use *k to access the value of the 5th element:
In this example, `*k` is used to dereference the pointer and access the value of the 5th
element, which is 'e'.
Question no.2:
Write a program that asks the user to enter two integers, obtains the numbers from the
user, and then prints the larger number followed by the words "is larger." If the
numbers are equal, print the message "These numbers are equal.
Input:
#include <stdio.h>
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
} else {
return 0;
Question no.3:
What is the difference between While and a Do-While loop?
Solution:
While Loop:
The while loop first checks the loop condition (the specified expression) before executing the
loop body.
If the loop condition is true, the loop body is executed, and then the condition is checked
again.
If the condition is false from the beginning, the loop body will not execute at all.
Syntax:
while (condition) {
// Loop body
Do-While Loop:
The do-while loop first executes the loop body and then checks the loop condition.
This means that the loop body will always execute at least once, regardless of whether the
condition is true or false.
If the condition is true after the first execution, the loop will continue to execute; otherwise, it
will terminate.
Syntax:
do {
// Loop body
} while (condition);
In summary, the key difference is that the while loop may never execute its body if the
condition is initially false, while the do-while loop will always execute the body at least once
before checking the condition.
Question no.4:
Use Bubble-sort/Quicksort algorithm to write a C-program for sorting array elements,
given below, in ascending order. The program should print the array elements before
and after sorting.
x[]= {72,19,62,32,24,29,34,15,13,54,47,58)
Solution:
#include <stdio.h>
int i;
printf("\n");
}
*a = *b;
*b = temp;
int i,j;
int main() {
int x[] = {72, 19, 62, 32, 24, 29, 34, 15, 13, 54, 47, 58};
printArray(x, size);
bubbleSort(x, size);
printArray(x, size);
return 0;
}
Question no.5:
What does the following C program display when executed? Show all work (i.e., show
the contents of the variables for each iteration of the for loop) in order to be eligible for
full credit for this problem. Hint: In ASCII A = 65, B=66, C=67, D = 68, E = 69, F = 70,
G = 71, H=72, 173, J = 74, K = 75, L= 76, M = 77, N = 78, O= 79 and so on and so forth
#include <stdio.h>
int main(){
for (num1 = 65, num2 = 90; num1 <= num2; num1 += 2, num2 -=2) printf("%c", (num1
+2));
return 0;
Solution:
Input:
#include <stdio.h>
int main() {
for (num1 = 65, num2 = 90; num1 <= num2; num1 += 2, num2 -= 2)
return 0;
Explanation:
The program initializes num1 to 65 and num2 to 90. It then enters the for loop with the
condition num1 <= num2, and it increments num1 by 2 and decrements num2 by 2 in each
iteration. Inside the loop, it prints the character corresponding to the value of (num1 + 2)
using %c format specifier in the printf function.
num1 = 65, num2 = 90: (num1 + 2) = 65 + 2 = 67, so it prints the character with ASCII value
67, which is 'C'.
num1 = 67, num2 = 88: (num1 + 2) = 67 + 2 = 69, so it prints the character with ASCII value
69, which is 'E'.
num1 = 69, num2 = 86: (num1 + 2) = 69 + 2 = 71, so it prints the character with ASCII value
71, which is 'G'.
num1 = 71, num2 = 84: (num1 + 2) = 71 + 2 = 73, so it prints the character with ASCII value
73, which is 'I'.
num1 = 73, num2 = 82: (num1 + 2) = 73 + 2 = 75, so it prints the character with ASCII value
75, which is 'K'.
num1 = 75, num2 = 80: (num1 + 2) = 75 + 2 = 77, so it prints the character with ASCII value
77, which is 'M'.
Question no.6:
Define a structure named EmployeeData to store employee's data ii. Declare and
initialize two variables of the EmployeeData type e.g., Mr. Abdul Majid is the CEO and
Mr. Mujahid is a sweeper etc.
Solution:
#include <stdio.h>
#include <string.h>
struct EmployeeData {
char name[50];
char designation[50];
int age;
double salary;
};
int main() {
strcpy(employee1.designation, "CEO");
employee1.age = 40;
employee1.salary = 100000.00;
strcpy(employee2.designation, "Sweeper");
employee2.age = 30;
employee2.salary = 15000.00;
printf("Employee 1:\n");
printf("\nEmployee 2:\n");
return 0;
Question no.7:
Write an algorithm to insert a node at:
Solution:
1) Insert a Node at the Starting Point of the Linked List:
Create a new node with the data to be inserted.
Make the next of the new node point to the current head of the linked list.
Update the head of the linked list to point to the new node.
Algorithm:
insertAtStart(data):
Algorithm:
insertAtLast(data):
if head is NULL:
else:
Explanation:
In both algorithms, createNode(data) is a function that creates a new node with the given data
and returns a pointer to the new node. Additionally, head represents the pointer to the first
node of the linked list. If head is NULL, it means the linked list is empty.