C Programming Questions and Answers on Character Pointers and Functions for Freshers
https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_18.html
1. What is the output of this C code?
#includeint main()
{char *str = "hello, world\n";
char *strc = "good morning\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}
a) hello, world
b) Crash/segmentation fault
c) Undefined behaviour
d) Run time error
b) Crash/segmentation fault
c) Undefined behaviour
d) Run time error
Answer: b
2. What is the output of this C code?
#includeint main()
{char *str = "hello world";
char strc[] = "good morning india\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}
a) hello world
b) hello worldg india
c) Compile time error
d) Undefined behaviour
b) hello worldg india
c) Compile time error
d) Undefined behaviour
Answer: a
3. What is the output of this C code?
#includeint main()
{char *str = "hello, world!!\n";
char strc[] = "good morning\n";
strcpy(strc, str);
printf("%s\n", strc);
return 0;
}
a) hello, world!!
b) Compile time error
c) Undefined behaviour
d) Segmenation fault
b) Compile time error
c) Undefined behaviour
d) Segmenation fault
Answer: c
4. What is the output of this C code?
#includeint main()
{char *str = "hello, world\n";
str[5] = '.';
printf("%s\n", str);
return 0;
}
a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
b) hello, world
c) Compile error
d) Segmentation fault
Answer: d
5. What is the output of this C code?
#includeint main()
{char str[] = "hello, world";
str[5] = '.';
printf("%s\n", str);
return 0;
}
a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
b) hello, world
c) Compile error
d) Segmentation fault
Answer: a
6. What is the output of this C code?
#includeint main()
{char *str = "hello world";
char strary[] = "hello world";
printf("%d %d\n", sizeof(str), sizeof(strary));
return 0;
}
a) 11 11
b) 12 12
c) 4 12
d) 4 11
b) 12 12
c) 4 12
d) 4 11
Answer: c
7. What is the output of this C code?
#includeint main()
{char *str = "hello world";
char strary[] = "hello world";
printf("%d %d\n", strlen(str), strlen(strary));
return 0;
}
a) 11 11
b) 12 11
c) 11 12
d) x 11 where x can be any positive integer.
b) 12 11
c) 11 12
d) x 11 where x can be any positive integer.
Answer: a
8. What is the output of this C code?
#includevoid f(char *k)
{k++;k[2] = 'm';
printf("%c\n", *k);
}void main()
{char s[] = "hello";
f(s);
}
a) l
b) e
c) h
d) o
b) e
c) h
d) o
Answer: b
9. What is the output of this C code?
#includevoid fun(char *k)
{printf("%s", k);
}void main()
{char s[] = "hello";
fun(s);
}
a) hello
b) Run time error
c) Nothing
d) h
b) Run time error
c) Nothing
d) h
Answer: a
10. Comment on the output of this C code?
#includeint main()
{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
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
11. What type 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
12. The syntax for constant pointer to address (i.e., fixed pointer address) is:
a) const
b)
c)
d) none of the mentioned
Answer: b
13. Comment on the output of this C code?
#includeint 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
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
14. The correct way to declare and assign a function pointer is done by:
(Assuming the function to be assigned is “int multi(int, int);”)
(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
15. 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
16. What is the output of this C code?
#includevoid f(char *k)
{k++;k[2] = 'm';
}void main()
{char s[] = "hello";
f(s);
printf("%c\n", *s);
}
a) h
b) e
c) m
d) o;
b) e
c) m
d) o;
Answer: a
17.What is the output of this C code?
#includevoid main()
{char s[] = "hello";
s++;printf("%c\n", *s);
}
a) Compile time error
b) h
c) e
d) o
b) h
c) e
d) o
Answer: a
