Interview Q & A 6
Ques 1. write a c program to print "Goodmorning" without using ; ?
ex1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
if(printf("\n Goodmorning"))
}
ex2:
#include <stdio.h>
#include <stdlib.h>
int main()
{
while(!printf("\n Goodmorning")){}
}
ex3:
#include <stdio.h>
#include <stdlib.h>
int main()
{
switch(!printf("\n Goodmorning")){}
}
Ques 2. exchange / swap two numbers without using third variable?
program 1:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n;
m=2;
n=5;
m = m+n;
n = m-n;
m = m-n;
printf("\n m =%d n=%d ", m,n);
return 0;
}
program 2:
int main()
{
int m,n;
m=2;
n=5;
m = m+n-(n=m);
printf("\n m =%d n=%d ", m,n);
return 0;
}
program 3:
int main()
{
int m,n;
m=2;
n=5;
m=m^n;
n=m^n;
m=m^n;
printf("\n m =%d n=%d ", m,n);
return 0;
}
program 4:
int main()
{
int m,n;
m=2;
n=5;
m = m - ~n - 1;
n = m + ~n + 1;
m = m + ~n + 1;
printf("\n m =%d n=%d ", m,n);
return 0;
}
Ques 3. what is dangling pointer in c?
Initially a pointer has been pointing some variable location, later the pointer pointing to the memory location
is deleted. but the pointer still pointing to same memory location.
This pointer is called dangling pointer. the error is called dangling pointer error.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m=34;
int *p=(int*)malloc(4);
*p=m;
free(p);
printf("\n m =%d ", *p);
return 0;
}
Ques 4. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int * fun()
{
int x=44;
return &x;
}
int main()
{
int m=34;
int *p;
p =fun();
printf("\n m =%d ", *p);
return 0;
}
Ans: runtime error/ garbage value prints, since the scope of the local variable
x is only inside the function fun(), so we can get the address of this variable.
so this is called dangling pointer error.
solution: if we make x as static then it works.
Ques 5. what is wild pointer in c?
A pointer which is not initialised is called wild pointer,
it returns the some random address and garbage value.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *p;
printf("\n p =%u =%d", p,*p);
return 0;
}
output: it gives garbage value or prints nothing. NULL pointer means pointing to memory location 0,
wild pointer means it pointing to some unknown location and gives some garbage value.
Ques 6. what is the advantage and disadvantage of arrays in c?
The advantage : once the array is declared the memory is allocated and we can access each element by using the array indexes.
it can store elements of the same data type.
we can declare array for different data types like int , float, struct, union.
we can initialise the array while declaration.
an array pointing to the address of the other elements is called pointer array.
disadvantage: if the array index goes out of boundary then there is a huge mistake, some wanted data will be
overwritten or the program may crash.
if array size is more than the usable then the remaining memory is wasted.
array declaration example:
int arr[4];
char str[33];
short int sarr[34];
float fr[12];
int (*funptr[4])();
long *larr[21];
double **p[5];
Ques 7. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
double d1=33.44, d2=33.55, d3=66.78;
double *dptr[3] ={&d1, &d2, &d3 };
printf("\n %e", *dptr[0]);
return 0;
}
ans : 3.344 e+001
Here dptr is a pointer array it points to the elements d1,d2,d3.
Ques 8. explain about do while loop and write a program?
do while loop we can use to execute the code in the loop before checking the condition.
it executes the loop atleast one time.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
do {
printf("\n this is do while loop");
} while(i !=0);
return 0;
}
Ques 9. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int i;
i=45;
main()
{
printf("\n %d", i);
return 0;
}
ans:
it gives compilation error, since assignment outside main function is not allowed.
Ques 10. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
static int i=10;
int main()
{
for(i=0; i<5; i++)
{
static int a=3;
printf("%d ", a++);
}
return 0;
}
ouput : 3 4 5 6 7
static variable initialises only once, so the variable a prints 3,4,5,6,7
Ques 11. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
static int i=10;
int main()
{
{
static int a=3;
printf("%d ", a++);
}
printf("%d ", a);
return 0;
}
output:
it gives compiler error because the static variable declared within the block is local to the block.
we cannot access outside of it.
Ques 12. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
static int a=10;
void fun();
int main()
{
printf("%d ", a++);
printf("\nmain b= %d",b);
return 0;
}
int b=323;
void fun()
{
printf("\n b= %d",b);
}
output: compiler error since the global variable b has to be declared above the main function.
Ques 13. what is the output for the below code in two different files file1.c and file2.c?
//file1.c
#include <stdio.h>
#include <stdlib.h>
extern int sum();
static int i=3, j=5;
int main()
{
int p;
p=sum();
printf("%d ", p);
return 0;
}
//file2.c
#include <stdio.h>
#include <stdlib.h>
extern int sum();
static int i=3, j=5;
int main()
{
int p;
p=sum();
printf("%d ", p);
return 0;
}
output:
It gives compiler error, since the global static variables declared i,j are only accessed in file1.c file,
we cannot access it in file2.c file.
global static variables declared accessed only in the file where they are declared.
Ques 14. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k=0;
{
static int p=5;
PRINT:
printf(" %d", p);
p++;
k++;
}
if(k<4)
goto PRINT;
return 0;
}
output|: 5 6 7 8
The life of static varible still exists the control come out and go inside the block.
Ques 15. what tells the prototype of a function ?
prototype of a function is declaration of a function.
it tells the return type of the function,
it tells the input arguments the function takes.
it tells the function name
ex:
int fun(float, char);
Ques 16. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int main()
{
const int k=20;
int *p;
p=&k;
*p=34;
printf("%d", *p);
return 0;
}
output: 34
we can modify the constant variables in c using pointers.
Ques 17. what is the output for the below code?
#include <stdio.h>
#include <stdlib.h>
int search(char);
int (*fun())(char c);
int main()
{
int m=0;
int (*p)(char);
p=fun();
m=(*p)('h');
printf("%d", m);
return 0;
}
int search(char d)
{
return d;
}
int (*fun())(char g)
{
return search;
}
output:
104
p=fun()
m=(*p)('h')
m=(*fun())('h') // since p=fun();
m= (*&search())('h') // since fun returns &search
m= search('h')
m=104.