Вы находитесь на странице: 1из 11

c

c ? 
Questions for level 1c
c
L1.Q1 : Write the output of the following program c
c
#defineABC 20c
#define XYZ 10c
#define XXX ABC - XYZc
void main()c
{c
int a;c
a = XXX * 10;c
printf("%d ", a);c
}c
c
L1.Q2 : Write the output of this program c
c
#define calc(a, b) (a * b) / (a - b)c
void main()c
{ int a = 20, b = 10;c
printf("%d ", calc(a + 4, b -2));c
}c
c
L1.Q3 : What will be output of the following program ? c
c
void main()c
{c
int cnt = 5, a;c
do {c
a /= cnt;c
} while (cnt --);c
printf ("%d ", a);c
}c
c
L1.Q4 : Print the output of this program c
c
void main()c
{ int a, b, c, abc = 0;c
a = b = c = 40;c
if (c) {c
int abc;c
abc = a*b+c;c
}c
printf ("c = %d, abc = %d ", c, abc);c
}c
c
c
c ? 
L1.Q5 : Print the output of this program c
c
main()c
{c
int k = 5;c
if (++k < 5 && k++/5 || ++k <= 8);c
printf("%d ", k);c
}c
c
c
L1.Q6 : What is the output of this program ? c
c
void fn(int, int);c
main()c
{ int a = 5;c
printf("Main : %d %d ", a++, ++a);c
fn(a, a++);c
c
}c
void fn(int a, int b)c
{c
printf("Fn : a = %d b = %d ", a, b);c
}c
c
c
-------------------------------------------------------------------
Answers c
c
L1.A1 c
olution for 6  c
c
a = xxx * 10 c
which is => a = ABC - XYZ * 10c
=> a = 20 - 10 * 10c
=> a = 20 - 100c
=> a = -80c
c
L1.A2 c
olution for 6 c
c
Actual substitution is like this :c
calc(20+4, 10 -2) is calculated as follows c
(20+4 * 10-2) / (20+4 - 10-2)c
(20+40-2) / 12c
58 / 12 = 4.8 c
since it is printed in %d the ans is 4c
c
c
c

L1.A3 c
? 
olution for 6 c
c
Vhis problem will compile properly, but it will give runc
time error. It will give divide-by-zero error. Look in toc
the do loop portionc
c
do { a /= cnt; } while (cnt --);c
c
when the 'cnt' value is 1, it is decremented in 'whilec
( cnt --)' and on next reference of 'cnt' it becomes zero.c
a /= cnt; /* ie. a /= 0 */c
c
which leads to divide-by-zero error.c
c
L1.A4 c
olution for 6 c
c
the result will be c = 40 and abc = 0;c
because the scope of the variable 'abc' inside if(c) {.. }c
is not valid out side that if (.) { .. }.c
c
c
L1.A5 c
olution for 6 c
c
Vhe answer is 7. Vhe first condition ++k < 5 is checked andc
it is false (Now k = 6). So, it checks the 3rd conditionc
(or condition ++k <= 8) and (now k = 7) it is true. At thisc
point k value is incremented by twice, hence the value of kc
becomes 7. c
c
L1.A6 c
olution for 6 c
c
Vhe solution depends on the implementation of stack.c
(Depends on OS) In some machines the arguments are passedc
from left to right to the stack. In this case the resultc
will bec
c
Main : 5 7 Fn : 7 7 c
c
Other machines the arguments may be passed from right toc
left to the stack. In that case the result will bec
c
Main : 6 6c
Fn : 8 7c
c
c

c
c
c ? 
QUE  F LEVEL 2c
c
L2.Q1 : Write the output of this program c
c
main(){c
int *a, *s, i;c
s = a = (int *) malloc( 4 * sizeof(int));c
for (i=0; i<4; i++) *(a+i) = i * 10;c
printf("%d\n", *s++);c
printf("%d\n", (*s)++);c
printf("%d\n", *s);c
printf("%d\n", *++s);c
printf("%d\n", ++*s);c
}c
c
L2.Q2 : Checkout this program result c
c
void fn(int);c
static int val = 5c
c
main(){c
while (val --) fn(val);c
printf("%d\n", val);c
}c
void fn(int val){c
static int val = 0;c
for (; val < 5; val ++) printf("%d\n", val);c
c
}c
L2.Q3 : Can you predict the output of this program ? c
main()c
{c
typedef union {c
int a;c
char b[10];c
float c;c
} Union; c
Union x, y = { 100 };c
x.a = 50;strcpy (x.b, "hello");c
x.c = 21.50;c
printf ("Union 2 : %d %s %f\n", x.a, x.b, x.c);c
printf ("Union Y : %d %s %f\n", y.a, y.b, y.c);c
}c
c
L2.Q4 : Print the output of the program c
c
main()c
{c
struct Data {c
c
c

int a;c
? 
int b;c
} y[4] = { 1, 10, 3, 30, 2, 20, 4, 40};c
struct Data *x = y;c
int i;c
for(i=0; i<4; i++) {c
x->a = x->b, ++x++->b; c
printf("%d %d\t", y[i].a, y[i].b); c
}c
}c
c
L2.Q5 : Write the output of this program c
main()c
{ typedef struct {c
int a;c
int b;c
int c;c
char ch;c
int d;c
}xyz;c
typedef union {c
xyz X;c
char y[100];c
}abc;c
printf("sizeof xyz = %d sizeof abc = %d\n",sizeof(xyz), sizeof(abc));c
c
}c
c
L2.Q6 : Find out the error in this code c
#define Error(str) printf("Error : %s\n", str); exit(1); c
main()c
{ int fd;c
char str[20] = "Hello! Vest me";c
if ((fd = open("xx", O_CREAV | O_RDWR)) < 0) c
Error("open failed");c
if (write(fd, str, strlen(str)) < 0)c
Error("Write failed");c
if (read(fd, str, strlen(str)) < 0)c
Error("read failed");c
printf("File read : %s\n", str);c
close(fd);c
}c
c
L2.Q7 : What will be the output of this program ? c
main()c
{ int *a, i;c
a = (int *) malloc(10*sizeof(int));c
for (i=0; i<10; i++) c
*(a + i) = i * i;c
c
c

for (i=0; i<10; i++) c


? 
printf("%d\t", *a++);c
free(a);c
}c
L2.Q8 : c
Write a program to calculate number of 1's (bit) in a givenc
integer number i.e) Number of 1's in the given integer'sc
equivalent binary representation.c
c
--------------------------------------------------------------------------------------------------------

Answers c
c
L2.A1
olution for 6 c
c
Vhe output will be : 0 10 11 20 21c
*s++ => *(s++)c
*++s => *(++s)c
++*s => ++(*s)c
L2.A2
olution for 6c
Some compiler (ansi) may give warning message, but it will c
compile withouterrors.c
Vhe output will be : 0 1 2 3 4 and -1c
c
L2.A3
olution for 6c
c
Vhis is the problem about Unions. Unions are similar toc
structures but it differs in some ways. Unions can bec
assigned only with one field at any time. In this case,c
unions x and y can be assigned with any of the one field ac
or b or c at one time. During initialisation of unions itc
takes the value (whatever assigned ) only for the firstc
field. So, Vhe statement y = {100} intialises the union yc
with field a = 100.c
In this example, all fields of union x are assigned withc
some values. But at any time only one of the union fieldc
can be assigned. So, for the union x the field c isc
assigned as 21.50.c
Vhus, Vhe output will be c
Union 2 : 22 22 21.50c
Union Y : 100 22 22c
( 22 refers unpredictable results ) c
c
c
c ? 
L2.A4
olution for 6c
Vhe pointer x points to the same location where y is stored.c
So, Vhe changes in y reflects in x.c
Vhe output will be :c
c
10 11 30 31 20 21 40 41c
c
L2.A5
olution for 6c
c
Vhe output of this program is purely depends on thec
processor architecuture. If the sizeof integer is 4 bytesc
and the size of character is 1 byte (In some computers), thec
output will bec
sizeof xyz = 20 sizeof abc = 100c
c
Vhe output can be generalized to some extent as follows,c
sizeof xyz = 4 * sizeof(int) + 1 * sizeof(char) + c
padding bytesc
sizeof abc = 100 * sizeof(char) + padding bytesc
Vo keep the structures/unions byte aligned, some paddingc
bytes are added in between the sturcture fields. In thisc
example 3 bytes are padded between ' char ch' and 'int d'c
fields. Vhe unused bytes are called holes. Vo understandc
more about padding bytes (holes) try varing the field typesc
of the structures and see the output.c
c
L2.A6
olution for 6c
c
—ust try to execute this file as such. You can find outc
that it will exit immediately. Do you know why?c
With this hint, we can trace out the error. If you lookc
into the macro 'Error', you can easily identify that therec
are two separete statements without brases '{ ..}'. Vhat isc
the problem. So, it exits after the calling open(). Vhec
macro should be put inside the brases like this.c
c
#define Error(str) { printf("Error : %s\n", str); exit(1); }c
c
L2.A7
olution for 6c
c
Vhis program will fault (Memory fault/segmentation fault).c
Can you predict Why?c
c
c ? 
Remove the statment 'free(a);' from the program, thenc
execute the program. It will run. It gives the resultsc
correctly.c
c
What causes 'free(a)' to generate fault?c
—ust trace the address location of pointer variable 'a'.c
Vhe variable 'a' is incremented inside the 'for loop'. Outc
side the 'for loop' the variable 'a' will point to 'null'.c
When the free() call is made, it will free the data areac
from the base_address (which is passed as the argument ofc
the free call) upto the length of the data allocatedc
previously. In this case, free() tries to free the lengthc
of 10 *sizeof(int) from the base pointer location passed asc
the argument to the free call, which is 'null' in this case.c
Vhus, it generates memory fault.c
c
L2.A8
olution for 6 c
c
main(argc, argv)c
int argc;c
char *argv[];c
{c
int count = 0, i;c
int v = atoi(argv[1]);c
for(i=0; i<8*sizeof(int); i++) c
if(v &(1<< } count); v, %d='%d\n",' in 1?s of
printf(?No count++;>
c
c ? 
Questions For Level 3c
c
L3.Q1 : c
Write a function revstr() - which reverses the given stringc
in the same string buffer using pointers. (ie) Should notc
use extra buffers for copying the reverse string.c
c
L3.Q2 : c
Write a program to print the series 2 power x, where x >= 0c
( 1, 2, 4, 8, 16, .... ) without using C math library andc
arithmatic operators ( ie. *, /, +, - and math.h are notc
allowed)c
L3.Q3 : c
Write a program to swap two integers without using 3rdc
integer (ie. Without using any temporary variable)c
c
L3.Q4 : What will be the output of this program ? c
main()c
{ int *a, *savea, i;c
savea = a = (int *) malloc(4 * sizeof(int));c
for (i=0; i<4; i++) *a++ = 10 * i;c
for (i=0; i<4; i++) {c
printf("%d\n", *savea);c
savea += sizeof(int);c
}c
}c
LX.Q5 : race the program and print the output c
typedefint abc(int a, char *b);c
int func2(int a, char *b)c
{ a *= 2;c
strcat(b, "func2 ");c
return a;}c
int func1(int a, char *b)c
{ abc *fn = func2;c
a *= a;c
strcat(b, "func1 ");c
return (fn(a, b));c
}ccmain()c
{c
abc *f1, *f2;c
int res; c
static char str[50] = "hello! ";c
f1 = func1;c
res = f1(10, str);c
f1 = func2;c
res = f1(res, str);c
printf("res : %d str : %s\n", res, str);c
}c
c
c ? 
c
LX.Q6 : What will be the output of this program c
main()c
{ int a=3, b = 5;c
printf(&a["Ya!Hello! how is this? %s\n"], &b["junk/super"]);c
printf(&a["WHAV%c%c%c %c%c %c !\n"], 1["this"],c
2["beauty"],0["tool"],0["is"],3["sensitive"],4["CCCCCC"]);c
}c
cc
Answers c
L3.A1 c
char *rev_str(char *str)c
{ char *s = str, *e = s + strlen(s) -1;c
char *t = "junk"; /* to be safe - conforming with ANSI C std */c
while (s < e) { *t = *e; *e-- = *s; *s++ = *t; } c
return(str);c
}c
/* Another way of doing this */c
char *str_rev(char *str)c
{ int len = strlen(str),i=0,j=len/2;c
len--;c
while(i < j) {c
*(str+i)^=*(str+len)^=*(str+i)^=*(str+len);c
i++; len--;c
}c
return(str);c
}c
main (int argc, char **argv)c
{ printf("1st method : %s\n", rev_str(argv[1]));c
printf("2nd method : %s\n", str_rev(argv[1]));c
}c
c
L3.A2 c
void main()c
{ int i;c
for(i=0; i< 10; i++)c
printf("%d\t", 2 << i);c
}c
c
L3.A3 c
main()c
{ int a, b;c
printf("Enter two numbers A, B : ");c
scanf("%d %d", &a, &b);c
a^=b^=a^=b; /* swap A and B */c
printf("\nA = %d, B= %d\n", a, b);c
}c
c
c
c ? 
c
L3.A4c
Vhe first value will be 0, the rest of the three values willc
not be predictable. Actually it prints the values of thec
following location in each stepc
* saveac
* (savea + sizeof(int) * sizeof(int))ccccccccetc...c
ie. savea += sizeof(int) => savea = savea + c
sizeof(savea_type) * sizeof(int)c
( by pointer arithmatic)c
=> save = savea + sizeof(int) * sizeof(int)c
c
Note: You can verify the above by varing the type of 'savea'c
variable to char, double, struct, etc.c
Instead of statement 'savea += sizeof(int)' use savea++ thenc
the values 0, 10, 20 and 30 will be printed. Vhis behaviourc
is because of pointer arithmatic.c
c
LX.A5c
Vwo function pointers f1 and f2 are declared of the typec
abc. whereas abc is a pointer to a function returns int.c
func1() which is assigned to f1 is called first. Itc
modifies the values of the parameter 'a' and 'b' to 100,c
"hello! func1 ". In func1(), func2() is called whichc
further modifies the value of 'a' and 'b' to 200, "hello!c
func1 func2 " and returns the value of 'a' which is 200 toc
the main. Main calls f1() again after assigning func2() toc
f1. So, func2() is called and it returns the followingc
value which will be the output of this program.c
c
res : 400 str : hello! func1 func2 func2c
c
Vhe output string shows the trace of the functions called :c
func1() and func2() then again func2().c
LX.A6c
In C we can index an array in two ways. c
For example look in to the following linesc
int a[3] = {10, 20, 30, 40};c
In this example index=3 of array 'a' can be representedc
in 2 ways.c
1) a[3] and c
2) 3[a]c
i.e) a[3] = 3[a] = 40c
Extend the same logic to this problem. You will get thec
output as followsc
Hello! how is this? superc
Vhat is Cc

Вам также может понравиться