1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Thursday, May 21, 2009

C Language Interview Questions Part-4

61. which one will over flow given two programs
prog 1: prog2:

main() main()
{ {
int fact; int fact=0
long int x; for(i=1;i<=n;i++)
fact=factoral(x); fact=fact*i;
} }

int factorial(long int x)
{
if(x>1) return(x*factorial(x-1);
}

ans: program 1 (program 2 is always zero since fact =0)

62. main()

{

char str[5]="hello";
if(str==NULL) printf("string null");
else printf("string not null");

}

ans: string not null

63. void f(int value)

{
for (i=0;i<16;i++)

{
if(value &0x8000>>1) printf("1")
else printf("0");
}

}

ans: binary output of value

64. void f(int *p)

{

static val=100;

val=&p;

}

main()

{

int a=10;

printf("%d ",a);

f(&a);

printf("%d ",a);

}

ans: nonportable pointer conversion (we can’t store address in integer variable, we have to take pointer to store address)

65. main()

{

int x, *y;

x = y;

printf(“%d”,x);

}

ans: nonportable pointer conversion

66. # define f(a,b) a+b
#define g(c,d) c*d
find value of f(4,g(5,6))

ans: 34

67. main()

{

char a[10]="hello";
strcpy(a,'\0');
printf("%s",a);

}

ans: arguments must be a string constant or character array variable

here it is constat character not a string constant. Hence program error

68. char a[5][15];
int b[5][15];
address of a 0x1000 and b is 0x2000 find address of a[3][4] and b[3][4]

interger takes 32-bits and character takes 8-bits

ans: a[3][4] = 0x1031 b[3][4] = 0x20C4

(Note: addresses are in hexadecimal)

69. Given an interger in binary form,find the number of ones in
that number without counting each bit.(This questin is not
multiple choice question. This question carries more
marks. So please take care for this question.)

ans: K.Ritchie

70. main()

{

a=2;
b=3;
x=SUM(a,b)*2;
printf("x=%d\n",x);

}

ans: 8

71. number(int i)

{

number++;

printf("%d\n",number);

}

main()

{

static int i=0;

number(i);

}

ans: lvalue required (function name is an address. So ++ operator should not be applied)

72. main()

{

unsigned char i;
int sum;
for(i=0; i<300; i++)
sum+ = i;
printf("\nSum = %d\n", sum);

}

ans: infinite loop

73. void fn(int *p)
{
static int val = 100;
p = &val;
}

main()
{
int i=10;
printf("i=%d\n", i);
fn(&i);
printf("i=%d\n", i);
}

ans: i=10

i=10

74. Swapping without using a temporary variables. (3 methods)

ans:

x = x+y;
y = x-y;
x = x-y;

x = x^y;
y = x^y;
x = x^y;

x = x*y;
y = x/y;
x = x/y;

75. Code 1 :
for(i=0; i<1000; i++)
for(j=0; j<100; j++)
x = y;

Code 2 :
for(i=0; i<100; i++)
for(j=0; j<1000; j++)
x = y;

Which code will execute faster

ans: Code2 (Code 1 = 1,01000 increment operations)

(Code 2 = 1,00100 increment operations)

76. main()

{

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, i, x=10, temp;
for(i=0; i

{
temp = a[i];
a[i] = a[x-i-1];
a[x-i-1] = temp;
}

ans: remains same

77. main()

{

int i = 1;
fork();
fork();
printf("\ni = %d\n", i+1);

}

ans: 4 printfs will occur and i = 2

78. #define MAX(a, b) a>b ? a:b

main()

{

int m, n;

m = 3 + MAX(2, 3);

n = 2 * MAX(3, 2);

printf("m = %d, n = %d\n", m, n);

}

ans: m = 2, n = 3

79. main()

{

int i=10;
fork();
fork();
fork();
printf("%d”,i);

}

ans: 8 printfs will occur and i = 10 (2 power no. of forks times printfs)

80. #define f(a,b) a+b

#define g(a,b) a*b

main()

{

int m;

m=2*f(3,g(4,5));

printf("\n m is %d",m);

}

ans: m is 26

81. main()

{

char a[10];

strcpy(a,"\0");

if (a==NULL)

printf("\a is null");

else

printf("\n a is not null");

}

ans: a is not null

82. main()

{

char a[5]="hello";

printf("%s",a);

}

ans: array size is small it should be 6

83. main()

{

unsigned int x=-1;

int y;

y = ~0;

if(x == y)

printf("same");

else

printf("not same");

}

ans: same (-1 is stored in 2’s complement form)

84. 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());

}

ans: The string is : oldstring

85. 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);

}

ans: The string is Oldstring

86. 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");

}

ans: Fail 1Pass 2

87. A code which had some declarations of some data items. There were a couple of normal data items (char, int..) and some pointers as well and a malloc call. You have to find the total memory taken up in the stack (Hint: Pointers and all are allocated in heap, not in stack, so don’t count them).Also in most of these questions, they were specifying that the OS was 32 bit.

88. A structure was given and it contained normal data as well as some bit-wise data. You had to find the total size taken up by the structure

89. Pointer to a function which returned an array of char pointers

ans: char *((*x)() ) []

90. Value of 2 particular variables in C(MAXINT and some other constant)

91. What do you need to do to open more than 10 files simultaneously in Microsoft Operating System?

ans: change stdio.h/change CONFIG.SYS/compiler dependent

92. main()

{

int i=7;

i = i++*i++;

printf("%d\n",i);

i=7;

printf("%d %d\n",i ,i++*i++);

i=2;

printf("%d %d\n" ,i, i++*++i*i++*i++);

i=1;

printf("%d %d %d\n", i, i++*i++,

i++*i++*++i*i++);

i=1;

printf("%d %d %d\n" ,i, i++*i++, i++*i++*++i*i++*i++*++i);

}

ans: 51

9 56

6 160

7 30 32

9 56 1120

93. main()

{

int d ;

int i=10;

d =sizeof(++i);

printf("%d",d);

}

ans: 2

94. //char *f();

main()

{

char *a,*f();

a=(char*)malloc(20*sizeof(char));

a=f();

printf("%s",a);

}

char *f()

{

static char n[20];

strcpy(n,"Hello World");

return(n);

}

ans: Hello World

95. char *f();

main()

{

char*a,*f();

a=(char*)malloc(20*sizeof(char));

a=f();

printf("%s",a);

}

char *f()

{char n[20];

strcpy(n,"Hello World");

return(n);

}

ans: unpredictable output. auto variable address should not be returned. It will lose its scope when it comes out of the block.

96. char *f()

main()

{

char *a,*f();

a=f();

printf("%s",a);

}

char *f()

{return("Hello World");}

ans: Hello World

97. what is the error

main()

{

int j=10;

switch(j)

{

case 20:

pritnf("Less than 20");

break;

case 30:

printf("Less than 30");

break;

default:

printf("hello");

}

ans: printf not pritnf and one brace } is missing

98. which is valid :
(i)char arr[10];
arr="hello";
(ii) char arr[]="hello";

ans: second is correct. In first lvalue required.

99. main()

{

char *str;

str=(char*)malloc(20*sizeof(char));

strcpy(str,"test");

strcat(str,'!');

printf("%s",str);

}

ans: strcat function arguments should be either a character array variable or a string constant. Instead of ‘!’ give “!”

100. How many times main is get called

main()

{
printf("Jumboree");
main();
}

ans: till stack overflow

C Language Interview Questions Part-3

31. int x;

main()

{

int x=0;

{

int x=10;

x++;

change_value(x);

x++;

Modify_value();

printf("First output: %d\n",x);

}

x++;

change_value(x);

printf("Second Output : %d\n",x);

Modify_value();

printf("Third Output : %d\n",x);

}

Modify_value()

{

return (x+=10);

}

change_value()

{

return(x+=1);

}

ans:

First output : 12

Second output : 1

Third output : 1

32. main()

{

int x=20,y=35;
x = y++ + x++;
y = ++y + ++x;
printf("%d %d\n",x,y);

}

ans: 57 94

33. main()

{

char *p1="Name";
char *p2;
p2=(char *)malloc(20);
while(*p2++=*p1++);
printf("%s\n",p2);

}

ans: No output since p2 is at null character to get output modify the program given below. (Note: should be included)

{

char *p1="Name";
char *p2,*p3;
p2=(char *)malloc(20);

p3=p2;
while(*p2++=*p1++);
printf("%s\n",p3);

}

34. main()

{

int x=5;
printf("%d %d %d\n",x,x<<2,x>>2);

}

ans: 5 20 1

35. #define swap1(a,b) a=a+b;b=a-b;a=a-b;

main()

{

int x=5,y=10;

swap1(x,y);

printf("%d %d\n",x,y);

swap2(x,y);

printf("%d %d\n",x,y);

}

int swap2(int a,int b)

{

int temp;

temp=a;

b=a;

a=temp;

return;

}

ans:

10 5

10 5

36. main()

{

char *ptr = "Ramco Systems";
(*ptr)++;
printf("%s\n",ptr);
ptr++;
printf("%s\n",ptr);

}

ans:

Samco Systems

amco Systems

37. main()

{

char s1[]="Ramco";
char s2[]="Systems";
s1=s2;
printf("%s",s1);

}

ans: lvalue required (s1 is base address of array)

38. main()

{

char *p1;
char *p2;
p1=(char *) malloc(25);
p2=(char *) malloc(25);
strcpy(p1,"Ramco");
strcpy(p2,"Systems");
strcat(p1,p2);
printf("%s",p1);

}

ans: RamcoSystems (Note: should be included)

39. A code like this is given.
a. for(i=0;ib. for(i=num;i>0;i--)
Assuming no code optimization and assume that the microprocessor
has flags etc. which one is faster.

Ans: b will execute faster.

40. main()

{

int a=1,b=2,c=3;
printf("%d,%d",a,b,c);

}

ans: 1, 2

41. main()

{

struct
{
char a[3];
int b;
}x;
char *cp;

printf(“%d %d”,sizeof(cp),sizeof(x));

}

ans: 4 5 since pointer cp stores address(32-bit) 4 bytes it takes and

and x takes 5 bytes(3 for character array a and 2 for int b)

42.main()

{
int p=3,q=4;
q = shw(&p);
printf("%d %d",p,q);
}

int shw(int *a)

{
*a = 10;
}

ans: 10 garbage

43. write 7*a interms of +,-,<<

ans: (x<<3-x)

44. main()

{

char *s1 = "hello",*s2 ="abce";
strcpy(s1,"");
s2[0] = s1[0];
printf("%d%d",strlen(s1),strlen(s2));

}

ans: 0 0

45. main()

{

int i=10;
printf("%d%d%d",i,i++,++i);

}

ans: 12 11 11 (compiler dependent)

46. const char *
char * const
What is the differnce between the above two?

ans: const char * pointer to a constant character

char * const constant pointer pointing to a character

47. main()

{

char *x="new";
char *y="dictonary";
char *t;
void swap (char * , char *);
swap (x,y);
printf("(%s, %s)",x,y);

char *t;
t=x;
x=y;
y=t;
printf("-(%s, %s)",x,y);
}
void swap (char *x,char *y)
{
char *t;
y=x;
x=y;
y=t;
}

ans: multiple declaration of t and all declarations should be before executable statement(errors)

48. main()

{

char p[]="string";
char t;
int i,j;
for(i=0,j=strlen(p);i{
t=p[i];
p[i]=p[j-i];
p[j-i]=t;
}
printf("%s",p);

}

ans: will not print anything since p will be pointing to a null string

49. main()

{

int i=10;
printf("%d %d %d",i,++i,i++);

}

ans: 12 12 10 (compiler dependent)

50. main()

{

void f(int,int);
int i=10;
f(i,i++);
}
void f(int i,int j)
{
if(i>50)
return;
i+=j;
f(i,j);
printf("%d,",i);

}

ans: 51 41 31 21 (i=11, j=10 for function ‘f’)

51. main()

{

void f(int,int);
int i=10;
f(i,++i);
}
void f(int i,int j)
{
if(i>50)
return;
i+=j;
f(i,j);
printf("%d,",i);

}

ans: 55 44 33 22 (i=11, j=11 for function ‘f’)

52. main()

{

char *s="hello world";

int i=7;

printf("%.*s",i,s);

}

ans: hello w

53. main()

{

int a,b;
printf("enter two numbers :");
scanf("%d%d",a,b);
printf("%d+%d=%d",a,b,a+b);

}

ans: will generate run time error /core dump

54. main()

{

union{

int x;

char y;

struct {

char x;

char y;

int xy;}p;

}q;

printf("\n %d,%d",sizeof(q),sizeof(int));

}

ans: 4,2

55. main()

{

char *x="String";
char y[] = "add";
char *z;
z=(char *) malloc(sizeof(x)+sizeof(y)+1);
strcpy(z,y);
strcat(z,x);
printf("%s+%s=%s",y,x,z);

}

ans: add+String=addString

56. an arrey of n pointers to function returning pointers to
functions returning pointers to characters

ans: char * (* (*x[n]) () ) ()

pointer to array of int, char etc., this is array pointer

ans: int (*x)[] char (*x)[]

array of pointer to int, char etc., this is pointer array

ans: int *x[] char *x[]

function returning pointer to int, char etc.,

ans: int *x() char *x()

pointer to function returning int, char etc.,

ans: int (*x)() char (*x)()

function returning pointer to array of pointer to function returning char

ans: char (*(*x()) []) ()

array of pointer to function returning pointer to array of char

ans: char (*(*x[]) () ) []

57. main()

{

enum number { a=-1, b= 4,c,d,e};

printf("%d",e);

}

ans: 7

58. 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);

}

}

ans: 16,21 (after case and default colon should be there)

59. main()

{

int i, count, x=1;

for(i=0, count=0;i<16;i++)

if( !(x&(1<

count++;

printf("%d",count);

}

ans: 15 (no. of zeros)

60. main()

{

int i, count, x=1;

for(i=0, count=0;i<16;i++)

if(x&(1<

count++;

printf("%d",count);

}

ans: 1 (no. of ones)

 
# #