C Programming Questions and Answers – Structures and Functions – 1

«
»
This section on C programming questions and answers focuses on “Structures and Functions”. One shall practice these questions to improve their C programming skills needed for various interviews (campus interviews, walkin interviews, company interviews), placements, entrance exams and other competitive exams. These questions can be attempted by anyone focusing on learning C Programming language. They can be a beginner, fresher, engineering graduate or an experienced IT professional. Our C programming questions come with detailed explanation of the answers which helps in better understanding of C concepts.

Here is a listing of C programming 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 student
  3.     {
  4.         char *name;
  5.     };
  6.     struct student s;
  7.     struct student fun(void)
  8.     {
  9.         s.name = "newton";
  10.         printf("%s\n", s.name);
  11.         s.name = "alan";
  12.         return s;
  13.     }
  14.     void main()
  15.     {
  16.         struct student m = fun();
  17.         printf("%s\n", m.name);
  18.         m.name = "turing";
  19.         printf("%s\n", s.name);
  20.     }

a) newton alan alan
b) alan newton alan
c) alan alan newton
d) compile time error
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 student
  3.     {
  4.         char *name;
  5.     };
  6.     void main()
  7.     {
  8.         struct student s, m;
  9.         s.name = "st";
  10.         m = s;
  11.         printf("%s%s", s.name, m.name);
  12.     }

a) Compile time error
b) Nothing
c) Junk values
d) st st
View Answer

Answer: d
Explanation: None.
advertisement

3. Which of the following return-type cannot be used for a function in C?
a) char *
b) struct
c) void
d) none of the mentioned
View Answer

Answer: d
Explanation: None.
advertisement

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

  1.     #include <stdio.h>
  2.     struct temp
  3.     {
  4.         int a;
  5.     } s;
  6.     void func(struct temp s)
  7.     {
  8.         s.a = 10;
  9.         printf("%d\t", s.a);
  10.     }
  11.     main()
  12.     {
  13.         func(s);
  14.         printf("%d\t", s.a);
  15.     }

a) 10 (Garbage Value)
b) 0 10
c) 10 0
d) (Garbage Value) 10
View Answer

Answer: c
Explanation: None.
advertisement

5. Which of the following is not possible under any scenario?
a) s1 = &s2;
b) s1 = s2;
c) (*s1).number = 10;
d) None of the mentioned
View Answer

Answer: d
Explanation: None.

6. Which of the following operation is illegal in structures?
a) Typecasting of structure
b) Pointer to a variable of the same structure
c) Dynamic allocation of memory for structure
d) All of the mentioned
View Answer

Answer: a
Explanation: None.

7. Presence of code like “s.t.b = 10” indicates __________
a) Syntax Error
b) Structure
c) double data type
d) An ordinary variable name
View Answer

Answer: b
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.         s.name = "turing";
  16.         printf("%s", m.name);
  17.     }

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

Answer: c
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