0% found this document useful (0 votes)
3 views

Past Paper 2022 Pf

The document contains a series of programming questions and solutions related to the C programming language, covering topics such as compiling code, data structures, loops, sorting algorithms, and linked lists. Each question includes a specific task or concept, followed by a detailed explanation or code implementation. The document serves as a study guide for understanding fundamental programming concepts in C.

Uploaded by

shafi76rb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Past Paper 2022 Pf

The document contains a series of programming questions and solutions related to the C programming language, covering topics such as compiling code, data structures, loops, sorting algorithms, and linked lists. Each question includes a specific task or concept, followed by a detailed explanation or code implementation. The document serves as a study guide for understanding fundamental programming concepts in C.

Uploaded by

shafi76rb
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Past paper 2022

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

b. It is said that C language is portable. What does this statement mean?

c. Assume float a = 1; and float b; (ignore everything else); what are the values of a and
b after b=a++; is executed?

d. What is the output of the following code:


#include <stdio.h>

int main(void)

char chr;

scanf("%c", &chr);

{ printf("This prints %c\n", chr);

printf("This prints %d\n", chr);

return 0; }

e. What is an array? Mention at least two characteristics.

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:

If you input the character 'X':

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:

1. **Contiguous Memory**: Arrays store elements in contiguous memory locations,


meaning all elements are stored sequentially one after another in memory. This property
allows for efficient and direct access to array elements using their indices.

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 arr[] = {'a', 'b', 'c', 'd', 'e'};

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:

printf("The 5th element is: %c\n", *k);

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() {

int num1, num2;

printf("Enter the first integer: ");

scanf("%d", &num1);

printf("Enter the second integer: ");

scanf("%d", &num2);

if (num1 > num2) {

printf("%d is larger.\n", num1);

} else if (num2 > num1) {

printf("%d is larger.\n", num2);

} else {

printf("These numbers are equal.\n");

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>

void printArray(int arr[], int size) {

int i;

for (i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");
}

void swap(int *a, int *b) {

int temp = *a;

*a = *b;

*b = temp;

void bubbleSort(int arr[], int size) {

int i,j;

for (i = 0; i < size - 1; i++) {

for (j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

swap(&arr[j], &arr[j + 1]);

int main() {

int x[] = {72, 19, 62, 32, 24, 29, 34, 15, 13, 54, 47, 58};

int size = sizeof(x) / sizeof(x[0]);

printf("Original array: ");

printArray(x, size);

bubbleSort(x, size);

printf("Sorted array: ");

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(){

int num1 = 0, num2 = 0;

for (num1 = 65, num2 = 90; num1 <= num2; num1 += 2, num2 -=2) printf("%c", (num1
+2));

return 0;

Solution:
Input:
#include <stdio.h>

int main() {

int num1 = 0, num2 = 0;

for (num1 = 65, num2 = 90; num1 <= num2; num1 += 2, num2 -= 2)

printf("%c", (num1 + 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.

Let's see the iteration-by-iteration output:

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'.

The loop terminates when num1 becomes greater than num2.

So, the final output of the program will be: CEGIKM.

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() {

struct EmployeeData employee1;

strcpy(employee1.name, "Mr. Abdul Majid");

strcpy(employee1.designation, "CEO");

employee1.age = 40;

employee1.salary = 100000.00;

struct EmployeeData employee2;


strcpy(employee2.name, "Mr. Mujahid");

strcpy(employee2.designation, "Sweeper");

employee2.age = 30;

employee2.salary = 15000.00;

printf("Employee 1:\n");

printf("Name: %s\n", employee1.name);

printf("Designation: %s\n", employee1.designation);

printf("Age: %d\n", employee1.age);

printf("Salary: %.2f\n", employee1.salary);

printf("\nEmployee 2:\n");

printf("Name: %s\n", employee2.name);

printf("Designation: %s\n", employee2.designation);

printf("Age: %d\n", employee2.age);

printf("Salary: %.2f\n", employee2.salary);

return 0;

Question no.7:
Write an algorithm to insert a node at:

1. Starting point of the linked list

ii. Last position of the linked list

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):

new_node <- createNode(data)

new_node->next <- head


head <- new_node

2) Insert a Node at the Last Position of the Linked List:


 Create a new node with the data to be inserted.
 Traverse the linked list to find the last node (node with NULL next pointer).
 Make the next of the last node point to the new node.

Algorithm:
insertAtLast(data):

new_node <- createNode(data)

if head is NULL:

head <- new_node

else:

current <- head

while current->next is not NULL:

current <- current->next

current->next <- new_node

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy