Professional Documents
Culture Documents
Answer - Key - Suraku Academy - CT DS - 301 - II
Answer - Key - Suraku Academy - CT DS - 301 - II
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
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]);
}
}