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

c Program Model Solved

This document is a model question paper for the Programming in C course at APJ Abdul Kalam Technological University for the December 2024 examination. It includes various programming questions covering topics such as conditional operators, identifiers, arrays, functions, file handling, and data structures. The paper is divided into two parts, with Part A consisting of short answer questions and Part B containing longer programming tasks.

Uploaded by

Xorus Dremus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

c Program Model Solved

This document is a model question paper for the Programming in C course at APJ Abdul Kalam Technological University for the December 2024 examination. It includes various programming questions covering topics such as conditional operators, identifiers, arrays, functions, file handling, and data structures. The paper is divided into two parts, with Part A consisting of short answer questions and Part B containing longer programming tasks.

Uploaded by

Xorus Dremus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

scan to join our watsapp group click here for watsapp group link

MODEL QUESTION PAPER


APJ ABDUL KALAM TECHNOLOGICAL UNIVERSITY
SECOND SEMESTER B.TECH DEGREE EXAMINATION, DECEMBER 2024

Course Code: GXEST204


Course Name: PROGRAMMING IN C
Max. Marks: 60 Duration: 2 hours 30 minutes

PARTA
Answer all questions. Each question carries 3 marks CO Marks
1 Convert the below if-else to conditional operator 1 (3)
if (first_check)
{
"access denied";
}
else
{
if (second_check)
{
"access denied";
}
else
{
"access granted";
}
}

2 State proper reasons to identify whether the given identifiers are valid or not. 1 (3)
i) 9marks
ii) Mark 2020
iii) v4vote
iv) _csdept
v) @cse
vi) AxB
3 Explain the different ways in which you can declare and initialize a single- 2 (3)
dimensional array.

4 How can a list of strings be stored within a two-dimensional array? How can the 2 (3)
individual strings be processed? What library functions are available to simplify
string processing?
5 Suppose function F1 calls function F2 within a C program. Does the order of the 3 (3)
function definitions make any difference? Explain.

6 Identify the storage class in the below code? 5 (3)


#include<stdio.h>
int a;
int main()
{
printf("a=%d",a);
return 0;
}

7 Suppose a function receives a pointer as an argument. Explain how the function 4 (3)

3
prototype is written. In particular, explain how the data type of the pointer argument
is represented.
8 While using the statement, fp = fopen( "myfile.c", "r" ) ; what happens if, 5 (3)
− ‘myfile.c’ does not exist on the disk
− ‘myfile.c’ exists on the disk
PARTB
Answer any one full question from each module. Each question carries 9 marks
Module 1
9 a 1 (5)
Write a C Program to check if a given number is a strong number or not.
A strong number is a number in which the sum of the factorial of the digits is equal
to the number itself. Eg:- 145=1!+4!+5!=1+24+120=145
b Write a C program that calculates and prints the result of the following expression: 1 (4)
result = 5 * (6 + 2) / 4 - 3. Explain how parentheses affect the precedence of
operators in this expression.
10 a Write a C program that reads employee's ID, name, status (‘F’ for full-time and ‘P’ 1 (6)
for part-time) and basic pay. DA and HRA for full-time employees are 75% and
10% of basic pay respectively. Part-time employees have basic pay only. Print the
total salary of the employee.

b 1 (3)
Explain the difference between primitive (basic) data types and derived data types
in C. Provide examples for each category. Why is it important to choose the
correct data type for variables in a program?
Module 2
11 a Write a C program to reverse the content of an array without using another array. 2 (4)
b Given a list of cities, their literacy level, and the population, write a program to print 2 (5)
the list cities sorted lexicographically. Assume duplication in city names.. Break the
ties with the value of literacy level, and then the population.
18

a Write a C program to rotate (re-insert rightmost element at the left) elements of an 2 (4)
12 array to the right by n-steps.
b Write a C program that allows the user to enter a line of text, store it in an array, 2 (5)
and then display it backward. The line length should be unspecified (terminated by
pressing the Enter key), but assume it will not exceed 80 characters.

Module 3
13 a Develop a function that takes the two dates as input and compares them. The 3 (4)
function should return 1 if date1 is earlier than date2, return 0 if date1 is later than
date2. Write code to display whether date1 is earlier, later, or the same as date2
based on the function’s result.
b Define a structure named Vehicle with members model (character array), year 2 (5)
(integer), and price (float). Create an array of 10 Vehicle structures and write a
function to accept user input for each vehicle's details and Sort the array in
descending order based on price.
14 a Define a structure that can describe a hotel with the following members: name, 3 (4)
address, grade, average room charge, and number of rooms. Write a function to
print out hotels of a given grade, sorted in order of their average room charges.
b Write a function to print out hotels with an average room charge less than a 3 (5)
specified value in the above problem.

3
scan to join our watsapp group
Module 4
15 a Write a c program snippet to open a file in write mode and check if the file opened 5 (4)
successfully. if it fails ,print an error message and terminate the program.
b Two persons want to access a file "sample.txt". First person want to read the data 5 (5)
from the file. The second person want to read and write the data from and to the
file simultaneously. Can you help them to do so by writing the corresponding
programming codes?
16 a Write a function calculates the roots of a quadratic equation with coefficients a, b, 4 (4)
and c (passed as pointer parameters).

b In a small firm, employee numbers are given in serial numerical order, that is 1, 2, 5 (5)
3, etc. − Create a file of employee data with following information: employee
number, name, sex, gross salary. − If more employees join, append their data to the
file. − If an employee with serial number 25 (say) leaves, delete the record by
making gross salary 0. − If some employee’s gross salary increases, retrieve the
record and update the salary. Write a program to implement the above operations.
*****

scan to join our watsapp group

3
Explanation of All Answers for
the C Programming Question
Paper
PART A scan to join our watsapp group

Question 1: Convert if-else to conditional operator


Answer:

first_check ? "access denied" : (second_check ? "access denied" : "access granted");

Explanation: The nested if-else is converted to a ternary conditional operator. The first condition
checks first_check, and if true, returns "access denied". If false, it checks second_check in
another nested ternary operation.

Question 2: Valid identifiers


Answer:

 i) 9marks - Invalid (starts with a digit)

 ii) Mark 2020 - Invalid (contains space)

 iii) v4vote - Valid

 iv) _csdept - Valid

 v) @cse - Invalid (contains special character @)

 vi) AxB - Valid

Explanation: C identifiers must start with a letter or underscore, can contain letters, digits, and
underscores, and cannot contain spaces or special characters (except underscore).

Question 3: Single-dimensional array declaration and initialization


Answer:
1. Declaration only: int arr[5];

2. Declaration with initialization: int arr[5] = {1, 2, 3, 4, 5};

3. Implicit size initialization: int arr[] = {1, 2, 3};

4. Partial initialization: int arr[5] = {1, 2}; (remaining elements are 0)

5. Designated initialization (C99): int arr[5] = {[2] = 5, [4] = 9};

Explanation: Arrays can be declared with or without initialization, with explicit or implicit size,
and can be partially initialized.

Question 4: Storing strings in 2D array


Answer:

 Stored as: char strings[][20] = {"hello", "world", "c programming"};

 Processed using loops: for(i=0; i<3; i++) printf("%s\n", strings[i]);

 Library functions: strlen(), strcpy(), strcmp(), strcat(), etc.

Explanation: Each string occupies one row in the 2D array. Columns must accommodate the
longest string + null terminator. Standard string functions simplify processing.

Question 5: Function definition order


Answer: Yes, the order matters if functions are called before they're defined without
prototypes. If F1 calls F2 and F2 is defined after F1, the compiler won't know about F2 unless
there's a prototype declaration before F1.

Explanation: C uses single-pass compilation. Function prototypes allow declaring functions


before their definitions, making order irrelevant if prototypes are used.

Question 6: Storage class


Answer: a has external (global) storage class.

Explanation: Variables declared outside all functions have static storage duration and file scope.
They're initialized to zero by default if not explicitly initialized.

Question 7: Pointer argument in function prototype


Answer: The prototype specifies the pointer's base type. Example:

void func(int *ptr); // pointer to int

scan to join our watsapp group


void func(char *ptr); // pointer to char

Explanation: The data type before the asterisk indicates what type of data the pointer points to.
This ensures type safety in pointer operations.

Question 8: fopen() behavior


Answer:

 If 'myfile.c' doesn't exist: fopen() returns NULL (opening in "r" mode fails for non-
existent files)

 If 'myfile.c' exists: fopen() returns a FILE pointer to the opened file

Explanation: "r" mode is for reading existing files. Always check the return value of fopen() to
handle errors.

PART B

Module 1
Question 9a: Strong number program
#include <stdio.h>

int factorial(int n) {

return (n == 0 || n == 1) ? 1 : n * factorial(n-1);

int isStrong(int num) {

int temp = num, sum = 0;

while(temp) {

sum += factorial(temp % 10);

temp /= 10;

}
return sum == num;

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("%d is%s a strong number\n", num, isStrong(num) ? "" : " not");

return 0;

Question 9b: Expression evaluation


#include <stdio.h>

int main() {

float result = 5 * (6 + 2) / 4 - 3;

printf("Result: %.2f\n", result);

return 0;

Explanation: Parentheses have highest precedence, so (6+2) is evaluated first (8). Then
multiplication and division (left to right): 5*8=40, 40/4=10. Finally subtraction: 10-3=7.

Question 10a: Employee salary program


#include <stdio.h>

int main() {

char id[10], name[50], status;

float basic, total;


printf("Enter ID, name, status (F/P), basic pay: ");

scanf("%s %s %c %f", id, name, &status, &basic);

if(status == 'F') {

total = basic + 0.75*basic + 0.10*basic;

} else {

total = basic;

printf("Total salary: %.2f\n", total);

return 0;

Question 10b: Data types


Primitive types: Fundamental types provided by the language (int, float, char, double, void)
Derived types: Constructed from primitive types (arrays, pointers, structures, unions)
Importance: Correct data types ensure efficient memory usage, proper range of values, and
prevent errors in calculations.

Module 2
Question 11a: Reverse array in place
void reverseArray(int arr[], int size) {

for(int i = 0; i < size/2; i++) {

int temp = arr[i];

arr[i] = arr[size-1-i];

arr[size-1-i] = temp;

}
}

Question 11b: Sort cities


#include <stdio.h>

#include <string.h>

struct City {

char name[50];

float literacy;

int population;

};

void sortCities(struct City cities[], int n) {

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

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

int cmp = strcmp(cities[j].name, cities[j+1].name);

if(cmp > 0 || (cmp == 0 && cities[j].literacy < cities[j+1].literacy) ||

(cmp == 0 && cities[j].literacy == cities[j+1].literacy &&

cities[j].population < cities[j+1].population)) {

struct City temp = cities[j];

cities[j] = cities[j+1];

cities[j+1] = temp;

}
Question 12a: Rotate array right by n steps
void rotateRight(int arr[], int size, int n) {

n = n % size;

for(int k = 0; k < n; k++) {

int temp = arr[size-1];

for(int i = size-1; i > 0; i--) {

arr[i] = arr[i-1];

arr[0] = temp;

Question 12b: Reverse input string


#include <stdio.h>

#include <string.h>

int main() {

char text[80];

printf("Enter text: ");

fgets(text, 80, stdin);

text[strcspn(text, "\n")] = '\0'; // Remove newline

int len = strlen(text);

for(int i = 0; i < len/2; i++) {

char temp = text[i];

text[i] = text[len-1-i];
text[len-1-i] = temp;

printf("Reversed: %s\n", text);

return 0;

Module 3
Question 13a: Date comparison
struct Date {

int day, month, year;

};

int compareDates(struct Date d1, struct Date d2) {

if(d1.year != d2.year) return d1.year < d2.year;

if(d1.month != d2.month) return d1.month < d2.month;

return d1.day < d2.day;

Question 13b: Vehicle structure


#include <stdio.h>

#include <string.h>

struct Vehicle {

char model[50];

int year;

float price;
};

void sortVehicles(struct Vehicle vehicles[], int n) {

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

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

if(vehicles[j].price < vehicles[j+1].price) {

struct Vehicle temp = vehicles[j];


scan to join our watsapp group
vehicles[j] = vehicles[j+1];

vehicles[j+1] = temp;

Question 14a: Hotel structure


struct Hotel {

char name[50];

char address[100];

char grade;

float avgRoomCharge;

int numRooms;

};

void printHotelsByGrade(struct Hotel hotels[], int n, char grade) {

// Sort by average room charge

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

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


if(hotels[j].avgRoomCharge > hotels[j+1].avgRoomCharge) {

struct Hotel temp = hotels[j];

hotels[j] = hotels[j+1];

hotels[j+1] = temp;

// Print matching grade

for(int i = 0; i < n; i++) {

if(hotels[i].grade == grade) {

printf("%s: %.2f\n", hotels[i].name, hotels[i].avgRoomCharge);

Question 14b: Hotels with charge less than value

void printAffordableHotels(struct Hotel hotels[], int n, float maxCharge) {

for(int i = 0; i < n; i++) {

if(hotels[i].avgRoomCharge < maxCharge) {

printf("%s: %.2f\n", hotels[i].name, hotels[i].avgRoomCharge);

Module 4
Question 15a: File open check
#include <stdio.h>

#include <stdlib.h>

int main() {

FILE *fp = fopen("file.txt", "w");

if(fp == NULL) {

perror("Error opening file");

exit(EXIT_FAILURE);

// File operations here

fclose(fp);

return 0;

Question 15b: File access modes

Copy

Download

// First person (read only)

FILE *fp1 = fopen("sample.txt", "r");

// Second person (read and write)

FILE *fp2 = fopen("sample.txt", "r+");

Question 16a: Quadratic roots


#include <stdio.h>
#include <math.h>

void findRoots(float a, float b, float c, float *root1, float *root2) {

float discriminant = b*b - 4*a*c;

if(discriminant > 0) {

*root1 = (-b + sqrt(discriminant)) / (2*a);

*root2 = (-b - sqrt(discriminant)) / (2*a);

} else if(discriminant == 0) {

*root1 = *root2 = -b / (2*a);

} else {

printf("Complex roots\n");

Question 16b: Employee file operations


#include <stdio.h>

#include <stdlib.h>

struct Employee {

int number;

char name[50];

char sex;

float grossSalary;

};

void appendEmployee(FILE *fp) {


struct Employee emp;

printf("Enter employee number, name, sex, gross salary: ");

scanf("%d %s %c %f", &emp.number, emp.name, &emp.sex, &emp.grossSalary);

fseek(fp, 0, SEEK_END);

fwrite(&emp, sizeof(struct Employee), 1, fp);

void deleteEmployee(FILE *fp, int empNo) {

struct Employee emp;

fseek(fp, 0, SEEK_SET);

while(fread(&emp, sizeof(struct Employee), 1, fp)) {

if(emp.number == empNo) {

emp.grossSalary = 0;

fseek(fp, -sizeof(struct Employee), SEEK_CUR);

fwrite(&emp, sizeof(struct Employee), 1, fp);

break;

void updateSalary(FILE *fp, int empNo, float newSalary) {

struct Employee emp;

fseek(fp, 0, SEEK_SET);

while(fread(&emp, sizeof(struct Employee), 1, fp)) {

if(emp.number == empNo) {

emp.grossSalary = newSalary;
fseek(fp, -sizeof(struct Employee), SEEK_CUR);

fwrite(&emp, sizeof(struct Employee), 1, fp);

break;

scan to join our watsapp group

You might also like