Pages

Tuesday

C Aptitude Questions with Answers Part 17

1)
# include<stdio.h> 
aaa() { 
  printf("hi"); 
 } 
bbb(){ 
 printf("hello"); 
 } 
ccc(){ 
 printf("bye"); 
 } 
main() 
{ 
  int (*ptr[3])(); 
  ptr[0]=aaa; 
  ptr[1]=bbb; 
  ptr[2]=ccc; 
  ptr[2]();
} 

Answer:
bye
Explanation:
ptr is array of pointers to functions of return type int.ptr[0] is assigned to address of the function aaa. Similarly ptr[1] and ptr[2] for bbb and ccc respectively. ptr[2]() is in effect of writing ccc(), since ptr[2] points to ccc.

2)
#include<stdio.h> 
main() {
FILE *ptr; 
char i; 
ptr=fopen("zzz.c","r"); 
while((i=fgetch(ptr))!=EOF) 
printf("%c",i); 
} 

Answer:
contents of zzz.c followed by an infinite loop
Explanation:
The condition is checked against EOF, it should be checked against NULL.

3) main(){
int i =0;j=0;
if(i && j++)
printf("%d..%d",i++,j);
printf("%d..%d,i,j);
}

Answer:
0..0
Explanation:
The value of i is 0. Since this information is enough to determine the truth value of the boolean expression. So the statement following the if statement is not executed. The values of i and j remain unchanged and get printed.


4)
main() {
 int i; 
 i = abc(); 
 printf("%d",i); 
} 
abc() {
 _AX = 1000; 
} 

Answer:
1000
Explanation:
Normally the return value from the function is through the information from the accumulator. Here _AH is the pseudo global variable denoting the accumulator. Hence, the value of the accumulator is set 1000 so the function returns value 1000.

5)
int i; 
main(){ 
 int t; 
for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) 
    printf("%d--",t--); 
} 
  // If the inputs are 0,1,2,3 find the o/p 

Answer:
4--0
3--1
2--2

Explanation:
Let us assume some x= scanf("%d",&i)-t the values during execution will be,
t i x
4 0 -4
3 1 -2
2 2 0


6)
main(){ 
  int a= 0;int b = 20;char x =1;char y =10; 
  if(a,b,x,y) 
  printf("hello"); 
 } 

Answer:
hello
Explanation:
The comma operator has associativity from left to right. Only the rightmost value is returned and the other values are evaluated and ignored. Thus the value of last variable y is returned to check in if. Since it is a non zero value if
becomes true so, "hello" will be printed.

No comments:

Post a Comment