Interview c coding question


IBM placement question with solution on c
(1) Output of the following program is
void main()
{
int i=0;
for(i=0;i<20;i++)
{
switch(i){
case 0:i+=5;
case 1:i+=2;
case 5:i+=5;
default i+=4;
break;
        }
    printf("%d,",i);
 }
}
(a)0,5,9,13,17
b) 5,9,13,17
c) 12,17,22
d) 16,21
e) Syntax error
Ans. (d)
(2) What is the output in the following program
main()
{
char c=-64;
int i=-32
unsigned int u =-16;
if(c>i)
{printf("pass1,");
if(c<u)
printf("pass2");
else
printf("Fail2");
}
else
printf("Fail1);
if(i<u)
printf("pass2");
else
printf("Fail2")
}
Pass1,Pass2
b) Pass1,Fail2
c) Fail1,Pass2
d) Fail1,Fail2
e) None of these
Ans. (c)
(3) What will the following program do?
void main()
{
int i;
char a[]="String";
char *p="New Sring";
char *Temp;
Temp=a;
a=malloc(strlen(p) + 1);
strcpy(a,p); //Line number:9//
p = malloc(strlen(Temp) +
1);
strcpy(p,Temp);
printf("(%s, %s)",a,p);
free(p);
free(a);
} //Line number 15//
a)contents of p & a and print:(New string, string)
b) Generate compilation error in line number 8
c) Generate compilation error in line number 5
d) Generate compilation error in line number 7
e) Generate compilation error in line number 1
Ans. (b)
(4) In the following code segment what will be the result of the function,value
of x , value of y
{unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");
}
same, MAXINT, -1
b) not same, MAXINT, -MAXINT
c) same , MAXUNIT, -1
d) same, MAXUNIT, MAXUNIT
e) not same, MAXINT, MAXUNIT
Ans. (a)
(5) What will be the result of the following program?
char *gxxx()
{static char xxx[1024];
return xxx;
}
main()
{char *g="string";
strcpy(gxxx(),g);
g = gxxx();
strcpy(g,"oldstring");
printf("The string is : %s",gxxx());
}
The string is : string
b) The string is :Oldstring
c) Run time error/Core dump
d) Syntax error during compilation
e) None of these
Ans. (b)
(6) What will be result of the following program?
void myalloc(char *x, int n)
{x= (char *)malloc(n*sizeof(char));
memset(x,\0,n*sizeof(char));
}
main()
{char *g="String";
myalloc(g,20);
strcpy(g,"Oldstring");
printf("The string is %s",g);
}
The string is : String
b) Run time error/Core dump
c) The string is : Oldstring
d) Syntax error during compilation
e) None of these
(7)What will be the result of the following program?
main()
{char p[]="String";
int x=0;
if(p=="String")
{printf("Pass 1");
if(p[sizeof(p)-2]=='g')
printf("Pass 2");
else
printf("Fail 2");
}
else
{
printf("Fail 1");
if(p[sizeof(p)-2]=='g')
printf("Pass
2");
else
printf("Fail 2");
}
}
Pass
1, Pass 2
b) Fail 1, Fail 2
c) Pass 1, Fail 2
d) Fail 1, Pass 2
e) syntax error during compilation
(8) Which of the choices is true for the mentioned declaration?
const char *p;
and
char * const p;
a)You can't change the character in both
b) First : You can't change the characterr & Second : You can;t change the pointer
c) You can't change the pointer in both
d) First : You can't change the pointer & Second : You can't chanage the character
e) None
(9) Given the following c program
func(int *i, int*j)
{*i=*i * *i;
*j=*j* *j;
}
main()
{ int i = 5, j = 2;
func(&i,&j);
printf("%d %d", i, j);}
What is the output?
(10) For the following C program
void insert(key,r)
typekey key,data array r;
{extern int n;
if(n>=max) /*error table if full */
else r[n++].k=key;
}
This on executing, enables
a)basic sequential search
b) Binary search
c) Interpolation search
d) None
(11) Find the output of the following C program
void f(char *p)
{p=(char *) malloc(6);
strcpy(p,"hello");
}
void main( )
{char *P="bye";
f(p);
printf("%s',p);
}
(12) What is the fallacy in the following program segment?
int *f1()
{
int a=5;
return &a;
}
f()
int *b=f1()
int c=*b;
}
(13) Give the C language equivalents of the following
a)Function returning an
int pointer
b)Function pointer returning an int pointer
c)Function pointer returning an array of integers
d)Array of function pointer returning an array of integers
(14) Find the fallacy in the following program segment?
int a;
short b;
b=a;
(15)Define function? Explain arguments in functions?
(16) How does C pass variables to a function?
(17) Explain the following program segment.
f(){
int *b;
*b=2;
}
(18) Find the output for the following C program
int array[4][4] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
for (i=2;i<0;i--)
for(j=2;j<=0;j--)
printf("%d", arr[i][j]);
(19) Find the output for the following C program
#include<stdio.h>
void main()
{int i,x,sum=0;
int arr[6]=[1,2,3,4,5,6]
for (i=0;i<4;i++)
sum+ = func(arr[i]);
printf("%d", sum);
}
func(int x)
{ int val,x;
val = 2;
return(x+ val++);
}
(20) For the following C program
int d=0;
for(int i=0;i<31;i++)
for(int j=0;j<31;j++)
for(int k=0;k<31;k++)
if (((i+j+k) % 3)==0)
d=d+1;
Find value of d

1 comment:

Durga Prasad said...

Great excellent job...