C Programming Multiple Choice Question - Functions And Pointers
This section focuses on the "Functions And Pointers" of the C programming. These Multiple Choice Questions (mcq) should be practiced to improve the C programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. The keyword used to transfer control from a function back to the calling function is
A. switch
B. goto
C. return
D. exit
View Answer
Ans : C
Explanation: The keyword return is used to transfer control from a function back to the calling function.
2. What is the default return type if it is not specified in function definition?
A. void
B. int
C. float
D. short int
View Answer
Ans : B
Explanation: None
3. The default parameter passing mechanism is
A. Call by value
B. call by reference
C. call by value result
D. None
View Answer
Ans : A
Explanation: None.
4. Pick the correct statements.
I. The body of a function should have only one return statement
II. The body of a function may have many return statements.
III. function can return only one value to the calling environment.
IV. If return statement is omitted, then the function does its job but returns no value to the calling environment.
A. I and II
B. I and III
C. II and III
D. II anf IV
View Answer
Ans : C
Explanation: None
5. Functions can return structure in c?
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: Yes, functions can return structure
6. Forward declaration is absolutely necessary
A. If a function returns a non-integer quantity
B. If the function call precedes its definition
C. If the function call precedes its definition and the function returns a non integer quantity
D. None of the above
View Answer
Ans : C
Explanation: If the function call precedes its definition and the function returns a non integer quantity
7. In C, what is the meaning of following function prototype with empty parameter list
void fun()
{
/* .... */
}
A. Function can only be called without any parameter
B. Function can be called with any number of parameters of any types
C. Function can be called with any number of integer parameters.
D. Function can be called with one integer parameter.
View Answer
Ans : B
Explanation: Empty list in C mean that the parameter list is not specified and function can be called with any parameters. In C, to declare a function that can only be called without any parameter, we should use "void fun(void)" As a side note, in C++, empty list means function can only be called without any parameter. In C++, both void fun() and void fun(void) are same.
8. A pointer is
A. A variable that stores address of an instruction
B. A variable that stores address of other variable
C. A keyword used to create variables
D. None of these
View Answer
Ans : B
Explanation: a pointer is a programming language object that stores the memory address of another value located in computer memory. A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.
9. The reason for using pointers in a Cprogram is
A. Pointers allow different functions to share and modify their local variables.
B. To pass large structures so that complete copy of the structure can be avoided.
C. Pointers enable complex "linked" data structures like linked lists and binary trees.
D. All of the above
View Answer
Ans : D
Explanation: (A) With pointers, address of variables can be passed different functions can use this address to access the variables. (B) When large structure variables passed or returned, they are copied as everything is passed and returned by value in C. This can be costly with structure containing large data. To avoid this copying of large variables, we generally use poitner for large structures so that only address is copied. (C) With pointers, we can implement "linked" data structures. Java uses reference variables to implement these data structures. Note that C doesn't support reference variables.
10. How can you write a[i][j][k][l] in equivalent pointer expression?
A. (((***(a+i)+j)+k)+l)
B. ((**(*(a+i)+j)+k)+l)
C. (*(*(*(a+i)+j)+k)+l)
D. *(*(*(*(a+i)+j)+k)+l)
View Answer
Ans : D
Explanation: *(*(*(*(a+i)+j)+k)+l)
11. What is wild pointer?
A. Pointer which is wild in nature
B. Pointer which has no value.
C. Pointer which is not initialized
D. None
View Answer
Ans : C
Explanation: Uninitialized pointers are known as wild pointers because they point to some arbitrary memory location and may cause a program to crash or behave badly.
12. Address stored in the pointer variable is of type __________.
A. Integer
B. Float
C. Array
D. Character
View Answer
Ans : A
Explanation: Pointer Variable of any type contains integer address because address of variable is always integer.
13. In order to fetch the address of the variable we write preceding _________ sign before variable name.
A. Percent(%)
B. Comma(,)
C. Ampersand(&)
D. Asteric(*)
View Answer
Ans : C
Explanation: "&" is used to get an address of the variable.
14. Comment on this const int *ptr;
A. You cannot change the value pointed by ptr
B. You cannot change the pointer ptr itself
C. Both (a) and (b)
D. You can change the pointer as well as the value pointed by it
View Answer
Ans : A
Explanation: None
15. Choose the best one - prior to using a pointer variable
A. It should be declared.
B. It should be initialized
C. It should be both declared and initialized.
D. None of the above.
View Answer
Ans : C
Explanation: None
16. Which of the following statements are correct about the given program?
#include <stdio.h>
int main()
{
printf("%p", main());
return 0;
}
A. Prints garbage value infinite times
B. Error
C. Runs infinite times without printing anything
D. None of the above
View Answer
Ans : C
Explanation: None
17. What is the output of this program?
#include <stdio.h>
void fun(int a, ...)
{
printf("%d ", a);
}
int main()
{
fun(1,2,3,4);
fun(5,6,7,8,9);
return 0;
}
A. 1 5
B. 2 5
C. 5 1
D. Compilation Error
View Answer
Ans : A
Explanation: In c three continuous dots is known as ellipsis which is variable number of arguments of function. .
18. What is the meaning of using static before function declaration?
static int demo(int a, int b)
{
return (a + b);
}
A. Access to static functions is restricted to the file where they are declared
B. Static means nothing, sum() is same without static keyword.
C. Function need not to be declared before its use
D. None of the above
View Answer
Ans : A
Explanation: static functions are restricted to the file where they are declared.
19. What is the output of this program?
#include <stdio.h>
int demo()
{
static int i = 0;
printf("%d ",i++);
}
int main()
{
for(int j = 0 ; j < 5 ; j++ )
{
demo();
}
}
A. 0 0 0 0 0
B. 1 1 1 1 1
C. 1 2 3 4 5
D. 0 1 2 3 4
View Answer
Ans : D
Explanation: Value of i will be initialized once and then the previous value is used when function is called . Initially i = 0 and function is called value of i is printed and incremented by 1 and now again function is called the value printed will be 1 and then incremented and process repeat till the funtion is called .
20. What is the output of this program?
#include <stdio.h>
int test()
{
static int n = 10;
return n--;
}
int main()
{
for(test(); test(); test())
printf("%d ", test());
return 0;
}
A. 7 4 1
B. 8 5 2
C. Infinite loop
D. Compilation Error
View Answer
Ans : B
Explanation: None
21. What is the output of this program?
int main()
{
void test(), temp();
temp();
}
void test()
{
printf("2 ");
}
void temp()
{
printf("1 ");
test();
}
A. 1 0
B. 0 1
C. Compile time error as foo is local to main
D. Compile time error due to declaration of functions inside main
View Answer
Ans : A
Explanation: None
22. What is the output of this program?
#include <stdio.h>
void main()
{
test();
void test()
{
printf("1");
}
}
A. 1
B. Compilation Error
C. Run Time Error
D. None of the above
View Answer
Ans : B
Explanation: test is called before declaration or defination .
23. What is the output of this program?
#include <stdio.h>
void test(int * , int *);
void main()
{
int a = 5 , b = 6;
test(&a,&b);
printf("%d %d",a,b);
}
void test(int *p, int *q)
{
*p = *p * *q;
*q = *p / *q;
*p = *p / *q;
}
A. 30 5
B. 6 5
C. 5 6
D. None of the above
View Answer
Ans : B
Explanation: function test will swap the values .
24. What is the output of this program?
#include <stdio.h>
void test(char**);
int main()
{
char *argv[] = {"a", "b", "c", "d"};
test(argv);
return 0;
}
void test(char **p)
{
char *m;
m = (p+= sizeof(int))[-2];
printf("%s", m);
}
A. a
B. b
C. c
D. d
View Answer
Ans : C
Explanation: None
25. What is the output of this program?
#include <stdio.h>
int main()
{
char *ptr = "Hello";
printf("%c", *&*&*ptr);
return 0;
}
A. H
B. Compilation Error
C. Run Time Error
D. Garbage Value
View Answer
Ans : A
Explanation: The operator * is used for dereferencing and the operator & is used to get the address. These operators cancel out effect of each other when used one after another. Therefore here only * left after cancelation so H will be printed since value at base address is H.
26. What is the output of this program?
#include <stdio.h>
void test(int *a, int *b)
{
a = b;
*a = 15;
}
int x = 10, y = 20;
int main()
{
test(&x, &y);
printf("%d %d", x, y);
return 0;
}
A. 15 15
B. 10 15
C. 10 20
D. 15 20
View Answer
Ans : B
Explanation: None
27. What does the following statment mean?
int (*ptr)[5];
A. ptr is an array of 5 integers
B. ptr is a pointer to an array of 5 integers
C. ptr is array of pointers to 5 integers
D. ptr is an pointer to array
View Answer
Ans : B
Explanation: None
28. What is the output of this program?
#include <stdio.h>
int main()
{
int a = 5;
void *ptr = &a ;
printf("%f", *(float*)ptr);
return 0;
}
A. 5
B. 5
C. 0
D. 0
View Answer
Ans : C
Explanation: None
29. What is the output of the given code ?
#include <stdio.h>
void main()
{
int a = 0 ;
int *const ptr = &a ;
printf("%p ", ptr);
ptr++;
printf("%p ", ptr);
}
A. 0 1
B. compilation Error
C. Run Time Error
D. None of the above
View Answer
Ans : B
Explanation: constant pointer cannot be modified .
30. What is the output of this program?
#include <stdio.h>
void main()
{
int a = 5 ;
int *b = &a ;
int **c = &b ;
printf("%d %d %d", a, *b, **c);
}
A. 5 garbage value garbage value
B. 5 5 garbage value
C. 5 5 5
D. Error
View Answer
Ans : C
Explanation: None
31. What is the output of this program?
#include <stdio.h>
void main()
{
int arr[3] = {1, 2, 3};
int *ptr = arr;
int **temp = &ptr ;
printf("%p %p", *temp, arr);
}
A. Same Address
B. Different Address
C. 1 2
D. 2 3
View Answer
Ans : A
Explanation: None
32. The pointer ptr points to which string?
char *ptr;
char myString[]="letsfind";
ptr=myString
ptr+=5;
A. find
B. ind
C. letsf
D. f
View Answer
Ans : B
Explanation: ptr+=5 means ptr=ptr+5
That means the pointer variable is incremented by 5. Hence it is pointing to the 6th location.
33. What will be the output ?
void main()
{
char far *farther, *farthest;
printf("%d %d", sizeof(farther), sizeof(farthest));
}
A. 4 2
B. 2 2
C. 4 4
D. 2 4
View Answer
Ans : A
Explanation: None
34. What is the output of this program?
#include <stdio.h>
void test(int * , int *);
void main()
{
int a = 5 , b = 6;
test(&a,&b);
printf("%d %d",a,b);
}
void test(int *p, int *q)
{
*p = *p * *q;
*q = *p + *q;
*p = *p - *q;
}
A. -6 36
B. 6 36
C. 36 6
D. -42
View Answer
Ans : A
Explanation: -6 36 is the output of this program.
35. Will the program compile?
#include<stdio.h>
int main()
{
char str[5] = "Letsfind";
return 0;
}
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : A
Explanation: C doesn't do array bounds checking at compile time, hence this compiles.But, the modern compilers like Turbo C++ detects this as 'Error: Too many initializers'.GCC would give you a warning.
36. Are the three declarations char **apple, char *apple[], and char apple[][] same?
A. TRUE
B. FALSE
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: Are the three declarations char **apple, char *apple[], and char apple[][] is not same.
37. Is the NULL pointer same as an uninitialised pointer?
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: No the NULL pointer same as an uninitialised pointer.
38. Which of the following statements correct about k used in the below statement?
char ****k;
A. k is a pointer to a pointer to a pointer to a char
B. k is a pointer to a pointer to a pointer to a pointer to a char
C. k is a pointer to a char pointer
D. k is a pointer to a pointer to a char
View Answer
Ans : B
Explanation: k is a pointer to a pointer to a pointer to a pointer to a char
39. Is there any difference between the following two statements?
char *p=0;
char *t=NULL;
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: NULL is #defined as 0 in the 'stdio.h' file. Thus, both p and t are NULL pointers.
40. Is this a correct way for NULL pointer assignment?
int i=0;
char *q=(char*)i;
A. Yes
B. No
C. May Be
D. Can't Say
View Answer
Ans : B
Explanation: The correct way is char *q=0 (or) char *q=(char*)0.
Also check :
Discussion