CS3353 QB - Question Bank
CS3353 QB - Question Bank
CS3353
C PROGRAMMING
&
DATASTRUCTURES
Question bank
UNIT I
C PROGRAMMING FUNDAMENTALS
PART A
1. Define programming paradigm.
Programming paradigm is a style or way of programming. The are procedural, functional,
object-oriented, logic programming.
Some of the programming languages are
Fortran
Cobol
Basic
Pascal
C
OUTPUT :
Value of (a + b) * c / d is : 90
N Switch( ) Nested if
o
.
i. The switch( ) can test only The if can evaluate relational or
constant values. logical expressions.
ii In switch( ) case nested if can In nested if statements, switch( )
. be used. case can be used
Bitwise operators
Special operators (sizeof, & and * , . and -->)
14. Recommend the suitable example for infinite loop using while.
#include<stdio.h>void main()
int i = 1;
while( i<10 )
printf(“%d\n”,i);
getch( );
Here we are not updating the value of i. so after each iteration value of i remains same. As
a result, the condition (i<10) will always true so itwill print infinity loop.
values.
Arrays can be initialized in following two ways :
i. At compile time
ii. At Run time
17..Define string.
String is a sequence / array of characters enclosed with double quotes.
Null character (‘\0’) is used to mark the end of the string
C O M P U T E R \0
Declaration:
int m1[10][10];
static int m2[2][2] = { {0,1}, {2,3} };
OUTPUT
Enter a string: Programming in C Length of string:16
more memory and are generally slow. Instead, you can use loop.
Example:
void recursion()
{
recursion(); /* function calls itself */
}
int main()
{
recursion();
PART B
10
UNIT II
C PROGRAMMING - ADVANCED FEATURES
PART A
1. Define Structure in C
C Structure is a collection of different data types which are grouped together and each
element in a C structure is called member.
If you want to access structure members in C, structure variable should bedeclared.
Many structure variables can be declared for same structure and memory will be allocated
for each separately.It is a best practice to initialize a structure to null while declaring, if we
don’tassign any values to structure members.
11
} student;
This defines a new type student variables of type student can be declared asfollows.
student st_rec;
6. Define typedef .
a. The typedef keyword enables the programmer to create a new data type name by using an
existing data type.
b. By using typedef, no new data is created, rather an alternate name is given to a known data
type.
2002
e. The pointer holds the address 2000. This value is added with 1.
f. The data type size of the constant is added with t
10. State the advantages of user defined functions over pre-defined function.
a. A user defined function allows the programmer to define the exact function of the module as
per requirement. This may not be the case with predefined function. It may or may not serve
the desired purpose completely.
b. A user defined function gives flexibility to the programmer to use optimal programming
instructions, which is not possible in predefined function.
Struct S
{
char datatype1;int datatype2; float datatype3;
};
Struct S *sptr //sptr ia pointer to structure S
Arrays Structures
An array cannot have bit fields. A structure may contain bit fields.
14
Structure Union
Every member has its own memory. All members use the same memory.
15. What are the functions supports for dynamic memory allocation?
a. The functions supports for dynamic memory allocation are,
i. malloc()
ii. realloc()
iii. calloc()
iv. free()
A self referential data structure is essentially a structure definition which includes at least one
member that is a pointer to the structure of its own kind. A chain of such structures can thus be
expressed as follows.
struct name {member 1;
member 2;
...
struct name *pointer;
};
The above illustrated structure prototype describes one node that comprises of two logical
segments. One of them stores data/information and the other one is a pointer indicating where
the next component can be found. .Several such inter-connected nodes create a chain of
structures.
19. Why files are needed?
When a program is terminated, the entire data is lost. Storing in a file will preserve your data
even if the program terminates.
17
r+ Open for both reading and If the file does not exist, fopen()
writing. returns NULL.
Open for both reading and If the file does not exist, fopen()
rb+ writing in returns NULL.
binary mode.
If the file exists, its contents are
w+ Open for both reading and overwritten. If
writing. the file does not exist, it will be
created.
Open for both reading and If the file exists, its contents are
wb+ writing in overwritten. If
binary mode. the file does not exist, it will be
created.
a+ Open for both reading and If the file does not exists, it will be
appending. created.
Open for both reading and If the file does not exists, it will be
ab+ appending created.
in binary mode.
18
#include <stdio.h>
int main( int argc, char *argv[] )
{
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 )
{
printf("Too many arguments supplied.\n");
}
else
{
printf("One argument expected.\n");
20
21
which are skipped backward (if negative) or forward( if positive) from the current
position.This is attached with L because this is a long integer.
Pointer position:
This sets the pointer position in the file.
0 Beginning 0
1 Current position
2 End of file
22
Syntax: feof(fptr);
23
PART B
1. What is a structure? Create a structure with data members of various types and declare two
structure variables. Write a program to read data into theseand print the same. Justify the need
for structured data type.
2. Write a C program to store the employee information using structure and search a particular
employee using Employee number.
3. Define and declare a nested structure to store date, which including day,month and year.
4. Define a structure called student tha
5.
6. t would contain name, regno and marks of five subjects and percentage. Write a C program to
read the details of name, regno and marks of five subjects for 30 students and calculate the
percentage and display the name, regno, marks of 30 subjects and percentageof each student.
7. Explain about array of structures and pointers in structures with exampleprogram
8. Write a C program to create mark sheet for students using self referentialstructure.
9. Discuss about dynamic memory allocation with suitable example C program.
24
UNIT III
LINEAR DATASTRUCTURES
PART A
25
26
27
28
program
that uses the ADT
lists from singly linked lists, the last node’s next field is connected to the first
node.
15
When doubly linked list can be represented as circular linked list?
In a doubly linked list, all nodes are connected with forward and backward
links to the
next and previous nodes respectively. In order to implement circular linked lists
from
doubly linked lists, the first node’s previous field is connected to the last node and
the
last node’s next field is connected to the first node.
29
30
struct polynomial
{
int coefficient;int exponent;struct polynomial *next;
};
31
32
33
34
35
33 Define Stack.
A stack is an ordered list in which all insertions and deletions are
made at one end, called the top. It is an abstract data type and based
on the principle of
LIFO (Last In First Out).
Push(Element X, Stack S)
{ if(IsFull(S)
S→Array[++S→TopOfStack]=X;
36
37
40 Define queue.
It is a linear data structure that maintains a list of elements such
that insertion happens at
rear end and deletion happens at front end.
FIFO – First In First Out principle
38
39
46
How the queue is implemented by linked list?
• It is based on the dynamic memory management techniques which allow
allocation and
De-allocation of memory space at runtime.
Insert operation
It involves the following subtasks:
1. Reserving memory space of the size of a queue element in memory
2. Storing the added value at the new location
3. Linking the new element with existing queue
4. Updating the rear pointer
Delete operation
It involves the following subtasks:
1. Checking whether queue is empty
2. Retrieving the front most element of the queue
3. Updating the front pointer
4. Returning the retrieved value
49
Define circular queue
A Circular queue is a queue whose start and end locations are logically
connected with
each other. That means the start location comes after the end location.
50
What are push and pop operations?
• Push – adding an element to the top of stack
• Pop – removing or deleting an element from the top of stack
41
51
What are enqueue and dequeue operations?
• Enqueue - adding an element to the queue at the rear end
If the queue is not full, this function adds an element to the back of the
queue, else it prints “OverFlow”.
void enqueue(int queue[], int element, int& rear, int arraySize) { if(rear ==
arraySize) // Queue is full
printf(“OverFlow\n”); else{
queue[rear] = element; // Add the element to the back rear++;
}
}
• Dequeue – removing or deleting an element from the queue at the front
end
If the queue is not empty, this function removes the element from the
front of the queue, else it prints “UnderFlow”. void dequeue(int queue[], int&
front, int rear) {
if(front == rear) // Queue is empty
printf(“UnderFlow\n”);
else {
queue[front] = 0; // Delete the front element front++;
}
}
42
52
STACK QUEUE
Insertion and deletion are made at Insertion at one end rear and
one end. deletion at other end front.
The element inserted last would be The element inserted first would be
removed first. So LIFO structure. removed first. So FIFO structure.
57
44
PART B
7.Explain the steps involved in insertion and deletion into a singly and doubly
linked list.
45
UNIT IV
NON-LINEAR DATASTRUCTURES
S.
No.
2 Define tree?
A tree is a data structure, which represents hierarchical relationship between
individual data items.
3 Define leaf?
In a directed tree any node which has out degree o is called a terminal
node or a leaf.
5 List out the steps involved in deleting a node from a binarysearch tree.
1. t has no right hand child node t->r == z
2. t has a right hand child but its right hand child node has no left sub tree
t->r->l == z
3.t has a right hand child node and the right hand child node has a left hand child
node t->r->l != z
46
7 What are the steps to convert a general tree into binary tree?
* use the root of the general tree as the root of the binary tree
* determine the first child of the root. This is the leftmost node in the general tree at
the next
level
* insert this node. The child reference of the parent node refers to this node
* continue finding the first child of each parent node and insert it below the parent
node with the
child reference of the parent to this node.
* when no more first children exist in the path just used, move back to the parent of
the last node
entered and repeat the above process. In other words, determine the
first sibling of the last
node entered.
* complete the tree for all nodes. In order to locate where the node fits you must
search for the
first child at that level and then follow the sibling references to a nil where the next
sibling can
be inserted. The children of any sibling node can be inserted by locating the parent
and then
inserting the first child. Then the above process is repeated.
47
48
21 Define binary tree and give the binary tree node structure.
49
24 Define hashing.
Hash function takes an identifier and computes the address of that identifier in the hash
table using some function
50
51
37 What is Rehashing?
If the table is close to full, the search time grows and may become equal to the table
size.
When the load factor exceeds a certain value (e.g. greater than 0.5) we do
Rehashing: Build a second table twice as large as the original and rehash there all the
keys of the original table.
Rehashing is expensive operation, with running time O(N) However, once done, the
new hash table will have good
performance.
52
PART B
4 Explain hashing
53
UNIT V
SORTING AND SEARCHING TECHNIQUES
1 Define sorting
Sorting arranges the numerical and alphabetical data present in a list in a
specific order or sequence. There are a number of sorting techniques
available. The algorithms can be chosen based on the following factors
● Size of the data structure
● Algorithm efficiency
Programmer’s knowledge of the technique
54
a. Pass 1: A[2] is compared with A[1] and placed them in sorted order.
b. Pass 2: A[3] is compared with both A[1] and A[2] and inserted at an appropriate
place. This makes A[1], A[2],A[3] as a sorted sub array.
c. Pass n-1: A[n] is compared with each element in the sub array
A [1], A [2] …A [n-1] and inserted at an appropriate position.
5 what is insertion sort? How many passes are required for theelements to be sorted ?
one of the simplest sorting algorithms is the insertion sort. Insertionsort consist of N-1 passes .
For pass P=1 through N-1 , insertion sort ensures that the elements in positions 0 through P-1
are in sorted order .It makes use of the fact that elements in position 0
through P-1 are already known to be in sorted order .
7 Differentiate between merge sort and quick sort? Merge sort quick
sort
1. Divide and conquer strategy Divide and conquer strategy
2. Partition by position Partition by value
8 Mention some methods for choosing the pivot element in quick sort?
1. Choosing first element
2. Generate random number
3. Median of three
55
9 What are the three cases that arise during the left to right scanin quick sort?
1. I and j cross each other
2. I and j do not cross each other
3. I and j points the same position
11 What is sorting?
Sorting is the process of arranging the given items in a logical order. Sorting is an example
where the analysis can be preciselyperformed.
56
17 Define searching
Searching refers to determining whether an element is present in a given list of
elements
or not. If the element is present, the search is considered as successful, otherwise it is
considered as an unsuccessful search. The choice of a searching technique is based on the
followingfactors
a. Order of elements in the list i.e., random or sorted
b. Size of the list
57
PART B
1
Explain the sorting algorithms
4 Write a C program to perform searching operations using linear and binary search.
58
59