Here is a listing of C Objective 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 point -
{ -
int x;
-
int y;
-
};
-
int main()
-
{ -
struct point p = {1};
-
struct point p1 = {1};
-
if(p == p1)
-
printf("equaln");
-
else -
printf("not equaln");
-
}
a) Compile time error
b) equal
c) depends on the standard
d) not equal
Answer: a
Clarification: None.
2. What will be the output of the following 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("%dn", p1.x);
-
} -
struct point foo()
-
{ -
struct point temp = {1, 2};
-
return temp;
-
}
a) Compile time error
b) 1
c) 2
d) Undefined behaviour
Answer: a
Clarification: None.
3. What will be the output of the following 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("%dn", p1.x);
-
}
a) Compile time error
b) 1
c) 0
d) Undefined
Answer: a
Clarification: None.
4. What will be the output of the following 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("%dn", p.x);
-
}
a) Compile time error
b) 1
c) 0
d) Undefined
Answer: a
Clarification: None.
5. What will be the output of the following 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("%dn", *p.x++);
-
}
a) Compile time error
b) Segmentation fault/code crash
c) 2
d) 1
Answer: a
Clarification: None.
6. What will be the output of the following 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("%dn", *p->x++);
-
}
a) Compile time error
b) 1
c) Segmentation fault/code crash
d) 2
Answer: a
Clarification: None.
7. What will be the output of the following 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
Answer: a
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();
-
printf("%s", m.name);
-
}
a) Nothing
b) alan
c) Run time error
d) Varies
Answer: b
Clarification: None.
