C Programming Questions and Answers – Character Pointers and Functions – 2

«
»

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Character Pointers and Functions – 2”.

Pre-requisite for C Character Pointers and Functions MCQ set: Video Tutorial on C Pointers.

advertisement

1. Comment on the output of the following C code.

  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *str = "This" //Line 1
  5.         char *ptr = "Program\n"; //Line 2
  6.         str = ptr; //Line 3
  7.         printf("%s, %s\n", str, ptr); //Line 4
  8.     }

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
View Answer

Answer: b
Explanation: None.
advertisement
advertisement

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
View Answer

Answer: b
Explanation: None.
Participate in C Programming Certification Contest of the Month Now!
advertisement

3. What is the syntax for constant pointer to address (i.e., fixed pointer address)?
a) const <type> * <name>
b) <type> * const <name>
c) <type> const * <name>
d) none of the mentioned
View Answer

Answer: b
Explanation: None.

4. What will be the output of the following C code?

advertisement
  1.     #include <stdio.h>
  2.     int add(int a, int b)
  3.     {
  4.         return a + b;
  5.     }
  6.     int main()
  7.     {
  8.         int (*fn_ptr)(int, int);
  9.         fn_ptr = add;
  10.         printf("The sum of two numbers is: %d", (int)fn_ptr(2, 3));
  11.     }

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
View Answer

Answer: d
Explanation: None.
advertisement

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
View Answer

Answer: a
Explanation: 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
View Answer

Answer: d
Explanation: None.

7. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     void f(char *k)
  3.     {
  4.         k++;
  5.         k[2] = 'm';
  6.     }
  7.     void main()
  8.     {
  9.         char s[] = "hello";
  10.         f(s);
  11.         printf("%c\n", *s);
  12.     }

a) h
b) e
c) m
d) o;
View Answer

Answer: a
Explanation: None.

8. What will be the output of the following C code?

  1.     #include <stdio.h>
  2.     void main()
  3.     {
  4.         char s[] = "hello";
  5.         s++;
  6.         printf("%c\n", *s);
  7.     }

a) Compile time error
b) h
c) e
d) o
View Answer

Answer: a
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement

Leave a Comment

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.