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?
-
#include -
struct student -
{ -
char *name;
-
};
-
struct student s;
-
struct student fun(void)
-
{ -
s.name = "newton";
-
printf("%sn", s.name);
-
s.name = "alan";
-
return s;
-
} -
void main()
-
{ -
struct student m = fun();
-
printf("%sn", m.name);
-
m.name = "turing";
-
printf("%sn", s.name);
-
}
a) newton alan alan
b) alan newton alan
c) alan alan newton
d) compile time error
Answer: a
Clarification: None.
2. What will be the output of the following 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
Answer: d
Clarification: None.
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
Clarification: None.
4. What will be the output of the following C code?
-
#include -
struct temp -
{ -
int a;
-
} s;
-
void func(struct temp s)
-
{ -
s.a = 10;
-
printf("%dt", s.a);
-
} -
main()
-
{ -
func(s);
-
printf("%dt", s.a);
-
}
a) 10 (Garbage Value)
b) 0 10
c) 10 0
d) (Garbage Value) 10
Answer: c
Clarification: None.
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
Clarification: 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
Answer: a
Clarification: 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
Answer: b
Clarification: None.
8. What will be the output of the following 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();
-
s.name = "turing";
-
printf("%s", m.name);
-
}
a) alan
b) Turing
c) Compile time error
d) Nothing
Answer: c
Clarification: None.
