Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

1. Which of these bests describes an array?

a. A Data Structure that shows a hierarchical behavior.


b. Container of object mixed types.
c. Container of object of similar types.
d. Container of object of heterogeneous types.
Ans. (c) – Container of object of similar types.
2. The stack is what types of list?
a. LIFO
b. FIFO
c. FILO
d. Both (a) and (b)
Ans. (d) – Both (a) and (b)
3. How do you initialize an array in C?
a. int arr[5]=(10,20,30,40,50);
b. int arr[ ]={10,20,30,40,50};
c. int arr[5]={10,20,30,40,50};
d. Both (b) and (c)
Ans. (d) – Both (b) and (c)
4. The process of inserting new element and removing an existing element to/from stack is called,
respectively.
a. Pop, Push
b. Insert, Delete
c. Push, Pop
d. Both (b) and (c)
Ans. (c) – Push, Pop
5. Which data structure is used for implementing recursion?
a. Queue
b. Stack
c. Array
d. Linked List
Ans. (b) – Stack
6. In a Stack, if a user tries to remove an element from empty stack it is called?
a. Underflow
b. Garbage Collection
c. Overflow
d. Empty Collection
Ans. (a) – Underflow
7. What are the advantages of Array?
a. Easier to store elements of same data type.
b. Convenient way to represent matrices as a 2-D array.
c. Used to implement other data structure like stack and queue.
d. All of the above.
Ans. (d) – All of the above
8. Consider the following operations performed on a stack of size 5; push(a); pop(); push(b); push(c);
pop(); push(d); pop(); pop(); push(e); which of the statement is correct?
a. Underflow occurs.
b. Stack operations are performed smoothly.
c. Overflow occurs.
d. NOT
Ans. (b) – Stack operations are performed smoothly.
9. Consider the following postfix notation expression: P: 8 2 3 ^ / 2 3 * + 5 1 * -
The top two elements of the stack after the first * is evaluated are:
a. 6,1
b. 5,7
c. 3,2
d. 1,5
Ans. (a) – 6.1
Input Action Stack Status Result
8 Push 8 #
2 Push 8 2 #
3 Push 8 2 3 #
^ Pop 8 2^3=8
# Push 8 8 #
/ Pop # 8/8=1
# Push 1 #
2 Push 1 2 #
3 Push 1 2 3 #
* Pop 1 2*3=6
# Push 1 6 #
+ Pop # 1+6=7
# Push 7 #
5 Push 7 5 #
1 Push 7 5 1 #
* Pop 7 5*1=5
# Push 7 5 #
- Pop # 7-5=2
# Push 2 #

10. Assume that the operators +,-,* are left associative and ^ is right associative. The postfix expression
corresponding to the infix expression a+b*c-d^e^f is:
a. abc*+def^^-
b. abc*+de^f^-
c. ab+c*d-e^f^
d. -+a*bc^^def
Ans. (b) - abc*+de^f^-
(a+b*c)-(d^e^f)
(a+bc*)-(de^f^)
(abc*+)-(de^f^)
abc*+de^f^+
11. The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is:
a. 213
b. 284
c. 142
d. NOT
Ans. (c) – 142
Input Action Stack Status Result
10 Push 10 #
5 Push 10 5 #
+ Pop # 10+5=15
# Push 15 #
60 Push 15 60 #
6 Push 15 60 6 #
/ Pop 15 60/6=10
# Push 15 10 #
* Pop # 15*10=150
# Push 150 #
8 Push 150 8 #
- Pop # 150-8=142
# Push 142 #

12. What is the output of the program for the following input: 5 2 * 3 3 2 + * +
a. 25
b. 15
c. 22
d. 150
Ans. (a) – 25
Input Action Stack Status Result
5 Push 5 #
2 Push 5 2 #
* Pop # 5*2=10
# Push 10 #
3 Push 10 3 #
3 Push 10 3 3 #
2 Push 10 3 3 2 #
+ Pop 10 3 3+2=5
# Push 10 3 5 #
* Pop 10 3*5=15
# Push 10 15 #
+ Pop # 10+15=25
# Push 25 #

13. If the sequence of operations: push(1), push(2), pop, push(1), push(2), pop, pop, pop, push(2), pop
is performed on a stack, the sequence of popped out values.
a. 2,2,1,1,2
b. 2,2,1,2,2
c. 2,1,2,2,1
d. 2,1,2,2,2
Ans. (a) – 2,2,1,1,2
Action Stack Status No. of Pop
Push(1) 1 #
Push(2) 1 2 #
Pop 1 2
Push(1) 1 1 #
Push(2) 1 1 2 #
Pop 1 1 2
Pop 1 1
Pop # 1
Push(2) 2 #
Pop # 2

14. Here is an infix expression: A+B*(C*D-E). Suppose that we are using the usual stack algorithm to
convert the expression from infix to postfix notation. The maximum number of symbols that will
appear on the stack At One Time during the conversion of this expression?
a. 5
b. 4
c. 6
d. 3
Ans. (b) – 4

15. WAP for reverse the given string using stack.


Ans.
#include <stdio.h>
#include <string.h>
#include <conio.h>
#define MAX 100
int top=-1;int item;
char stack_string[MAX];
void stackPush(char item);
char stackPop();
int main()
{
char character[MAX]; int i;
printf("Input a string: ");
gets(character);
for(i=0;i<strlen(character);i++)
stackPush(character[i]);
for(i=0;i<strlen(character);i++)
character[i]=stackPop();
printf("Reversed String is: %s\n",character);
getch();
}
void stackPush(char item)
{
if(top==MAX-1)
printf("Stack Overflow");
else
{
top=top+1;
stack_string[top]=item;
}
}
char stackPop()
{
if(top==-1)
printf("Stack Underflow");
else
{
item = stack_string[top];
top=top-1;
return item;
}
}

16. Convert the given infix notation expression into equivalent postfix notation expression. Show stack
status after every step. Q: A+B*C-(C^D-E)^(F+G*H)-I
17. What is function? Explain need of user defined function.
Ans. A function is a block of one or more statements that execute when it is called.
Types of Function –
Pre-defined Function – The function whose definition is already defined is called pre-defined function. For
example: printf(), scanf(), gets(), puts(), etc.
User-defined Function – The function whose definition is defined according to the user requirement is
called User-defined Function.
Elements of Function:
i. Function declaration
Syntax: return_type function_name (argument list); For example: void Demo (void);
ii. Function call
Syntax: function_name (argument list); For example: void Demo(void);
iii. Function Body
Syntax: return_type function_name(argument list)
{
//function body
}
Example: void Demo(void)
{
//function body
}
18. WAP for conversion from decimal to binary using stack.
Ans.
#include<stdio.h>
#include<conio.h>
#define MAX 10
int top=-1;
void stackPush(int *,int);void stackPop(int *);
int main()
{
int stack[MAX],dec,rem,bin=0,place=1;
printf("Enter a Decimal Number: ");
scanf("%d",&dec);
while(dec!=0)
{
rem=dec%2;
stackPush(stack,rem);
bin=bin+rem*place;
place=place*10;
dec=dec/2;
}
stackPop(stack);
getch();
}
void stackPush(int *stack,int num)
{
if(top==MAX-1)
printf("Stack Overflow");
else
{
top++;
stack[top]=num;
}
}
void stackPop(int *stack)
{
int i;
if(top==-1)
printf("Stack Underflow");
else
{
printf("Binary Equivalent is: ");
for(i=top;i>=0;i--)
printf("%d",stack[i]);
}
}

19. WAP to calculate Nth term of Fibonacci series using recursion.


Ans.
#include<stdio.h>
#include<conio.h>
int fibonacci(int);
int main()
{
int num,res;
printf("Which term: ");
scanf("%d",&num);
res=fibonacci(num);
printf("The %d term of Fibonacci Series is %d.",num,res);
getch();
}
int fibonacci(int n)
{
int calculate;
if(n==1||n==2)
return(1);
else
{
calculate=fibonacci(n-1)+fibonacci(n-2);
return(calculate);
}
}

20. Write Algorithm for various operations performed on stack.


Ans. Stack is a list of data elements in which insertion and deletion are done from one end called TOP of
the stack.
In Stack the last added element will be first removed, thus it is called Last-In-First-Out type list.
Operations on stack:
i. Push Operation
The process of adding new element in the stack is called PUSH OPERATION. After every
PUSH Operation the value of TOP is incremented by 1. When no memory space is available for
new element and the PUSH Operation is performed, this situation is called “Stack Overflow”.
ii. POP Operation
The process of deleting existing element in the stack is called POP OPERATION. After every
POP Operation the value of TOP is decremented by 1. When no element is available in the
stack and the POP Operation is performed, this situation is called “Stack Underflow”.
Algorithm for PUSH OPERATION:
PUSH (STACK, TOP, ITEM, MAXSIZE)
i. Start
ii. [check Stack already full]
if (TOP==MAXSIZE-1]
print “STACK OVERFLOW” and go to step v.
iii. [increase TOP]
TOP:=TOP+1
iv. [Insert Item]
STACK[TOP]:=ITEM
v. Exit.

Algorithm for POP OPERATION:


POP (STACK, TOP, ITEM)
i. Start
ii. [check Stack already empty]
if (TOP== -1]
print “STACK UNDERFLOW” and go to step v.
iii. [store deleted element]
ITEM:=STACK[TOP]
iv. [decrease TOP]
TOP:=TOP-1
v. Exit.

You might also like