Interview Q & A 8

Interview Q & A 8



1. write a code for removing repeated digits in a number?
ex : number = 45678678 here the digits 6,7,8 has to be removed.



2. write a code to search the largest element in the array of your choice?



3. write a program to convert the integer string to number using atoi function.?


4. write a source code to find the sum of 1/3+1/5+1/7+ ....+1/n using recursive function?


5. write a code for linked list print function in reverse order?


6. what is the difference between address of variable and address of ponter variable, 
   explain using an example?


7. write a source code for insert a char/word/substring into a string?


8. can we return multiple values from the function explain with an example?


9.  what is the output for the below code?
	static int k= 8;
	int main()
	{
		if(-k)
		printf("%d", k);
		printf("error");
	}


10. what are the errors in the below source code?

int add();
int main()
{
	printf("function ",add());
}

int add()
{
	int x=5,y=4,z;
	z=x+y;
	printf("%d",z);
}


11. explain for the below code, the memory allocated is safe or not?

struct person
{
char per_name[10];
int per_age;
};
int main()
{
struct person *per;
per=(struct per*)malloc(sizeof(struct person));
free(per);
return 0;
}


12. explain the memory is safe for the below mentioned code?

struct person *per;
per=(struct person*)malloc(sizeof(struct person)*100);
char pername[] = "ptinstitute.in" ;
per = *pername;
free(per);
return 0;


13 what is the output for the below code explain?

char *name="ptinstitute.in";
char  name1[]="ptinstitute.in";
printf("%d %d ",strlen(name),strlen(name[]));
printf(" %d %d",sizeof(name++),sizeof(name[0]));


14. what is the output for the below program explain?
int main()
{
	int x=4,y=2,p,q,r;
	p=x||y;
	q=p&&x;
	r=p^q;
printf("%c %x %d",p,q,r);
return 0;
}


15. what is the output for the below code explain?

int main()
{
int p=4;
for( ;p++;printf("%d",p))
{
printf("ptinstitute.in");
}
return 0;
}

16. what is the output for the below program?

int main()
{
static int m=3;
main();
printf("%d",m++);
return 0;
}


answers:

10.  no errors. 
     output: 9 function

11. safe, because memory is allocated to pointer per.

12. here memory allocated to per , 
    then per = *pername         //it stores the 'p' in the per variable.
    we copying the ascii value of p to pointer p which gives runtime error.
    the allocated memory is not freed, so memory leakage happened here.

13. the prototype of strlen is
       size_t strlen ( const char * str );
    we need to pass the address of the string to find the length.
    in the program we passing strlen with name[], we are passing without index.
    we suppose to pass the address of the string. that is why it gives compile time error.
14. ascii character for 1, 1 0

15. it gives an lengthy loop, in the for loop we incrementing p++ in condition checking position,
    p++ doing continuously and the loop stops after p=0.

16 It gives an runtime error like stack over flow issue.
   here we are not exiting the recursive function main, it is calling continuously main recusively.