C Programming Questions and Answers on Character Pointers and Functions for Freshers

1. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, world\n";
  5.         char *strc = "good morning\n";
  6.         strcpy(strc, str);
  7.         printf("%s\n", strc);
  8.         return 0;
  9.     }
a) hello, world
b) Crash/segmentation fault
c) Undefined behaviour
d) Run time error
Answer: b
2. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strc[] = "good morning india\n";
  6.         strcpy(strc, str);
  7.         printf("%s\n", strc);
  8.         return 0;
  9.     }
a) hello world
b) hello worldg india
c) Compile time error
d) Undefined behaviour
Answer: a
3. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, world!!\n";
  5.         char strc[] = "good morning\n";
  6.         strcpy(strc, str);
  7.         printf("%s\n", strc);
  8.         return 0;
  9.     }
a) hello, world!!
b) Compile time error
c) Undefined behaviour
d) Segmenation fault
Answer: c
4. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello, world\n";
  5.         str[5] = '.';
  6.         printf("%s\n", str);
  7.         return 0;
  8.     }
a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
Answer: d
5. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char str[] = "hello, world";
  5.         str[5] = '.';
  6.         printf("%s\n", str);
  7.         return 0;
  8.     }
a) hello. world
b) hello, world
c) Compile error
d) Segmentation fault
Answer: a
6. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strary[] = "hello world";
  6.         printf("%d %d\n", sizeof(str), sizeof(strary));
  7.         return 0;
  8.     }
a) 11 11
b) 12 12
c) 4 12
d) 4 11
Answer: c
7. What is the output of this C code?
  1.     #include 
  2.     int main()
  3.     {
  4.         char *str = "hello world";
  5.         char strary[] = "hello world";
  6.         printf("%d %d\n", strlen(str), strlen(strary));
  7.         return 0;
  8.     }
a) 11 11
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?
  1.     #include 
  2.     void f(char *k)
  3.     {
  4.         k++;
  5.         k[2] = 'm';
  6.         printf("%c\n", *k);
  7.     }
  8.     void main()
  9.     {
  10.         char s[] = "hello";
  11.         f(s);
  12.     }
a) l
b) e
c) h
d) o
Answer: b
9. What is the output of this C code?
  1.     #include 
  2.     void fun(char *k)
  3.     {
  4.         printf("%s", k);
  5.     }
  6.     void main()
  7.     {
  8.         char s[] = "hello";
  9.         fun(s);
  10.     }
a) hello
b) Run time error
c) Nothing
d) h
Answer: a
10. Comment on the output of this C code?
  1.     #include 
  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
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) * const
c) const *
d) none of the mentioned
Answer: b
13. Comment on the output of this C code?
  1.     #include 
  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
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);”)

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?
  1.     #include 
  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;
Answer: a
17.What is the output of this C code?
  1.     #include 
  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
Answer: a

Related

Multiple Choice Questions 1846915036188722372

Post a Comment

emo-but-icon

item