TCS C Programming Interview placement Questions and Answers for Fresher

TCS Placement Interview questions answers and Solutions


1.

What will be output of following c program?

#include<stdio.h>
int main(){
enum number { a=-1, b= 4,c,d,e};
printf("%d",e);
return 0;
}

(A) 1
(B) 3
(C) 5
(D) 7
(E) 9


Explanation:
**

2.


What will be output of following c program?

#include<stdio.h>
int 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);
}
return 0;
}

(A) 0 5 9 13 17
(B) 5 9 13 17
(C) 12 17 22
(D) 16 21
(E) Syntax error


Explanation:
**

3.

What will be output of following c program?

#include<stdio.h>
int main(){
char c=-64;
int i=-32;
unsigned int u =-16;
if(c>i){
printf("pass1");
if(c<i)
printf("pass2");
else
printf("Fail2");
}
else
printf("Fail1”);

if(c==i)
printf("pass2");
else
printf("Fail2");
return 0;
}


(A) Pass1Pass2
(B) Pass1Fail2
(C) Fail1Pass2
(D) Fail1Fail2
(E) None

Explanation:
**

4.

What will the following program do?

#include<stdio.h>
#include<string.h>
#include<malloc.h>
int 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);
return 0;
} //Line no 15//

Chose correct option:


(A) Swap contents of p and a and print 
(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


Explanation:
**

5.

In the following code segment what will be the result of the function

#include<stdio.h>
int main(){
unsigned int x=-1;
int y;
y = ~0;
if(x == y)
printf("same");
else
printf("not same");
printf("%u %d",x,y);
return 0;
}

(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


Explanation:
**

6.

What will be the result of the following program?

#include<stdio.h>
#include<string.h>
char *gxxx(){
static char xxx[1024];
return xxx;
}
int main(){
char *g="string";
strcpy(gxxx(),g);
g = gxxx();
strcpy(g,"oldstring");
printf("The string is : %s",gxxx());
return 0;
}


(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


Explanation:
**

7.

What will be result of the following program?

#include<stdio.h>
#include<malloc.h>
int myalloc(char *x, int n){
x= (char *)malloc(n*sizeof(char));
memset(x,\0,n*sizeof(char));
}
int main(){
char *g="String";
myalloc(g,20);
printf("The string is %s",g);
return 0;
}

(A) The string is: String
(B) Run time error/Core dump
(C) The string is: Oldstring
(D) Syntax error during compilation
(E) None of these


Explanation:
**

8.

What will be the result of the following program?

#include<stdio.h>
int 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");
}
return 0;
}

(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


Explanation:
**

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 case you can't change the character
(E) None


Explanation:
**

7 comments:

omy said...

nice qes

sneha said...

for similar ques refer to 'TEST YOUR C SKILLS' by Yashwant Kanetkar.

Balaji Kulkarni said...

good questions ty helped alot...:)

Unknown said...

wer is explanation for all the above questions

Unknown said...

questions are very conceptual but somewhat difficult

Unknown said...

questions are very conceptual but somewhat difficult

Unknown said...

where is the explanation of these answers