Friday, December 26, 2008

TCS PLACEMENT Technical QUESTION WITH SOLUTIONS

Q1.
void main()
{
enum number { a=-1, b= 4,c,d,e}
printf(“%d”,e);
}
Output: 7
Q(2)Result of the following program is
void main()
{
int i=0;
for(i=0;i<20;i++) c="-64;" i="-32" u ="-16;">i){
printf("pass1,");
if(c
printf("pass2");
else
printf("Fail2");}
else
printf("Fail1);
if(i
printf("pass2");
else
printf("Fail2")
}
a)Pass1,Pass2
b)Pass1,Fail2
c)Fail1,Pass2
d)Fail1,Fail2
e)none
Ans is (c)
Q(4) 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 no:9//
p = malloc(strlen(Temp) + 1);
strcpy(p,Temp);
printf("(%s, %s)",a,p);
free(p);
free(a);
} //Line no 15//
Chose option
(a) Swap 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)
Q(5)In the following code segment what will be the result of the function,
void main()

//value of x , value of y
{
unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");

printf("%u %d",x,y);
getch();
}

(a) same, and x=MAXINT, y=-1
(b)not same, and x= MAXINT, y= -MAXINT
(c)same , and x=MAXUNIT,y -1
(d)same, iand x=y=MAXUNIT,
(e) not same, and x=MAXINT, y=MAXUNIT
Ans: (a)
Q(6) 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());
}
a) 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)
Q(7) 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
Run time error/Core dump
The string is : Oldstring
Syntax error during compilation
None of these
Ans: ( c )
Q(8)What will be the result of the following program?
void main()
{
char p[]="String";
int x;
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");
}
getch();
}


Output will be:
(a) Pass 1, Pass 2
(b) Fail 1, Fail 2
(c) Pass 1, Fail 2
(d) Fail 1, Pass 2
(e) syntax error during compilation
Ans:(d)
Q(9) Which of the choices is true for the mentioned declaration ?
const char *p;
and
char * const p;
Choose one of them :
(a) You can't change the character in both
(b) In first case, You can't change the character and second case You can’t change the pointer
(c) You can't change the pointer in both
(d) In first case you can't change the pointer and in second caseYou can't chanage the character
(e) None
Ans: (b)

WIPRO INTERVIEW QUESTION WITH SOLUTION on c

INFOSIS PLACEMENT QUESTION WITH SOLUTION on c

(1) In C,”X? Y : Z " is equal to

(a) if (X==0) then Y ;else Z
(b) if (X!=0) then Y ;else Z
(c) if (X==0) then Y ; else Z
Ans. (b)
Q(2). From the following program
// foo();

int foo(int a, int b)
{
if (a&b) return 1;
return 0;
}

(a) if either a or b are zero returns always 0
(b) if both a & b are non zero returns always 1
(c) if both a and b are negative returns 0
Ans: (a)
Q(3) The following function gives some error. What changes have to be made
void india( int a,int b)
{
int t;
t=a;
a=b;
b=t;
}

Ans: Actually by this function sorting is not possible because a and b is auto type variable which scope is only within the function. Use call by reference or assign a and b as static data type or return the sorted value by return keyword.

Q(4) Which of the following is incorrect
a) if a and b are defined as int arrays then (a==b) can never be true
b) parameters are passed to functions only by values
c) defining functions in nested loops
Ans: All are false
Q(8) What will value of cnt at the end ?
#include
void swap(int*,int*);
void main()
{
int arr[8]={36,8,97,0,161,164,3,9},i,j ;
for (i=0; i<7; i++)
{
for (j=i+1; j<8;j++)
if(arr[i]
}
getch();
}
void swap(int*x,int*y)
{
int temp; static int cnt=0;
temp= *x;
*x=*y;
*y=temp;
cnt++;
printf(" %d",cnt);
}

a) 7
b) 15
c) 1
d) none of these
Ans: (b)
Q(9) int main()
{
FILE *fp;
fp=fopen("test.dat","w");
fprintf(fp,'hello\n");
fclose(fp);
fp=fopen ("test.dat","w");
fprintf (fp, "world");
fclose(fp);
return 0;
}

If text.dat file is already present after compiling and execution how many bytes does the file occupy ?

a) 0 bytes
b) 5 bytes
c) 11 bytes
d) data is insufficient
Ans:

What is the output?
f1(int*x,int flag)
{
int *y;
*y=*x+3;
switch(flag)
{
case 0:
*x=*y+1;
break;
case 1:
*x=*y;
break;
case 2:
*x=*y-1;
break;
}
return(*y);
}
void main()
{
int y,j,i;
int *x=5;
i=f1(x,0);
j=f1(x,1);
printf("%d %d %d ",i,j,*x);
getch();
}


a) 8 8 8
b) 5 8 8
c) 8 5 8
d) none of these
Ans: (d)



INFOSIS INTERVIEW QUESTION WITH SOLUTION on c

49. What is meant by Recursion ?
Ans: Calling same fuction either directly or in directly is called function recursion.

50. For the following C program output will be:
Given that if sizeof(int)=4 and sizeof(long)=4

void main()
{
struct s
{
int a;
long b;
};
union u
{
int a;
long b;
};
printf("%d", sizeof(struct s));
printf("%d", sizeof(union u)) ;
getch();
}
Output 6 4


51. For the following C program
void main()
{
int i=1;
switch (i)
{
case 1:
i++;
case 2:
++i;
break;
case 3:
--i;
}
clrscr();
printf("%d",i);
getch();
}
Output 3



52. For the following C program
char S;
char S[6]= " HELLO";
printf("%s ",S[6]);
output of the above program ?

(a) 0
(b) ASCII 0
(c) I
(d) unpredictable

No comments:

Blog List