C Programming Questions and Answers – Structures and Functions – 2

«
»
Sanfoundry’s 1000+ Interview Questions & Answers on C helps anyone preparing for Huawei and other companies C interviews. One should practice these 1000+ interview questions and answers continuously for 2-3 months to clear Huawei interviews on C Programming language.

Here is a listing of C interview questions on “Structures and Functions” along with answers, explanations and/or solutions:

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

advertisement
  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     int main()
  8.     {
  9.         struct point p = {1};
  10.         struct point p1 = {1};
  11.         if(p == p1)
  12.             printf("equal\n");
  13.         else
  14.             printf("not equal\n");
  15.     }

a) Compile time error
b) equal
c) depends on the standard
d) not equal
View Answer

Answer: a
Explanation: None.
advertisement
advertisement

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

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     struct notpoint
  8.     {
  9.         int x;
  10.         int y;
  11.     };
  12.     struct point foo();
  13.     int main()
  14.     {
  15.         struct point p = {1};
  16.         struct notpoint p1 = {2, 3};
  17.         p1 = foo();
  18.         printf("%d\n", p1.x);
  19.     }
  20.     struct point foo()
  21.     {
  22.         struct point temp = {1, 2};
  23.         return temp;
  24.     }

a) Compile time error
b) 1
c) 2
d) Undefined behaviour
View Answer

Answer: a
Explanation: None.
advertisement

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

advertisement
  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     struct notpoint
  8.     {
  9.         int x;
  10.         int y;
  11.     };
  12.     int main()
  13.     {
  14.         struct point p = {1};
  15.         struct notpoint p1 = p;
  16.         printf("%d\n", p1.x);
  17.     }

a) Compile time error
b) 1
c) 0
d) Undefined
View Answer

Answer: a
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     struct notpoint
  8.     {
  9.         int x;
  10.         int y;
  11.     };
  12.     void foo(struct point);
  13.     int main()
  14.     {
  15.         struct notpoint p1 = {1, 2};
  16.         foo(p1);
  17.     }
  18.     void foo(struct point p)
  19.     {
  20.         printf("%d\n", p.x);
  21.     }

a) Compile time error
b) 1
c) 0
d) Undefined
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1 = {1, 2};
  11.         foo(&p1);
  12.     }
  13.     void foo(struct point *p)
  14.     {
  15.         printf("%d\n", *p.x++);
  16.     }

a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct point
  3.     {
  4.         int x;
  5.         int y;
  6.     };
  7.     void foo(struct point*);
  8.     int main()
  9.     {
  10.         struct point p1 = {1, 2};
  11.         foo(&p1);
  12.     }
  13.     void foo(struct point *p)
  14.     {
  15.         printf("%d\n", *p->x++);
  16.     }

a) Compile time error
b) 1
c) Segmentation fault/code crash
d) 2
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct student fun(void)
  3.     {
  4.         struct student
  5.         {
  6.             char *name;
  7.         };
  8.         struct student s;
  9.         s.name = "alan";
  10.         return s;
  11.     }
  12.     void main()
  13.     {
  14.         struct student m = fun();
  15.         printf("%s", m.name);
  16.     }

a) Compile time error
b) alan
c) Nothing
d) Varies
View Answer

Answer: a
Explanation: None.

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

  1.     #include <stdio.h>
  2.     struct student
  3.     {
  4.         char *name;
  5.     };
  6.     struct student fun(void)
  7.     {
  8.         struct student s;
  9.         s.name = "alan";
  10.         return s;
  11.     }
  12.     void main()
  13.     {
  14.         struct student m = fun();
  15.         printf("%s", m.name);
  16.     }

a) Nothing
b) alan
c) Run time error
d) Varies
View Answer

Answer: b
Explanation: None.

Sanfoundry Global Education & Learning Series – C Programming Language.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter