Here is a listing of C Objective Questions on “Character Pointers and Functions” along with answers, explanations and/or solutions:
1. Comment on the output of the following C code.
-
#include
-
int main()
-
{
-
char *str = "This" //Line 1
-
char *ptr = "Programn"; //Line 2
-
str = ptr; //Line 3
-
printf("%s, %sn", 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
Answer: b
Clarification: None.
2. What type of initialization is needed for the segment “ptr[3] = ‘3’;” to work?
a) char *ptr = “Hello!”;
b) char ptr[] = “Hello!”;
c) both char *ptr = “Hello!”; and char ptr[] = “Hello!”;
d) none of the mentioned
Answer: b
Clarification: None.
3. What is the syntax for constant pointer to address (i.e., fixed pointer address)?
a) const
b)
c)
d) none of the mentioned
Answer: b
Clarification: None.
4. What will be the output of the following C code?
-
#include
-
int add(int a, int b)
-
{
-
return a + b;
-
}
-
int main()
-
{
-
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
Answer: d
Clarification: None.
5. What is the correct way to declare and assign a function pointer?
(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) none of the mentioned
Answer: a
Clarification: None.
6. Calling a function f with a an array variable a[3] where a is an array, is equivalent to __________
a) f(a[3])
b) f(*(a + 3))
c) f(3[a])
d) all of the mentioned
Answer: d
Clarification: None.
7. What will be the output of the following C code?
-
#include
-
void f(char *k)
-
{
-
k++;
-
k[2] = 'm';
-
}
-
void main()
-
{
-
char s[] = "hello";
-
f(s);
-
printf("%cn", *s);
-
}
a) h
b) e
c) m
d) o;
Answer: a
Clarification: None.
8. What will be the output of the following C code?
-
#include
-
void main()
-
{
-
char s[] = "hello";
-
s++;
-
printf("%cn", *s);
-
}
a) Compile time error
b) h
c) e
d) o
Answer: a
Clarification: None.