Sunteți pe pagina 1din 3

Following are the questions asked (30 marks / 90 minutes)

For all the answers verification is required. Please comment/answer if you find
anything wrong.
====================================================
=====

(1) Find output for following recursive function (4 marks) :

int main()
{
int i=32242,j=find(i);
}
int find(int j)
{
if(j)
{
j = j%10 + find(j/10);
printf(" %d ",j);
}
return j;
}
[The question was almost like above, don't remember the exact value of i but it was
something less than 32667.]

Ans : This is a same old boring recursive function. Output can be found by
running code actually on PC.
====================================================
=====

(2) What is the problem with following code : (4 marks)

char* AddnewlinetoString(char *s)


{
char buffer[1024];
strcpy(buffer,s);
buffer[strlen(s)-1] = '\n';
return buffer;
}

Ans : Explaining in brief,


<1>'buffer' is a local variable array, its scope will finish with function, so
return address leads to undefined behavior.
<2> No check of strlen(s), it could be more than 1024 (sizeof(buffer)) and
cause a buffer overflow
<3> No null termination after putting '\n'.

====================================================
=====

(3) Given an 2D array A[M][N], we are converting it into A[N][M] along with rotating
it anti clockwise. What should be the mapping,
A[i][j] = A[?][?] (i,j are 0 based indices) (4 marks)
example:
.............................................4......8
1...2...3...4..............................3......7
.......................======>..........2......6
5...6...7...8..............................1......5

Ans : A[i][j] = A[N-j-1][i]

====================================================
=====

(4) Assume two sorted singly link lists. Merge them into a one sorted singly link list.
struct Node{
int data;
Node* next
};
Implement,
Node* SortedList(Node* list1, Node* list2); ( 6 marks )

Ans : Assume the sorted order is descending:


Node* SortedList(Node* list1, Node* list2)
{
Node* head = (list1->data > list2->data)? list1 : list2;
Node* T1 = head;
Node* T2 = (head == list1)? list2 : list1;
Node* Tmp;

while(1)
{
if(T1->next->data < T2->data)
{
Tmp = T2;
T2 = T2->next;
Tmp->next = T1->next;
T1->next = Tmp;
}
T1 = T1->next;

if(T1 == NULL) { T1 = T2; return head; }


if(T2 == NULL) { T2 = T1; return head; }
}
}
====================================================
=====

(5) Write test case(s) and explanation the same for above program. (6 marks)

Ans : Explaining in brief,


(1) Check whether function is not dumping core in any situation.
(2) Write a small routine which will traverse the link list and check whether
the data of current node is greater than the next node (for descending
order). If not then throw an error.
====================================================
======
(6) Write test cases for following function, mention your assumption(s) if any:
( 6 marks )
BOOL CopyFiletoString(FILE *hfile, char *str, size filesize);
calling the function like following:
vbool = CopyFiletoString (hfile, str, 100);

Answer : This was a boring question, can't answer properly:


(1) Check for the filesize and string size compatiblity. string size should not
be less than the file size
(2) Check 0 termination at the end of the string
(3) Check for NULL ness of both 'hfile' and 'str', to avoid the coredump.
====================================================
======

S-ar putea să vă placă și