Sonata Previous Years Solved Sample Placement Papers
-
What error would the following function give on compilation?
f(int a, int b)
{
int a;
a=20;
return a;
}A. missing parenthesis in the return statement
B. The function should be declared as
int f(int a, int b)
C. redeclaration of
a
Answer: Option CD. None of the above
-
Solution: The variable
a
is redeclared within the same scope, which is not allowed. -
Point out the error in the following program:
main()
{
const char *fun();
*fun()='A';
}
const char *fun()
{
return "Hello";
}Ans: fun() returns to a const char pointer which cannot be modified
-
What would be the output of the following program?
main()
{
const int x=5;
int *ptrx;
ptrx=&x;
*ptrx=10;
printf("%d",x);
}A. 5
B. 10
C. Error
D. Garbage value Answer: Option A
-
Solution: Attempting to modify a constant variable through a pointer will not change its value in most compilers, but it is undefined behavior. The program prints the original value of
x
, which is 5. -
A switch statement cannot include:
A. constants as arguments
B. constant expression as arguments
C. string as an argument Answer: Option C
D. None of the above
- Solution: A switch statement only accepts constants or constant expressions as arguments; strings are not valid.
-
How long will the following program run?
main()
{
printf("\nSonata Software");
main();
}A. infinite loop
B. until the stack overflows Answer: Option B
C. All of the above
D. None of the above
- Solution: The function calls itself recursively without any termination condition, leading to stack overflow eventually.
-
On combining the following statements, you will get
char *p; p=malloc(100);
A.
char *p= malloc(100)
B.
p= (char*)malloc(100)
C. All of the above Answer: Option C
D. None of the above
- Solution: Both forms are valid ways of allocating memory for a char pointer.
-
What is the output of the following program?
main()
{
int n=5;
printf("\nn=%*d",n,n);
}A. n=5
B. n= 5 Answer: Option B
C. n=5
D. error
-
Solution: The
%*d
format specifier dynamically sets the field width, which in this case is the value ofn
.