Here is a listing of C interview questions on “Character Pointers and Functions” along with answers, explanations and/or solutions:
1. Comment on the output of this C code?
char *str = "This" //Line 1
char *ptr = "Program\n"; //Line 2
str = ptr; //Line 3
printf("%s, %s\n", str, ptr); //Line 4
a) Memory holding “this” is cleared at line 3
b) Memory holding “this” loses its reference at line 3
c) You cannot assign pointer like in Line 3
d) Output will be This, Program
2. What type initialization is needed for the segment “ptr = ‘3’;” to work?
a) char *ptr = “Hello!”;
b) char ptr = “Hello!”;
c) Both (a) and (b)
d) None of the mentioned
3. The syntax for constant pointer to address (i.e., fixed pointer address) is:
a) const <type> * <name> b) <type> * const <name> c) <type> const * <name> d) Both (a) and (c)
4. Comment on the output of this C code?
int add(int a, int b)
return a + b;
int (*fn_ptr)(int, int);
fn_ptr = add;
printf("The sum of two numbers is: %d", (int)fn_ptr(2, 3));
a) Compile time error, declaration of a function inside main.
b) Compile time error, no definition of function fn_ptr.
c) Compile time error, illegal application of statement fn_ptr = add.
d) No Run time error, output is 5.
5. The correct way to declare and assign a function pointer is done by:
(Assuming the function to be assigned is “int multi(int, int);”)
a) int (*fn_ptr)(int, int) = multi;
b) int *fn_ptr(int, int) = multi;
c) int *fn_ptr(int, int) = &multi;
d) Both (b) & (c)
6. Calling a function f with a an array variable a where a is an array, is equivalent to
b) f(*(a + 3))
d) All of the mentioned
7. What is the output of this C code?
void f(char *k)
k = 'm';
char s = "hello";
8.What is the output of this C code?
char s = "hello";
a) Compile time error
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all features of C programming language, here is complete set of 1000+ Multiple Choice Questions and Answers on C.