IMP MCQ

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

1) void solve()

{
int a[] = {1, 2, 3, 4, 5};
int sum = 0;
for(int i = 0; i < 5; i++) {
if(i % 2 == 0) {
sum += *(a + i); }
else {
sum -= *(a + i);
}}
cout << sum << endl;
}
1) 3 -A 2) 15 3) 2 4) syntax error

2) What does the following function do for a given Linked List with first node as head?

void fun1(struct node* head)


{
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data);
}

1) Prints all nodes of linked lists


2) Print alternate nodes in reverse order
3) Print alternate nodes of Linked List
4) Prints all nodes of linked list in reverse order -A

3) Consider the following function that takes reference to head of a Doubly Linked List as
parameter. Assume that a node of doubly linked list has previous pointer as prev and
next pointer as next.

void fun(struct node **head_ref)


{
struct node *temp = NULL;
struct node *current = *head_ref;
while (current != NULL)
{
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if(temp != NULL )
*head_ref = temp->prev;
}
Assume that reference of head of following doubly linked list is passed to above function 1 <--
> 2 <--> 3 <--> 4 <--> 5 <-->6. What should be the modified linked list after the function call?

1) 2 <--> 1 <--> 4 <--> 3 <--> 6 <-->5


2) 5 <--> 4 <--> 3 <--> 2 <--> 1 <-->6.
3) 6 <--> 5 <--> 4 <--> 3 <--> 2 <--> 1.
4) 6 <--> 5 <--> 4 <--> 3 <--> 1 <--> 2

4) The following function reverse() is supposed to reverse a singly linked list. There is one
line missing at the end of the function.
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* head_ref is a double pointer which points to head (or start) pointer of linked list */
static void reverse(struct node** head_ref)
{
struct node* prev = NULL;
struct node* current = *head_ref;
struct node* next;
while (current != NULL)
{
next = current->next;
current->next = prev;
prev = current;
current = next;
}
/*ADD A STATEMENT HERE*/
}

What should be added in place of "/*ADD A STATEMENT HERE*/", so that the function
correctly reverses a linked list.

1) *head_ref = prev;
2) *head_ref = current;
3) *head_ref = next;
4) *head_ref = NULL;
5) What is the output of following function in which start is pointing to the first node of the
following linked list 1->2->3->4->5->6 ?
void fun(struct node* start)
{
if(start == NULL)
return;
printf("%d ", start->data);
if(start->next != NULL )
fun(start->next->next);
printf("%d ", start->data);
}
1) 1 4 6 6 4 1
2) 1 3 5 1 3 5
3) 1 2 3 5
4) 1 3 5 5 3 1

6) The following C function takes a simply-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 is left blank. Choose the correct alternative to replace the blank line.
typedef struct node
{
int value;
struct node *next;
}Node;
Node *move_to_front(Node *head)
{
Node *p, *q;
if ((head == NULL: || (head->next == NULL))
return head;
q = NULL; p = head;
while (p-> next !=NULL)
{
q = p;
p = p->next;
}
return head;
}

1) q = NULL; p->next = head; head = p;


2) q->next = NULL; head = p; p->next = head;
3) head = p; p->next = q; q->next = NULL;
4) q->next = NULL; p->next = head; head = p;

7) void fun(Queue *Q)


{
Stack S; // Say it creates an empty stack S
// Run while Q is not empty
while (!isEmpty(Q))
{
// deQueue an item from Q and push the dequeued item to S
push(&S, deQueue(Q));
}
// Run while Stack S is not empty
while (!isEmpty(&S))
{
// Pop an item from S and enqueue the popped item to Q
enQueue(Q, pop(&S));
}
}

1) Removes the last from Q


2) Keeps the Q same as it was before the call
3) ----
4) ---

8) Following function is supposed to calculate the maximum depth or height of a Binary


tree -- the number of nodes along the longest path from the root node down to the farthest
leaf node.
int maxDepth(struct node* node)
{
if (node==NULL)
return 0;
else {
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
/* use the larger one */
if (lDepth > rDepth)
return X;
else return Y;
}}

What should be the values of X and Y so that the function works correctly?
1) X= lDepth, Y=rDepth
2) X= lDepth + 1, Y=rDepth + 1
3) X= lDepth - 1, Y=rDepth – 1
4) None of the above

9) What does the following function do for a given binary tree?

int fun(struct node *root)


{
if (root == NULL)
return 0;
if (root->left == NULL && root->right == NULL)
return 0;
return 1 + fun(root->left) + fun(root->right);
}
1) Count leaf nodes
2) Count internal nodes
3) Return height where height is defined as number of edges on the path from root to
deepest node
4) Return diameter where diameter is number of edges on the longest path between
any two nodes

10) struct CellNode


{
struct CelINode *leftchild;
int element;
struct CelINode *rightChild;
}
int Dosomething(struct CelINode *ptr)
{
int value = 0;
if (ptr != NULL)
{
if (ptr->leftChild != NULL)
value = 1 + DoSomething(ptr->leftChild);
if (ptr->rightChild != NULL)
value = max(value, 1 + DoSomething(ptr->rightChild));
}
return (value);
}
The value returned by the function DoSomething when a pointer to the root of non-empty tree is passed
1) The number of leaf nodes in the tree
2) The number of nodes in the tree
3) The number of internal nodes in the tree
4) The height of the tree-

11) #include<stdio.h>
int main()
{
int a = 1, *p, **pp;
p = &a;
pp = p;
printf("%d", **pp);
}

1) Garbage value
2) Compiler error
3) Segmentation fault
4) 1

12) Assuming a little endian machine, what will be the output of the following program?

#include<stdio.h>
fun(int a)
{
char *arr[] = {"0000",
"0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","
1101","1110","1111"};
unsigned char* p = (unsigned char*) &a ;
p+=3;
int i;
for(i = 0; i < sizeof a; i++)
{
int d = (*p)>>4;
printf("%s", arr[d]);
d = (*p) & 0xf;
printf("%s ", arr[d]);
p--;
}}
int main()
{
int a;
scanf("%d", &a);
fun(a);
}
1) Runtime error
2) Compiler error
3) Print the binary of the input number
4) Compiler dependent output

13) Consider the following function


[P1] int *P(void)
{
Int x=10;
return(&x);
}
[P2] int *P(void)
{
Int *px;
*px=10;
return px;
}
[P3] int *P(void)
{
Int *px;
px=(int)malloc(sizeof(int));
*px=10;
return px;
}

What is the above three functions are likely to cause problem with pointers?

1) Only P3
2) Only P1 and P3
3) Only P1 and P2
4) P1,P2 and P3

You might also like