Sunday, May 10, 2009
Friday, May 8, 2009
C Language model questiob papers for JNTU BTECH STUDENTS
1. (a) What are the general characteristics of C?
C is a middle level language. Its characteristics lie between those of problem oriented
language such as Fortran, Basic, Pascal and low level Languages. It is a procedural
language. It is reliable, efficient, simple and easy to understand.The approach of
programming in C is bottom-up. The modularity feature of C language, i.e. breaking the
problem into modules called the function makes it easier to organize and maintain.
1. (b) Give and explain the structure of a C program.
The basic structure of a C program is as follows:
#include
#include
.
.
.#include
int main()
{
statement 1;
statement 2;
.
.
statement n;
return value;
}
Any C program is a set of functions. One and the must among these is main(). Empty
Parenthesis after main() is necessary.
Every function contains one or more C statements or instructions which are enclosed
within a pair of braces. Each instruction should end with a;.
If the program uses library functions, i.e. functions which are defined elsewhere, the
program must include that library file using a #include preprocessor directive at the
beginning of the program. If the keyword ‘void’ is not preceded with the main(), the main()
should have a return statement at the end of program.
1. (c) Write a C program to print the Pascal’s triangle.
#include
void main()
{
int a[10][10];
int i,j,c,n;
printf("Enter how many lines do you want");
scanf("%d",&n);
a[1][1]=1;
printf("%5d",a[1][1]);
a[2][1]=1;a[2][2]=2;a[2][3]=1;
printf("%d %d %d",a[2][1],a[2][2],a[2][3]);
for(i=3;i<=n;i++)
{
a[i][1]=1;
printf("%d",a[i][1]);
j=2;c=3;
while(j<=i)
{
a[i][j]=a[i-1][c-1]+a[i-1][c-2];
printf("%5d",a[i][j]);
c=c+1;
j=j+1;
}
a[i][j]=1;
printf("%d",a[i][j]);
}
}
2. (a) In what way is an array different from an ordinary variable?
An ordinary variable is a single variable while an array is a set of variables which are
arranged in contiguous memory locations. If we want to store a number of items of similar
types we use array instead of a variable. Each ordinary variable used in any C program has
different names but in case of array each variable in the set has same name, i.e. the name of
the array. Each variable in the set or group is referred by its position in the sequence of
variables in the array.
2. (b) What conditions must be satisfied by the entire elements of a given array?
The only condition that must be satisfied by the entire elements of an array is that all the
elements of the array must be of the same data types of the declared array.
2. (c) What are subscripts? How are they written? What restrictions apply to values that
can be assigned to subscripts?
A subscript is the number which indicates the position of an individual variable within an
array. It is used to refer to individual array element. It is also called an index. The following
syntax is used to refer any variable in a array using subscript:
arrname[subscript];
where arrname is the name of the array and subscript is the index or position of that element
in the array.
In case of C, the subscript starts with 0, i.e. the first variable in the array is given a position
number 0, the second is given the position 1 and so on. In this way the position of the last
variable will always be one less than the size of the array. Hence the values of the subscript
can lie only within the range 0 to (size-1).
2. (d) What is the advantage of defining an array size in terms of a symbolic constant
rather than a fixed integer quantity?
The main advantage of defining the size of an array in terms of a symbolic constant say a
preprocessor directive like #define or using a constant variable is that if we are referring
the size of the variable at number of places in the program and at a later time if we need to
change the size of the array, we need not change the size by searching it and changing at
each place. Even if we change the size given by the symbolic constant, it will get replaced
at each place. Hence there are fewer chances of errors because of mismatching of size.
2. (e) Write a C program to find the largest element in an array.
/* Program to find the largest element in an array */
#include
#include
#define maxsize 50
int main()
{
int arr[maxsize], maxNo;
int i;
for(i=0;i {
//Accept the array elements
printf("\n Enter the next element");
scanf("%d", &arr[i]);
maxNo=arr[0];
//find the largest no
for(i=0;i {
if (arr[i]>maxNo)
maxNo=arr[i];
}
printf("\n The largest element is %d ", maxNo);
return 0;
}
3. (a) Explain the different ways of passing structure as arguments in functions.
Structure variable can be passed to the function using one of the following ways.
1. By passing individual member of the structure.
2. By passing the entire structure variable.
Following program demonstrates the way of passing individual member of the structure.
/*** Passing individual structure elements ****/
main()
{
struct book
{
char name[25];
char name[25];
int callno;
};
stuct book b1={"Lets us C","YPK",101};
display(b1.name,b1.author,b1.callno);
}
void display (char *s, char *t ,int n)
{
printf("\n%s %s %d",s,t,n);
}
3. (b) Write a C program to illustrate the method of sending an entire structure as a
parameter to a function.
#include
#include
struct book
{
char bname[20];
float price;
};
//prototype
void display(struct book);
int main()
{
struct book b1={" C Primer", 250.00};
display (b1);
return 0;
}
Void display (struct book b2)
{
Puts(b2.bname);
puts(b2.price);
}
4. (a) How to use pointer variables in expressions? Explain through examples.
Pointer variables can be used in expressions in place of ordinary variables. For this
purpose, we need to prefix the pointer variable with an asterisk. For example, consider the
following.
int i = 100;
int *p = &i;
Now we can use *p wherever we would use i. For example, suppose that we want to
multiply the value of i by 2 to compute the value of another variable j. Then, the following
two expressions are the same.
int j = i * 2;
int j = *p * 2;
Note that we have simply substituted i with *p. Similarly, we can have the following
equivalent if conditions.
if (i > 100)
printf ("Hello");
if (*p > 100)
printf ("Hello");
4. (b) Write a ‘C’ program to illustrate the use of pointers in arithmetic operations.
Pointer variables can be used in arithmetic operations in place of ordinary variables. For
this purpose, we need to prefix the pointer variable with an asterisk. For example, consider
the following:
int i = 100;
int *p = &i;
Now we can use *p wherever we would use i. For example, suppose that we want to find out
if i is an even or odd number. Then the following two arithmetic operations are equivalent.
int j = i / 2;
if (j == 0)
printf ("Even");
else
printf ("Odd");
int j = *p / 2;
if (j == 0)
printf ("Even");
else
printf ("Odd");
Another example follows.
/ Program of addition of two variables using pointer.*/
#include
#include
main()
{
int var1,var2,result;
var1=20;
var2=30;
add(&var1 ,&var2,&result);
printf("Result of addition is %d ",result);
}
void add( int *a , int *b, int *c)
{
*c= *a+*b;
}
5. Explain in detail about file handling functions in C.
If we want to store data in a file in the secondary memory, we must specify certain things about
the file, to the operating system. They include:
1. Filename.
2. Data structure.
3. Purpose.
Filename is a string of characters that make up a valid filename for the operating system. It
may contain two parts, a primary name and an optional period with the extension. Examples:
Input.data
store
PROG.C
Student c
Text.out
Data structure of a file is defined as FILE in the library of standard I/O function definitions.
Therefore, all files should be declared as type FILE before they are used. FILE is a defined
data type.
When we open a file, we must specify what we want to do with the file. For example, we may
write data to the file or read the already existing data.
Following is the general format for declaring and opening a file:
FILE *fp;
fp = fopen(“filename”, “mode”);
The first statement declares the variable fp as a “pointer to the data type FILE”. As stated
earlier. FILE is a structure that is defined in the I/O library. The second statement opens the file
named filename and assigns an identifier to the FILE type pointer fp. This pointer which contains
all the information about the file is subsequently used as a communication link between
the system and the program.
The second statement also specifies the purpose of opening this file. The mode does this job.
Mode can be one of the following:
r open the file for reading only.
w open the file for writing only.
a open the file for appending (or adding) data to it.
Note that both the filename and mode are specified as strings. They should be enclosed in
double quotation marks.
When trying to open a file, one of the following things may happen:
1. When the mode is ‘writing’ a file with the specified name is created if the file does not
exist. The contents are deleted, if the file already exists.
2. When the purpose is ‘appending’, the file is opened with the current contents safe. A file
with the specified name is created if the file does not exist.
3. If the purpose is ‘reading’, and if it exists, then the file is opened with the current contents
safe; otherwise an error occurs.
Consider the following statements:
FILE *p1, *p2;
p1 = fopen(“data”, “r”);
p2 = fopen(“results”, “w”);
The file data is opened for reading and results is opened for writing. In case, the results file
already exists, its contents are deleted and the file is opened as a new file. If data file does not
exist, an error will occur.
Many recent compilers include additional modes of operation. They include:
r+ The existing file is opened to the beginning for both reading and writing.
w+ Same as w except both for reading and writing.
a+ Same as a except both for reading and writing.
We can open and use a number of files at a time. This number however depends on the system
we use.
6. Declare two stacks of varying length in a single array. Write C program for push1,
push2, pop1, pop2 to manipulate the two stacks.
#define MAXSIZE 50
struct stack
{
int arr[50];
int top1;
int top2;
};
int pop1()
{
if (top1= = -1)
{
printf("Stack1 underflow");
return -1;
}
return (arr[top1--]);
}
int pop2()
{
if (top2= =top1)
{
printf("\n Stack2 underflow");
return -1;
}
return (arr[top2--]);
}
void push1(int num)
{
if(top1= =top2-1)
{
printf("\n Stack1 overflow");
return;
}
arr[++top1] = num;
}
void push2(int num)
{
if (top2= =MAXSIZE -1)
{
printf("Stack2 underflow");
return;
}
arr[++top2] = num;
}
7. Write a function in C for a singly linked list, which reverses the direction of links
#include
#include
struct node
{
int data;
struct node *next;
};
struct node *root;
void addnode()
{
int item;
struct node *newnode,*p;
printf("\n Enter the item");
scanf("%d", &item);
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data=item;
newnode->next=NULL;
if(root==NULL)
{
root=newnode;
return;
}
p=root;
while(p->next!=NULL)
p=p->next;
p->next=newnode;
}
void display()
{
struct node*p=root;
while(p!=NULL)
{
printf("\n %d ",p->data);
p=p->next;
}
}
void reverse()
{
struct node *p,*oldnode=NULL,*newnode;
p=root;
while(p!=NULL)
{
newnode=p;
p=p->next;
if(oldnode==NULL)
{
oldnode=newnode;
oldnode->next=NULL;
}
else
{
newnode->next=oldnode;
oldnode=newnode;
}
}
root=oldnode;
}
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
addnode();
}
printf("\nbefore reverse");
display();
printf("\n After reverse");
reverse();
display();
return 0;
}
8. Explain the algorithm of selection sort and give a suitable example.
void selectionSort(int numbers[], int array_size)
{
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
C is a middle level language. Its characteristics lie between those of problem oriented
language such as Fortran, Basic, Pascal and low level Languages. It is a procedural
language. It is reliable, efficient, simple and easy to understand.The approach of
programming in C is bottom-up. The modularity feature of C language, i.e. breaking the
problem into modules called the function makes it easier to organize and maintain.
1. (b) Give and explain the structure of a C program.
The basic structure of a C program is as follows:
#include
#include
.
.
.#include
int main()
{
statement 1;
statement 2;
.
.
statement n;
return value;
}
Any C program is a set of functions. One and the must among these is main(). Empty
Parenthesis after main() is necessary.
Every function contains one or more C statements or instructions which are enclosed
within a pair of braces. Each instruction should end with a;.
If the program uses library functions, i.e. functions which are defined elsewhere, the
program must include that library file using a #include preprocessor directive at the
beginning of the program. If the keyword ‘void’ is not preceded with the main(), the main()
should have a return statement at the end of program.
1. (c) Write a C program to print the Pascal’s triangle.
#include
void main()
{
int a[10][10];
int i,j,c,n;
printf("Enter how many lines do you want");
scanf("%d",&n);
a[1][1]=1;
printf("%5d",a[1][1]);
a[2][1]=1;a[2][2]=2;a[2][3]=1;
printf("%d %d %d",a[2][1],a[2][2],a[2][3]);
for(i=3;i<=n;i++)
{
a[i][1]=1;
printf("%d",a[i][1]);
j=2;c=3;
while(j<=i)
{
a[i][j]=a[i-1][c-1]+a[i-1][c-2];
printf("%5d",a[i][j]);
c=c+1;
j=j+1;
}
a[i][j]=1;
printf("%d",a[i][j]);
}
}
2. (a) In what way is an array different from an ordinary variable?
An ordinary variable is a single variable while an array is a set of variables which are
arranged in contiguous memory locations. If we want to store a number of items of similar
types we use array instead of a variable. Each ordinary variable used in any C program has
different names but in case of array each variable in the set has same name, i.e. the name of
the array. Each variable in the set or group is referred by its position in the sequence of
variables in the array.
2. (b) What conditions must be satisfied by the entire elements of a given array?
The only condition that must be satisfied by the entire elements of an array is that all the
elements of the array must be of the same data types of the declared array.
2. (c) What are subscripts? How are they written? What restrictions apply to values that
can be assigned to subscripts?
A subscript is the number which indicates the position of an individual variable within an
array. It is used to refer to individual array element. It is also called an index. The following
syntax is used to refer any variable in a array using subscript:
arrname[subscript];
where arrname is the name of the array and subscript is the index or position of that element
in the array.
In case of C, the subscript starts with 0, i.e. the first variable in the array is given a position
number 0, the second is given the position 1 and so on. In this way the position of the last
variable will always be one less than the size of the array. Hence the values of the subscript
can lie only within the range 0 to (size-1).
2. (d) What is the advantage of defining an array size in terms of a symbolic constant
rather than a fixed integer quantity?
The main advantage of defining the size of an array in terms of a symbolic constant say a
preprocessor directive like #define or using a constant variable is that if we are referring
the size of the variable at number of places in the program and at a later time if we need to
change the size of the array, we need not change the size by searching it and changing at
each place. Even if we change the size given by the symbolic constant, it will get replaced
at each place. Hence there are fewer chances of errors because of mismatching of size.
2. (e) Write a C program to find the largest element in an array.
/* Program to find the largest element in an array */
#include
#include
#define maxsize 50
int main()
{
int arr[maxsize], maxNo;
int i;
for(i=0;i
//Accept the array elements
printf("\n Enter the next element");
scanf("%d", &arr[i]);
maxNo=arr[0];
//find the largest no
for(i=0;i
if (arr[i]>maxNo)
maxNo=arr[i];
}
printf("\n The largest element is %d ", maxNo);
return 0;
}
3. (a) Explain the different ways of passing structure as arguments in functions.
Structure variable can be passed to the function using one of the following ways.
1. By passing individual member of the structure.
2. By passing the entire structure variable.
Following program demonstrates the way of passing individual member of the structure.
/*** Passing individual structure elements ****/
main()
{
struct book
{
char name[25];
char name[25];
int callno;
};
stuct book b1={"Lets us C","YPK",101};
display(b1.name,b1.author,b1.callno);
}
void display (char *s, char *t ,int n)
{
printf("\n%s %s %d",s,t,n);
}
3. (b) Write a C program to illustrate the method of sending an entire structure as a
parameter to a function.
#include
#include
struct book
{
char bname[20];
float price;
};
//prototype
void display(struct book);
int main()
{
struct book b1={" C Primer", 250.00};
display (b1);
return 0;
}
Void display (struct book b2)
{
Puts(b2.bname);
puts(b2.price);
}
4. (a) How to use pointer variables in expressions? Explain through examples.
Pointer variables can be used in expressions in place of ordinary variables. For this
purpose, we need to prefix the pointer variable with an asterisk. For example, consider the
following.
int i = 100;
int *p = &i;
Now we can use *p wherever we would use i. For example, suppose that we want to
multiply the value of i by 2 to compute the value of another variable j. Then, the following
two expressions are the same.
int j = i * 2;
int j = *p * 2;
Note that we have simply substituted i with *p. Similarly, we can have the following
equivalent if conditions.
if (i > 100)
printf ("Hello");
if (*p > 100)
printf ("Hello");
4. (b) Write a ‘C’ program to illustrate the use of pointers in arithmetic operations.
Pointer variables can be used in arithmetic operations in place of ordinary variables. For
this purpose, we need to prefix the pointer variable with an asterisk. For example, consider
the following:
int i = 100;
int *p = &i;
Now we can use *p wherever we would use i. For example, suppose that we want to find out
if i is an even or odd number. Then the following two arithmetic operations are equivalent.
int j = i / 2;
if (j == 0)
printf ("Even");
else
printf ("Odd");
int j = *p / 2;
if (j == 0)
printf ("Even");
else
printf ("Odd");
Another example follows.
/ Program of addition of two variables using pointer.*/
#include
#include
main()
{
int var1,var2,result;
var1=20;
var2=30;
add(&var1 ,&var2,&result);
printf("Result of addition is %d ",result);
}
void add( int *a , int *b, int *c)
{
*c= *a+*b;
}
5. Explain in detail about file handling functions in C.
If we want to store data in a file in the secondary memory, we must specify certain things about
the file, to the operating system. They include:
1. Filename.
2. Data structure.
3. Purpose.
Filename is a string of characters that make up a valid filename for the operating system. It
may contain two parts, a primary name and an optional period with the extension. Examples:
Input.data
store
PROG.C
Student c
Text.out
Data structure of a file is defined as FILE in the library of standard I/O function definitions.
Therefore, all files should be declared as type FILE before they are used. FILE is a defined
data type.
When we open a file, we must specify what we want to do with the file. For example, we may
write data to the file or read the already existing data.
Following is the general format for declaring and opening a file:
FILE *fp;
fp = fopen(“filename”, “mode”);
The first statement declares the variable fp as a “pointer to the data type FILE”. As stated
earlier. FILE is a structure that is defined in the I/O library. The second statement opens the file
named filename and assigns an identifier to the FILE type pointer fp. This pointer which contains
all the information about the file is subsequently used as a communication link between
the system and the program.
The second statement also specifies the purpose of opening this file. The mode does this job.
Mode can be one of the following:
r open the file for reading only.
w open the file for writing only.
a open the file for appending (or adding) data to it.
Note that both the filename and mode are specified as strings. They should be enclosed in
double quotation marks.
When trying to open a file, one of the following things may happen:
1. When the mode is ‘writing’ a file with the specified name is created if the file does not
exist. The contents are deleted, if the file already exists.
2. When the purpose is ‘appending’, the file is opened with the current contents safe. A file
with the specified name is created if the file does not exist.
3. If the purpose is ‘reading’, and if it exists, then the file is opened with the current contents
safe; otherwise an error occurs.
Consider the following statements:
FILE *p1, *p2;
p1 = fopen(“data”, “r”);
p2 = fopen(“results”, “w”);
The file data is opened for reading and results is opened for writing. In case, the results file
already exists, its contents are deleted and the file is opened as a new file. If data file does not
exist, an error will occur.
Many recent compilers include additional modes of operation. They include:
r+ The existing file is opened to the beginning for both reading and writing.
w+ Same as w except both for reading and writing.
a+ Same as a except both for reading and writing.
We can open and use a number of files at a time. This number however depends on the system
we use.
6. Declare two stacks of varying length in a single array. Write C program for push1,
push2, pop1, pop2 to manipulate the two stacks.
#define MAXSIZE 50
struct stack
{
int arr[50];
int top1;
int top2;
};
int pop1()
{
if (top1= = -1)
{
printf("Stack1 underflow");
return -1;
}
return (arr[top1--]);
}
int pop2()
{
if (top2= =top1)
{
printf("\n Stack2 underflow");
return -1;
}
return (arr[top2--]);
}
void push1(int num)
{
if(top1= =top2-1)
{
printf("\n Stack1 overflow");
return;
}
arr[++top1] = num;
}
void push2(int num)
{
if (top2= =MAXSIZE -1)
{
printf("Stack2 underflow");
return;
}
arr[++top2] = num;
}
7. Write a function in C for a singly linked list, which reverses the direction of links
#include
#include
struct node
{
int data;
struct node *next;
};
struct node *root;
void addnode()
{
int item;
struct node *newnode,*p;
printf("\n Enter the item");
scanf("%d", &item);
newnode=(struct node*) malloc(sizeof(struct node));
newnode->data=item;
newnode->next=NULL;
if(root==NULL)
{
root=newnode;
return;
}
p=root;
while(p->next!=NULL)
p=p->next;
p->next=newnode;
}
void display()
{
struct node*p=root;
while(p!=NULL)
{
printf("\n %d ",p->data);
p=p->next;
}
}
void reverse()
{
struct node *p,*oldnode=NULL,*newnode;
p=root;
while(p!=NULL)
{
newnode=p;
p=p->next;
if(oldnode==NULL)
{
oldnode=newnode;
oldnode->next=NULL;
}
else
{
newnode->next=oldnode;
oldnode=newnode;
}
}
root=oldnode;
}
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
addnode();
}
printf("\nbefore reverse");
display();
printf("\n After reverse");
reverse();
display();
return 0;
}
8. Explain the algorithm of selection sort and give a suitable example.
void selectionSort(int numbers[], int array_size)
{
int i, j;
int min, temp;
for (i = 0; i < array_size-1; i++)
{
min = i;
for (j = i+1; j < array_size; j++)
{
if (numbers[j] < numbers[min])
min = j;
}
temp = numbers[i];
numbers[i] = numbers[min];
numbers[min] = temp;
}
}
JNTU ONLINE BITS FOR BTECH I YEAR (UNIT WISE)
UNIT-8
Trees And Graphs
Q.1 The _________ of a tree is the number of levels in it.
(a) Height (b) Depth
(c) Height or Depth
Q.2 The _________ of an element is the number of children it has
(a) Height (b) Depth
(c) Degree
Q.3 The degree of a leaf is
(a) 0 (b) 2
(c) 1 (d) 3
Q.4 A binary tree cannot be empty
(a) True (b) False
Q.5 A tree cannot be empty
(a) True (b) False
Q.6 Each element in a tree can have any number of sub trees
(a) True (b) False
Q.7 The drawing of every binary tree with n elements, n > 0, has exactly… edges.
(a) n (b) n - 1
(c) n - 2
Q.8 A binary tree of height h, h >= 0 has at least_________ elements in it
(a) h (b) h - 1
(c) h - 2
Q.9 A binary tree of height h, h>=0 has at most_________ elements in it
(a) 2 ^ h (b) 2 ^ h - 3
(c) 2 ^ h- 1
Q.10 The height of a binary tree that contains n, n >= 0, elements is at most
(a) n - 1 (b) n
(c) n + 1
Q.11. The height of a binary tree that contains n, n>=0, elements is at least
(a) log n (b) log(n - 1)
(c) log(n + 1)
Q.12 A binary tree of height h that contains exactly 2^h-1 elements is called a
(a) Complete binary tree (b) Full binary tree
(c) Compound binary tree
Q.13 The height of a complete binary tree that contains n elements is
(a) log (n + 1) (b) log n
(c) log (n - 1)
113
Q.14 There are _________ common ways to traverse a binary tree.
(a) 1 (b) 3
(c) 4 (d) 2
Q.15 The _________ form of an expression is the form in which we normally write
an expression.
(a) prefix (b) pos fix
(c) infix
Q.16 In _________ form each operator comes immediately before its operands.
(a) prefix (b) postfix
(c) infix
Q.17 In _________form each operator comes immediately after its operands
(a) prefix (b) post fix
(c) infix
Q.18 An edge with an orientation is called_________ edge.
(a) directed (b) undirected
(c) Bibirectional
Q.19 An edge with no orientation is called_________edge.
(a) directed (b) undirected
(c) Bibirectional
Q.20 A directed graph is also called
(a) bigraph (b) digraph
(c) Directing graph
Q.21 A self-edge is also called
(a) directed edge (b) undirected edge
(c) loop
Q.22 A simple path is a path in which all vertices except possibly the first and last are
(a) different (b) same
(c) may not be different
Q.23 A subgraph of a graph 'G' that contains all the vertices of G and is a tree is a
(a) Bipartite graphs (b) spanning tree
(c) both
Q.24 The_________ of a vertex of an unidirected graph is the no of edges incident on
vertex
(a) degree (b) order
(c) height
Q.25 A n-vertex undirected graph with n*(n-1)/2 edges is a_________ graph
(a) subgraph (b) full graph
(c) complete graph
114
Q.26 The _________ of a vertex is the number of edges incident to vertex ‘i’
(a) in degree (b) out degree
(c) subdegree
Q.27 The _________of a vertex is the number of edges incident from vertex ‘i’
(a) in degree (b) out degree
(c) subdegree
Q.28 A complete digraph on n vertices contains exactly _________directed edges.
(a) n (b) 0 n*(n - 1)/2
(c) n*(n - 1)
Q.29 How many graph search methods are there?
(a) 1 (b) 3
(c) 2
Q.30. Which graph search method is used more frequently?
(a) Breadth first search (b) depth first search
(c) both
Q.31 The method of starting at a vertex and identifying all vertices reachable from it
is called
(a) Depth first search (b) breadth first search
(c) Horizontal search
Q.32_________ is quite similar to pre-order traversal of a binary tree.
(a) Breadth-first search (b) Depth-first search
(c) Horizontal search
Q.33 Of the following tree structures, which is more efficient considering space and time
complexities?
(a) Incomplete Binary Tree (b) Complete BinaryTree
(c) Full Binary Tree d) none
Q.34 What is the order of complexity for binary search tree?
(a) log n2 (b) n log n
(c) log n (d)none
Answers
1. (c) 2. (c) 3. (a) 4. (b)
5. (a) 6. (a) 7. (b) 8. (a)
9. (c) 10. (b) 11. (c) 12. (a)
13. (a) 14. (c) 15. (c) 16. (a)
17. (b) 18. (a) 19. (b) 20. (b)
21. (c) 22. (a) 23. (b) 24. (a)
25. (c) 26. (a) 27. (a) 28. (c)
29. (c) 30. (b) 31. (b) 32. (b)
33. (b) 34. (a)
115
Data Structures
Q. How to distinguish between a binary tree and a tree?
Ans: A node in a tree can have any number of branches, while a binary tree is a tree
structure in which any node can have at most two branches. For binary trees, we
distinguish between the subtree on the left and subtree on the right, whereas for
trees the order of the subtrees is irrelevant.
Consider the following figure...
Fig.
This above figure shows two binary trees, but these binary trees are different. The
first has an empty right subtree while the second has an empty left subtree. If the
above are regarded as trees (not the binary trees), then they are same despite the fact
that they are drawn differently. Also, an empty binary tree can exist, but there is no
tree having zero nodes.
Trees And Graphs
Q.1 The _________ of a tree is the number of levels in it.
(a) Height (b) Depth
(c) Height or Depth
Q.2 The _________ of an element is the number of children it has
(a) Height (b) Depth
(c) Degree
Q.3 The degree of a leaf is
(a) 0 (b) 2
(c) 1 (d) 3
Q.4 A binary tree cannot be empty
(a) True (b) False
Q.5 A tree cannot be empty
(a) True (b) False
Q.6 Each element in a tree can have any number of sub trees
(a) True (b) False
Q.7 The drawing of every binary tree with n elements, n > 0, has exactly… edges.
(a) n (b) n - 1
(c) n - 2
Q.8 A binary tree of height h, h >= 0 has at least_________ elements in it
(a) h (b) h - 1
(c) h - 2
Q.9 A binary tree of height h, h>=0 has at most_________ elements in it
(a) 2 ^ h (b) 2 ^ h - 3
(c) 2 ^ h- 1
Q.10 The height of a binary tree that contains n, n >= 0, elements is at most
(a) n - 1 (b) n
(c) n + 1
Q.11. The height of a binary tree that contains n, n>=0, elements is at least
(a) log n (b) log(n - 1)
(c) log(n + 1)
Q.12 A binary tree of height h that contains exactly 2^h-1 elements is called a
(a) Complete binary tree (b) Full binary tree
(c) Compound binary tree
Q.13 The height of a complete binary tree that contains n elements is
(a) log (n + 1) (b) log n
(c) log (n - 1)
113
Q.14 There are _________ common ways to traverse a binary tree.
(a) 1 (b) 3
(c) 4 (d) 2
Q.15 The _________ form of an expression is the form in which we normally write
an expression.
(a) prefix (b) pos fix
(c) infix
Q.16 In _________ form each operator comes immediately before its operands.
(a) prefix (b) postfix
(c) infix
Q.17 In _________form each operator comes immediately after its operands
(a) prefix (b) post fix
(c) infix
Q.18 An edge with an orientation is called_________ edge.
(a) directed (b) undirected
(c) Bibirectional
Q.19 An edge with no orientation is called_________edge.
(a) directed (b) undirected
(c) Bibirectional
Q.20 A directed graph is also called
(a) bigraph (b) digraph
(c) Directing graph
Q.21 A self-edge is also called
(a) directed edge (b) undirected edge
(c) loop
Q.22 A simple path is a path in which all vertices except possibly the first and last are
(a) different (b) same
(c) may not be different
Q.23 A subgraph of a graph 'G' that contains all the vertices of G and is a tree is a
(a) Bipartite graphs (b) spanning tree
(c) both
Q.24 The_________ of a vertex of an unidirected graph is the no of edges incident on
vertex
(a) degree (b) order
(c) height
Q.25 A n-vertex undirected graph with n*(n-1)/2 edges is a_________ graph
(a) subgraph (b) full graph
(c) complete graph
114
Q.26 The _________ of a vertex is the number of edges incident to vertex ‘i’
(a) in degree (b) out degree
(c) subdegree
Q.27 The _________of a vertex is the number of edges incident from vertex ‘i’
(a) in degree (b) out degree
(c) subdegree
Q.28 A complete digraph on n vertices contains exactly _________directed edges.
(a) n (b) 0 n*(n - 1)/2
(c) n*(n - 1)
Q.29 How many graph search methods are there?
(a) 1 (b) 3
(c) 2
Q.30. Which graph search method is used more frequently?
(a) Breadth first search (b) depth first search
(c) both
Q.31 The method of starting at a vertex and identifying all vertices reachable from it
is called
(a) Depth first search (b) breadth first search
(c) Horizontal search
Q.32_________ is quite similar to pre-order traversal of a binary tree.
(a) Breadth-first search (b) Depth-first search
(c) Horizontal search
Q.33 Of the following tree structures, which is more efficient considering space and time
complexities?
(a) Incomplete Binary Tree (b) Complete BinaryTree
(c) Full Binary Tree d) none
Q.34 What is the order of complexity for binary search tree?
(a) log n2 (b) n log n
(c) log n (d)none
Answers
1. (c) 2. (c) 3. (a) 4. (b)
5. (a) 6. (a) 7. (b) 8. (a)
9. (c) 10. (b) 11. (c) 12. (a)
13. (a) 14. (c) 15. (c) 16. (a)
17. (b) 18. (a) 19. (b) 20. (b)
21. (c) 22. (a) 23. (b) 24. (a)
25. (c) 26. (a) 27. (a) 28. (c)
29. (c) 30. (b) 31. (b) 32. (b)
33. (b) 34. (a)
115
Data Structures
Q. How to distinguish between a binary tree and a tree?
Ans: A node in a tree can have any number of branches, while a binary tree is a tree
structure in which any node can have at most two branches. For binary trees, we
distinguish between the subtree on the left and subtree on the right, whereas for
trees the order of the subtrees is irrelevant.
Consider the following figure...
Fig.
This above figure shows two binary trees, but these binary trees are different. The
first has an empty right subtree while the second has an empty left subtree. If the
above are regarded as trees (not the binary trees), then they are same despite the fact
that they are drawn differently. Also, an empty binary tree can exist, but there is no
tree having zero nodes.
JNTU ONLINE BITS FOR BTECH I YEAR (UNIT WISE)
UNIT-7
Q.1 Is a linked list a linear or non-linear data structure?
(a) Linear (b) Non-linear
(c) Can’t say (d) None
Q.2 How can I search for data in a linked list?
(a) Non-linear search (b) Linear search
(c) Can’t say (d) None
Q.3 What is each entry in a linked list called?
(a) Element (b) Node
(c) Value (d) None
Q.4 The value of the first linked-list index is ____________
(a) –1 (b) 0
(c) 1 (d) none
Q.5 ____________ form of access is used to add and remove nodes from a queue.
(a) FIFO (b) LIFO
(c) FILO (d) None
Q.6 New nodes are added to the ____________ of the queue.
(a) front (b) back
(c) middle (d) none
Q.7 A dequeue (or double-ended queue) is a sequence of elements with the property that
elements can be added, inspected, and removed at ____________
(a) either end (b) one end
(c) both ends (d) none
Q.8 What is the benefit of using a queue linked list?
(a) Queue for scheduling (b) Queue is faster than stack
(c) Both (d) None
Q.9 The StackLinkedList class inherits the LinkedList class
(a) True (b) False
(c) Can’t say (d) None
Q.10 The stack top is initialized to____________ value.
(a) 0 (b) 1
(c) –1 (d) none
Q.11 Convert the infix expression (A – B) * C + D to postfix.
(a) A B – C * D + (b) AB-CD+*
(c) AB–CD*+ (d) none
Q.12 Entries in a stack are ‘ordered’. What is the meaning of this statement?
(a) A collection of stacks can be sorted.
(b) Stack entries may be compared with the ‘<’ operation.
106
(c) The entries must be stored in a linked list.
(d) There is a first entry, a second entry, and so on.
Q.13 The operation for adding an entry to a stack is traditionally called
(a) add (b) append
(c) insert (d) push
Q.14 The operation for removing an entry from a stack is traditionally called
(a) delete (b) peek
(c) pop (d) remove
Q.15 Which of the following stack operations could result in stack underflow?
(a) is_empty (b) pop
(c) push (d) Two or more of the above answers
Q.16 Which of the following applications may use a stack?
(a) A parentheses balancing program
(b) Keeping track of local variables at run time
(c) Syntax analyzer for a compiler
(d) All of the above
Q.17 In the linked-list implementation of the stack class, where does the push method
place the new entry on the linked list?
(a) At the head
(b) At the tail
(c) After all other entries that are greater than the new entry
(d) After all other entries that are smaller than the new entry
Q.18 Convert (6 + 2) * 5 – 8 / 4 into postfix.
(a) 62+584/–* (b) 62+5*84–/
(c) 6 2 + 5 * 8 4 / – (d) None
Q.19 Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent polish
notation.
(a) –^*+ABC–DE+FG (b) ^ – * +ABC – DE + FG
(c) ^*–+ABC–DE+FG (d) None
Q.20 Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent reverse
polish notation.
(a) AB+C*DE-FG+^– (b) AB+CDE*--FG+^
(c) AB + C * DE - - FG + ^ (d) None
Q.21 The minimum number of queues needed to implement the priority queue are
(a) one (b) two
(c) can’t say (d) none
Q.22 In tree construction which is the suitable efficient data structure?
(a) Array (b) Linked list
(c) Stack (d) Queue
107
Q.23 The first operation performed on a stack is____________
(a) deletion (b) insertion
(c) both (d) none
Q.24 Stack is said to be overflow when____________
(a) top=max–1 (b) top=max
(c) top=–1 (d) none
Q.25 The process of allocating memory at run time is called____________
(a) run time allocation (b) compile time allocation
(c) dynamic memory allocation (d) both a and c
Q.26 Local variables are stored in____________
(a) stack (b) heap
(c) memory (d) none
Q.27 Operations performed on a linked list is/are____________
(a) traversing the list (b) inserting an item
(c) creating a list (d) All the above
Q.28 The free memory region is called____________
(a) heap (b) stack
(c) both (d) none
Q.29 A block of memory can be requested at run time using____________
(a) malloc (b) calloc
(c) both (d) none
Q.30 Multiple blocks of memory can be allocated using____________
(a) malloc (b) calloc
(c) can’t say (d) both
Q.31 Memory space must be explicitly released in dynamic run-time allocation.
(a) True (b) False
(c) Can’t say (d) None
Q.32 Realloc is used for____________
(a) Reallocation of memory (b) free memory space
(c) none (d) can’t say
Q.33 A double-linked list is said to be empty when____________
(a) rear=front–1 (b) rear =front=0
(c) rear=front (d) none
Q.34 A double-linked list is said to be overflow when____________
(a) rear=front (b) rear>=max–1
(c) none
Q.35 Evaluate the postfix expression 22*1+
(a) 6 (b) 4
108
(c) 5 (d) 7
Q.36 Convert a$b*c-d+e/f/(g+h) into a postfix expression.
(a) ab$cd-* ef/gh+/+ (b) ab$c*d-ef/gh/++
(c) ab$c*d-ef/gh+/+ (d) None
Q.37 A circular queue is said to be underflow when____________
(a) (rear+1)%max=front (b) (front+1)%max=rear
(c) rear=front (d) none
Q.38 A ___________ is a way of organizing data that considers not only the items stored,
but also their relationship to each other.
(a) linked list (b) data structure
(c) both (d) none
Q.39 The areas in which data structures are applied extensively are
(a) Numerical Analysis, (b) Graphics,
(c) Artificial Intelligence (d) All
Q.40 A circular queue is said to be overflow when____________
(a) (rear+1)%max=front (b) (front+1)%max=rear
(c) rear=front (d) none
Q.41 The highest precedence operator is____________
(a) $ (b) ^
(c) * (d) ()
Q.42 Among the following which is fastest to implement?
(a) Array (b) Linked list
(c) Depends on application (d) Both a and b
Q.43 To place the elements in a particular place ____________ is used.
(a) array (b) linked list
(c) both (d) can’t say
Q.44 In an input restricted deque elements are insert from____________
(a) left (b) right
(c) either side (d) none
Q.45 Convert abc$+ into infix.
(a) a$b+c (b) a+b$c
(c) a$bc+ (d) None
Q.46 The data structures used to perform recursion are
(a) queue (b) stacks
(c) both (d) none
Q.47 Evaluate the postfix expression 23+5*.
(a) 11 (b) 25
(c) 13 (d) None
Q.48 Stacks are used in____________
(a) calculators (b) computers
109
(c) both (d) none
Q.49 Convert ab$c*d-ef/gh+/+ into infix.
(a) a*b$c-d+e/f/(g+h) (b) a$b*c+d-e/f/(g+h)
(c) a$b*c-d+e/f/(g+h) (d) none
Q.50 Structures which contain a member field that points to the same structure type are
called-____________
(a) self-referential structure (b) recursive structure
(c) both (d) none
Answers
1. (a) 2. (b) 3. (b) 4. (d)
5. (a) 6. (b) 7. (c) 8. (c)
9. (a) 10. (c) 11. (a) 12. (d)
13. (d) 14. (c) 15. (a) 16. (d)
17. (a) 18. (c) 19. (b) 20. (c)
21. (b) 22. (b) 23. (b) 24. (a)
25. (d) 26. (a) 27. (d) 28. (a)
29. (c) 30. (b) 31. (a) 32. (a)
33. (a) 34. (b) 35. (c) 36. (c)
37. (a) 38. (b) 39. (d) 40. (a)
41. (d) 42. (c) 43. (b) 44. (b)
45. (b) 46. (b) 47. (b) 48. (c)
49. (c) 50. (a)
Q.51 Which of the following is the feature of stack? All operations are at one end and it
cannot reuse its memory
Ans: Any element can be accessed from it directly.
Q.52 When stacks created, they are…
Ans: initially empty
Q.53 What is time required to insert an element in a stack with linked implementation?
Ans: (log 2n)
Q.54 The time taken for addition of an element in a queue is
Ans: (log n)
Q.55 When is a linear queue said to be empty? Front==rear
Ans: Front=rear
Q.56 When queues are created, they are
Ans: initially empty
Q.57 What is the type of algorithm used in solving the 8 Queens problem?
(a) Queues (b) Semaphores
(c) Backtracking (d) Dijkstra
Q.58 In which data structure, elements can be added or removed at either end, but not in
110
the middle?
(a) Linked List (b) Double ended Queue
(c) stack (d) none
Q.59 Which sort shows the best average behavior?
(a) Quick sort (b) Bubble sort
(c) Insertion sort (d) Heap sort
Q.60 Which data structure is needed to convert infix notations to post fix notations?
(a) stack (b) queue
(c) linked list (d) d-queue
Q.61 What data structure would you mostly likely see in a non recursive implementation
of a recursive algorithm?
(a) queue (b) linked list
(c) stack (d) double linked list
Miscellaneous Questions
Q.1 What is a data structure?
Ans: A data structure is a way of organizing data that considers not only the items stored,
but also their relationship to each other. Advance knowledge about the relationship
between data items allows designing of efficient algorithms for the manipulation of
data.
Q.2 List the areas in which data structures are applied extensively?
Ans: Compiler Design, Operating System, Database Management System, Statistical
analysis package, Numerical Analysis, Graphics, Artificial Intelligence, Simulation
Q.3 What are the major data structures used in the following areas: RDBMS, Network
data model and Hierarchical data model?
Ans: RDBMS-Array (i.e., Array of structures)
Network data model-Graph
Hierarchical data model-Trees
Q.4 If you are using C language to implement the heterogeneous linked list, what pointer
type will you use?
Ans: The heterogeneous linked list contains different data types in its nodes and we need
a link and pointer to connect them. It is not possible to use ordinary pointers for this.
So we go for void pointer. A void pointer is capable of storing a pointer to any type
as it is a generic pointer type.
Q.5 What is the minimum number of queues needed to implement the priority queue?
Ans: Two. One queue is used for actual storing of data and another for storing priorities.
Q.6 What is the data structures used to perform recursion?
Ans: Stack. Because of its LIFO (Last In First Out) property, it remembers its 'caller' so
knows whom to return when the function has to return. Recursion makes use of
system stack for storing the return addresses of the function calls. Every recursive
111
function has its equivalent iterative (non-recursive) function. Even when such
equivalent iterative procedures are written, an explicit stack is to be used.
Answers
57. (c) 58. (b) 59. answer missing 60. (a)
61. (c)
Q.1 Is a linked list a linear or non-linear data structure?
(a) Linear (b) Non-linear
(c) Can’t say (d) None
Q.2 How can I search for data in a linked list?
(a) Non-linear search (b) Linear search
(c) Can’t say (d) None
Q.3 What is each entry in a linked list called?
(a) Element (b) Node
(c) Value (d) None
Q.4 The value of the first linked-list index is ____________
(a) –1 (b) 0
(c) 1 (d) none
Q.5 ____________ form of access is used to add and remove nodes from a queue.
(a) FIFO (b) LIFO
(c) FILO (d) None
Q.6 New nodes are added to the ____________ of the queue.
(a) front (b) back
(c) middle (d) none
Q.7 A dequeue (or double-ended queue) is a sequence of elements with the property that
elements can be added, inspected, and removed at ____________
(a) either end (b) one end
(c) both ends (d) none
Q.8 What is the benefit of using a queue linked list?
(a) Queue for scheduling (b) Queue is faster than stack
(c) Both (d) None
Q.9 The StackLinkedList class inherits the LinkedList class
(a) True (b) False
(c) Can’t say (d) None
Q.10 The stack top is initialized to____________ value.
(a) 0 (b) 1
(c) –1 (d) none
Q.11 Convert the infix expression (A – B) * C + D to postfix.
(a) A B – C * D + (b) AB-CD+*
(c) AB–CD*+ (d) none
Q.12 Entries in a stack are ‘ordered’. What is the meaning of this statement?
(a) A collection of stacks can be sorted.
(b) Stack entries may be compared with the ‘<’ operation.
106
(c) The entries must be stored in a linked list.
(d) There is a first entry, a second entry, and so on.
Q.13 The operation for adding an entry to a stack is traditionally called
(a) add (b) append
(c) insert (d) push
Q.14 The operation for removing an entry from a stack is traditionally called
(a) delete (b) peek
(c) pop (d) remove
Q.15 Which of the following stack operations could result in stack underflow?
(a) is_empty (b) pop
(c) push (d) Two or more of the above answers
Q.16 Which of the following applications may use a stack?
(a) A parentheses balancing program
(b) Keeping track of local variables at run time
(c) Syntax analyzer for a compiler
(d) All of the above
Q.17 In the linked-list implementation of the stack class, where does the push method
place the new entry on the linked list?
(a) At the head
(b) At the tail
(c) After all other entries that are greater than the new entry
(d) After all other entries that are smaller than the new entry
Q.18 Convert (6 + 2) * 5 – 8 / 4 into postfix.
(a) 62+584/–* (b) 62+5*84–/
(c) 6 2 + 5 * 8 4 / – (d) None
Q.19 Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent polish
notation.
(a) –^*+ABC–DE+FG (b) ^ – * +ABC – DE + FG
(c) ^*–+ABC–DE+FG (d) None
Q.20 Convert the expression ((A + B) * C – (D – E) ^ (F + G)) to equivalent reverse
polish notation.
(a) AB+C*DE-FG+^– (b) AB+CDE*--FG+^
(c) AB + C * DE - - FG + ^ (d) None
Q.21 The minimum number of queues needed to implement the priority queue are
(a) one (b) two
(c) can’t say (d) none
Q.22 In tree construction which is the suitable efficient data structure?
(a) Array (b) Linked list
(c) Stack (d) Queue
107
Q.23 The first operation performed on a stack is____________
(a) deletion (b) insertion
(c) both (d) none
Q.24 Stack is said to be overflow when____________
(a) top=max–1 (b) top=max
(c) top=–1 (d) none
Q.25 The process of allocating memory at run time is called____________
(a) run time allocation (b) compile time allocation
(c) dynamic memory allocation (d) both a and c
Q.26 Local variables are stored in____________
(a) stack (b) heap
(c) memory (d) none
Q.27 Operations performed on a linked list is/are____________
(a) traversing the list (b) inserting an item
(c) creating a list (d) All the above
Q.28 The free memory region is called____________
(a) heap (b) stack
(c) both (d) none
Q.29 A block of memory can be requested at run time using____________
(a) malloc (b) calloc
(c) both (d) none
Q.30 Multiple blocks of memory can be allocated using____________
(a) malloc (b) calloc
(c) can’t say (d) both
Q.31 Memory space must be explicitly released in dynamic run-time allocation.
(a) True (b) False
(c) Can’t say (d) None
Q.32 Realloc is used for____________
(a) Reallocation of memory (b) free memory space
(c) none (d) can’t say
Q.33 A double-linked list is said to be empty when____________
(a) rear=front–1 (b) rear =front=0
(c) rear=front (d) none
Q.34 A double-linked list is said to be overflow when____________
(a) rear=front (b) rear>=max–1
(c) none
Q.35 Evaluate the postfix expression 22*1+
(a) 6 (b) 4
108
(c) 5 (d) 7
Q.36 Convert a$b*c-d+e/f/(g+h) into a postfix expression.
(a) ab$cd-* ef/gh+/+ (b) ab$c*d-ef/gh/++
(c) ab$c*d-ef/gh+/+ (d) None
Q.37 A circular queue is said to be underflow when____________
(a) (rear+1)%max=front (b) (front+1)%max=rear
(c) rear=front (d) none
Q.38 A ___________ is a way of organizing data that considers not only the items stored,
but also their relationship to each other.
(a) linked list (b) data structure
(c) both (d) none
Q.39 The areas in which data structures are applied extensively are
(a) Numerical Analysis, (b) Graphics,
(c) Artificial Intelligence (d) All
Q.40 A circular queue is said to be overflow when____________
(a) (rear+1)%max=front (b) (front+1)%max=rear
(c) rear=front (d) none
Q.41 The highest precedence operator is____________
(a) $ (b) ^
(c) * (d) ()
Q.42 Among the following which is fastest to implement?
(a) Array (b) Linked list
(c) Depends on application (d) Both a and b
Q.43 To place the elements in a particular place ____________ is used.
(a) array (b) linked list
(c) both (d) can’t say
Q.44 In an input restricted deque elements are insert from____________
(a) left (b) right
(c) either side (d) none
Q.45 Convert abc$+ into infix.
(a) a$b+c (b) a+b$c
(c) a$bc+ (d) None
Q.46 The data structures used to perform recursion are
(a) queue (b) stacks
(c) both (d) none
Q.47 Evaluate the postfix expression 23+5*.
(a) 11 (b) 25
(c) 13 (d) None
Q.48 Stacks are used in____________
(a) calculators (b) computers
109
(c) both (d) none
Q.49 Convert ab$c*d-ef/gh+/+ into infix.
(a) a*b$c-d+e/f/(g+h) (b) a$b*c+d-e/f/(g+h)
(c) a$b*c-d+e/f/(g+h) (d) none
Q.50 Structures which contain a member field that points to the same structure type are
called-____________
(a) self-referential structure (b) recursive structure
(c) both (d) none
Answers
1. (a) 2. (b) 3. (b) 4. (d)
5. (a) 6. (b) 7. (c) 8. (c)
9. (a) 10. (c) 11. (a) 12. (d)
13. (d) 14. (c) 15. (a) 16. (d)
17. (a) 18. (c) 19. (b) 20. (c)
21. (b) 22. (b) 23. (b) 24. (a)
25. (d) 26. (a) 27. (d) 28. (a)
29. (c) 30. (b) 31. (a) 32. (a)
33. (a) 34. (b) 35. (c) 36. (c)
37. (a) 38. (b) 39. (d) 40. (a)
41. (d) 42. (c) 43. (b) 44. (b)
45. (b) 46. (b) 47. (b) 48. (c)
49. (c) 50. (a)
Q.51 Which of the following is the feature of stack? All operations are at one end and it
cannot reuse its memory
Ans: Any element can be accessed from it directly.
Q.52 When stacks created, they are…
Ans: initially empty
Q.53 What is time required to insert an element in a stack with linked implementation?
Ans: (log 2n)
Q.54 The time taken for addition of an element in a queue is
Ans: (log n)
Q.55 When is a linear queue said to be empty? Front==rear
Ans: Front=rear
Q.56 When queues are created, they are
Ans: initially empty
Q.57 What is the type of algorithm used in solving the 8 Queens problem?
(a) Queues (b) Semaphores
(c) Backtracking (d) Dijkstra
Q.58 In which data structure, elements can be added or removed at either end, but not in
110
the middle?
(a) Linked List (b) Double ended Queue
(c) stack (d) none
Q.59 Which sort shows the best average behavior?
(a) Quick sort (b) Bubble sort
(c) Insertion sort (d) Heap sort
Q.60 Which data structure is needed to convert infix notations to post fix notations?
(a) stack (b) queue
(c) linked list (d) d-queue
Q.61 What data structure would you mostly likely see in a non recursive implementation
of a recursive algorithm?
(a) queue (b) linked list
(c) stack (d) double linked list
Miscellaneous Questions
Q.1 What is a data structure?
Ans: A data structure is a way of organizing data that considers not only the items stored,
but also their relationship to each other. Advance knowledge about the relationship
between data items allows designing of efficient algorithms for the manipulation of
data.
Q.2 List the areas in which data structures are applied extensively?
Ans: Compiler Design, Operating System, Database Management System, Statistical
analysis package, Numerical Analysis, Graphics, Artificial Intelligence, Simulation
Q.3 What are the major data structures used in the following areas: RDBMS, Network
data model and Hierarchical data model?
Ans: RDBMS-Array (i.e., Array of structures)
Network data model-Graph
Hierarchical data model-Trees
Q.4 If you are using C language to implement the heterogeneous linked list, what pointer
type will you use?
Ans: The heterogeneous linked list contains different data types in its nodes and we need
a link and pointer to connect them. It is not possible to use ordinary pointers for this.
So we go for void pointer. A void pointer is capable of storing a pointer to any type
as it is a generic pointer type.
Q.5 What is the minimum number of queues needed to implement the priority queue?
Ans: Two. One queue is used for actual storing of data and another for storing priorities.
Q.6 What is the data structures used to perform recursion?
Ans: Stack. Because of its LIFO (Last In First Out) property, it remembers its 'caller' so
knows whom to return when the function has to return. Recursion makes use of
system stack for storing the return addresses of the function calls. Every recursive
111
function has its equivalent iterative (non-recursive) function. Even when such
equivalent iterative procedures are written, an explicit stack is to be used.
Answers
57. (c) 58. (b) 59. answer missing 60. (a)
61. (c)
JNTU ONLINE BITS FOR BTECH I YEAR (UNIT WISE)
UNIT-6
Searching And Sorting Techniques
Q.1 The __________ refers to the operation of arranging data in some given order, such
as increasing or decreasing.
(a) sorting (b) searching
(c) merging
Q.2 Which sorting technique needs to scan through the entire array and swap adjacent
elements whenever required?
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort
Q.3 The number of moves required for sorting an array using the Bubble Sort is
(a) O (n2) (b) O (Log (n)
(c) O (n)
Q.4 The technique which involves selecting the minimum element is
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort
Q.5 The number of moves required for sorting an array using the Selection Sort is
(a) O (n) (b) O (Log (n)
(c) O (n2)
Q.6 __________sort works by considering the elements one at a time and inserting the
element in its proper place among those already considered.
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort
Q.7 Factors that help in deciding the sorting algorithm to be used are
(a) Memory (b) Performance
(c) Flexibility (d) All
Q.8 Which Sorting technique is simplest to use?
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort (d) Quick Sort
Q.9 Which searching technique is the best among the three searching techniques?
(a) Quick Search (b) Binary Search
(c) Linear Search
Q.10.In __________search, as the number of elements increases, the time taken to perform
the search increases linearly.
(a) Quick Search (b) Binary Search
(c) Linear Search
Q.11 The linear search requires __________comparisons in the worst case.
(a) N (b) N*N
(c) log (N)
101
Q.12 __________search works by comparing your search element with the center element
of the array.
(a) Quick Search (b) Binary Search
(c) Linear Search
Q.14 The disadvantage of binary search is
(a) Takes more time (b) Complexity is high
(c) The array has to be in the sorted order
Q.14 Which sorting technique is the best one?
(a) Merge Sort (b) Quick Sort
(c) Selection Sort
Q.15 Which sorting technique makes use of a pivot element?
(a) Merge Sort (b) Quick Sort
(c) Selection Sort
Q.16 Which sorting technique is the fastest one?
(a) Quick Sort (b) Merge Sort
(c) Insertion Sort
Q.17 What is the best-case time complexity of linear search?
(a) 1 (b) O (log (n))
(c) O (n)
Q.18 What is the average-case time complexity of linear search?
(a) 1 (b) O (log (n))
(c) O (n)
Q.19 What is the worst-case time complexity of linear search?
(a) 1 (b) O(n)
(c) O(n2)
Q.20 What is the best-case time complexity of binary search?
(a) 1 (b) O (log (n))
(c) O (n2)
Q.21 What is the worst-case time complexity of binary search?
(a) 1 (b) O (log (n))
(c) O (n2)
Q.22 What is the average-case time complexity of binary search?
(a) 1 (b) O(log(n))
(c) O(n2)
Q.23 _________refers to finding whether a data item is present in the set of items or not.
(a) Searching (b) Sorting
(c) Merging
Q.24 The time required to search depends on the following factors:
(a) Whether the data is arranged in a particular order or not
(b) The location of the data to be searched
102
(c) The total number of searches to be done
(d) All
Q.25 When the data is arranged in a particular order then the time taken to search for the
item is
(a) more (b) less
(c) cannot say
Q.26 In __________.search, the searching process starts from the first item.
(a) binary (b) linear
(c) quick
Q.27 A__________search is a searching technique that can be applied only to a sorted list
of items.
(a) binary search (b) linear search
(c) quick search
Q.28 Which searching technique is also called dictionary search?
(a) Binary Search (b) Linear Search
(c) Quick Search
Q.29 Binary Search can be applied to sorted and unsorted lists.
(a) Yes (b) No
Q.30 _________searching technique can be applied to both sorted and unsorted list.
(a) Binary Search (b) Linear Search
(c) Quick Search
Q.31 Searching time is less for
(a) quick search (b) linear search
(c) binary search
Q.32 What is the average-case time complexity of bubble sort?
(a) 1 (b) O(log(n)
(c) O(n2)
Q.33 What is the worst-case time complexity of bubble sort?
(a) 1 (b) O(n)
(c) O(n2)
Q.34 What is the best-case time complexity of bubble sort?
(a) O(n) (b) O (nlog (n)
(c) O (n2)
Q.35 What is the worst-case time complexity of insertion sort?
(a) 1 (b) O(log(n)
(c) O(n2)
Q.36 What is the average-case time complexity of insertion sort?
(a) 1 (b) O (log(n)
(c) O(n2)
103
Q.37 What is the best-case time complexity of selection sort?
(a) 1 (b) O (nlog (n)
(c) O (n2)
Q.38 What is the average-case time complexity of selection sort?
(a) 1 (b) O (log (n))
(c) O (n2)
Q.39 What is the worst-case time complexity of selection sort?
(a) 1 (b) O (n)
(c) O (n2)
Q.40 What is the best-case time complexity of quick sort?
(a) 1 (b) O (nlog (n)
(c) O (n2)
Q.41 What is the average-case time complexity of quick sort?
(a) 1 (b) O (log (n)
(c) O (n2)
Q.42 What is the worst-case time complexity of quick sort?
(a) 1 (b) O (n)
(c) O (n2)
Q.43 What is the average- case time complexity of merge sort?
(a) 1 (b) O (nlog (n)
(c) O (n2)
Q.44 Merge sort is also called
(a) partition exchange Sort (b) exchange sort
(c) binary sort
Q.45 Bubble sort is also called
(a) partition exchange sort (b) exchange sort
(c) binary sort
Q.46 Time complexity is inversely proportional to
(a) space complexity (b) diverse complexity
(c) none
Q.47 What is the worst-case time complexity of merge sort?
(a) 1 (b) O (nlogn)
(c) O (n2)
Q.48 Bubble sort is yet another sorting technique which works on the design of
(a) Brute force (b) greedy technique
(c) divide and conquer (d) dynamic programming
Q.49 What are the methods available in storing sequential files?
(a) Straight merging
(b) Natural merging
(c) Polyphase sort
104
(d) Distribution of Initial runs
(e) All the above
Q.50 Linear search works on the principle of
(a) brute force (b) greedy technique
(c) divide and conquer (d) dynamic programming
Q.51 Linear search is also called
(a) sequential search (b) binary search
(c) both (d) none
Q.5 Binary search works on the strategy of
(a) divide and conquer (b) brute force
(c) greedy technique (d) dynamic programming
Q.53 Bubble sort is yet another sorting technique which works on the design of
(a) brute force (b) greedy technique
(c) divide and conquer (d) dynamic programming
Answers
1. (a) 2. (a) 3. (a) 4. (b)
5. (d) 6. (c) 7. (d) 8. (a)
9. (b) 10. (c) 11. (a) 12. (b)
13. (c) 14. (b) 15. (b) 16. (a)
17. (a) 18. (c) 19. (b) 20. (b)
21. (c) 22. (c) 23. (a) 24. (d)
25. (b) 26. (b) 27. (a) 28. (a)
29. (b) 30. (b) 31. (c) 32. (c)
33. (c) 34. (c) 35. (c) 36. (c)
37. (c) 38. (c) 39. (c) 40. (b)
41. (c) 42. (c) 43. (b) 44. (a)
45. (b) 46. (a) 47. (c) 48. (a)
49. (e) 50. (a) 51. (a) 52. (a)
53. (a)
Searching And Sorting Techniques
Q.1 The __________ refers to the operation of arranging data in some given order, such
as increasing or decreasing.
(a) sorting (b) searching
(c) merging
Q.2 Which sorting technique needs to scan through the entire array and swap adjacent
elements whenever required?
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort
Q.3 The number of moves required for sorting an array using the Bubble Sort is
(a) O (n2) (b) O (Log (n)
(c) O (n)
Q.4 The technique which involves selecting the minimum element is
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort
Q.5 The number of moves required for sorting an array using the Selection Sort is
(a) O (n) (b) O (Log (n)
(c) O (n2)
Q.6 __________sort works by considering the elements one at a time and inserting the
element in its proper place among those already considered.
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort
Q.7 Factors that help in deciding the sorting algorithm to be used are
(a) Memory (b) Performance
(c) Flexibility (d) All
Q.8 Which Sorting technique is simplest to use?
(a) Bubble Sort (b) Selection Sort
(c) Insertion Sort (d) Quick Sort
Q.9 Which searching technique is the best among the three searching techniques?
(a) Quick Search (b) Binary Search
(c) Linear Search
Q.10.In __________search, as the number of elements increases, the time taken to perform
the search increases linearly.
(a) Quick Search (b) Binary Search
(c) Linear Search
Q.11 The linear search requires __________comparisons in the worst case.
(a) N (b) N*N
(c) log (N)
101
Q.12 __________search works by comparing your search element with the center element
of the array.
(a) Quick Search (b) Binary Search
(c) Linear Search
Q.14 The disadvantage of binary search is
(a) Takes more time (b) Complexity is high
(c) The array has to be in the sorted order
Q.14 Which sorting technique is the best one?
(a) Merge Sort (b) Quick Sort
(c) Selection Sort
Q.15 Which sorting technique makes use of a pivot element?
(a) Merge Sort (b) Quick Sort
(c) Selection Sort
Q.16 Which sorting technique is the fastest one?
(a) Quick Sort (b) Merge Sort
(c) Insertion Sort
Q.17 What is the best-case time complexity of linear search?
(a) 1 (b) O (log (n))
(c) O (n)
Q.18 What is the average-case time complexity of linear search?
(a) 1 (b) O (log (n))
(c) O (n)
Q.19 What is the worst-case time complexity of linear search?
(a) 1 (b) O(n)
(c) O(n2)
Q.20 What is the best-case time complexity of binary search?
(a) 1 (b) O (log (n))
(c) O (n2)
Q.21 What is the worst-case time complexity of binary search?
(a) 1 (b) O (log (n))
(c) O (n2)
Q.22 What is the average-case time complexity of binary search?
(a) 1 (b) O(log(n))
(c) O(n2)
Q.23 _________refers to finding whether a data item is present in the set of items or not.
(a) Searching (b) Sorting
(c) Merging
Q.24 The time required to search depends on the following factors:
(a) Whether the data is arranged in a particular order or not
(b) The location of the data to be searched
102
(c) The total number of searches to be done
(d) All
Q.25 When the data is arranged in a particular order then the time taken to search for the
item is
(a) more (b) less
(c) cannot say
Q.26 In __________.search, the searching process starts from the first item.
(a) binary (b) linear
(c) quick
Q.27 A__________search is a searching technique that can be applied only to a sorted list
of items.
(a) binary search (b) linear search
(c) quick search
Q.28 Which searching technique is also called dictionary search?
(a) Binary Search (b) Linear Search
(c) Quick Search
Q.29 Binary Search can be applied to sorted and unsorted lists.
(a) Yes (b) No
Q.30 _________searching technique can be applied to both sorted and unsorted list.
(a) Binary Search (b) Linear Search
(c) Quick Search
Q.31 Searching time is less for
(a) quick search (b) linear search
(c) binary search
Q.32 What is the average-case time complexity of bubble sort?
(a) 1 (b) O(log(n)
(c) O(n2)
Q.33 What is the worst-case time complexity of bubble sort?
(a) 1 (b) O(n)
(c) O(n2)
Q.34 What is the best-case time complexity of bubble sort?
(a) O(n) (b) O (nlog (n)
(c) O (n2)
Q.35 What is the worst-case time complexity of insertion sort?
(a) 1 (b) O(log(n)
(c) O(n2)
Q.36 What is the average-case time complexity of insertion sort?
(a) 1 (b) O (log(n)
(c) O(n2)
103
Q.37 What is the best-case time complexity of selection sort?
(a) 1 (b) O (nlog (n)
(c) O (n2)
Q.38 What is the average-case time complexity of selection sort?
(a) 1 (b) O (log (n))
(c) O (n2)
Q.39 What is the worst-case time complexity of selection sort?
(a) 1 (b) O (n)
(c) O (n2)
Q.40 What is the best-case time complexity of quick sort?
(a) 1 (b) O (nlog (n)
(c) O (n2)
Q.41 What is the average-case time complexity of quick sort?
(a) 1 (b) O (log (n)
(c) O (n2)
Q.42 What is the worst-case time complexity of quick sort?
(a) 1 (b) O (n)
(c) O (n2)
Q.43 What is the average- case time complexity of merge sort?
(a) 1 (b) O (nlog (n)
(c) O (n2)
Q.44 Merge sort is also called
(a) partition exchange Sort (b) exchange sort
(c) binary sort
Q.45 Bubble sort is also called
(a) partition exchange sort (b) exchange sort
(c) binary sort
Q.46 Time complexity is inversely proportional to
(a) space complexity (b) diverse complexity
(c) none
Q.47 What is the worst-case time complexity of merge sort?
(a) 1 (b) O (nlogn)
(c) O (n2)
Q.48 Bubble sort is yet another sorting technique which works on the design of
(a) Brute force (b) greedy technique
(c) divide and conquer (d) dynamic programming
Q.49 What are the methods available in storing sequential files?
(a) Straight merging
(b) Natural merging
(c) Polyphase sort
104
(d) Distribution of Initial runs
(e) All the above
Q.50 Linear search works on the principle of
(a) brute force (b) greedy technique
(c) divide and conquer (d) dynamic programming
Q.51 Linear search is also called
(a) sequential search (b) binary search
(c) both (d) none
Q.5 Binary search works on the strategy of
(a) divide and conquer (b) brute force
(c) greedy technique (d) dynamic programming
Q.53 Bubble sort is yet another sorting technique which works on the design of
(a) brute force (b) greedy technique
(c) divide and conquer (d) dynamic programming
Answers
1. (a) 2. (a) 3. (a) 4. (b)
5. (d) 6. (c) 7. (d) 8. (a)
9. (b) 10. (c) 11. (a) 12. (b)
13. (c) 14. (b) 15. (b) 16. (a)
17. (a) 18. (c) 19. (b) 20. (b)
21. (c) 22. (c) 23. (a) 24. (d)
25. (b) 26. (b) 27. (a) 28. (a)
29. (b) 30. (b) 31. (c) 32. (c)
33. (c) 34. (c) 35. (c) 36. (c)
37. (c) 38. (c) 39. (c) 40. (b)
41. (c) 42. (c) 43. (b) 44. (a)
45. (b) 46. (a) 47. (c) 48. (a)
49. (e) 50. (a) 51. (a) 52. (a)
53. (a)
JNTU ONLINE BITS FOR BTECH I YEAR UNIT WISE
UNIT-5
Q.1 A __________ is a place on the disk where a group of related data is stored.
(a) disk (b) file
(c) both (d) none
Q.2 Basic operations performed on files is/are__________
(a) opening a file (b) reading data from file
(c) closing a file (d) all the above
Q.3 getw() is used to__________
(a) read an integer from a file (b) read a string from a file
(c) none
Q.4 ftell() is used to__________
(a) give the current position of a file
(b) give the specified character position
(c) none
Q.5 fopen() is used to__________
(a) create a new file (b) open an existing file
(c) both (d) none
Q.6 What does FILE *fp indicate?
(a) fp as a pointer to datatype FILE (b) FILE as a pointer to datatype fp
(c) none
Q.7 The mode r is used to open a file for__________
(a) read only (b) write only
(c) none
Q.8 The mode a is used to open a file for__________
(a) read (b) adding data
(c) none
Q.9 The mode w+ is used for__________operations.
(a) reading (b) writing
(c) both (d) none
Q.10 If a non-existing file is opened in read mode then__________occurs.
(a) Error (b) new file is created
(c) none
Q.11 Standard i/o routines are __________
(a) fopen() (b) getw()
(c) rewind() (d) all of these
Q.12 When end of file is reached getc() returns__________
(a) -1 (b) EOF
(c) both
98
Q.13 The function putc() is used for__________ purpose
(a) read a character (b) both
(c) write a character (d) none
Q.14 The first argument in fprintf() is used for__________
(a) file pointer (b) file name
(c) both (d) none
Q.15 Status-inquiry library functions are__________
(a) feof (b) ferror
(c) both (d) none
Q.16 Which of the following is an error situation?
(a) Device overflow
(b) opening file with invalid file name
(c) Attempting to write to a write-protected file
(d) all the above
Q.17 The feof() function is used to test__________condition
(a) end of file (b) start of file
(c) none
Q.18 feof() returns a non-zero integer value if__________
(a) error occurs (b) all the data has been read
(c) both
Q.19 The ferror() function is used to report__________
(a) status of file indicated (b) status of error in file
(c) none
Q.20 ferror() takes__________as argument.
(a) pointer (b) file name
(c) file pointer (d) any one of these
Q.21 If an error is detected by ferror(),then it returns __________
(a) zero integer (b) non zero integer
(c) can't say
Q.22 fopen() returns__________ value when file can't be opened using fopen().
(a) NULL (b) -1
(c) 0 (d) can't say
Q.23 fseek(fp,0L,1) indicates __________
(a) stays at beginning (b) stays at end
(c) stays at current position
Q.24 fseek(fp,-m,1) indicates __________
(a) Go backward by m bytes (b) Go forward by m bytes
(c) none
99
Q.25 fseek() returns __________ value when file pointer moves beyond the file boundaries.
(a) error0 (b) -1
(c) 0 (d) none
Q.26 rewind() is used for__________
(a) reset the position to start of file (b) reset the position to end of file
(c) none
Q.27 The first byte in a file is numbered __________
(a) 1 (b) 0
(c) can't say
Q.28 fseek()s used for__________
(a) sets the position to start of file (b) sets the position to end of file
(c) sets the position to desired point in file (d) none
Q.29 getc() is used to__________
(a) Read a character (b) read a string
(c) none
Q.30 To store data in a file in the secondary memory __________is/are needed to OS.
(a) Filename (b) Data Structure
(c) Purpose (d) All these
Answers
1. (b) 2. (d) 3. (a) 4. (a)
5. (c) 6. (a) 7. (a) 8. (b)
9. (c) 10. (a) 11. (d) 12. (b)
13. (c) 14. (a) 15. (c) 16. (d)
17. (a) 18. (b) 19. (a) 20. (c)
21. (b) 22. (a) 23. (c) 24. (a)
25. (b) 26. (a) 27. (b) 28. (c)
29. (a) 30. (d)
Q.1 A __________ is a place on the disk where a group of related data is stored.
(a) disk (b) file
(c) both (d) none
Q.2 Basic operations performed on files is/are__________
(a) opening a file (b) reading data from file
(c) closing a file (d) all the above
Q.3 getw() is used to__________
(a) read an integer from a file (b) read a string from a file
(c) none
Q.4 ftell() is used to__________
(a) give the current position of a file
(b) give the specified character position
(c) none
Q.5 fopen() is used to__________
(a) create a new file (b) open an existing file
(c) both (d) none
Q.6 What does FILE *fp indicate?
(a) fp as a pointer to datatype FILE (b) FILE as a pointer to datatype fp
(c) none
Q.7 The mode r is used to open a file for__________
(a) read only (b) write only
(c) none
Q.8 The mode a is used to open a file for__________
(a) read (b) adding data
(c) none
Q.9 The mode w+ is used for__________operations.
(a) reading (b) writing
(c) both (d) none
Q.10 If a non-existing file is opened in read mode then__________occurs.
(a) Error (b) new file is created
(c) none
Q.11 Standard i/o routines are __________
(a) fopen() (b) getw()
(c) rewind() (d) all of these
Q.12 When end of file is reached getc() returns__________
(a) -1 (b) EOF
(c) both
98
Q.13 The function putc() is used for__________ purpose
(a) read a character (b) both
(c) write a character (d) none
Q.14 The first argument in fprintf() is used for__________
(a) file pointer (b) file name
(c) both (d) none
Q.15 Status-inquiry library functions are__________
(a) feof (b) ferror
(c) both (d) none
Q.16 Which of the following is an error situation?
(a) Device overflow
(b) opening file with invalid file name
(c) Attempting to write to a write-protected file
(d) all the above
Q.17 The feof() function is used to test__________condition
(a) end of file (b) start of file
(c) none
Q.18 feof() returns a non-zero integer value if__________
(a) error occurs (b) all the data has been read
(c) both
Q.19 The ferror() function is used to report__________
(a) status of file indicated (b) status of error in file
(c) none
Q.20 ferror() takes__________as argument.
(a) pointer (b) file name
(c) file pointer (d) any one of these
Q.21 If an error is detected by ferror(),then it returns __________
(a) zero integer (b) non zero integer
(c) can't say
Q.22 fopen() returns__________ value when file can't be opened using fopen().
(a) NULL (b) -1
(c) 0 (d) can't say
Q.23 fseek(fp,0L,1) indicates __________
(a) stays at beginning (b) stays at end
(c) stays at current position
Q.24 fseek(fp,-m,1) indicates __________
(a) Go backward by m bytes (b) Go forward by m bytes
(c) none
99
Q.25 fseek() returns __________ value when file pointer moves beyond the file boundaries.
(a) error0 (b) -1
(c) 0 (d) none
Q.26 rewind() is used for__________
(a) reset the position to start of file (b) reset the position to end of file
(c) none
Q.27 The first byte in a file is numbered __________
(a) 1 (b) 0
(c) can't say
Q.28 fseek()s used for__________
(a) sets the position to start of file (b) sets the position to end of file
(c) sets the position to desired point in file (d) none
Q.29 getc() is used to__________
(a) Read a character (b) read a string
(c) none
Q.30 To store data in a file in the secondary memory __________is/are needed to OS.
(a) Filename (b) Data Structure
(c) Purpose (d) All these
Answers
1. (b) 2. (d) 3. (a) 4. (a)
5. (c) 6. (a) 7. (a) 8. (b)
9. (c) 10. (a) 11. (d) 12. (b)
13. (c) 14. (a) 15. (c) 16. (d)
17. (a) 18. (b) 19. (a) 20. (c)
21. (b) 22. (a) 23. (c) 24. (a)
25. (b) 26. (a) 27. (b) 28. (c)
29. (a) 30. (d)
Thursday, May 7, 2009
JNTU ONLINE BITS FOR BTECH I YEAR (UNIT WISE)
UNIT-4
Structures and Unions
Q.1 What will be the output of the following program?
struct {
int i;
float f;
}var;
int main()
{
var.i=5;
var.f=9.76723;
printf("%d %.2f”,var.i,var.f);
return(0);
}
(a) Compile-Time Error (b) 5 9.76723
(c) 5 9.76 (d) 5 9.77
Q.2 What will be the output of the following program?
int main()
{
int val=1234;
int* ptr=&val;
printf(“%d %d”,val,*ptr++);
return(0);
}
(a) Compile-Time Error (b) 5 9.76723
(c) 5 9.76 (d) 5 9.77
Q.3 What will be the output of the following program?
struct values {
int i;
float f;
};
int main()
{
struct values var={555,67.05501};
printf(“%2d %.2f”,var.i,var.f);
return(0);
}
80
(a) 1234 1234 (b) 1235 1235
(c) 1234 1235 (d) 1235 1234
Q.4 What will be the output of the following program?
typedef struct {
int i;
float f;
}values;
int main()
{
static values var={555,67.05501};
printf(“%2d %.2f”,var.i,var.f);
return(0);
}
(a) Compile-Time Error (b) 55 67.05
(c) 555 67.06 (d) 555 67.05
Q.5 What will be the output of the following program?
struct my_struct {
int i=7;
float f=999.99;
}var;
int main()
{
var.i=5;
printf(“%d %.2f”,var.i,var.f);
return(0);
(a) Compile-Time Error (b) 7 999.99
(c) 5 999.99 (d) None of these
Q.6 What will be the output of the following program?
struct first {
int a;
float b;
}s1={32760,12345.12345};
typedef struct {
char a;
int b;
}second;
struct my_struct {
float a;
unsigned int b;
81
};
typedef struct my_struct third;
int main()
{
static second s2={‘A’,- -4};
third s3;
s3.a=~(s1.a-32760);
s3.b=-++s2.b;
printf(“%d %.2f\n%c %d\n%.2f %u”,(s1.a)--,s1.b+0.005,s2.a+32,s2.b,++(s3.a),--
s3.b);
return(0);
}
(a) Compile-Time Error
(b) 32760 12345.12
A 4
1 -5
(c) 32760 12345.13
a -5
0.00 65531
(d) 32760 12345.13
a 5
0.00 65530
Q.7 What will be the output of the following program?
struct {
int i,val[25];
}var={1,2,3,4,5,6,7,8,9},*vptr=&var;
int main()
{
printf(“%d %d %d\n”,var.i,vptr->i,(*vptr).i);
printf(“%d %d %d %d %d %d”,var.val[4],*(var.val+4),vptr-
>val[4],*(vptr->val+4),(*vptr).val[4],*((*vptr).val+4));
return(0);
}
(a) Compile-Time Error
(b) 1 1 1
6 6 6 6 6 6
(c) 1 1 1
5 5 5 5 5 5
(d) None of these
82
Q.8 What will be the output of the following program?
typedef struct {
int i;
float f;
}temp;
void alter(temp *ptr,int x,float y)
{
ptr->i=x;
ptr->f=y;
}
int main()
{
temp a={111,777.007};
printf(“%d %.2f\n”,a.i,a.f);
alter(&a,222,666.006);
printf(“%d %.2f”,a.i,a.f);
return(0);
}
(a) Compile-Time error
(b) 111 777.007
222 666.006
(c) 111 777.01
222 666.01
(d) None of these
Q.9 What will be the output of the following program?
typedef struct {
int i;
float f;
}temp;
temp alter(temp tmp,int x,float y)
{
tmp.i=x;
tmp.f=y;
return tmp;
}
int main()
{
temp a={111,777.007};
83
printf(“%d %.3f\n”,a.i,a.f);
a=alter(a,222,666.006);
printf(“%d %.3f”,a.i,a.f);
return(0);
}
(a) Compile-Time error
(b) 111 777.007
222 666.006
(c) 111 777.01
222 666.01 (d)None of these
Q.10 What will be the output of the following program?
typedef struct {
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
temp tmp=*ptr;
printf(“%d %.2f\n”,tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
int main()
{
temp a={65535,777.777};
a=alter(&a,-1,666.666);
printf(“%d %.2f”,a.i,a.f);
return(0);
}
(a) Compile-Time error
(b) 65535 777.777
-1 666.666
(c) 65535 777.78
-1 666.67
(d) -1 777.78
-1 666.67
Q.11 What will be the output of the following program?
struct my_struct1 {
int arr[2][2];
84
};
typedef struct my_struct1 record;
struct my_struct2 {
record temp;
}list[2]={1,2,3,4,5,6,7,8};
int main()
{
int i,j,k;
for (i=1; i>=0; i--)
for (j=0; j<2; j++)
for (k=1; k>=0; k--)
printf(“%d”,list[i].temp.arr[j][k]);
return(0);
}
(a) Compile-Time Error (b) Run-Time Error
(c) 65872143 (d) 56781243
Q.12 What will be the output of the following program?
struct my_struct {
int i;
unsigned int j;
};
int main()
{
struct my_struct temp1={-32769,-1},temp2;
temp2=temp1;
printf(“%d %u”,temp2.i,temp2.j);
return(0);
}
(a) 32767 -1 (b) -32769 -1
(c) -32769 65535 (d) 32767 65535
Q.14 What will be the output of the following program?
struct names {
char str[25];
struct names *next;
};
typedef struct names slist;
int main()
{
slist *list,*temp;
85
list=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(list->str,“Hai”);
list->next=NULL;
temp=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(temp->str,“Friends”);
temp->next=list;
list=temp;
while (temp != NULL)
{
printf(“%s”,temp->str);
temp=temp->next;
}
return(0);
}
(a) Compile-Time Error (b) HaiFriends
(c) FriendsHai (d) None of these
Q.14 What will be the output of the following program?
(i) struct A {
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;
(ii) struct B {
int b;
struct B *next;
};
struct A {
int a;
struct B tempB;
struct A *next;
};
(iii) struct B {
int b;
}tempB;
86
struct {
int a;
struct B *nextB;
};
(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;
(a) (iv) Only (b) (iii) Only
(c) All of the these (d) None of these
Q.15 What will be the output of the following program?
union A {
char ch;
int i;
float f;
}tempA;
int main()
{
tempA.ch=‘A’;
tempA.i=777;
tempA.f=12345.12345;
printf(“%d”,tempA.i);
return(0);
}
(a) Compile-Time Error (b) 12345
(c) Erroneous output (d) 777
Q.16 What will be the output of the following program?
struct A {
int i;
float f;
union B {
char ch;
int j;
87
}temp;
}temp1;
int main()
{
struct A temp2[5];
printf(“%d %d”,sizeof temp1,sizeof(temp2));
return(0);
}
(a)6 30 (b)8 40 (c)9 45 (d)None of these
Q.17 What will be the output of the following program?
int main()
{
static struct my_struct {
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
unsigned :6; // Fill out first word
}v={1,2,7,12};
printf(“%d %d %d %d“,v.a,v.b,v.c,v.d);
printf(“\nSize=%d bytes”,sizeof v);
return(0);
}
(a) Compile-Time Error
(b) 1 2 7 12
Size=2 bytes
(c) 1 2 7 12
Size=4 bytes
(d)None of these
Q.18 What are the largest values that can be assigned to each of the bit fields defined in
Q.17 above?
(a) a=0 b=2 c=3 d=4 (b) a=1 b=2 c=7 d=15
(c) a=1 b=3 c=7 d=15 (d) None of these
Q.19 What will be the output of the following program?
int main()
{
struct sample {
unsigned a:1;
unsigned b:4;
88
}v={0,15};
unsigned *vptr=&v.b;
printf(“%d %d”,v.b,*vptr);
return(0);
}
(a) Compile-Time Error (b) 0 0
(c) 15 15 (d) None of these
Q.20 What will be the output of the following program?
int main()
{
static struct my_struct {
unsigned a:1;
int i;
unsigned b:4;
unsigned c:10;
}v={1,10000,15,555};
printf(“%d %d %d %d”,v.i,v.a,v.b,v.c);
printf(“\nSize=%d bytes”,sizeof v);
return(0);
}
(a) Compile-Time Error
(b) 1 10000 15 555
Size=4 bytes
(c) 10000 1 15 555
Size=4 bytes
(d) 10000 1 15 555
Size=5 bytes
Solutions
A.1 (d)
Though both and are optional, one of the
two must appear. In the above program,, i.e., var is used. (2
decimal places or) 2-digit precision of 9.76723 is 9.77
A.2 (d)
Both and are optional. Thus, the structure
defined in the above program has no use and the program executes in the normal way.
A.3 (c)
The members of a structure variable can be assigned initial values in much the same
89
manner as the elements of an array. The initial values must appear in order in which
they will be assigned to their corresponding structure members, enclosed in braces
and separated by commas.
A.4 (c)
In the above program, values is the user-defined structure type or the new user-defined
data type. Structure variables can then be defined in terms of the new data type.
A.5 (a)
The C language does not permit the initialization of individual structure members
within the template. The initialization must be done only in the declaration of the
actual variables. The correct way to initialize the values is shown in A.3 or A.4.
A.6 (d)
Illustrating 3 different ways of declaring the structures: first, second and third are
the user-defined structure type. s1, s2 and s3 are structure variables. Also, an expression
of the form ++variable.member is equivalent to ++(variable.member), i.e.,
the ++ operator will apply to the structure member, not the entire structure variable.
A.7 (b)
The value of the member ‘i’ can be accessed using var.i, vptr->i and (*vptr).i Similarly,
the 5th value of the member ‘val’ can be accessed using var.val[4],
*(var.val+4), vptr->val[4], *(vptr->val+4), (*vptr).val[4] and *((*vptr).val+4)
A.8 (c)
This program illustrates the transfer of a structure to a function by passing the
structure‘s address (a pointer) to the function.
A.9 (b)
This program illustrates the transfer of a structure to a function by value. Also, the
altered structure is now returned directly to the calling portion of the program.
A.10 (d)
This program illustrates the transfer of a structure to a function by passing the
structure’s address (a pointer) to the function. Also, the altered structure is now
returned directly to the calling portion of the program.
A.11 (c)
This program illustrates the implementation of a nested structure, i.e., a structure
inside another structure.
A.12(d)
An entire structure variable can be assigned to another structure variable, provided
both variables have the same composition.
A.13 (c)
It is sometimes desirable to include within a structure one member, i.e,. a pointer to
the parent structure type. Such structures are known as self-referential structures.
90
These structures are very useful in applications that involve linked data structures,
such as lists and trees. [A linked data structure is not confined to some maximum
number of components. Rather, the data structure can expand or contract in size as
required.]
A.14 (d)
Since all the above structure declarations are valid in C.
A.15 (c)
The above program produces erroneous output (which is machine dependent). In
effect, a union creates a storage location that can be used by any one of its members
at a time. When a different member is assigned a new value, the new value supersedes
the previous member’s value. [NOTE: The compiler allocates a piece of storage
that is large enough to hold the largest variable type in the union, i.e., all members
share the same address.]
A.16 (b)
Since int (2 bytes) + float (4 bytes) = (6 bytes) + Largest among union int (2 bytes)
is equal to (8 bytes). Also, the total number of bytes in the array ‘temp2’ requires
(8 bytes) * (5 bytes) = (40 bytes)
A.17 (b)
The four fields within ‘v’ require a total of 10 bits and these bits can be accommodated
within the first word (16 bits). Unnamed fields can be used to control the
alignment of bit fields within a word of memory. Such fields provide padding within
the word. [NOTE: Some compilers order bit-fields from right-to-left (i.e., from
lower-order bits to higher-order bits) within a word, whereas other compilers order
the fields from left-to-right (higher-order to lower-order bits).
A.18 (c)
a=1 (1 bit: 0 or 1)
b=3 (2 bits: 00 or 01 or 10 or 11),
c=7 (3 bits: 000 or 001 or 010 or 011 or 100 or 101 or 110 or 111)
d=15 (4 bits: 0000 or 0001 or 0010 or 0011 or 0100 or 0101 or 0110 or 0111 or 1000
or 1001 or 1010 or 1011 or 1100 or 1101 or 1110 or 1111)
A.19 (a)
Since we cannot take the address of a bit field variable, i.e., use of pointer to access
the bit fields is prohibited. Also, we cannot use the scanf() function to read values
into a bit field as it requires the address of a bit field variable. Also, array of bitfields
are not permitted and a function cannot return a bit field.
A.20 (d)
Here, the bit field variable ‘a’ will be in the first byte of one word, the variable ‘i’
will be in the second word and the bit fields ‘b’ and ‘c’ will be in the third word. The
variables ‘a’, ‘b’ and ‘c’ would not get packed into the same word. [NOTE: one
word=2 bytes]
91
Structures, Unions, and Enumerations
Q.1 What’s the difference between these two declarations?
struct x1 { ... };
typedef struct { ... } x2;
Ans: The first form declares a ‘structure tag’; the second declares a ‘typedef’. The main
difference is that you subsequently refer to the first type as struct x1 and the second
simply as x2.That is, the second declaration is of a slightly more abstract type-its
users don’t necessarily know that it is a structure, and the keyword struct is not used
when declaring instances of it.
Q.2 Why doesn’t the following code work?
struct x { ... };
x thestruct;
Ans: C is not C++. Typedef names are not automatically generated for structure tags.
Q.3 Can a structure contain a pointer to itself?
Ans: Most certainly.
Q. 4 What’s the best way of implementing opaque (abstract) data types in C?
Ans: One good way is for clients to use structure pointers (perhaps additionally hidden
behind typedefs) which point to structure types which are not publicly defined.
Q.5 I came across some code that declared a structure like this:
struct name {
int namelen;
char namestr[1];
};
and then did some tricky allocation to make the namestr array act like it had several
elements. Is this legal or portable?
Ans: This technique is popular, although Dennis Ritchie has called it ‘unwarranted
chumminess with the C implementation’. An official interpretation has deemed that
it is not strictly conforming to the C Standard, although it does seem to work under
all known implementations. (Compilers which check array bounds carefully might
issue warnings.) Another possibility is to declare the variable-size element very
large, rather than very small; in the case of the above example:
...
char namestr[MAXSIZE];
where MAXSIZE is larger than any name which will be stored. However, it looks
like this technique is disallowed by a strict interpretation of the Standard as well.
Furthermore, either of these ‘chummy’ structures must be used with care, since the
programmer knows more about their size than the compiler does. (In particular,
92
they can generally only be manipulated via pointers.)C9X will introduce the concept
of a ‘flexible array member’, which will allow the size of an array to be omitted
if it is the last member in a structure, thus providing a well-defined solution.
Q.6 Is there a way to compare structures automatically?
Ans: No. There is no single, good way for a compiler to implement implicit structure
comparison (i.e., to support the == operator for structures) which is consistent with
C’s low-level flavor. A simple byte-by-byte comparison could founder on random
bits present in unused ‘holes’ in the structure (such padding is used to keep the
alignment of later fields correct; A field-by-field comparison might require unacceptable
amounts of repetitive code for large structures. If you need to compare two
structures, you’ll have to write your own function to do so, field by field.
Q.7 How can I pass constant values to functions which accept structure arguments?
Ans: As of this writing, C has no way of generating anonymous structure values. You will
have to use a temporary structure variable or a little structure-building function. The
C9X Standard will introduce ‘compound literals’; one form of compound literal
will allow structure constants. For example, to pass a constant coordinate pair to a
plotpoint() function which expects a struct point, you will be able to call
plotpoint((struct point){1, 2});Combined with ‘designated initializers’ (another
C9X feature), it will also be possible to specify member values by
name:plotpoint((struct point){.x=1, .y=2});
Q.8 How can I read/write structures from/to data files?
Ans: It is relatively straightforward to write a structure usingfwrite():fwrite(&somestruct,
sizeof somestruct, 1, fp);
and a corresponding fread() invocation can read it back in. However, data files so
written will *not* be portable. Note also that if the structure contains any pointers,
only the pointer values will be written, and they are most unlikely to be valid when
read back in. Finally, note that for widespread portability you must use the “b” flag
when fopening() the files;
A more portable solution, though it’s a bit more work initially, is to write a pair of
functions for writing and reading a structure, field-by-field, in a portable (perhaps
even human- readable) way.
Q.9 My compiler is leaving holes in structures, which is wasting space and preventing
‘binary’ I/O to external data files. Can I turn off the padding, or otherwise control
the alignment of structure fields?
Ans: Your compiler may provide an extension to give you this control (perhaps a
#pragma; but there is no standard method.
Q.10 Why does sizeof() report a larger size than I expect for a structure type, as if there
were padding at the end?
Ans: Structures may have this padding (as well as internal padding), if necessary, to ensure
that alignment properties will be preserved when an array of contiguous struc93
tures is allocated. Even when the structure is not part of an array, the end padding
remains, so that sizeof() can always return a consistent size.
Q.11 How can I determine the byte offset of a field within a structure?
Ans: ANSI C defines the offsetof() macro, which should be used if available; see <
stddef.h>. If you don’t have it, one possible implementation is #define offsetof(type,
mem) ((size_t) \ ((char *)&((type *)0) > mem - (char *)(type *)0))This implementation
is not 100% portable; some compilers may legitimately refuse to accept it.
Q.12 How can I access structure fields by name at run time?
Ans: Build a table of names and offsets, using the offsetof() macro. The offset of field b
in struct a is
offsetb = offsetof(struct a, b)
If structp is a pointer to an instance of this structure, and field b is an int (with offset
as computed above), b’s value can be set indirectly with
*(int *)((char *)structp + offsetb) = value;
Q.13 This program works correctly, but it dumps core after it finishes. Why?
struct list {
char *item;
struct list *next;
}
/* Here is the main program. */
main(argc, argv)
{ ... }
Ans: A missing semicolon causes main() to be declared as returning a structure. (The
connection is hard to see because of the intervening comment.) Since structurevalued
functions are usually implemented by adding a hidden return pointer, the
generated code for main() tries to accept three arguments, although only two are
passed (in this case, by the C start-up code).
Q.14 Can I initialize unions?
Ans: The current C Standard allows an initializer for the first-named member of a union.
C9X will introduce ‘designated initializers’ which can be used to initialize
any member.
Q.15 What is the difference between an enumeration and a set of preprocessor #defines?
Ans: At the present time, there is little difference. The C Standard says that enumerations
may be freely intermixed with other integral types, without errors. (If, on the other
hand, such intermixing were disallowed without explicit casts, judicious use of enumerations
could catch certain programming errors.)Some advantages of enumerations
are that the numeric values are automatically assigned, that a debugger may be
able to display the symbolic values when enumeration variables are examined, and
that they obey block scope. (A compiler may also generate non-fatal warnings when
enumerations and integers are indiscriminately mixed, since doing so can still be
94
considered bad style even though it is not strictly illegal.) A disadvantage is that the
programmer has little control over those non-fatal warnings; some programmers
also resent not having control over the sizes of enumeration variables.
Q.16 Is there an easy way to print enumeration values symbolically?
Ans: No. You can write a little function to map an enumeration constant to a string. (For
debugging purposes, a good debugger should automatically print enumeration constants
symbolically.
Q.14.What is the output of this program?
struct num
{
int no;
char name[25];
};
void main()
{
struct num n1[]={{25,“rose”},{20,”gulmohar”},{8,“geranium”},{11,“dahalia”}};
printf(“%d%d” ,n1[2].no,(*&n1+2)->no+1);
}
Ans: 8 9
Q15. What is the output of this program?
struct Foo
{
char *pName;
};
main()
{
struct Foo *obj = malloc(sizeof(struct Foo));
clrscr();
strcpy(obj->pName,“Your Name”);
printf(“%s”, obj->pName);}.
Ans: Your Name
Q16. What is the output of this program?
struct Foo
{
char *pName;
char *pAddress;
};
main()
{
95
struct Foo *obj = malloc(sizeof(struct Foo));
clrscr();
obj->pName = malloc(100);
obj->pAddress = malloc(100);
strcpy(obj->pName,“Your Name”);
strcpy(obj->pAddress, “Your Address”);
free(obj);
printf(“%s”, obj->pName);
printf(“%s”, obj->pAddress);
}
Ans: printd Nothing, as after free(obj), no memory is there containing
obj->pName & pbj->pAddress
Q.17 What is the output of this program?
main()
{
char *a = “Hello ”;
char *b = “World”;
clrscr();
printf(“%s”, strcat(a,b));
}
Ans: Hello World
Q.18 What is the output of this program?
main()
{
char *a = “Hello”;
char *b = “World”;
clrscr();
printf(“%s”, strcpy(a,b));
}
Ans: World, copies World on a, overwrites Hello in a.
Q.19 What is the output of this program?
union u
{
struct st
{
int i : 4;
int j : 4;
int k : 4;
int l;
96
}st;
int i;
}u;
main()
{
u.i = 100;
printf(“%d, %d, %d”,u.i, u.st.i, u.st.l);
}
Ans: 100, 4, 0
Q.20 What is the output of this program?
union u
{
union u
{
int i;
int j;
}a[10];
int b[10];
}u;
main()
{
printf(“n%d”, sizeof(u));
printf(“ %d”, sizeof(u.a));
// printf(“%d”, sizeof(u.a[4].i));
}
Ans: 20, 200, error for 3rd printf()
Structures and Unions
Q.1 What will be the output of the following program?
struct {
int i;
float f;
}var;
int main()
{
var.i=5;
var.f=9.76723;
printf("%d %.2f”,var.i,var.f);
return(0);
}
(a) Compile-Time Error (b) 5 9.76723
(c) 5 9.76 (d) 5 9.77
Q.2 What will be the output of the following program?
int main()
{
int val=1234;
int* ptr=&val;
printf(“%d %d”,val,*ptr++);
return(0);
}
(a) Compile-Time Error (b) 5 9.76723
(c) 5 9.76 (d) 5 9.77
Q.3 What will be the output of the following program?
struct values {
int i;
float f;
};
int main()
{
struct values var={555,67.05501};
printf(“%2d %.2f”,var.i,var.f);
return(0);
}
80
(a) 1234 1234 (b) 1235 1235
(c) 1234 1235 (d) 1235 1234
Q.4 What will be the output of the following program?
typedef struct {
int i;
float f;
}values;
int main()
{
static values var={555,67.05501};
printf(“%2d %.2f”,var.i,var.f);
return(0);
}
(a) Compile-Time Error (b) 55 67.05
(c) 555 67.06 (d) 555 67.05
Q.5 What will be the output of the following program?
struct my_struct {
int i=7;
float f=999.99;
}var;
int main()
{
var.i=5;
printf(“%d %.2f”,var.i,var.f);
return(0);
(a) Compile-Time Error (b) 7 999.99
(c) 5 999.99 (d) None of these
Q.6 What will be the output of the following program?
struct first {
int a;
float b;
}s1={32760,12345.12345};
typedef struct {
char a;
int b;
}second;
struct my_struct {
float a;
unsigned int b;
81
};
typedef struct my_struct third;
int main()
{
static second s2={‘A’,- -4};
third s3;
s3.a=~(s1.a-32760);
s3.b=-++s2.b;
printf(“%d %.2f\n%c %d\n%.2f %u”,(s1.a)--,s1.b+0.005,s2.a+32,s2.b,++(s3.a),--
s3.b);
return(0);
}
(a) Compile-Time Error
(b) 32760 12345.12
A 4
1 -5
(c) 32760 12345.13
a -5
0.00 65531
(d) 32760 12345.13
a 5
0.00 65530
Q.7 What will be the output of the following program?
struct {
int i,val[25];
}var={1,2,3,4,5,6,7,8,9},*vptr=&var;
int main()
{
printf(“%d %d %d\n”,var.i,vptr->i,(*vptr).i);
printf(“%d %d %d %d %d %d”,var.val[4],*(var.val+4),vptr-
>val[4],*(vptr->val+4),(*vptr).val[4],*((*vptr).val+4));
return(0);
}
(a) Compile-Time Error
(b) 1 1 1
6 6 6 6 6 6
(c) 1 1 1
5 5 5 5 5 5
(d) None of these
82
Q.8 What will be the output of the following program?
typedef struct {
int i;
float f;
}temp;
void alter(temp *ptr,int x,float y)
{
ptr->i=x;
ptr->f=y;
}
int main()
{
temp a={111,777.007};
printf(“%d %.2f\n”,a.i,a.f);
alter(&a,222,666.006);
printf(“%d %.2f”,a.i,a.f);
return(0);
}
(a) Compile-Time error
(b) 111 777.007
222 666.006
(c) 111 777.01
222 666.01
(d) None of these
Q.9 What will be the output of the following program?
typedef struct {
int i;
float f;
}temp;
temp alter(temp tmp,int x,float y)
{
tmp.i=x;
tmp.f=y;
return tmp;
}
int main()
{
temp a={111,777.007};
83
printf(“%d %.3f\n”,a.i,a.f);
a=alter(a,222,666.006);
printf(“%d %.3f”,a.i,a.f);
return(0);
}
(a) Compile-Time error
(b) 111 777.007
222 666.006
(c) 111 777.01
222 666.01 (d)None of these
Q.10 What will be the output of the following program?
typedef struct {
int i;
float f;
}temp;
temp alter(temp *ptr,int x,float y)
{
temp tmp=*ptr;
printf(“%d %.2f\n”,tmp.i,tmp.f);
tmp.i=x;
tmp.f=y;
return tmp;
}
int main()
{
temp a={65535,777.777};
a=alter(&a,-1,666.666);
printf(“%d %.2f”,a.i,a.f);
return(0);
}
(a) Compile-Time error
(b) 65535 777.777
-1 666.666
(c) 65535 777.78
-1 666.67
(d) -1 777.78
-1 666.67
Q.11 What will be the output of the following program?
struct my_struct1 {
int arr[2][2];
84
};
typedef struct my_struct1 record;
struct my_struct2 {
record temp;
}list[2]={1,2,3,4,5,6,7,8};
int main()
{
int i,j,k;
for (i=1; i>=0; i--)
for (j=0; j<2; j++)
for (k=1; k>=0; k--)
printf(“%d”,list[i].temp.arr[j][k]);
return(0);
}
(a) Compile-Time Error (b) Run-Time Error
(c) 65872143 (d) 56781243
Q.12 What will be the output of the following program?
struct my_struct {
int i;
unsigned int j;
};
int main()
{
struct my_struct temp1={-32769,-1},temp2;
temp2=temp1;
printf(“%d %u”,temp2.i,temp2.j);
return(0);
}
(a) 32767 -1 (b) -32769 -1
(c) -32769 65535 (d) 32767 65535
Q.14 What will be the output of the following program?
struct names {
char str[25];
struct names *next;
};
typedef struct names slist;
int main()
{
slist *list,*temp;
85
list=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(list->str,“Hai”);
list->next=NULL;
temp=(slist *)malloc(sizeof(slist)); // Dynamic Memory Allocation
strcpy(temp->str,“Friends”);
temp->next=list;
list=temp;
while (temp != NULL)
{
printf(“%s”,temp->str);
temp=temp->next;
}
return(0);
}
(a) Compile-Time Error (b) HaiFriends
(c) FriendsHai (d) None of these
Q.14 What will be the output of the following program?
(i) struct A {
int a;
struct B {
int b;
struct B *next;
}tempB;
struct A *next;
}tempA;
(ii) struct B {
int b;
struct B *next;
};
struct A {
int a;
struct B tempB;
struct A *next;
};
(iii) struct B {
int b;
}tempB;
86
struct {
int a;
struct B *nextB;
};
(iv) struct B {
int b;
struct B {
int b;
struct B *nextB;
}tempB;
struct B *nextB;
}tempB;
(a) (iv) Only (b) (iii) Only
(c) All of the these (d) None of these
Q.15 What will be the output of the following program?
union A {
char ch;
int i;
float f;
}tempA;
int main()
{
tempA.ch=‘A’;
tempA.i=777;
tempA.f=12345.12345;
printf(“%d”,tempA.i);
return(0);
}
(a) Compile-Time Error (b) 12345
(c) Erroneous output (d) 777
Q.16 What will be the output of the following program?
struct A {
int i;
float f;
union B {
char ch;
int j;
87
}temp;
}temp1;
int main()
{
struct A temp2[5];
printf(“%d %d”,sizeof temp1,sizeof(temp2));
return(0);
}
(a)6 30 (b)8 40 (c)9 45 (d)None of these
Q.17 What will be the output of the following program?
int main()
{
static struct my_struct {
unsigned a:1;
unsigned b:2;
unsigned c:3;
unsigned d:4;
unsigned :6; // Fill out first word
}v={1,2,7,12};
printf(“%d %d %d %d“,v.a,v.b,v.c,v.d);
printf(“\nSize=%d bytes”,sizeof v);
return(0);
}
(a) Compile-Time Error
(b) 1 2 7 12
Size=2 bytes
(c) 1 2 7 12
Size=4 bytes
(d)None of these
Q.18 What are the largest values that can be assigned to each of the bit fields defined in
Q.17 above?
(a) a=0 b=2 c=3 d=4 (b) a=1 b=2 c=7 d=15
(c) a=1 b=3 c=7 d=15 (d) None of these
Q.19 What will be the output of the following program?
int main()
{
struct sample {
unsigned a:1;
unsigned b:4;
88
}v={0,15};
unsigned *vptr=&v.b;
printf(“%d %d”,v.b,*vptr);
return(0);
}
(a) Compile-Time Error (b) 0 0
(c) 15 15 (d) None of these
Q.20 What will be the output of the following program?
int main()
{
static struct my_struct {
unsigned a:1;
int i;
unsigned b:4;
unsigned c:10;
}v={1,10000,15,555};
printf(“%d %d %d %d”,v.i,v.a,v.b,v.c);
printf(“\nSize=%d bytes”,sizeof v);
return(0);
}
(a) Compile-Time Error
(b) 1 10000 15 555
Size=4 bytes
(c) 10000 1 15 555
Size=4 bytes
(d) 10000 1 15 555
Size=5 bytes
Solutions
A.1 (d)
Though both
two must appear. In the above program,
decimal places or) 2-digit precision of 9.76723 is 9.77
A.2 (d)
Both
defined in the above program has no use and the program executes in the normal way.
A.3 (c)
The members of a structure variable can be assigned initial values in much the same
89
manner as the elements of an array. The initial values must appear in order in which
they will be assigned to their corresponding structure members, enclosed in braces
and separated by commas.
A.4 (c)
In the above program, values is the user-defined structure type or the new user-defined
data type. Structure variables can then be defined in terms of the new data type.
A.5 (a)
The C language does not permit the initialization of individual structure members
within the template. The initialization must be done only in the declaration of the
actual variables. The correct way to initialize the values is shown in A.3 or A.4.
A.6 (d)
Illustrating 3 different ways of declaring the structures: first, second and third are
the user-defined structure type. s1, s2 and s3 are structure variables. Also, an expression
of the form ++variable.member is equivalent to ++(variable.member), i.e.,
the ++ operator will apply to the structure member, not the entire structure variable.
A.7 (b)
The value of the member ‘i’ can be accessed using var.i, vptr->i and (*vptr).i Similarly,
the 5th value of the member ‘val’ can be accessed using var.val[4],
*(var.val+4), vptr->val[4], *(vptr->val+4), (*vptr).val[4] and *((*vptr).val+4)
A.8 (c)
This program illustrates the transfer of a structure to a function by passing the
structure‘s address (a pointer) to the function.
A.9 (b)
This program illustrates the transfer of a structure to a function by value. Also, the
altered structure is now returned directly to the calling portion of the program.
A.10 (d)
This program illustrates the transfer of a structure to a function by passing the
structure’s address (a pointer) to the function. Also, the altered structure is now
returned directly to the calling portion of the program.
A.11 (c)
This program illustrates the implementation of a nested structure, i.e., a structure
inside another structure.
A.12(d)
An entire structure variable can be assigned to another structure variable, provided
both variables have the same composition.
A.13 (c)
It is sometimes desirable to include within a structure one member, i.e,. a pointer to
the parent structure type. Such structures are known as self-referential structures.
90
These structures are very useful in applications that involve linked data structures,
such as lists and trees. [A linked data structure is not confined to some maximum
number of components. Rather, the data structure can expand or contract in size as
required.]
A.14 (d)
Since all the above structure declarations are valid in C.
A.15 (c)
The above program produces erroneous output (which is machine dependent). In
effect, a union creates a storage location that can be used by any one of its members
at a time. When a different member is assigned a new value, the new value supersedes
the previous member’s value. [NOTE: The compiler allocates a piece of storage
that is large enough to hold the largest variable type in the union, i.e., all members
share the same address.]
A.16 (b)
Since int (2 bytes) + float (4 bytes) = (6 bytes) + Largest among union int (2 bytes)
is equal to (8 bytes). Also, the total number of bytes in the array ‘temp2’ requires
(8 bytes) * (5 bytes) = (40 bytes)
A.17 (b)
The four fields within ‘v’ require a total of 10 bits and these bits can be accommodated
within the first word (16 bits). Unnamed fields can be used to control the
alignment of bit fields within a word of memory. Such fields provide padding within
the word. [NOTE: Some compilers order bit-fields from right-to-left (i.e., from
lower-order bits to higher-order bits) within a word, whereas other compilers order
the fields from left-to-right (higher-order to lower-order bits).
A.18 (c)
a=1 (1 bit: 0 or 1)
b=3 (2 bits: 00 or 01 or 10 or 11),
c=7 (3 bits: 000 or 001 or 010 or 011 or 100 or 101 or 110 or 111)
d=15 (4 bits: 0000 or 0001 or 0010 or 0011 or 0100 or 0101 or 0110 or 0111 or 1000
or 1001 or 1010 or 1011 or 1100 or 1101 or 1110 or 1111)
A.19 (a)
Since we cannot take the address of a bit field variable, i.e., use of pointer to access
the bit fields is prohibited. Also, we cannot use the scanf() function to read values
into a bit field as it requires the address of a bit field variable. Also, array of bitfields
are not permitted and a function cannot return a bit field.
A.20 (d)
Here, the bit field variable ‘a’ will be in the first byte of one word, the variable ‘i’
will be in the second word and the bit fields ‘b’ and ‘c’ will be in the third word. The
variables ‘a’, ‘b’ and ‘c’ would not get packed into the same word. [NOTE: one
word=2 bytes]
91
Structures, Unions, and Enumerations
Q.1 What’s the difference between these two declarations?
struct x1 { ... };
typedef struct { ... } x2;
Ans: The first form declares a ‘structure tag’; the second declares a ‘typedef’. The main
difference is that you subsequently refer to the first type as struct x1 and the second
simply as x2.That is, the second declaration is of a slightly more abstract type-its
users don’t necessarily know that it is a structure, and the keyword struct is not used
when declaring instances of it.
Q.2 Why doesn’t the following code work?
struct x { ... };
x thestruct;
Ans: C is not C++. Typedef names are not automatically generated for structure tags.
Q.3 Can a structure contain a pointer to itself?
Ans: Most certainly.
Q. 4 What’s the best way of implementing opaque (abstract) data types in C?
Ans: One good way is for clients to use structure pointers (perhaps additionally hidden
behind typedefs) which point to structure types which are not publicly defined.
Q.5 I came across some code that declared a structure like this:
struct name {
int namelen;
char namestr[1];
};
and then did some tricky allocation to make the namestr array act like it had several
elements. Is this legal or portable?
Ans: This technique is popular, although Dennis Ritchie has called it ‘unwarranted
chumminess with the C implementation’. An official interpretation has deemed that
it is not strictly conforming to the C Standard, although it does seem to work under
all known implementations. (Compilers which check array bounds carefully might
issue warnings.) Another possibility is to declare the variable-size element very
large, rather than very small; in the case of the above example:
...
char namestr[MAXSIZE];
where MAXSIZE is larger than any name which will be stored. However, it looks
like this technique is disallowed by a strict interpretation of the Standard as well.
Furthermore, either of these ‘chummy’ structures must be used with care, since the
programmer knows more about their size than the compiler does. (In particular,
92
they can generally only be manipulated via pointers.)C9X will introduce the concept
of a ‘flexible array member’, which will allow the size of an array to be omitted
if it is the last member in a structure, thus providing a well-defined solution.
Q.6 Is there a way to compare structures automatically?
Ans: No. There is no single, good way for a compiler to implement implicit structure
comparison (i.e., to support the == operator for structures) which is consistent with
C’s low-level flavor. A simple byte-by-byte comparison could founder on random
bits present in unused ‘holes’ in the structure (such padding is used to keep the
alignment of later fields correct; A field-by-field comparison might require unacceptable
amounts of repetitive code for large structures. If you need to compare two
structures, you’ll have to write your own function to do so, field by field.
Q.7 How can I pass constant values to functions which accept structure arguments?
Ans: As of this writing, C has no way of generating anonymous structure values. You will
have to use a temporary structure variable or a little structure-building function. The
C9X Standard will introduce ‘compound literals’; one form of compound literal
will allow structure constants. For example, to pass a constant coordinate pair to a
plotpoint() function which expects a struct point, you will be able to call
plotpoint((struct point){1, 2});Combined with ‘designated initializers’ (another
C9X feature), it will also be possible to specify member values by
name:plotpoint((struct point){.x=1, .y=2});
Q.8 How can I read/write structures from/to data files?
Ans: It is relatively straightforward to write a structure usingfwrite():fwrite(&somestruct,
sizeof somestruct, 1, fp);
and a corresponding fread() invocation can read it back in. However, data files so
written will *not* be portable. Note also that if the structure contains any pointers,
only the pointer values will be written, and they are most unlikely to be valid when
read back in. Finally, note that for widespread portability you must use the “b” flag
when fopening() the files;
A more portable solution, though it’s a bit more work initially, is to write a pair of
functions for writing and reading a structure, field-by-field, in a portable (perhaps
even human- readable) way.
Q.9 My compiler is leaving holes in structures, which is wasting space and preventing
‘binary’ I/O to external data files. Can I turn off the padding, or otherwise control
the alignment of structure fields?
Ans: Your compiler may provide an extension to give you this control (perhaps a
#pragma; but there is no standard method.
Q.10 Why does sizeof() report a larger size than I expect for a structure type, as if there
were padding at the end?
Ans: Structures may have this padding (as well as internal padding), if necessary, to ensure
that alignment properties will be preserved when an array of contiguous struc93
tures is allocated. Even when the structure is not part of an array, the end padding
remains, so that sizeof() can always return a consistent size.
Q.11 How can I determine the byte offset of a field within a structure?
Ans: ANSI C defines the offsetof() macro, which should be used if available; see <
stddef.h>. If you don’t have it, one possible implementation is #define offsetof(type,
mem) ((size_t) \ ((char *)&((type *)0) > mem - (char *)(type *)0))This implementation
is not 100% portable; some compilers may legitimately refuse to accept it.
Q.12 How can I access structure fields by name at run time?
Ans: Build a table of names and offsets, using the offsetof() macro. The offset of field b
in struct a is
offsetb = offsetof(struct a, b)
If structp is a pointer to an instance of this structure, and field b is an int (with offset
as computed above), b’s value can be set indirectly with
*(int *)((char *)structp + offsetb) = value;
Q.13 This program works correctly, but it dumps core after it finishes. Why?
struct list {
char *item;
struct list *next;
}
/* Here is the main program. */
main(argc, argv)
{ ... }
Ans: A missing semicolon causes main() to be declared as returning a structure. (The
connection is hard to see because of the intervening comment.) Since structurevalued
functions are usually implemented by adding a hidden return pointer, the
generated code for main() tries to accept three arguments, although only two are
passed (in this case, by the C start-up code).
Q.14 Can I initialize unions?
Ans: The current C Standard allows an initializer for the first-named member of a union.
C9X will introduce ‘designated initializers’ which can be used to initialize
any member.
Q.15 What is the difference between an enumeration and a set of preprocessor #defines?
Ans: At the present time, there is little difference. The C Standard says that enumerations
may be freely intermixed with other integral types, without errors. (If, on the other
hand, such intermixing were disallowed without explicit casts, judicious use of enumerations
could catch certain programming errors.)Some advantages of enumerations
are that the numeric values are automatically assigned, that a debugger may be
able to display the symbolic values when enumeration variables are examined, and
that they obey block scope. (A compiler may also generate non-fatal warnings when
enumerations and integers are indiscriminately mixed, since doing so can still be
94
considered bad style even though it is not strictly illegal.) A disadvantage is that the
programmer has little control over those non-fatal warnings; some programmers
also resent not having control over the sizes of enumeration variables.
Q.16 Is there an easy way to print enumeration values symbolically?
Ans: No. You can write a little function to map an enumeration constant to a string. (For
debugging purposes, a good debugger should automatically print enumeration constants
symbolically.
Q.14.What is the output of this program?
struct num
{
int no;
char name[25];
};
void main()
{
struct num n1[]={{25,“rose”},{20,”gulmohar”},{8,“geranium”},{11,“dahalia”}};
printf(“%d%d” ,n1[2].no,(*&n1+2)->no+1);
}
Ans: 8 9
Q15. What is the output of this program?
struct Foo
{
char *pName;
};
main()
{
struct Foo *obj = malloc(sizeof(struct Foo));
clrscr();
strcpy(obj->pName,“Your Name”);
printf(“%s”, obj->pName);}.
Ans: Your Name
Q16. What is the output of this program?
struct Foo
{
char *pName;
char *pAddress;
};
main()
{
95
struct Foo *obj = malloc(sizeof(struct Foo));
clrscr();
obj->pName = malloc(100);
obj->pAddress = malloc(100);
strcpy(obj->pName,“Your Name”);
strcpy(obj->pAddress, “Your Address”);
free(obj);
printf(“%s”, obj->pName);
printf(“%s”, obj->pAddress);
}
Ans: printd Nothing, as after free(obj), no memory is there containing
obj->pName & pbj->pAddress
Q.17 What is the output of this program?
main()
{
char *a = “Hello ”;
char *b = “World”;
clrscr();
printf(“%s”, strcat(a,b));
}
Ans: Hello World
Q.18 What is the output of this program?
main()
{
char *a = “Hello”;
char *b = “World”;
clrscr();
printf(“%s”, strcpy(a,b));
}
Ans: World, copies World on a, overwrites Hello in a.
Q.19 What is the output of this program?
union u
{
struct st
{
int i : 4;
int j : 4;
int k : 4;
int l;
96
}st;
int i;
}u;
main()
{
u.i = 100;
printf(“%d, %d, %d”,u.i, u.st.i, u.st.l);
}
Ans: 100, 4, 0
Q.20 What is the output of this program?
union u
{
union u
{
int i;
int j;
}a[10];
int b[10];
}u;
main()
{
printf(“n%d”, sizeof(u));
printf(“ %d”, sizeof(u.a));
// printf(“%d”, sizeof(u.a[4].i));
}
Ans: 20, 200, error for 3rd printf()
PPL FAQS for JNTU BTECH CSE students (I mid and external Unit wise)
PRINCIPLES OF PROGRAMMING LANGUAGES -UNIT-I
1)Explain about the reasons for studying Programming Language
2a) What are the advantages of studying Programming Language
b) Explain about Programming domains
3) Explain about the Language Evaluation Criteria in detail
4) Explain about the influences in Language Design
5)Differentiate between Object oriented Language anf Function oriented language
6)Explain about the Imperative and Logic Programming Language in detail
7)Explain about the Compilation and Virtual Machines
8) Explain about the Programming Environments
PRINCIPLES OF PROGRAMMING LANGUAGES -UNIT-II
1a) What do you understand by syntax and semantics
b) Explain about general problem of describing syntax
2)Explain the Formal methods of describing syntax
3)Explain about static semantics and Attribute Grammars along with examples
4a)Write about Intrinsic Grammars
b)Write the Attribute Grammar for simple Assignment statements
5)Explain about the computing Attribute values and flow of attributes in the parse tree
6)Explain about Operational Semantics in detail
7)Explain about Axiomatic Semantics in detail
8)Write the Attribute Grammar for Simple if – statement
9)Explain about Denotional Semantics in detail
PRINCIPLES OF PROGRAMMING LANGUAGES -UNIT-III
1) Explain about the primitive data types in detail
2) Explain about the Character String Types in detail
3) Explain about the User defined Ordinal types in detail
4) Explain about the primitive data types in detail
5a) Explain about Arrays and Indices
b) Explain about the Design Issues for Array data types detail
6) Explain about Array Subscript Bindings and Array Categories
7) Explain about Array Initialization and Array Operations
8) Explain about Implementation of Array Types
9) Explain about Associative Arrays in detail
10) Explain about Record Types in detail
11) Explain about Union Types in detail
12a)Explain about Design Issues of Pointers and Pointer Operations
b)Explain about problems in Pointers in detail
13)Explain the pointers in ADA,C and C++
14)Explain about the implementation of pointer and Reference Types
15)Explain about the Names and their design issues
16)Explain the concept of variable in detail
17)Explain about the type bindings and type inference
18)Explain about the storage binding and lifetime
19)Explain the Type checking and strong typing
20)Explain about the Type Equivalence
21)Explain the Evaluation of Static Scoping
22)Explain the Evaluation of Dynamic Scoping
23a)Explain about the Scope and Life time
b)Explain about the Referencing Environments
24)Explain about the named constants
UNIT IV
1) Explain about the Operator Evaluation order in Arithmetic Expressions
2) Explain about the Operand Evaluation order in Arithmetic Expressions
3) Explain about the Overloaded Operators
4) Explain about the Type Conversions in detail
5) Explain about the Relational and Boolean Expressions in detail
6) Explain about the Short Circuit Evaluation
7) Explain about the Assignment Statements in detail
8) Explain about the Mixed-mode Assignment in detail
9) Explain about the two way Selection statements
10) Explain about the Multiple- Selection Constructs
11) Explain the ‘DO’ Statement of FORTRAN 95
12) Explain the ‘for’ Statement of Ada Language
13) Explain the ‘for’ Statement of C based Languages
14) Explain the ‘for’ Statement of Python Language
15) Explain the Logically Controlled Loops
16) Explain the User-Located Loop Control Mechanisms
17) Explain the Iteration Based on Data Structures
18) Explain the Unconditional Branching
19) Explain about the Guarded Commands
20a)What is the purpose of a compound Assignment operator
b)What is the associativity of C’s unary arithmetic operators
1)Explain about the reasons for studying Programming Language
2a) What are the advantages of studying Programming Language
b) Explain about Programming domains
3) Explain about the Language Evaluation Criteria in detail
4) Explain about the influences in Language Design
5)Differentiate between Object oriented Language anf Function oriented language
6)Explain about the Imperative and Logic Programming Language in detail
7)Explain about the Compilation and Virtual Machines
8) Explain about the Programming Environments
PRINCIPLES OF PROGRAMMING LANGUAGES -UNIT-II
1a) What do you understand by syntax and semantics
b) Explain about general problem of describing syntax
2)Explain the Formal methods of describing syntax
3)Explain about static semantics and Attribute Grammars along with examples
4a)Write about Intrinsic Grammars
b)Write the Attribute Grammar for simple Assignment statements
5)Explain about the computing Attribute values and flow of attributes in the parse tree
6)Explain about Operational Semantics in detail
7)Explain about Axiomatic Semantics in detail
8)Write the Attribute Grammar for Simple if – statement
9)Explain about Denotional Semantics in detail
PRINCIPLES OF PROGRAMMING LANGUAGES -UNIT-III
1) Explain about the primitive data types in detail
2) Explain about the Character String Types in detail
3) Explain about the User defined Ordinal types in detail
4) Explain about the primitive data types in detail
5a) Explain about Arrays and Indices
b) Explain about the Design Issues for Array data types detail
6) Explain about Array Subscript Bindings and Array Categories
7) Explain about Array Initialization and Array Operations
8) Explain about Implementation of Array Types
9) Explain about Associative Arrays in detail
10) Explain about Record Types in detail
11) Explain about Union Types in detail
12a)Explain about Design Issues of Pointers and Pointer Operations
b)Explain about problems in Pointers in detail
13)Explain the pointers in ADA,C and C++
14)Explain about the implementation of pointer and Reference Types
15)Explain about the Names and their design issues
16)Explain the concept of variable in detail
17)Explain about the type bindings and type inference
18)Explain about the storage binding and lifetime
19)Explain the Type checking and strong typing
20)Explain about the Type Equivalence
21)Explain the Evaluation of Static Scoping
22)Explain the Evaluation of Dynamic Scoping
23a)Explain about the Scope and Life time
b)Explain about the Referencing Environments
24)Explain about the named constants
UNIT IV
1) Explain about the Operator Evaluation order in Arithmetic Expressions
2) Explain about the Operand Evaluation order in Arithmetic Expressions
3) Explain about the Overloaded Operators
4) Explain about the Type Conversions in detail
5) Explain about the Relational and Boolean Expressions in detail
6) Explain about the Short Circuit Evaluation
7) Explain about the Assignment Statements in detail
8) Explain about the Mixed-mode Assignment in detail
9) Explain about the two way Selection statements
10) Explain about the Multiple- Selection Constructs
11) Explain the ‘DO’ Statement of FORTRAN 95
12) Explain the ‘for’ Statement of Ada Language
13) Explain the ‘for’ Statement of C based Languages
14) Explain the ‘for’ Statement of Python Language
15) Explain the Logically Controlled Loops
16) Explain the User-Located Loop Control Mechanisms
17) Explain the Iteration Based on Data Structures
18) Explain the Unconditional Branching
19) Explain about the Guarded Commands
20a)What is the purpose of a compound Assignment operator
b)What is the associativity of C’s unary arithmetic operators
Saturday, May 2, 2009
C Programming Exercises for JNTU BTECH STUDENTS
UNIT-I
1) Write a program to illustrate bitwise operators.
2) Write a program to print all the alphabets and their equivalent ASCII values.
3) Write a program to calculate the sum of squares of first n natural numbers using while loop.
4) Write a program to illustrate short hand operators used in C.
5) Write a program to print the multiplication table in the following format.
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
6) Write a program to calculate the factorial of a given number.
7) Write a program to calculate sum of squares of cubes of first n natural numbers.
8) Write a program to reverse the given number.
9) Write a program to calculate mn value using do-while loop.
10)Write a program to check whether the given number is a palindrome or not.
11)Write a program to check whether the given number is an Amstrong number or not.
12)Write a program to check whether the given number is a perfect number or not.
13)Write a program to print the following format.
*
* *
* * *
* * * *
* * * * *
14) Program to calculate lucky number.
15)Write a program to calculate the result of the series accurate up to 7th digit.
x+x3/3!+x5/5!+……………….
16) An electric power distribution company charges its domestic consumers as follows. Consumption Units Rate of Charge 0-200 Rs.0.50 per unit 201-400 Rs.100 plus Rs.0.65 per unit excess 200 401-600 Rs.230 plus Rs.0.80 per unit excess of 400. Write a C program that reads the customer number and power consumed and prints the amount to be paid by the customer.
17) Program to find the roots of the quadratic equation using bisection method.
UNIT-II
18)Write a program to print the elements of an array in reverse order.
19)Write a program to add all the elements of a two dimensional array.
20)Write a program to find the transpose of a given matrix.
21)Write a program to find the smallest and largest element in a two dimensional array.
22)Write a program to illustrate call by value.
23)Write a function to calculate the factorial of a given number.
24)Write a function to sort the elements of an array.
25)Write a recursive function to find the roots of the quadratic expression using bisection method.
26)Write a program to copy string to another without using string handling functions.
27)Write a program to check whether a given string is a palindrome or not.
28)Write a function to find the largest element in an array.
29)Write a recursive function power (base, exponent) that when invoked returns base exponent.
30) Program to sort the characters in a given string.
31)Write a function to convert all the upper case letters to lower case and lower case letters to upper case in a given string.
32) Program to undefined an already defined macro.
33) Program to illustrate conditional compilation using #if, #end if, #else.
UNIT-III
34)Write a program to calculate length of the string using pointers.
35)Write a function to swap two variables using pointers.
36) Program to illustrate pointer arithmetic.
37)Write a function to calculate sum of two numbers using pointers to functions.
38)Write a program to perform matrix multiplication using pointers.
39)Write a program to find the largest in an array using pointers.
40) Program to arrange the given numbers in ascending order.
41)Write a C program using pointer for string comparison.
42) Program to reverse the string using pointers.
43) Program to perform sorting using command line arguments.
44) The names of employees of an organization are stored in three arrays, namely, first name, second name, and last name. Write a program using pointers to concatenate the three parts into one string to be called name.
45) Program to merge two sorted arrays so that the resultant array is in sorted order using pointers.
UNIT-IV
46) Write a C program to compute the monthly pay of 100 employees using each employee’s name, basic-pay. The DA is computed as 52% of the basic pay. Gross-salary (Basic-pay+DA).Print the employees name and gross salary.
47)Write a program to calculate and print student wise total for 50 students and 3 subjects. The structure should contain 3 subjects and total.
48)Write a program to calculate and print student wise total for 50 students and 3 subjects using pointers. The structure should contain 3 subjects.
49)Write a program to store the information of vehicles use bit fields to store the status information. Assume the vehicle object consists of type, fuel and model member fields. Assume appropriate number of bits for each field.
50) Program for illustration of user defined data types using typedef.
51) Program for illustration of nested structures.
52) Program to add or delete a record of a particular employee based on his code. The structure should contain name, designation, code, salary. Program should also provide the flexibility to update any employees record.
53) Define a structure that represent a complex number (contains two floating point members, called real and imaginary). Write functions to add, subtract, multiply and divide two complex numbers.
54) A company markets Hardware items. Create a structure “hwitem” that stores the title of the item, it’s price, an array of three floats so that it can record the sale in rupees of a particular item for the last three months, category of the item and it’s original equipment manufacturer. Write a short program that provides facility to read N number of items information, append new item, and displays all records.
55) Define a structure to represent a data. Use your structures that accept two different dates in the format mmdd of the same year. And do the following: Write a C program to display the month names of both dates.
56) Consider a structure master include the information like name, code, pay, experience write a program to delete and display the information contained in master variables for a given code.
57)Write a program to use structure within union. Display the contents of structure elements.
UNIT-V
58)Write a C program to read a text file and to count Number of characters, Number of words and Number of sentences and write in an output file.
59)Write a C program to read the text file containing some paragraph. Use fseek() and read the text after skipping n characters from the beginning of the file.
60)Write a C program to read the text file containing some paragraph. Print the first n characters from the beginning of the file.
61)Write a program to print the output of the following format in an OUTPUT file.
Number Square Cube
2 4 8
3 9 27
62)Write a C program to read data from a file named INPUT containing numbers. Write all even numbers to file called EVEN and odd numbers to file called ODD.
63) Program to print the n characters from mth position in the file.
64) Program to print the current position value of the file pointer.
65) Program to check whether end of file has been reached or not using feof() function.
66) Program to check whether any data errors are present and print the relevant information using ferror() function.
67)Write a program to detect error while opening a file that does not exist.
68)Write a C program to open a pre-existing file and add information at the end of file. Display the contents of the file before and after appending
69) Program to copy one file to another.
70)Write program to read a text file and convert the file contents in capital (upper-case) and write the contents in an output file.
71) Candidates have to score 90 or above in the IQ test to be considered eligible for taking further tests. All candidates who do not clear the IQ test are sent reject letters
and others are sent call letters for further tests. The selected list and other conversation have to be written to file.
UNIT-VI
72)Write a non-recursive simulation of towers of Hanoi.
73)Write recursive function for towers of Hanoi.
74) Program to convert Prefix to postfix using stacks.
75) Program to convert postfix expression to prefix expression.
76)Write a program to evaluate postfix expression
77) Program to evaluate prefix expression.
78)Write a program to implement dequeues
79) Program to implement priority queues.
80) Program to check whether a given string is palindrome or not using stacks.
UNIT-VII
81)Write a program in ‘C’ to form a list consisting the intersection of the elements of two lists
82)Write a routine to reverse elements of a doubly linked list by traversing the list only once
83)Write a program to count the number of non-leaf nodes in a binary tree
84)Write a program to count the number of leaf nodes in a binary tree
85)Write a program to implement circular using double linked list.
86)Write a program to perform polynomial addition.
87)Write a program to perform polynomial multiplication.
88) Function to reverse the single linked list.
89)Write non recursive functions for binary tree traversals.
90) Program to implement binary search tree using arrays.
91)Write a C program to exchange two nodes of a singly linked list.
92) Program to implement breadth first search.
93) Program to implement depth first search.
94) Represent a singly linked list using an array. Write routines to insert and delete elements for this representation.
95)Write a routine SPLIT() to split a singly linked list into two lists so that all elements in odd position are in one list and those in even position are in another list.
96) Represent a doubly linked list using an array. Write routines to insert and delete elements for this representation.
97)Write a function to merge to doubly linked lists and arrange the numbers in ascending order.
UNIT-VIII
98)Write a program to implement merge sort
99) Derive the efficiency of merge sort
100) Derive the efficiency of binary search.
1) Write a program to illustrate bitwise operators.
2) Write a program to print all the alphabets and their equivalent ASCII values.
3) Write a program to calculate the sum of squares of first n natural numbers using while loop.
4) Write a program to illustrate short hand operators used in C.
5) Write a program to print the multiplication table in the following format.
1 2 3 4 5
1 1 2 3 4 5
2 2 4 6 8 10
3 3 6 9 12 15
4 4 8 12 16 20
5 5 10 15 20 25
6) Write a program to calculate the factorial of a given number.
7) Write a program to calculate sum of squares of cubes of first n natural numbers.
8) Write a program to reverse the given number.
9) Write a program to calculate mn value using do-while loop.
10)Write a program to check whether the given number is a palindrome or not.
11)Write a program to check whether the given number is an Amstrong number or not.
12)Write a program to check whether the given number is a perfect number or not.
13)Write a program to print the following format.
*
* *
* * *
* * * *
* * * * *
14) Program to calculate lucky number.
15)Write a program to calculate the result of the series accurate up to 7th digit.
x+x3/3!+x5/5!+……………….
16) An electric power distribution company charges its domestic consumers as follows. Consumption Units Rate of Charge 0-200 Rs.0.50 per unit 201-400 Rs.100 plus Rs.0.65 per unit excess 200 401-600 Rs.230 plus Rs.0.80 per unit excess of 400. Write a C program that reads the customer number and power consumed and prints the amount to be paid by the customer.
17) Program to find the roots of the quadratic equation using bisection method.
UNIT-II
18)Write a program to print the elements of an array in reverse order.
19)Write a program to add all the elements of a two dimensional array.
20)Write a program to find the transpose of a given matrix.
21)Write a program to find the smallest and largest element in a two dimensional array.
22)Write a program to illustrate call by value.
23)Write a function to calculate the factorial of a given number.
24)Write a function to sort the elements of an array.
25)Write a recursive function to find the roots of the quadratic expression using bisection method.
26)Write a program to copy string to another without using string handling functions.
27)Write a program to check whether a given string is a palindrome or not.
28)Write a function to find the largest element in an array.
29)Write a recursive function power (base, exponent) that when invoked returns base exponent.
30) Program to sort the characters in a given string.
31)Write a function to convert all the upper case letters to lower case and lower case letters to upper case in a given string.
32) Program to undefined an already defined macro.
33) Program to illustrate conditional compilation using #if, #end if, #else.
UNIT-III
34)Write a program to calculate length of the string using pointers.
35)Write a function to swap two variables using pointers.
36) Program to illustrate pointer arithmetic.
37)Write a function to calculate sum of two numbers using pointers to functions.
38)Write a program to perform matrix multiplication using pointers.
39)Write a program to find the largest in an array using pointers.
40) Program to arrange the given numbers in ascending order.
41)Write a C program using pointer for string comparison.
42) Program to reverse the string using pointers.
43) Program to perform sorting using command line arguments.
44) The names of employees of an organization are stored in three arrays, namely, first name, second name, and last name. Write a program using pointers to concatenate the three parts into one string to be called name.
45) Program to merge two sorted arrays so that the resultant array is in sorted order using pointers.
UNIT-IV
46) Write a C program to compute the monthly pay of 100 employees using each employee’s name, basic-pay. The DA is computed as 52% of the basic pay. Gross-salary (Basic-pay+DA).Print the employees name and gross salary.
47)Write a program to calculate and print student wise total for 50 students and 3 subjects. The structure should contain 3 subjects and total.
48)Write a program to calculate and print student wise total for 50 students and 3 subjects using pointers. The structure should contain 3 subjects.
49)Write a program to store the information of vehicles use bit fields to store the status information. Assume the vehicle object consists of type, fuel and model member fields. Assume appropriate number of bits for each field.
50) Program for illustration of user defined data types using typedef.
51) Program for illustration of nested structures.
52) Program to add or delete a record of a particular employee based on his code. The structure should contain name, designation, code, salary. Program should also provide the flexibility to update any employees record.
53) Define a structure that represent a complex number (contains two floating point members, called real and imaginary). Write functions to add, subtract, multiply and divide two complex numbers.
54) A company markets Hardware items. Create a structure “hwitem” that stores the title of the item, it’s price, an array of three floats so that it can record the sale in rupees of a particular item for the last three months, category of the item and it’s original equipment manufacturer. Write a short program that provides facility to read N number of items information, append new item, and displays all records.
55) Define a structure to represent a data. Use your structures that accept two different dates in the format mmdd of the same year. And do the following: Write a C program to display the month names of both dates.
56) Consider a structure master include the information like name, code, pay, experience write a program to delete and display the information contained in master variables for a given code.
57)Write a program to use structure within union. Display the contents of structure elements.
UNIT-V
58)Write a C program to read a text file and to count Number of characters, Number of words and Number of sentences and write in an output file.
59)Write a C program to read the text file containing some paragraph. Use fseek() and read the text after skipping n characters from the beginning of the file.
60)Write a C program to read the text file containing some paragraph. Print the first n characters from the beginning of the file.
61)Write a program to print the output of the following format in an OUTPUT file.
Number Square Cube
2 4 8
3 9 27
62)Write a C program to read data from a file named INPUT containing numbers. Write all even numbers to file called EVEN and odd numbers to file called ODD.
63) Program to print the n characters from mth position in the file.
64) Program to print the current position value of the file pointer.
65) Program to check whether end of file has been reached or not using feof() function.
66) Program to check whether any data errors are present and print the relevant information using ferror() function.
67)Write a program to detect error while opening a file that does not exist.
68)Write a C program to open a pre-existing file and add information at the end of file. Display the contents of the file before and after appending
69) Program to copy one file to another.
70)Write program to read a text file and convert the file contents in capital (upper-case) and write the contents in an output file.
71) Candidates have to score 90 or above in the IQ test to be considered eligible for taking further tests. All candidates who do not clear the IQ test are sent reject letters
and others are sent call letters for further tests. The selected list and other conversation have to be written to file.
UNIT-VI
72)Write a non-recursive simulation of towers of Hanoi.
73)Write recursive function for towers of Hanoi.
74) Program to convert Prefix to postfix using stacks.
75) Program to convert postfix expression to prefix expression.
76)Write a program to evaluate postfix expression
77) Program to evaluate prefix expression.
78)Write a program to implement dequeues
79) Program to implement priority queues.
80) Program to check whether a given string is palindrome or not using stacks.
UNIT-VII
81)Write a program in ‘C’ to form a list consisting the intersection of the elements of two lists
82)Write a routine to reverse elements of a doubly linked list by traversing the list only once
83)Write a program to count the number of non-leaf nodes in a binary tree
84)Write a program to count the number of leaf nodes in a binary tree
85)Write a program to implement circular using double linked list.
86)Write a program to perform polynomial addition.
87)Write a program to perform polynomial multiplication.
88) Function to reverse the single linked list.
89)Write non recursive functions for binary tree traversals.
90) Program to implement binary search tree using arrays.
91)Write a C program to exchange two nodes of a singly linked list.
92) Program to implement breadth first search.
93) Program to implement depth first search.
94) Represent a singly linked list using an array. Write routines to insert and delete elements for this representation.
95)Write a routine SPLIT() to split a singly linked list into two lists so that all elements in odd position are in one list and those in even position are in another list.
96) Represent a doubly linked list using an array. Write routines to insert and delete elements for this representation.
97)Write a function to merge to doubly linked lists and arrange the numbers in ascending order.
UNIT-VIII
98)Write a program to implement merge sort
99) Derive the efficiency of merge sort
100) Derive the efficiency of binary search.
Subscribe to:
Posts (Atom)