C Programming Questions and Answers on Structures and Functions for Freshers

https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_25.html
1. What is the output of this C code?
#include
struct student
{
char *name;
};
struct student s;
struct student fun(void)
{
s.name = "newton";
printf("%s\n", s.name);
s.name = "alan";
return s;
}
void main()
{
struct student m = fun();
printf("%s\n", m.name);
m.name = "turing";
printf("%s\n", s.name);
}
a) newton alan alan
b) alan newton alan
c) alan alan newton
d) Compile time error
b) alan newton alan
c) alan alan newton
d) Compile time error
Answer: a
2. What is the output of this C code?
#include
struct student
{
char *name;
};
void main()
{
struct student s, m;
s.name = "st";
m = s;
printf("%s%s", s.name, m.name);
}
a) Compile time error
b) Nothing
c) Junk values
d) st st
b) Nothing
c) Junk values
d) st st
Answer: d
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
Answer: d
4. What’s the output of the following code?
#include
struct temp
{
int a;
} s;
void func(struct temp)
{
s.a = 10;
printf("%d\t", s.a); s
}
main()
{
func(s);
printf("%d\t", s.a);
}
a) 10 (Garbage Value)
b) 0 10
c) 10 0
d) (Garbage Value) 10
b) 0 10
c) 10 0
d) (Garbage Value) 10
Answer: c
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
Answer: d
6. Which of the following operation is illegal in structures?
a) Typecasting of structure
b) Pointer to a variable of same structure
c) Dynamic allocation of memory for structure
d) All of the mentioned
Answer: a
7. Presence of code like “s.t.b = 10” indicate.
a) Syntax Error
b) structure
c) double data type
d) An ordinary variable name
Answer: b
8. The output of the code below is
#include
struct student
{
char *name;
};
struct student fun(void)
{
struct student s;
s.name = "alan";
return s;
}
void main()
{
struct student m = fun();
s.name = "turing";
printf("%s", m.name);
}
a) alan
b) Turing
c) Compile time error
d) Nothing
b) Turing
c) Compile time error
d) Nothing
Answer: c
9. What is the output of this C code?
#include
struct point
{
int x;
int y;
};
int main()
{
struct point p = {1};
struct point p1 = {1};
if(p == p1)
printf("equal\n");
else
printf("not equal\n");
}
a) Compile time error
b) equal
c) depends on the standard
d) not equal
b) equal
c) depends on the standard
d) not equal
Answer: a
10. What is the output of this C code?
#include
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
struct point foo();
int main()
{
struct point p = {1};
struct notpoint p1 = {2, 3};
p1 = foo();
printf("%d\n", p1.x);
}
struct point foo()
{
struct point temp = {1, 2};
return temp;
}
a) Compile time error
b) 1
c) 2
d) Undefined behaviour
b) 1
c) 2
d) Undefined behaviour
Answer: a
11. What is the output of this C code?
#include
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
int main()
{
struct point p = {1};
struct notpoint p1 = p;
printf("%d\n", p1.x);
}
a) Compile time error
b) 1
c) 0
d) Undefined
b) 1
c) 0
d) Undefined
Answer: a
12. What is the output of this C code?
#include
struct point
{
int x;
int y;
};
struct notpoint
{
int x;
int y;
};
void foo(struct point);
int main()
{
struct notpoint p1 = {1, 2};
foo(p1);
}
void foo(struct point p)
{
printf("%d\n", p.x);
}
a) Compile time error
b) 1
c) 0
d) Undefined
b) 1
c) 0
d) Undefined
Answer: a
13. What is the output of this C code?
#include
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1 = {1, 2};
foo(&p1);
}
void foo(struct point *p)
{
printf("%d\n", *p.x++);
}
a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
b) Segmentation fault/code crash
c) 2
d) 1
Answer: a
14. What is the output of this C code?
#include
struct point
{
int x;
int y;
};
void foo(struct point*);
int main()
{
struct point p1 = {1, 2};
foo(&p1);
}
void foo(struct point *p)
{
printf("%d\n", *p->x++);
}
a) Compile time error
b) 1
c) Segmentation fault/code crash
d) 2
b) 1
c) Segmentation fault/code crash
d) 2
Answer: a
15. What is the output of this C code?
#include
struct student fun(void)
{
struct student
{
char *name;
};
struct student s;
s.name = "alan";
return s;
}
void main()
{
struct student m = fun();
printf("%s", m.name);
}
a) Compile time error
b) alan
c) Nothing
d) Varies
b) alan
c) Nothing
d) Varies
Answer: a
16. What is the output of this C code?
#include
struct student
{
char *name;
};
struct student fun(void)
{
struct student s;
s.name = "alan";
return s;
}
void main()
{
struct student m = fun();
printf("%s", m.name);
}
a) Nothing
b) alan
c) Run time error
d) Varies
b) alan
c) Run time error
d) Varies
Answer: b