Keymt22232 (1)

.pdf
School
Eastern Mediterranean University**We aren't endorsed by this school
Course
CMSE 231
Subject
Computer Science
Date
Jan 4, 2025
Pages
9
Uploaded by GrandSummer15498
1 EASTERN MEDITERRANEAN UNIVERSITYComputer Engineering Department CMPE-231 DATA STRUCTURES MIDTERM EXAMINATION 29 April 2023Duration: 80 minutes Name, Surname . SOLUTION KEY. . . . . . . . . Student ID # . . . . . . . . . . Grading: Please, check your exam sheet and make sure that it contains9 pages. In case ofany missing pages,inform the invigilator IMMEDIATELY ! You MUST give your answer (and nothing else) in the corresponding box. Otherwise, you will get NO MARKSfrom that questionX 7 = Total/98+2
Background image
2 C programming language: Operators OPERATORS ASSOCIATIVITY ( ) [ ] -> . Left to right ! ++ -- + - * & (type) sizeof Right to left (Unary) * / % Left to right + - Left to right < <= > >= Left to right == != Left to right && Left to right || Left to right ?: Right to left = += -= *= /= %= Right to left , Left to right
Background image
3 There is only one correct answer for multiple choice questions. 1-Consider the functionvoid alter(char **p) {printf("%s\n", *++p + 1);} which of the following is not correct for the code char *str[3]; alter(&str[1]); if ABCis printed:a)*str[0]may be Bb)*str[1]may be Bc)The string starting at str[2]+1is definetely "ABC"d)*str[2]is definetely A2-Consider the code int *fun(int *p) { while(p[2] >= 0) p++; return p; } void main() { int *q; int v[8]={1,2,3,-4,5,6,7,8}; q = fun(v); printf("%d ", ____Missing_1___); printf("%d ", ____Missing_2___); }However, part of the code is missing (indicated by __________). The code is supposed to give the output 3 -4 What can the missing parts be? a)Missing_1: *q Missing_2: q[2]b)Missing_1: v[4] Missing_2: q[2]c)Missing_1: *q Missing_2: q[1]d)Missing_1: *(q+1) Missing_2: *(q+2)d d
Background image
4 3-Consider the code struct stack { int array[100]; int top; }; struct stack s; void fun(struct stack *ps, int x) { if(x <= 0) return; ps -> array[++ps -> top] = x; fun(&s, x-1); } Suppose that we execute s.top = -1; fun(&s,8); Which of the following is not correct? a)s.top is 7 b)Sum of the items in the stack is 36 c)There are 8 items in the stack d)The item at the top is 84-Consider the code char x = 'A'; void test(int n) { printf("%d ", n); if(n > 0) ____Missing_1____; if(n == 0) printf("\n"); printf("%c ", ____Missing_2____) ; } main() { test(4); } d
Background image
5 However, part of the code is missing (indicated by __________). The code is supposed to give the output 4 2 0 A B C What can the missing parts be? a)Missing_1: test(n - 2) Missing_2: x++b)Missing_1: test(n - 1) Missing_2: x -= 2c)Missing_1: x -= 2Missing_2: test(n - 1) d)Missing_1: test(n +2) Missing_2: x--5-Consider the code struct crr { char *name; struct crr *next; }; struct crr x[] = {{"PENNY", x+1}, {"DIME", x}}; struct crr *cp = x; printf("%c\n", ____Missing_1____ ); printf("%c ", ____Missing_2____); printf("%c ", *cp++->name); However, part of the code is missing (indicated by __________). The code is supposed to give the output I P E What can the missing parts be? a) Missing_1: cp->next->name[1] Missing_2: *cp->name++b)Missing_1: cp->name[1] Missing_2: *cp->name++c)Missing_1: cp->name[1] Missing_2: *++cp->named)Missing_1: cp->next->name[1] Missing_2: *++cp->namea a
Background image
6 6-Consider the code char *v[2]; int k; v[0] = (char *) malloc(10); v[1] = (char *) malloc(15); if(sizeof(v[0]) == sizeof(v[1])) printf("____Missing_1____ \n"); else printf("____Missing_2____ \n"); for(k=0; k<4; k++) ____Missing_3____ = 'A' + k; v[1] = v[0] + 2; printf("%s\n", v[0]); printf("%s\n", v[1]); However, part of the code is missing (indicated by __________). The code is supposed to give the output TT ABCD CD Which of the following is correct? a) Missing_1: TTMissing_2: FFMissing_3: *(v[0] + k)b)Missing_1: TTMissing_2: FFMissing_3: *v[0] + kc)Missing_1: FFMissing_2: TTMissing_3: *(v[1] + k)d)Missing_1: FFMissing_2: TTMissing_3: *v[0] + k 7-Consider the circular array implementation of the queue: struct queue { int items[MAX]; int front, rear; }q; Which of the following is not correct?a)If q.front == q.rear == MAX 1the queue is emptyb)The queue can contain at most MAX 1itemsc)q.items[q.front+1] gives the item at the front of the queue when q.front < MAX-1d)q.items[q.rear+1] gives the item at the rear of the queuewhen q.rear < MAX-1a d
Background image
7 8-Consider the circular array implementation of the queue: struct queue { int items[MAX]; int front, rear; }q; Which of the following is not correct?a)If q.front == q.rear == 0 the queue is emptyb)Forq.front < MAX-1, printf(%d, q.items[q.front++]); prints the item at the front of the queue and removes itc)q.items[0] gives the item at the front of the queue when q.front == MAX-1d)q.items[MAX-1] gives the item at the rear of the queuewhen q.rear == MAX-19-The following C function takes a singly linked list as input argument. It modifies the list by moving the last element to the front of the list and returns the modified list. Some part of the code left blank. typedef struct node { int value; struct node* next; }Node; Node* move_to_front(Node* list { Node* p, *q; if((list ==NULL) || (list -> next==NULL)) return list; q=NULL; p= list; while(p->next != NULL) { q=p; p=p->next; } __________________________//Blank return list; } b
Background image
8 Choose the correct alternative to replace the blank line. a) q=NULL; p->next=list; list =p ; b) q->next=NULL; list =p; p->next = list; c) head=p; p->next=q; q->next=NULL; d) q->next=NULL; p->next= list; list =p; 10-What does the following function do for a given Linked List with the pointer to the first node as list? struct node { int data; struct node *list; }; void fun1(struct node* list) { if(list == NULL) return; printf("%d ", list -> data); fun1(list -> next); printf("%d ", list -> data); } a)Prints all nodes b)Prints all nodes in reverse order c)Prints all nodes and then prints all nodes in reverse order d)Prints all nodes in reverse order and then prints all nodes11-When the following function is called as fun(&list, p);it removes all the nodes in the list up to the node prior to the node pointed to byp. Ex; if the listcontains data: 5 9 3 7 4 2 and if ppoints to the node of 7 then the modified list will contain 3 7 4 2. Assume that the list includes at least three nodes and pdoes not point to the first or second nodes. struct node { int data; struct node * next; }; typedef struct node * NODEPTR; void fun(NODEPTR *plist, NODEPTR p) { NODEPTR q; q = *plist; d c
Background image
9 while(___Missing_1___) q = q ->next; ___Missing_2___} However, parts of the code are missing (indicated by __________). What can the missing parts be? a)Miss_1: q != p Miss_2: plist =q; b)Miss_1: q != *plistMiss_2: *plist = p; c)Miss_1: q!= p-1Miss_2: list = q; d)Miss_1: q->next != p Miss_2: *plist = q;12-The postfix form of the expression (A+B )* (C*D-E )*F/Gis a) AB+CD*E-FG/**b) AB+CD*E-F**G/c) AB+CD*E-*F*G/d) AB+CDE*-*F*G/13-Suppose that theinfix-to-postfixconversion algorithm is applied to the expressionA/(B$C$D*E+F*G)Which of the following can not be the contents of the stack at some instant? The rightmost symbol denotes the top of the stack. a) /($* b) / ($$c) /(+*d) /(* 14-) What is the value of the postfix expressionabcda+**d+a-in case ofa = 4, b = 1, c = 3, d = 2? d 52C A
Background image