0% found this document useful (0 votes)
7 views9 pages

ICT Cp&Ds Anskey

The document is an intensive coaching test for CS3353 - C Programming and Data Structures at Dhirajlal Gandhi College of Technology. It includes questions on recursive functions, ternary operators, unions, preprocessor directives, abstract data types, queues, conditional compilation, linked lists, and file handling in C. The test consists of both multiple-choice and programming questions, covering various concepts and practical applications in C programming.

Uploaded by

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

ICT Cp&Ds Anskey

The document is an intensive coaching test for CS3353 - C Programming and Data Structures at Dhirajlal Gandhi College of Technology. It includes questions on recursive functions, ternary operators, unions, preprocessor directives, abstract data types, queues, conditional compilation, linked lists, and file handling in C. The test consists of both multiple-choice and programming questions, covering various concepts and practical applications in C programming.

Uploaded by

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

DHIRAJLAL GANDHI COLLEGE OF TECHNOLOGY, SALEM

Department of Electrical and Electronics Engineering

Year/Sem/ Sec II/III Time 3 hours

Max.Marks 100 Date 25.10.24 AN


INTENSIVE COACHING TEST
CS3353– C PROGRAMMING AND DATASTRUCTURES
PART-A (10X2=20) BL COs
What is recursive function?
A recursive function is a function that calls itself in order to solve a problem. It typically
consists of two parts: L1 1
1
Base Case – The condition that stops the recursion.
Recursive Case – The function calls itself with a modified argument, gradually moving
towards the base case.
When is the ternary operator used? 1
In C, the ternary operator (? :) is used as a shorthand for simple if-else conditions. It is L2
2
primarily used for concise conditional assignments.
condition ? expression_if_true : expression_if_false;
State the usage of the union in C. 1
A union in C is a special data type that allows multiple variables to share the same memory L1
3
location. It is similar to a struct, but with a key difference: all members of a union share the
same memory, meaning only one member can hold a value at a time.
What are preprocessor directives? 1
Preprocessor directives in C are instructions that are processed before the actual L2
4 compilation of the program. They start with a # (hash) symbol and are used to manage
compilation settings, include files, define macros, and control conditional compilation.

What are Abstract Data Types (ADT) 3


An Abstract Data Type (ADT) is a concept in programming that defines a data structure L1
5
along with operations that can be performed on it, without specifying its internal
implementation. It focuses on what the data structure does rather than how it does it.
State the applications of queue. 3
Process Scheduling: Operating systems use queues for job scheduling (e.g., Round Robin
scheduling). L2
6 CPU and Disk Scheduling: Tasks waiting for CPU execution are managed using queues.
Print Spooling: Print jobs are stored in a queue and processed sequentially.
Network Data Transmission: Packets are queued before being sent over the network.
I/O Buffers and Disk Scheduling: Data is stored in queues before being processed.
State the purpose of Conditional compilation. 3
Conditional compilation allows specific portions of code to be compiled or ignored based on
predefined conditions.
Platform-Specific Code: Writing code that runs on different operating systems or hardware
architectures. L4
7
Debugging and Testing: Enabling or disabling debugging code without modifying the main
source.
Feature Management: Compiling only selected features based on user requirements.
Performance Optimization: Removing unnecessary code sections to optimize performance.
Backward Compatibility: Supporting multiple versions of a program with minimal changes.
List the advantages of linked list over arrays. 3
Here are some key advantages of linked lists over arrays:
Dynamic Size: Unlike arrays, linked lists can grow and shrink dynamically without needing to
define a fixed size in advance.
Efficient Insertions/Deletions: Inserting or deleting elements in a linked list is faster (O(1) for L2
8
head/tail insertions) compared to arrays, which require shifting elements (O(n) time).
No Wastage of Memory: Arrays may have unused memory due to fixed allocation, while
linked lists use memory efficiently by allocating nodes as needed.
Ease of Reordering: Elements in a linked list can be easily rearranged without moving actual
data, just by updating pointers.
Differentiate between prefix and postfix increment operator. L1 1
9 The prefix (++x) and postfix (x++) increment operators both increase the value of a
variable by 1, but they differ in when the increment takes effect.
What is the role of pointers in call by reference. 2
In Call by Reference, pointers serve as a mechanism to pass the memory address of a
variable to a function, allowing the function to modify the original variable's value directly. L1
10
Instead of passing a copy of the value (as in Call by Value), you pass the address of the
variable, enabling the function to access and change the value at that address.

PART-B (3X10=30)

11 (a) (i) Distinguish between structures and unions with example.(5) L2 1


Structures and Unions are both user-defined data types in C and
C++ that allow grouping different types of data together. However,
they have key differences in how they store data.

(ii) State the need of pointers in C program? (8) OR


Pointers are a powerful feature in C programming and serve several
important purposes that improve the efficiency, flexibility, and functionality
of a program.
1. Memory Management
2. Efficient Data Manipulation
3. Accessing and Modifying Memory Locations Directly
4. Working with Arrays and Strings
5. Implementing Complex Data Structures
6. Function Pointers
7. Efficiency and Performance
8. Interfacing with Hardware (Low-level Programming)
(b) What are different types of files available. Write a C program to copy the content of one
file to another. (13)
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *source, *destination;
char ch;
// Open the source file in read mode
source = fopen("source.txt", "r");
if (source == NULL) {
printf("Error: Could not open source file\n");
return 1;
}
// Open the destination file in write mode
destination = fopen("destination.txt", "w");
if (destination == NULL) {
printf("Error: Could not open destination file\n");
fclose(source);
return 1;
}
// Copy the content of source file to destination file
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination); // Write each character to the destination file
}
printf("File copied successfully.\n");
// Close both files
fclose(source);
fclose(destination);
return 0;
}
12 (a) What is doubly linked list. Write the function to insert and remove elements from a L2 1
doubly
linked list. (13) OR

A Doubly Linked List is a type of linked list where each node contains three parts:

1. A data part to store the value.


2. A next pointer, which points to the next node in the list.
3. A prev pointer, which points to the previous node in the list.

Functions to Insert and Remove Elements in a Doubly Linked List:

1. Insert at the Beginning:


o The new node is added at the start of the list, and the prev pointer of the
original head node is updated.

2. Insert at the End:


o The new node is added at the end, and the next pointer of the current last
node is updated.

3. Remove from the Beginning:


o The node at the beginning is removed, and the next pointer of the previous
head node is updated to NULL.

4. Remove from the End:


o The node at the end is removed, and the prev pointer of the new last node is
updated.

(b) How to implement the stack ADT using two queues? Write the function for the push and
pop operations (13).
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. The two
primary operations for a stack are:

Push: Add an element to the top of the stack.


Pop: Remove and return the element from the top of the stack.
#include <stdio.h>
#include <stdlib.h>
#define MAX 5 // Define the maximum size of the stack
// Structure for stack
struct Stack {
int arr[MAX];
int top;
};
// Function to initialize the stack
void initializeStack(struct Stack* stack) {
stack->top = -1; // Stack is empty initially
}
// Function to check if the stack is full
int isFull(struct Stack* stack) {
return stack->top == MAX - 1;
}
// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
return stack->top == -1;
}
// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
if (isFull(stack)) {
printf("Stack Overflow! Cannot push %d\n", value);
} else {
stack->arr[++(stack->top)] = value; // Increment top and insert value
printf("%d pushed onto stack\n", value);
}
}
// Function to pop an element from the stack
int pop(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack Underflow! Stack is empty\n");
return -1; // Return -1 to indicate an error
} else {
int poppedValue = stack->arr[(stack->top)--]; // Return the top element and
decrement top
return poppedValue;
}
}

// Function to peek the top element of the stack


int peek(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
return -1;
} else {
return stack->arr[stack->top]; // Return the top element
}
}
// Function to print the stack
void printStack(struct Stack* stack) {
if (isEmpty(stack)) {
printf("Stack is empty\n");
} else {
printf("Stack elements: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->arr[i]);
}
printf("\n");
}
}
int main() {
struct Stack stack;
initializeStack(&stack);
// Push some elements onto the stack
push(&stack, 10);
push(&stack, 20);
push(&stack, 30);
push(&stack, 40);
push(&stack, 50);
printStack(&stack);
// Try pushing when the stack is full
push(&stack, 60);
// Pop an element from the stack
int popped = pop(&stack);
if (popped != -1) {
printf("%d popped from stack\n", popped);
}
printStack(&stack);
// Peek the top element
int top = peek(&stack);
if (top != -1) {
printf("Top element is %d\n", top);
}
// Pop remaining elements
pop(&stack);
pop(&stack);
pop(&stack);
pop(&stack);
// Try popping when the stack is empty
pop(&stack);
return 0;
}
13 a.Write C functions for finding maximum and minimum of array elements with two-
dimensional arrays. (13) OR
To find the maximum and minimum elements in a two-dimensional array in C, we need to
iterate through all the elements of the array and compare each element with the current
maximum and minimum values.
#include <stdio.h>
#include <limits.h> // For INT_MAX and INT_MIN
// Function to find the maximum element in a 2D array
int findMax(int arr[3][3], int rows, int cols) {
int max = INT_MIN; // Initialize max to the smallest possible integer value
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] > max) {
max = arr[i][j]; // Update max if a larger element is found
}
}
}
return max;
}
// Function to find the minimum element in a 2D array
int findMin(int arr[3][3], int rows, int cols) {
int min = INT_MAX; // Initialize min to the largest possible integer value
for (int i = 0; i < rows; i++) { L2 1
for (int j = 0; j < cols; j++) {
if (arr[i][j] < min) {
min = arr[i][j]; // Update min if a smaller element is found
}
}
}
return min;
}
printf("The maximum element in the array is: %d\n", max);
printf("The minimum element in the array is: %d\n", min);

return 0;
}
(a) Write and explain the algorithms of enqueue and dequeue operations of
queue.
A Queue is a linear data structure that follows the First In, First Out (FIFO) principle. In a
queue, the first element added is the first one to be removed, like a line of people at a ticket
counter.

There are two primary operations in a queue:

Enqueue: This operation adds an element to the back (rear) of the queue.
Dequeue: This operation removes an element from the front of the queue.
14 (a) Write short notes on circular linked list with few operations.(13) OR
Singly Circular Linked List (SCLL): Each node has a pointer to the next node, and the last
node points to the first node.
Doubly Circular Linked List (DCLL): Each node has two pointers: one pointing to the next
node and one pointing to the previous node. The last node points back to the first node, and

the first node points to the last node. Operations on Circular Linked List:
Insertion:
At the beginning: Insert a new node at the head, and make the last node's next point to the
new head.
At the end: Insert a new node at the end of the list and make the last node's next point to
the new node, and the new node's next point to the head.
After a specific node: Insert a node after a specified node and adjust the pointers
accordingly.
Deletion:
At the beginning: Remove the first node and make the second node the new head.
At the end: Remove the last node, and make the second-to-last node the last node.
After a specific node: Remove a node after a specified node and adjust the pointers
accordingly.
Traversal:
Traversing a circular linked list involves starting at the head node and following the next
pointers until you return to the head node.
Search:
Searching in a circular linked list is similar to a regular linked list, except we loop back to the
head node if we haven’t found the target.
Display:
Display the list by starting from the head node and continuing until we reach the head node
again.
(b) With appropriate diagram explain any one application of queue.(13)
15 (a) Explain about File Handling in c with a suitable example .(13) OR
File handling in C is a crucial aspect of programming that allows a program to store and
retrieve data from external storage devices like hard drives, SSDs, or networked storage.
This is essential for managing data persistently between program executions.
#include <stdio.h>
int main() {
FILE *file;
char ch;
// Open a file for reading
file = fopen("example.txt", "r");
if (file == NULL) {
printf("File not found!\n");
return 1;
}
// Read and display file content
printf("File content:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch); // Print characters to console
}
// Close the file
fclose(file);
// Open a file for writing
file = fopen("output.txt", "w");
if (file == NULL) {
printf("Error opening file for writing!\n");
return 1;
}
// Write content to file
fprintf(file, "This is a test.\nFile Handling in C.\n");
// Close the file
fclose(file);
printf("\nContent written to 'output.txt'.\n");
return 0;
}
(b) Explain about Decision Making and Conditional Statements.(13)
Conditional statements allow the program to make decisions and execute different
actions depending on whether a condition is true or false.
(c) The main conditional statements used in C are:
(d) if statement
(e) if-else statement
(f) else-if ladder
(g) switch statement
(h) Ternary (conditional) operator

PART-C (1X10=10)
16 (a)Explain about Expressions using operators with a suitable example .(13) OR L1,L2 1

An expression in C is a combination of variables, constants, operators, and functions that are


evaluated to produce a result. The operators used in C are classified into different categories
based on their functionality. They help perform arithmetic, relational, logical, bitwise, and
other operations.

(b) Explain about Functions and datatypes .(13)


In C programming, functions and data types are fundamental concepts that help in

organizing code and defining how data is stored and manipulated. A function in C is a block
of code that performs a specific task. Functions help to modularize code, making it reusable

and easier to maintain. Function Declaration:


The function must be declared before it can be used. The declaration consists of the return
type, function name, and parameters (if any).
return_type function_name(parameter_list); return_type: The type of value the function will
return (e.g., int, float, void).
function_name: The name of the function.
parameter_list: A list of parameters (if any) passed to the function. If no parameters are
required, it is left empty.

Course Coordinator Module Coordinator HOD/Programme Coordinator

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