Vous êtes sur la page 1sur 13

C questions and answers: BCA

1. Q. WHAT IS PREPROCESSOR?
Ans: The C preprocessor is a macro processor that is used automatically by
the C compiler to transform the program before actual compilation. It is called a
macro processor because it allows the programmer to define macros, which are
brief abbreviations for longer constructs. All preprocessor commands begin
with a pound symbol (#). It must be the first nonblank character, and for
readability, a preprocessor directive should begin in first column.
2. Q. WHAT IS ADT?
Ans: In computer science, an abstract data type (ADT) is a mathematical model
for data types where a data type is defined by its behavior (semantics) from the
point of view of a user of the data, specifically in terms of possible values,
possible operations on data of this type, and the behavior of these operations. A
data type can be considered abstract when it is defined in terms of operations
on it, and its implementation is hidden.
3. Q. Quick Sort :- (Short notes)
Quicksort is an efficient sorting algorithm, serving as a systematic method for
placing the elements of an array in order. It is still a very commonly used
algorithm for sorting. When implemented well, it can be about two or three
times faster than its main competitors, merge sort and heapsort.
Quicksort is a comparison sort, meaning that it can sort items of any type for
which a "less-than" relation (formally, a total order) is defined. In efficient
implementations it is not a stable sort, meaning that the relative order of equal
sort items is not preserved. Quicksort can operate in-place on an array, requiring
small additional amounts of memory to perform the sorting.

Quick sort is a divide and conquer algorithm. Its divided large list in mainly
three parts:
1.

Elements less than pivot element.

2.

Pivot element.

3.

Elements greater than pivot element.


Where pivot as middle element of large list. Lets understand through example:
List : 3 7 8 5 2 1 9 5 4
In above list assume 4 is pivot element so rewrite list as:
312458957
Example: in C
#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
2

i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
4.Q. Differentiate between iteration and recussion?
For recursion to terminate, each time the recursion method calls itself
with a slightly simpler version of the original problem, the sequence of
smaller and smaller problems must converge on the base case. When the
method recognizes the base case, the result is returned to the previous
method call and a sequence of returns ensures all the way up the line until
the original call of the method eventually returns the final result.
Both iteration and recursion are based on a control structure: Iteration
uses a repetition structure; recursion uses a selection structure.
3

Both iteration and recursion involve repetition: Iteration explicitly uses a


repetition structure; recursion achieves repetition through repeated
method calls.
Iteration and recursion each involve a termination test: Iteration
terminates when the loop-continuation condition fails; recursion
terminates when a base case is recognized.
5.Q. What is pointer to Pointer? Give Example.
Ans: A pointer to a pointer is a form of multiple indirection, or a chain of
pointers. Normally, a pointer contains the address of a variable. When we
define a pointer to a pointer, the first pointer contains the address of the second
pointer, which points to the location that contains the actual value as shown
below.

A variable that is a pointer to a pointer must be declared as such. This is done


by placing an additional asterisk in front of its name. For example, following is
the declaration to declare a pointer to a pointer of type int:
int **var;
6.Q. Write down the general syntax of calloc() statement.
Ans: calloc() is another memory allocation function that is used for allocating
memory at runtime. calloc function is normally used for allocating memory to
derived data types such as arrays and structures. If it fails to locate enough
space it returns a NULL pointer.
Syntax :
(cast_type *)calloc(blocks , size_of_block);

7.Q. Define Binary tree? Give example.


Ans: A binary tree is made of nodes, where each node contains a "left"
reference, a "right" reference, and a data element. The topmost node in the tree
is called the root.
4

Every node (excluding a root) in a tree is connected by a directed edge from


exactly one other node. This node is called a parent. On the other hand, each
node can be connected to arbitrary number of nodes, called children. Nodes
with no children are called leaves, or external nodes. Nodes which are not
leaves are called internal nodes. Nodes with the same parent are called siblings.

Example:

8.Q. What is Circular link list?


Circular linked list is a linked list where all nodes are connected to form a
circle. There is no NULL at the end. A circular linked list can be a singly
circular linked list or doubly circular linked list.

Advantages of Circular Linked Lists:


Any node can be a starting point. We can traverse the whole list by starting from
any point. We just need to stop when the first visited node is visited again.

In Circular Linked List Address field of Last node contain address of


First Node.
In short First Node and Last Nodes are adjacent.
Linked List is made circular by linking first and last node, so it looks
like circular chain.
Two way access is possible only if we are using Doubly Circular Linked
List
Sequential movement is possible
No direct access is allowed.

Circular Linked List is divided into 2 Categories.


5

Singly Circular Linked List

Doubly Circular Linked List

Example:
1.

/* CREATING CIRCULAR HEADER LINKED LIST */

2.

#include<stdio.h>

3.

#include<conio.h>

4.

#include<stdlib.h>

5.

struct link

6.

7.

int data;

8.

struct link *next;

9.

};

10.

int i=0;

11.

struct link *node,*start;

12.
13.

void create_link(struct link *node)

14.

15.

char ch;

16.

start->next=NULL;

17.

node=start;

18.

fflush(stdin);

19.

printf("\n Enter 'n' for break:");

20.

ch=getchar();

21.

while(ch!='n')

22.

23.

node->next=(struct link *)malloc(sizeof(struct link));


6

24.

node=node->next;

25.
26.

printf("\n Enter data for node:");

27.

scanf("%d",&node->data);

28.

node->next=start;

29.

fflush(stdin);

30.

printf("\n Enter 'n' for break:");

31.

ch=getchar();

32.

i++;

33.
34.

}
}

35.
36.

void display(struct link *node)

37.

38.

int count;

39.

node=start->next;

40.

count=i;

41.
42.

while(count)

43.

44.

printf("\t %d",node->data);

45.

node=node->next;

46.

count--;

47.
48.

}
}

49.
7

50.

void main()

51.

52.

char ch;

53.

clrscr();

54.

create_link(node);

55.

printf("List Item Are:-\n");

56.

display(node);

57.

getch();

58.

9.Q. Define a graph.


Ans: A graph data structure consists of a finite set of nodes or vertices, together
with a set of ordered pairs of these nodes (or, in some cases, a set of unordered
pairs). These pairs are known as edges or arcs. The nodes may be part of the
graph structure, or may be external entities represented by integer indices
or references. Formally, a graph is a set of vertices and a binary relation
between vertices, adjacency.
10.Q. Define Recursion.
Ans: A recursive function is a function which either calls itself or is in a
potential cycle of function calls. As the definition specifies, there are two types
of recursive functions. Consider a function which calls itself: we call this type
of recursion immediate recursion.
Recursion is generally used when the result of the current function call is the
input to the successive call of itself. For example, factorial of a digit. By
definition, the factorial of the current digit is the factorial of its previous digit
and the digit. In order to get the factorial of the previous digit, the same function
should return the factorial.
11.Q. What is tail recursion?
Ans: A function call is said to be tail recursive if there is nothing to do after the
function returns except return its value. Since the current recursive instance is
done executing at that point, saving its stack frame is a waste. Specifically,
8

creating a new stack frame on top of the current, finished, frame is a waste. A
compiler is said to implement Tail Recursion if it recognizes this case and
replaces the caller in place with the callee, so that instead of nesting the stack
deeper, the current stack frame is reused. This is equivalent in effect to a
"GoTo", and lets a programmer write recursive definitions without worrying
about space inefficiency during execution.
12.Q. C Program to Find GCD of given Numbers using Recursion.
/*C Program to find GCD of given Numbers using Recursion*/
#include <stdio.h>
int gcd(int, int);
int main()
{
int a, b, result;
printf("Enter the two numbers to find their GCD: ");
scanf("%d%d", &a, &b);
result = gcd(a, b);
printf("The GCD of %d and %d is %d.\n", a, b, result);
}
int gcd(int a, int b)
{
while (a != b)
{
if (a > b)
{
return gcd(a - b, b);
}
else
{
return gcd(a, b - a);
}
}
return a;
}
}

Input / Output:
Enter the two numbers to find their GCD: 100 70
The GCD of 100 and 70 is 10.

13.Q. C Program to Find the Factorial of a Number using


Recursion.
/*C Program to find factorial of a given number using recursion*/
#include <stdio.h>
int factorial(int);
int main()
{
int num;
int result;
printf("Enter a number to find it's Factorial: ");
scanf("%d", &num);
if (num < 0)
{
printf("Factorial of negative number not possible\n");
}
else
{
result = factorial(num);
printf("The Factorial of %d is %d.\n", num, result);
}
return 0;
}
int factorial(int num)
{
if (num == 0 || num == 1)
{
return 1;
}
else
{
return(num * factorial(num - 1));
}
}

Input /Output:
Enter a number to find it's Factorial: 6
The Factorial of 6 is 720.
14.Q. Explain Switch statement in C.
A switch statement allows a variable to be tested for equality against a list
of values. Each value is called a case, and the variable being switched on is
checked for each switch case. The expression used in a switch statement
must have an integral or enumerated type, or be of a class type in which the
10

class has a single conversion function to an integral or enumerated type.


When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached. When
a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.
A switch statement can have an optional default case, which must appear at
the end of the switch. The default case can be used for performing a task
when none of the cases is true. No break is needed in the default case.

Syntax:
The syntax for a switch statement in C programming language is as follows:
switch(expression){
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
/* you can have any number of case statements */
default : /* Optional */
statement(s);
}

15.Q. Differentiate between Structure and Union.

Structure
1.The keyword struct is used to define a
structure

Union
1. The keyword union is used to define a
union.

2. When a variable is associated with a


structure, the compiler allocates the memory
for each member. The size of structure is
greater than or equal to the sum of sizes of its
members. The smaller members may end with
unused slack bytes.

2. When a variable is associated with a union,


the compiler allocates the memory by
considering the size of the largest memory. So,
size of union is equal to the size of largest
member.

3. Each member within a structure is assigned

3. Memory allocated is shared by individual

11

unique storage area of location.

members of union.

4. The address of each member will be in


ascending order This indicates that memory for
each member will start at different offset
values.

4. The address is same for all the members of a


union. This indicates that every member begins
at the same offset value.

5 Altering the value of a member will not affect 5. Altering the value of any of the member will
other members of the structure.
alter other member values.
6. Individual member can be accessed at a time

6. Only one member can be accessed at a time.

7. Several members of a structure can initialize


at once.

7. Only the first member of a union can be


initialized.

16.Q. What is a Pointer?


A pointer is a variable whose value is the address of another variable, i.e.,
direct address of the memory location. Like any variable or constant, you must
declare a pointer before you can use it to store any variable address. The
general form of a pointer variable declaration is:
type *var-name;
Here, type is the pointer's base type; it must be a valid C data type and varname is the name of the pointer variable. The asterisk * you used to declare a
pointer is the same asterisk that you use for multiplication. However, in this
statement the asterisk is being used to designate a variable as a pointer.
17.Q. Difference between Break and Continue statement.

12

13