C MCQ Questions and Answers on Functions and Pointers 3

Image
C MCQ Questions and Answers

Study C MCQ Questions and Answers on Functions and Pointers. Questions are on Recursion, Pass by Value and Pass By Reference. Attend C technical interviews easily after reading these Multiple Choice Questions.

Go through C Theory Notes on Functions before reading questions.



1) What are the data type of variables that can be returned by a C Function.?
A) int, float, double, char
B) struct, enum
C) Pointers to variables, arrays, functions, struct variables, enum variables etc
D) All the above
Answer [=]
D
2) What is the output of a C Program with functions and pointers.?
void texas(int *,int *);
int main()
{
    int a=11, b=22;
    printf("Before=%d %d, ", a, b);
    texas(&a, &b);
    printf("After=%d %d", a, b);
    
    return 0;
}
void texas(int *i, int *j)
{
    *i = 55;
    *j = 65;
}
A) Before=11 22, After=11 22
B) Before=11 22, After=55 65
C) Before=11 22, After=0 0
D) Compiler error
Answer [=]
B
Explanation:

a and b are passed through Call By Reference &a, &b. So the passed addresses are collected by variables i and j. So changes made using pointers are retained even after the function execution is over.

3) What is the output of C Program with functions.?
int main()
{
    int a = 66;
    printf("%d %d %d,\n", a, ++a, a++);
    a = 66;
    printf("%d %d %d,\n", ++a, a++, a);
    a = 66;
    printf("%d %d %d", ++a, a, a++);
    return 0;
}
A)
68 68 66,
68 66 66,
68 68 66
B)
68 68 66,
66 66 68,
68 68 66
C)
68 68 66,
68 66 68,
68 68 66
D)
68 68 66,
68 66 68,
68 68 68
Answer [=]
C
Explanation:

Order of processing arguments is right to left. First keep 3 placeholder buckets. Right to left check for higher priority of variables like ++a or a++ etc. Always process higher priority variables first. At last, fill the value of low priority variable a.

4) What is the output of a C program.?
int main()
{
    int a = 1;
    printf("%d %d %d,\n", a, ++a, a++);
    a = 1;
    printf("%d %d %d,\n", ++a, a++, a);
    a = 1;
    printf("%d %d %d", ++a, a, a++);
    return 0;
}
A)
3 3 1,
3 3 3,
3 3 1
B)
3 1 1,
3 1 3,
3 3 1
C)
3 3 1,
3 1 3,
3 1 1
D)
3 3 1,
3 1 3,
3 3 1
Answer [=]
D
5) What is the output of a C Program.?
void show(int,int,int);
int main()
{
    int a = 1;
    show(++a, a++, a);
    return 0;
}
void show(int i, int j, int k)
{
    printf("%d %d %d,\n", i, j, k);
}
A)
1 1 3,
B)
3 1 3,
C)
3 1 1,
D)
3 3 3,
Answer [=]
B
Explanation:

1) ++a, 2) a++ 3) a. Second and third arguments have higher priority than third parameter A. So Second is processed. Than first is processed. Finally the actual value of a at that point is given to third place holder.

6) What is the output of C Program with pointers.?
int main()
{
    int a = 4;
    int *p;
    p=&a;
    while(*p > 0)
    {
        printf("%d ", *p);
        (*p)--;
    }
    return 0;
}
A) 0 0 0 0
B) 4 4 4 4
C) 4 3 2 1
D) Compiler error
Answer [=]
C
Explanation:

Notice the decrement operation on variable "a" using (*p)--. Without parantheses, variable is not decremented but memory location pointed is decremented and pointed to a garbage value.

7) What is the output of C Program with functions.?
void show();
int show();
int main()
{
    printf("ANT\n");
    return 0;
}
void show()
{
     printf("Integer") ;
}
int show()
{
    printf("Void");
}
A) ANT
B) Integer
C) Void
D) Compiler error
Answer [=]
D
Explanation:

You can not declare same function with different return types. Here both void show() and int show() can not be written.



8) What is the output of C Program with functions.?
void show(int);
void show(float);
int main()
{
    printf("ANT\n");
    return 0;
}
void show(int a)
{
     printf("Integer") ;
}
void show(float b)
{
    printf("Void");
}
A) Integer Void
B) ANT Integer Void
C) ANT
D) Compiler error
Answer [=]
D
Explanation:

You can not re declare and redefine the a function with same name and different arguments. Function name can not be duplicate in any combination.

9) What is the output of C Program with pointers.?
int main()
{
    int a=10;
    int *p, **q;
    p=&a;
    q=&p;
    printf("%d ", a);
    *p=15;
    printf("%d ", a);
    **q=20;
    printf("%d ", a);
    return 0;
}
A) 10 10 10
B) 10 0 0
C) 10 15 20
D) Compiler error
Answer [=]
C
Explanation:

Here p is pointer to an integer. q is a pointer to pointer. That is why you should use two STARs **.

10) What is the output of C Program with pointers.?
int main()
{
    int a=20;
    int *p, *q;
    p=&a;
    q=p;
    printf("%d ", a);
    *p=30;
    printf("%d ", a);
    *q=40;
    printf("%d ", a);
    return 0;
}
A) 20 0 0
B) 20 20 20
C) 20 30 40
D) Compiler error
Answer [=]
C
Explanation:

P is already a pointer to an integer a. Q copied P. So Q is also a pointer to the same integer a.

11) What is the output of C Program with pointers.?
int main()
{
    int a=20;
    //a memory location=1234
    printf("%d %d %d", a, &a, *(&a));
    return 0;
}
A) 20 20 20
B) 20 1234 1234
C) 20 1234 20
D) 20 20 20
Answer [=]
C
Explanation:

If VALUEAT operator * and ADDRESSOF operator & are used immediately, it is same as the variable itself. So *(&a) == a.

12) What is the output of C Program with functions.?
int main()
{
    int a=20;
    printf("CINEMA ");
    return 1;
    printf("DINOSAUR");
    return 1;
}
A) CINEMA DINOSAUR
B) CINEMA
C) DINOSAUR
D) Compiler error
Answer [=]
B
Explanation:

There are two return statements in main() function. Only first return is executed. All the statements or lines of code below first return is not reachable and hence not executed.

13) What is the output of C Program with recursive function.?
int sum(int);
int main()
{
    int b;
    b = sum(4);
    printf("%d", b);
    
}
int sum(int x)
{
    int k=1;
    if(x<=1)
     return 1;
    k = x + sum(x-1);
    return k;
}
A) 10
B) 11
C) 12
D) 15
Answer [=]
A
Explanation:

4 + 3 + 2 + 1 = 10.

14) What is the output of C Program with Recursive Function.?
int mul(int);
int main()
{
    int b;
    b = mul(3);
    printf("%d", b);
}
int mul(int x)
{
    if(x<=1)
     return 1;
    return (x * mul(x-1));
}
A) 2
B) 3
C) 6
D) 1
Answer [=]
C
Explanation:

3*2*1 = 6.



15) A recursive function can be replaced with __ in c language.
A) for loop
B) while loop
C) do while loop
D) All the above
Answer [=]
D
16) A recursive function is faster than __ loop.
A) for
B) while
C) do while
D) None of the above
Answer [=]
D
Explanation:

Yes. Recursion is slow. Variable are kept and remove on STACK memory multiple times.

17) A recursive function without If and Else conditions will always lead to.?
A) Finite loop
B) Infinite loop
C) Incorrect result
D) Correct result
Answer [=]
B
Explanation:

Yes. To come out of recursion, you must use IF or ELSE blocks.

18) What is the C keyword that must be used to achieve expected result using Recursion.?
A) printf
B) scanf
C) void
D) return
Answer [=]
D
Explanation:

Recursion is entirely based on RETURN value; statement.

19) How many functions are required to create a recursive functionality.?
A) One
B) Two
C) More than two
D) None of the above
Answer [=]
A
Explanation:

Only one function is required to achieve recursion. Recursion is calling the same function within. How ever to use the value or output of all recursive calls, at least one main() function is also required.

20) Choose a correct statement about Recursive Function in C language.
A) Each recursion creates new variables at different memory locations
B) There is no limit on the number of Recursive calls
C) Pointers can also be used with Recursion but with difficulty.
D) All the above
Answer [=]
D