C Programming Questions and Answers on Arrays of Structures for Freshers
https://www.computersprofessor.com/2017/12/c-programming-questions-and-answers-on_26.html
1. The correct syntax to access the member of the ith structure in the array of structures is?
Assuming: struct temp { int b; }s[50];
a) s.b.[i];
b) s.[i].b;
c) s.b[i];
d) s[i].b;
b) s.[i].b;
c) s.b[i];
d) s[i].b;
Answer: d
2. Comment on the output of this C code?
#includestruct temp{int a;
int b;
int c;
};
main()
{struct temp p[] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
}
a) No Compile time error, generates an array of structure of size 3
b) No Compile time error, generates an array of structure of size 9
c) Compile time error, illegal declaration of a multidimensional array
d) Compile time error, illegal assignment to members of structure
b) No Compile time error, generates an array of structure of size 9
c) Compile time error, illegal declaration of a multidimensional array
d) Compile time error, illegal assignment to members of structure
Answer: a
3. Which of the following uses structure?
a) Array of structures
b) Linked Lists
c) Binary Tree
d) All of the mentioned
Answer: d
4. What is the correct syntax to declare a function foo() which receives an array of structure in function?
a) void foo(struct *var);
b) void foo(struct *var[]);
c) void foo(struct var);
d) none of the mentioned
Answer: a
5. What is the output of this C code?
(Assuming size of int be 4)
(Assuming size of int be 4)
#includestruct temp{int a;
int b;
int c;
} p[] = {0};
main()
{printf("%d", sizeof(p));
}
a) 4
b) 12
c) 16
d) Can’t be estimated due to ambigous initialization of array
b) 12
c) 16
d) Can’t be estimated due to ambigous initialization of array
Answer: b
6. What is the output of this C code?
#includestruct student{char *name;
};
struct student s[2];
void main()
{s[0].name = "alan";
s[1] = s[0];
printf("%s%s", s[0].name, s[1].name);
s[1].name = "turing";
printf("%s%s", s[0].name, s[1].name);
}
a) alan alan alan turing
b) alan alan turing turing
c) alan turing alan turing
d) run time error
b) alan alan turing turing
c) alan turing alan turing
d) run time error
Answer: a
7. What is the output of this C code?
#includestruct student{char *name;
};
struct student s[2], r[2];
void main()
{s[0].name = "alan";
s[1] = s[0];
r = s;
printf("%s%s", r[0].name, r[1].name);
}
a) alan alan
b) Compile time error
c) Varies
d) Nothing
b) Compile time error
c) Varies
d) Nothing
Answer: b
8. What is the output of this C code?
#includestruct student{char *name;
};
void main()
{struct student s[2], r[2];
s[1] = s[0] = "alan";
printf("%s%s", s[0].name, s[1].name);
}
a) alan alan
b) Nothing
c) Compile time error
d) Varies
b) Nothing
c) Compile time error
d) Varies
Answer: c
9. What is the output of this C code?
#includestruct student{};
void main()
{struct student s[2];
printf("%d", sizeof(s));
}
a) 2
b) 4
c) 8
d) 0
b) 4
c) 8
d) 0
Answer: d
10. What is the output of this C code?
#includestruct point{int x;
int y;
};
void foo(struct point*);
int main()
{struct point p1[] = {1, 2, 3, 4};
foo(p1);
}void foo(struct point p[])
{printf("%d\n", p[1].x);
}
a) Compile time error
b) 3
c) 2
d) 1
b) 3
c) 2
d) 1
Answer: b
11. What is the output of this C code?
#includestruct point{int x;
int y;
};
void foo(struct point*);
int main()
{struct point p1[] = {1, 2, 3, 4};
foo(p1);
}void foo(struct point p[])
{printf("%d\n", p->x);
}
a) 1
b) 2
c) 3
d) Compile time error
b) 2
c) 3
d) Compile time error
Answer: a
12. What is the output of this C code?
#includestruct point{int x;
int y;
};
void foo(struct point*);
int main()
{struct point p1[] = {1, 2, 3, 4};
foo(p1);
}void foo(struct point p[])
{printf("%d %d\n", p->x, ++p->x);
}
a) 1 2
b) 2 2
c) Compile time error
d) Undefined behaviour
b) 2 2
c) Compile time error
d) Undefined behaviour
Answer: b
13. What is the output of this C code?
#includestruct point{int x;
int y;
} p[] = {1, 2, 3, 4, 5};
void foo(struct point*);
int main()
{foo(p);
}void foo(struct point p[])
{printf("%d %d\n", p->x, p[2].y);
}
a) 1 0
b) Compile time error
c) 1 somegarbagevalue
d) Undefined behaviour
b) Compile time error
c) 1 somegarbagevalue
d) Undefined behaviour
Answer: a
14. What is the output of this C code?
#includestruct point{int x;
int y;
};
void foo(struct point*);
int main()
{struct point p1[] = {1, 2, 3, 4, 5};
foo(p1);
}void foo(struct point p[])
{printf("%d %d\n", p->x, p[3].y);
}
a) Compile time error
b) 1 0
c) 1 somegarbagevalue
d) None of the mentioned
b) 1 0
c) 1 somegarbagevalue
d) None of the mentioned
Answer: c
15 #include
struct point{int x;
int y;
};
void foo(struct point*);
int main()
{struct point p1[] = {1, 2, 3, 4, 5};
foo(p1);
}void foo(struct point p[])
{printf("%d %d\n", p->x, (p + 2).y);
}
a) Compile time error
b) 1 0
c) 1 somegarbagevalue
d) Undefined behaviour
b) 1 0
c) 1 somegarbagevalue
d) Undefined behaviour
Answer: a
16. What is the output of this C code?
#includestruct point{int x;
int y;
};
void foo(struct point*);
int main()
{struct point p1[] = {1, 2, 3, 4, 5};
foo(p1);
}void foo(struct point p[])
{printf("%d %d\n", p->x, (p + 2)->y);
}
a) Compile time error
b) 1 0
c) 1 somegarbagevalue
d) undefined behaviour
b) 1 0
c) 1 somegarbagevalue
d) undefined behaviour
Answer: b
17. What is the output of this C code?
#includestruct student{char *c;
};
void main()
{struct student s[2];
printf("%d", sizeof(s));
}
a) 2
b) 4
c) 16
d) 8
b) 4
c) 16
d) 8
Answer: d
