CPC Test3 Key

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Answers for test-3

1. d
Both Merge sort and Insertion sort can be used for linked lists. The slow random-access
performance of a linked list makes other algorithms (such as quicksort) perform poorly, and
others (such as heapsort) completely impossible. Since worst case time complexity of Merge Sort
is O(nLogn) and Insertion sort is O(n^2), merge sort is preferred. See following for
implementation of merge sort using Linked List.
2. b
fun1() prints the given Linked List in reverse manner. For Linked List 1->2->3->4->5, fun1()
prints 5->4->3->2->1.
3.d
4.a
Explanation: The algorithm for evaluating any postfix expression is fairly straightforward:
1. While there are input tokens left
*Read the next token from input.
*If the token is a value Push it onto the stack.
* Otherwise, the token is an operator (operator here includes both operators, and functions).
* It is known a priori that the operator takes n arguments.
* If there are fewer than n values on the stack(Error) The user has not input sufficient values
in the expression.
* Else, Pop the top n values from the stack.
* Evaluate the operator, with the values as arguments.
* Push the returned results, if any, back onto the stack.
2. If there is only one value in the stack That value is the result of the calculation.
3. If there are more values in the stack (Error) The user input has too many values.
Let us run the above algorithm for the given expression.
First three tokens are values, so they are simply pushed. After pushing 8, 2 and 3, the stack is as
follows
8, 2, 3
When ^ is read, top two are popped and power(2^3) is calculated
8, 8
When / is read, top two are popped and division(8/8) is performed
1
Next two tokens are values, so they are simply pushed. After pushing 2 and 3, the stack is as
follows
1, 2, 3
When * comes, top two are popped and multiplication is performed. 1, 6
5.d
The function takes a queue Q as an argument. It dequeues all items of Q and pushes them to a
stack S. Then pops all items of S and enqueues the items back to Q. Since stack is LIFO order,
all items of queue are reversed.
6.c
To keep the First In First Out order, a queue can be implemented using linked list in any of the
given two ways.
7.a
Inorder traversal of a BST always gives elements in increasing order. Among all four options, a)
is the only increasing order sequence.
8.a
Number of nodes is maximum for a perfect binary tree.
A perfect binary tree of height h has 2h+1 - 1 nodes

Number of nodes is minimum for a skewed binary tree.


A perfect binary tree of height h has h+1 nodes.

9.b
Constructed binary search tree will be..
10
/ \
1 15
\ / \
3 12 16
\
5
10.d
The following is the constructed tree

30
/ \
20 39
/ \ / \
10 25 35 42
\ /
15 23
11.b
When we know either preorder or postorder traversal, we can construct the BST. Note that we
can always sort the given traversal and get the inorder traversal. Inorder traversal of BST is
always sorted.
12. b
The function basically does reverse inorder traversal of the given Binary Search Tree. The
reverse inorder traversal produces data in reverse sorted order. Whenever a nod is visited, count
is incremented by 1 and data of a node is printed only when count becomes k.
13.b
Explanation: 71, 65, 84, 69, 67, 83
71
/ \
65 84
\ /
69 83
/
67
14.a
If we take look at the inner statements of first loops, we can notice that the statements swap
A[i][j] and A[j][i] for all i and j. Since the loop runs for all elements, every element A[l][m]
would be swapped twice, once for i = l and j = m and then for i = m and j = l. Swapping twice
means the matrix doesn’t change.
15.c
A sorting algorithm is said to be stable if two objects with equal keys appear in the same order in
sorted output as they appear in the input unsorted array. Some sorting algorithms are stable by
nature like Insertion sort, Merge Sort, Bubble Sort, etc. And some sorting algorithms are not, like
Heap Sort, Quick Sort, etc.
However, any given sorting algo which is not stable can be modified to be stable. There can be
sorting algo specific ways to make it stable, but in general, any comparison based sorting
algorithm which is not stable by nature can be modified to be stable by changing the key
comparison operation so that the comparison of two keys considers position as a factor for
objects with equal keys.
16.a
Worst case complexities for the above sorting algorithms are as follows: Merge Sort — nLogn
Bubble Sort — n^2 Quick Sort — n^2 Selection Sort — n^2
17.b
The data can be sorted using external sorting which uses merging technique. This can be done as
follows: 1. Divide the data into 10 groups each of size 100. 2. Sort each group and write them to
disk. 3. Load 10 items from each group into main memory. 4. Output the smallest item from the
main memory to disk. Load the next item from the group whose item was chosen. 5. Loop step
#4 until all items are not outputted. The step 3-5 is called as merging technique.
18. C
f() is a recursive function which adds f(a+1, n-1) to *a if *a is even. If *a is odd then f() subtracts
f(a+1, n-1) from *a. See below recursion tree for execution of f(a, 6).
.
f(add(12), 6) /*Since 12 is first element. a contains address of 12 */
|
|
12 + f(add(7), 5) /* Since 7 is the next element, a+1 contains address of 7 */
|
|
7 - f(add(13), 4)
|
|
13 - f(add(4), 3)
|
|
4 + f(add(11), 2)
|
|
11 - f(add(6), 1)
|
|
6+0
So, the final returned value is 12 + (7 – (13 – (4 + (11 – (6 + 0))))) = 15

19.a
Explanation:
count is static variable in incr(). Statement static int count = 0 will assign count to 0 only in first
call. Other calls to this function will take the old values of count.
Count will become 0 after the call incr(0)
Count will become 1 after the call incr(1)
Count will become 3 after the call incr(2)
Count will become 6 after the call incr(3)
Count will become 10 after the call incr(4)
20.c
Explanation:
Short array s[5] will take 10 bytes as size of short is 2 bytes. Since u is a union, memory
allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes
(10 + 8).
Programing:
1. #include<stdio.h>
int main()
{
int i;

for(i=0;i<=255;i++)
printf("ASCII value of character %c: %d\n",i,i);
return 0;
}
2.

#include<stdio.h>
int main(){
int num,sum=0,r;
printf("Enter a number: ");
scanf("%d",&num);
for(;num!=0;num=num/10){
r=num%10;
sum=sum+r;
}
printf("Sum of digits of number: %d",sum);
return 0;
}
3. #include<stdio.h>
int main(){
int i,range;
long int arr[40];
printf("Enter the number range: ");
scanf("%d",&range);
arr[0]=0;
arr[1]=1;

for(i=2;i<range;i++){
arr[i] = arr[i-1] + arr[i-2];
}
printf("Fibonacci series is: ");
for(i=0;i<range;i++)
printf("%ld ",arr[i]);
return 0;
}

4. #include<stdio.h>
long fact(int);
int main(){
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);

for(i=0;i<line;i++){
for(j=0;j<line-i-1;j++)
printf(" ");
for(j=0;j<=i;j++)
printf("%ld ",fact(i)/(fact(j)*fact(i-j)));
printf("\n");
}
return 0;
}
long fact(int num){
long f=1;
int i=1;
while(i<=num){
f=f*i;
i++;
}
return f;
}

5. #include<stdio.h>
int main(){
int n1,n2,x,y;
printf("\nEnter two numbers:");
scanf("%d %d",&n1,&n2);
x=n1,y=n2;
while(n1!=n2){
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf("L.C.M=%d",x*y/n1);
return 0;
}
6. #include<stdio.h>
int main(){
int *ptr = 0;
ptr++;
printf("Size of int data type: %d",ptr);
return 0;

}
7.refer linked list chapter in karumanchi narasimham
8.refer searching chapter in karumanchi
9.refer searching chapter in karumanchi
10.refer queues chapter in karumanchi

You might also like