IMP MCQ
IMP MCQ
IMP MCQ
{
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?
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.
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;
}
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
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
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