Question - 1
int main()
{
int x[10]={0,1,2,3,4,5,6,7,8,9};
int *ptr1,*ptr2;
ptr1=&x[0];
ptr2=&x[5];
printf("%p\n",(ptr1+ptr2));
return 0;
}
Question - 2
int main()
{
int color=2;
switch(color)
{
case 0: printf("Black");
case 1: printf("Blue");
case 2: printf("Green");
case 3: printf("Aqua");
default: printf("Other");
}
return 0;
}
Question - 3
int main()
{
char str[10]="Hello";
printf("%d,%d\n",strlen(str),sizeof(str));
return 0;
}
Question - 4
int main()
{
char *str="Hello";
printf("%d,%d\n",strlen(str),sizeof(str));
return 0;
}
Question - 5
void test(struct number n)
{
n.x=100;
}
struct number{ int x; };
int main()
{
struct number num;
test(num);
printf("%d\n",num.x);
return 0;
}
Question - 6
int main()
{
if(0);
printf("Hello");
printf("Hi");
return 0;
}
Question - 7
int main()
{
int x,y;
int *ptr;
x=100;
ptr=&x;
y=*ptr;
printf("%d\n",y);
return 0;
}
Question - 8
int main()
{
int val=1;
do{
val++;
++val;
}while(val++>25);
printf("%d\n",val);
return 0;
}
Question - 9
int main()
{
char *str="A%%B";
printf("A%%B ");
printf("%s\n",str);
return 0;
}
Question - 10
int main()
{
printf("%d,%d,%d\n",sizeof(char*),
sizeof(int*),sizeof(float*));;
return 0;
}
Answer Question 1:
Output
Explanation
Error in the statement printf("%p\n",(ptr1+ptr2));
Addition of two pointers is not allowed.
Answer Question 2:
Output
Explanation
There are no break statements, so all the statements after case 2 will be executed including default statement.
Answer Question 3:
Output
Explanation
strlen gives length of the string that is 5; sizeof gives total number of occupied memory for a variable that is 8; since str is a pointer so sizeof(str) may be 2,4 or 8. It depends on the computer architecture.
Answer Question 4:
Output
Explanation
strlen gives length of the string that is 5; sizeof gives total number of occupied memory for a variable that is 10; Since str is a variable with maximum number of characters 10, so sizeof will be 10.
Answer Question 5:
Output
Explanation
Structure number should be defined before test function definition.
Answer Question 6:
Output
Explanation
There is a semicolon after the if statement, so this statement will be considered as separate statement; and here printf("Hello"); will not be associated with the if statement. Both printf statements will be executed.
Answer Question 7:
Output
Explanation
Here, y is assigned the value stored at the pointee variable, which is pointer through ptr.
Answer Question 8:
Output
Explanation
Here, do while loop executes once and then it will check condition while will be false meanwhile value will be increased 3 times (two times in do while body and once while checking the condition); hence value will be 4.
Answer Question 9:
Output
Explanation
printf("A%%B"); will print "A%B" because "%%" will always print "%" while str contains "A%%B" will print "A%%B" - in this statement "%%" will not replace with single "%"
Answer Question 10:
Output
Explanation
No matter which kind of pointers are. Pointer variables take same bytes in the memory. The value may be 2, 4 or 8 depending on the computer architecture.
In our case we are using 64 bits computer architecture, so output is 8, 8, 8.
need an explanation for this answer? contact us directly to get an explanation for this answer