Sapient Previous Years Solved Sample Placement Papers
-
int main() {
struct myStruct ms = {400, 'A'};
printf("%d %d", ptr->a, ptr->b);
return 0;
}
A. 400 A
B. 400 65
C. 400 97
D. 0 0 Answer: Option D
- Solution: In this program, the pointer `ptr` is declared but never initialized. Accessing uninitialized pointers leads to undefined behavior. The program outputs `0 0` because the pointer does not point to valid memory.
-
What will be the output of the following program?
#include
typedef struct stu1 { int roll; char *name; double marks; } STU1; typedef struct stu2 { int roll; char *name; double marks; } STU2; void main() { STU1 s1 = {25, "Rohit", 87.43}, *p1; STU2 *p2; p1 = &s1; memcpy(p2, p1, 4); printf("Roll : %d\n", p2->roll); printf("Name : %s\n", p2->name); printf("Marks : %lf\n", p2->marks); } A. Roll : 25 Name : Rohit Marks : 87.430000
B. Roll : 25 Name : Rohit Marks : 0.000000 Answer: Option B
C. Roll : 0 Name : Rohit Marks : 87.430000
D. Roll : 0 Name : null Marks : 0.000000
- Solution: The `memcpy` function is used to copy memory. However, the pointer `p2` is not initialized, so it points to an invalid memory location. As a result, the `marks` value is overwritten as `0.000000`, and the output is incorrect.