Consider the following three C functions:
[PI] int * g (void)
{
int x= 10;
return (&x);
}
[P2] int * g (void)
{
int * px;
*px= 10;
returnpx;
}
[P3] int *g (void)
{
int *px;
px = (int *) malloc (sizeof(int));
*px= 10;
returnpx;
}
Which of the above three functions are likely to cause problems with pointers?
Only P3
Only P1 and P3
Only P1 and P2
P1, P2 and P3
Answer: Option C.
Assume the following C variable declaration
int *A[10], B[10][10];
Of the following expressions
A[2]
A[2][3]
B[1]
B[2][3]
which will not give compile-time errors if used as left hand sides of assignment statements in a C program?
Answer: Option A.
Consider the C program shown below
#include
#define print(x) printf("%d", x)
int x;
void Q(int z)
{
z+=x;
print(z);
}
void P(int *y)
{
int x = *y + 2;
Q(x);
*y = x - 1;
print(x);
}
main(void) {
x = 5;
P(&x);
print(x);
}
The output of this program is
What is the output of the following program?
#include
intfuncf (int x);
intfuncg (int y);
main()
{
int x = 5, y = 10, count;
for (count = 1; count < = 2; ++count)
{
y + = funcf (x) + funcg(x); printf(«%d",y);
} }funcf(int x)
{
inty;
y = funcg(g);
return (y);
} funcg(int x)
{
staticint y = 10;
y + = l;
return (y + x);
43 80
42 74
33 37
32 32
Answer: Option A.
funcf(x) + funcg(x)
funcf or funcg can be executed first. Let's assume funcf is executed first. It calls funcg.
So even if the order of call is revrsed, result will be same.
In first call of funcg, y becomes 11 and it returns 5+11 = 16.
In second call of funcg, y becomes 12 and it returns 5+12 - 17.
So, in main y is incremented by 16 + 17 = 33 to become 10+33 = 43 (option (a))In the second iteration y will be incremented by 18 + 19 = 37 to give 43 + 37 = 80
Consider the following C program segment:
char p[20];
char *s = "string";
int length = strlen(s);
int i;
for (i = 0; i < length; i++)
p[i] = s[length — i];
printf("%s", p);
The output of the program is?
gnirts
Gnirt
String
no output is printed
Answer: Option D
Because of first character is '\0' nothing will be printed.