puts(someFun());
} Answer:
string constant Explanation:
The program suffers no problem and gives the output correctly because the character constants are stored in code/data area and not allocated in stack, so this doesn’t lead to dangling pointers.
194. char *someFun1() {
char temp[ ] = “string";
return temp;
}
char *someFun2() {
char temp[ ] = {‘s’, ‘t’,’r’,’i’,’n’,’g’};
return temp;
}
int main() {
puts(someFun1());
puts(someFun2());
} Answer:
Garbage values.
Explanation:
Both the functions suffer from the problem of dangling pointers. In someFun1() temp is a character array and so the space for it is allocated in heap and is initialized with character string “string”. This is created dynamically as the function is called, so is also deleted dynamically on exiting the function so the string data is not available in the calling function main() leading to print some garbage values. The function someFun2() also suffers from the same problem but the problem can be easily identified in this case.
End-Semester Examination
1) main()
{
int x[]={0,0,0,0,0};
int i;
for(i=1;i<=4;i++)
x[x[i]]++;
for(i=0;i<5;i++) printf("%d",x[i]);
}
What is the output of the code?
(a) 00000
(b) 11111 (c) 40000 (d) 12345 Answer: c
2) #define fun(a,b) a=a+b;b=a-b;a=a-b;
main() {
int x=5,y=10;
fun(x,y);
printf("%d %d\n",x,y);
}
What’s the output of the code?
(a) 15,5 (b) 10,5 (c) 10,10 (d) 5,15 Answer: b
3) main()
{
int A=5,x;
x=fun(&A,A);
printf("%d",x);
}
fun(int *x, int y)
{
*x=*x+1;
return(*x*y);
}
What is the output of the above program?
(a) 36 (b) 30 (c) 25
(d) Compiler dependent Answer: b
4) main()
{
fib(5);
}
fib(int n) {
if(n==0 || n==1)
return 1;
return fib(n-1)+fib(n-2);
}
How many times functions fib() is called (a) 14 (b) 15 (c) 16 (d) 3 Answer: b
5) #define scanf "%s is a string"
main() {
printf(scanf,scanf);
}
What is the output of the code?
(a) compile error (b) scanf is a string
(c) %s is a string is a string (d) %s is a string
Answer: c
6) int n=7623,result=0, temp;
while(n>0) {
temp=n/10;
result+=temp*10;
n=n/10;
}
What is the value of result after the loop has executed?
a. 7623 b. 7600 c. 8450 d. 8400 Answer: c
7) struct A
{
int a;
char b;
int c;
};
union B {
char a;
int b;
int c;
};
Which of the following is correct?
a. size of A is always diff. form size of B.
b. size of A is always same as size of B.
c. we can't say anything because A,B are not homogenous d. size of a can be same depending on usage of B.
Answer: a
8) main()
{
int a[]={ 2,4,6,8,10};
int i;
change(a,5);
for( i = 0; i <= 4; i++) printf("\n %d",a[i]);
}
change( int *b, int n) {
int i;
for( i = 0; i < n; i++) *(b+i) = *(b+i) + 5;
}
What’s the output of the code?
a. 7 9 11 13 15 b. 2 4 6 8 10 c. 8 10 12 14 16 d. none of the above Answer: a
9) 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);
} }
What is the output?
(a) 0,5,9,13,17, (b) 5,9,13,17, (c) 12,17,22,
(d) 16,21, Answer: d
10) What does following program print?
main()
{
inc(); inc(); inc();
}
inc() {
static int x;
printf("%d", ++x);
}
(a) 012 (b) 123
(c) 3 consecutive unprectiable numbers (d) 111.
Answer: b 11) main() {
unsigned char ch;
FILE *fp=fopen("Trial.txt", "r");
while((ch=getc(fp))!=EOF)
printf("%c\n", ch);
fclose(fp);
}
What does the above program do?
a) Prints the word "Trail.txt"
b) Prints the contents of the file "Trail.txt"
c). Infinite loop/Segmentation Fault d). Both b and c
Answer: d 12) main()
{
unsigned char ch = 0x80;
printf("%d\n", ch<<1);
}
What will be the output of the program?
a) Memory Overflow b) 256
c) 128 d) 64 Answer: b
13) If the following statement is successfully executed then fp points to...
FILE *fp = fopen("trial", "r");
a) The first character in the file
b) A structure which contains a char pointer to the first character in the file c) The name of the file
d) None of the above Answer: b
14) main() {
char a[]="Visual C++";
char *b ="Visual C";
int i, *c=&i;
printf("%d %d %d, ", sizeof(a), sizeof(b), sizeof(c));
printf("%d %d %d\n", sizeof(*a), sizeof(*b), sizeof(*c));
}
What will be the output?
a) 11 1 4, 1 1 4 b) 11 4 4, 1 1 4 c) 10 1 4, 1 1 4 d) 10 4 4, 1 1 4 Answer: b 15) main()
{
int a[]={2, 3, 1, 5, 6};
printf("%u %d ", &a[1]-a, sizeof(a)/sizeof(int));
} It prints:
a) 1 5 b) 4 5 c) 1 20 d) 4 20 Answer: a
16) What is the output of following program?
main() {
printf(5+"FiftySix");
} a) Compilation Error b) SixtyOne
c) Six
d) 5+FiftySix Answer: c
17) #define int char
main() {
int i=65;
printf("sizeof(i)=%d",sizeof(i));
} What is the output?
a) sizeof(i)=1 b) sizeof(i)=4 c) sizeof(i)=65 d) sizeof(i)=0 Answer: a
18) What is the output of the following program?
main() {
char s1[]="Hello";
char s2[]="There!";
s1=s2;
printf("%s",s1);
} a) Hello There!
b) Hello c) There!
d) Compilation Error Answer: d
19) A memory of 20 bytes is allocated to a string declared as char *s then the following two statements is executed:
s="Entrance"
l=strlen(s);
What is the value of l?
a) 20 b) 8 c) 9 d) 21 Answer: b
20) What is the output of the following program?
# define prod(a,b) a*b main()
{
int x=2, y=3;
printf("%d\n",prod(x+2,y-10));
} A) 0
B) -2 C) -28
D) None Answer: B
21) main()
{
char *ptr;
ptr = (char *)malloc(sizeof(char)*20);
strcpy(ptr,"IIT Kanpur");
(*ptr)++;
printf("%s ",ptr);
ptr++;
printf("%s\n",ptr);
}
a) JIT Kanpur IT Kanpur b) IIT Kanpur IIT Kanpur c) IT Kanpur JIT Kanpur d) IT Kanpur IT Kanpur Answer: a
22) main() {
float i=1.5;
switch(i) {
case 1: printf("1");
case 2: printf("2");
default : printf("0");
} }
a) 120
b) 1
c) 0
d) Compiler Error Answer: d
23) Have a look at these two statements i. (a<=20) ? (b=30) : (c=30);
ii. *((a<=20) ? &b : &c) = 30;
Which of the following is correct?
a) (i) and (ii) are same b) (i) and (ii) are different c) (ii) results in syntax error d) None of the above
Answer: a
24) What is the output of the following program?
main()
{
int i=-3, j=2, k=0, m;
m = ++i && ++j || ++k;
printf("%d %d %d %d\n", i, j, k, m);
}
a) Depends upon the mood of the compiler b) -2 3 1 1
c) -2 3 0 1 d) Syntax error Answer: c
25) a[i][j][k][l] is equivalent to ...
a) *(*(*(*(a+i)+j)+k)+l) b) *(*(*(*a+i)+j)+k)+l c) *a+i+j+k+l
d) *(a+i+j+k+l) Answer: a
26) How will you declare "an array of 3 function pointers where each function receives an int and returns a float"...?
a) float (*arr[3]) (int);
b) float *arr (int) [3];
c) float (*arr) (int) [3];
d) (float *) arr[3] (int);
Answer: a 27) main() {
int i = 0;
switch(i) {
printf("Common to both the cases. ");
case 0:
printf("Slow and Steady wins the race");
break;
case 1:
printf("Who are they...?");
break;
}
switch(i) {
}
}
a) Common to both the cases. Slow and Steady wins the race.
b) Slow and Steady wins the race.
c) Error in First switch...case d) Error in Second switch...case
Answer: b 28) main() {
int i=5, j=4;
switch(i) {
default: printf("Default");
break;
case 1:
printf("Case 1");
break;
case 7/2.0:
printf("Case 2");
break;
case (3*2)-4:
printf("Case 3");
break;
case j:
printf("Case 4");
break;
}
} a) Default
b) Error in Second, Fourth cases c) Error in Second, Third, Fourth cases d) Error in Fourth case
Answer: b 29) main()
{
int a=10, b;
a>=5 ? b=100 : b=200;
printf("a = %d, b = %d\n", a, b);
}
a) a = 10, b = 100 b) a = 10, b = 200 c) Compilation Error d) Segmentation Fault Answer: c
30) main() {
char ch='a';
printf(ch ? "%d" : "%c", ch);
} It prints:
a) a b) 97 c) 65 d)Syntax Error Answer: b
31) main() {
int i=1;
switch(i++)
{
case 1: printf("One... "); continue;
case 2: printf("Two... "); break;
}
} What is the output?
a) One... Two...
b) One...
c) Infinite Loop d) Syntax error Answer: d
32) main()
{
static int a[20];
int i=0;
a[i] = i++;
printf("%d %d %d\n", a[0], a[1], i);
} a) 0 0 1
b) 1 0 1 c) 0 1 1
d) Compiler Dependent Answer: d
33) main()
{
int i=040, j=0x20, k, l, m;
k = i | j;
l = i & j;
m = i ^ j;
printf("%d %d %d %d %d\n", i, j, k, l, m);
} What is the output?
a) 40 20 60 0 60 b) 32 32 64 0 -1 c) 32 32 32 32 0 d) 32 32 32 60 0 Answer: c
34) main(){
int a,b;
int *p,*q;
a=10;b=19;
p=&(a+b);
} a) error in p=&(a+b) b) warning in p=&max
c) compiles successfully with no warnings
d) compiles successfully but segmentation fault at runtime Answer: a
35) main() {
int i=5;
printf("%d %d\n", i++, i++);
} Output is:
a) 5 5 b) 6 5 c) 5 6
d) Compiler Dependent Answer: d
36) What does the following function print?
func(int i)
{
if(i%2)return 0;
else return 1;
}
main() { {
int i =3;
i=func(i);
i=func(i);
printf("%d",i);
} a) 3
b) 1 c) 0 d) 2 Answer: b
37) When an array is passed as parameter to a function, which of the following statement is correct choice:
a) The function can change values in the original array
b) In C parameters are passed by value. The function cannot change the original value in the array.
c) It results in compilation error when the function tries to access the elements in the array.
d) Results in a run time error when the funtion tries to access the elements in the array.
Answer: a
38) int foo(int a, int b) {
if (a&b) return 1;
return 0;
}
Which of the following is true?
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 always 0 d) if both are positive returns always 0
Answer: a
39) What would the following program results in main()
{
char p[]="string";
char t;
int i,j;
for(i=0,j=strlen(p);i<j;i++) {
t=p[i];
p[i]=p[j-i];
p[j-i]=t;
}
printf("%s",p);
} a) will print:string
b) will not print anything since p will be pointing to a null string c) will print:gnirtS
d) will result in a complication error Answer: b
40) main() {
unsigned int x=-1;
int y;
y = ~0;
if(x == y) printf("same");
else
printf("not same");
}
What will be the output and the values of x and y respectively?
a) same, MAXINT, -1
b) not same, MAXINT, -MAXINT c) same, MAXUNIT, -1
d) same, MAXUNIT, MAXUNIT e) not same, MAXINT, MAXUNIT Answer: a