Basic C Questions to test ur expertise-2

What will print out?
main()
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%s\n”,p2);
}

The pointer p2 value is also increasing with p1 .
*p2++ = *p1++ means copy value of *p1 to *p2 , then increment both addresses (p1,p2) by one , so that they can point to next address . So when the loop exits (ie when address p1 reaches next character to “name” ie null) p2 address also points to next location to “name” . When we try to print string with p2 as starting address , it will try to print string from location after “name” … hense it is null string ….

eg :
initially p1 = 2000 (address) , p2 = 3000
*p1 has value “n” ..after 4 increments , loop exits … at that time p1 value will be 2004 , p2 =3004 … the actual result is stored in 3000 - n , 3001 - a , 3002 - m , 3003 -e … we r trying to print from 3004 …. where no data is present … thats why its printing null .

Answer:empty string.


What will be printed as the result of the operation below:

main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%d\n”,x,y)
;
}

Answer : 5794


What will be printed as the result of the operation below:
main()
{
int x=5;
printf(“%d,%d,%d\n”,x,x<<2,>>2)
;
}

Answer: 5,20,1


What will be printed as the result of the operation below:
#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %d\n”,x,y)
; swap2(x,y);
printf(“%d %d\n”,x,y)
; }

int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;
}

as x = 5 = 0×0000,0101; so x << 0100 =" 20;">7gt; 2 -> 0×0000,0001 = 1. Therefore, the answer is 5, 20 , 1

the correct answer is
10, 5
5, 10


Answer: 10, 5


What will be printed as the result of the operation below:
main()
{
char *ptr = ” Cisco Systems”;
*ptr++; printf(“%s\n”,ptr)
; ptr++;
printf(“%s\n”,ptr);

}

1) ptr++ increments the ptr address to point to the next address. In the prev example, ptr was pointing to the space in the string before C, now it will point to C.

2)*ptr++ gets the value at ptr++, the ptr is indirectly forwarded by one in this case.

3)(*ptr)++ actually increments the value in the ptr location. If *ptr contains a space, then (*ptr)++ will now contain an exclamation mark.

Answer:Cisco Systems


What will be printed as the result of the operation below:
main()
{
char s1[]=“Cisco”;
char s2[]= “systems”;
printf(“%s”,s1)
; }

Answer: Cisco


What will be printed as the result of the operation below:
main()
{
char *p1;
char *p2;

p1=(char *)malloc(25);
p2=(char *)malloc(25);

strcpy(p1,”Cisco”);
strcpy(p2,“systems”);
strcat(p1,p2);

printf(“%s”,p1)
;
}

Answer: Ciscosystems


The following variable is available in file1.c, who can access it?: static int average;

Answer: all the functions in the file1.c can access the variable.



courtesy:DEVFYI - Developer Resource - FYI

2 comments:

  1. The answer to the SWAP is incorrect. the print is
    10 5
    10 5
    since the swap2() function passes its parameters by value and not by reference.

    ReplyDelete
    Replies
    1. above comment is "wrong".. The answers are correct

      Delete