Sapient Previous Years Solved Sample Placement Papers
-
What will be the output of the following C program?
void main()
{
int a[] = {5, 10, 15};
int i = 0, num;
num = a[++i] + ++i + (++i);
printf("%d", num);
}A. 6 Answer: A
B. 17
C. 16
D. 12
Solution: The `++i` modifies the index before accessing the array. Calculations proceed as:
a[1] + 2 + 3 = 6.
-
What will be the output of the program?
#include
int main()
{
int i = 5;
while (i-- >= 0)
printf("%d,", i);
i = 5;
printf("n");
while (i-- >= 0)
printf("%i,", i);
while (i-- >= 0)
printf("%d,", i);
return 0;
}A. 4, 3, 2, 1, 0, -1
4, 3, 2, 1, 0, -1 Answer: AB. 5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0C. Error
D. 5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0
5, 4, 3, 2, 1, 0Solution: The `--` operator decrements `i` during the checks, leading to two identical decrements for the first two loops.
-
Point out the error in the following program:
#include
int main()
{
char str[] = "Freshersworld";
printf("%.#s %2s", str, str);
return 0;
}A. In array declaration
B. printf statement
C. Unspecified character in printf
D. No error Answer: D
Solution: The syntax may seem unusual, but it will compile and run successfully without any issues.
-
Find the output of the following snippet:
#include
void main()
{
char letter = 'Z';
printf("%c", letter);
}A. Z Answer: A
B. 90
C. Garbage Value
D. Error
Solution: The character `Z` is printed directly as it is assigned to the variable.
-
What will be the output of the program?
#include
#define SQR(x) (x * x)
int main()
{
int a, b = 3;
a = SQR(b + 2);
printf("%d", a);
return 0;
}A. 25
B. 11 Answer: B
C. Error
D. Garbage Value
Solution: The macro replacement happens without parentheses around the expression. So,
SQR(b+2)
becomesb + 2 * b + 2 = 3 + 6 + 2 = 11.
-
What will be the output of the program if value 25 is given to scanf()?
#include
int main()
{
int i;
printf("%d", scanf("%d", &i));
return 0;
}A. 1 Answer: A
B. 2
C. 5
D. 25
Solution: The `scanf` function returns the number of successful input variables. Here, only one value (25) is read successfully, so the return value is `1`.