C Programming Interview Questions and Answers-6

Q: – Explain “method”?
A Method is a programmed procedure. It is defined as part of a class also included in any object of that class. Class can have more than one methods. It can be re-used in multiple objects.
Q: – What is the output of the bellow program?
segmentation fault.because we are trying to read/assign character into const string.

#include<stdio.h>
main()
{
char *ptr="HELLO";
ptr[0]='A';
printf("%s",ptr);
}

Q: – Write a program to display number from the bellow string?
char *ptr=”HELLO57hai”

#include<stdio.h>
int main()
{
char *ptr="HELLO57hai";
while(*ptr)
{
if(*ptr<'A')
printf("%c",*ptr);
ptr++;
}
}

Q: – what are compilation process or steps?
prepocessor -> compiler -> assembler -> linkEditor == Executable code
Q: – There are numbers range from 1 to n randamly stored in the array and one number is missed in that array.write a program to find out missing number?

int missing_number(int a[],int n)
{
int Total,i;
Total=(n+1)*(n+2)/2;
for(i=0;i<n;i++)
Total -=a[i];
return Total;
}

Q: – What is the output of this Program?

#include<stdio.h>
int main()
{
int x[]={1,3,2,4,6};
int *ptr,y;
ptr=x+4;
y=ptr-x;
printf("%dn",y);
}

Answer: 4
Q: – How do you find middle element in the single linked list by traversing only one time from first to last?
we can findout middle element by taking two pointers.increment twice ptr1 while incrementing the ptr2 once ,so when ptr1 reaches to end of the node in the list,ptr2 will be pointing to middle element in the list. so we just return adress of middle element.

struct person*find_middle(struct person **listhead)
{
struct person *ptr1,*ptr2;
ptr1=ptr2=*listhead;
int i=0;
while(ptr1->link!=NULL)
{
if(i==0)
{
ptr1=ptr1->link;
i=1;
}
else if(i==1)
{
ptr1=ptr1->link;
ptr2=ptr2->link;
i=0;
}
}
return ptr2;
}

Q: – What are “<<” and “>>” in C language. what is the output of the bellow code program?

#include<stdio.h>
int main()
{
int x=0x00AA;
int y=0x0055;
int z;
z=x<<8 + y>>8;
printf("%dn",z);
}

In C language <<(Left shift operator) and >>(Right shift operator) are called Bitwise operators and used to work on bits.
Z value is 4194304 for the above program.
Q: – What is Void pointer? can we perform arithmetic operations on void pointers?why? give one example?
void pointer is one kind of pointer ,it supports to hold data of all data-types(like int,char,float etc). it’s also called generic pointer.we can’t perform arithmetic operations on void pointer because compiler doesn’t know the type of data to increment.
Ex:

#include<stdio.h>
int main()
{
int a[]={100,200};
void *ptr;
ptr=a;
ptr++;
printf("%dn",*ptr); //here error occur.
}

Q: – What is the output of this program?

#include<stdio.h>
char *myfun(char *p)
{
p+=3;
return p;
}
int main()
{
int *p;
int *q;
p="HELLO";
q=myfun(p);
printf("%s",q);
}

Answer: LO.

Tagged , . Bookmark the permalink.

Leave a Reply