c1

11
THE TEST ON C (1) Explain the output of following progam. int main() { char arr[] = "Its very beautiful"; char *ptr = "Hello world"; char *str = "Sieze the day"; ptr = &arr[0]; printf("%s\n", ptr); ptr = &str[0]; printf("%s\n", ptr); } ? The program will starts from main function. The content of arr will be arr--> its very beautiful, arr is constant which is not supposed to be chang string with another string. ptr, is pointer variable which is pointing to constant string "Hello world", supposed to change the content "Hello world", but ptr may refer to another s str, is pointer variable which is refering to "Sieze the say". ptr = &arr[0], which means ptr is pointing to the string "Its very beautiful printf("%s\n", ptr);, it prints "Its very beautiful" on console ptr = &str[0], now starting address of str is placed in ptr, which means ptr pointing to the same string "Seize the say" printf`("%s\n", ptr);, it prints "Sieze the day" on the console exit from main program will be teriminated, actually there must be return statement, it wil PR Attribute ID Marks New Marks C_2 3 3 typedef struct emp { int emp_code; char* emp_name; char grade; Page 1 of 11 2/6/2009 http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

description

nnm

Transcript of c1

Page 1: c1

THE TEST ON C

�������

(1)

Explain the output of following progam. int main() { char arr[] = "Its very beautiful"; char *ptr = "Hello world"; char *str = "Sieze the day"; ptr = &arr[0]; printf("%s\n", ptr); ptr = &str[0]; printf("%s\n", ptr); } ?

The program will starts from main function. The content of arr will be arr--> its very beautiful, arr is constant which is not supposed to be changed but we override the string with another string. ptr, is pointer variable which is pointing to constant string "Hello world", here we are not supposed to change the content "Hello world", but ptr may refer to another string as well. str, is pointer variable which is refering to "Sieze the say". ptr = &arr[0], which means ptr is pointing to the string "Its very beautiful" printf("%s\n", ptr);, it prints "Its very beautiful" on console ptr = &str[0], now starting address of str is placed in ptr, which means ptr and str are now pointing to the same string "Seize the say" printf`("%s\n", ptr);, it prints "Sieze the day" on the console exit from main program will be teriminated, actually there must be return statement, it will be warning.

PR Attribute ID Marks New MarksC_2 3 3

typedef struct emp { int emp_code; char* emp_name; char grade;

Page 1 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 2: c1

(2)

double salary; char gender; struct emp* link; }employee; How do you modify the above structure to have less size in view of byte padding? Justify your answer. (You may assume 32 or 64 bit architecture). ?

Let us consider the machine be 32 bit machine, so that in one cycle 32 bits of data will be accessed. According to given structure the, the memory of 4 (emp_code)+ 4 (char ptr)+ 1 (char grade)+ 3 (pad)+ 8 (sal)+ 1 (char)+ 3 (pad)+ 4 (link) -------- 28 bytes , for double some compilers choose memory location which is divisible by 8.-------- if the structure is modified typedef struct emp { double salary; char *emp_name; struct emp* link; int emp_code; char grade; char gender; } now it will take 8+4+4+4+1+1 = 22, even in 64 bit machine we will get more optimizaton according to

PR Attribute ID Marks New MarksC_4 4 4

(3)

int main() { long num1 = 0; long num2 = 0; long *pnum = NULL; pnum = &num1; *pnum = 2; ++num2; num2 += *pnum; pnum = &num2; ++*pnum; } What will be the final value of num1 and num2? Explain. ?

Page 2 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 3: c1

The final values of num1 and num2 are 2 and 4 resp; initially num1 = 0, num2 = 0, pnum= NULL, pnum = &num1, pnum is pointing to num1. *pnum = 2 => the value 2 will be stored in num1. ++num2; => the value 0 in num2 will be incrimented by 1 => num2 becomes 1; num2 += *pnum which is : num2 = num2 + *pnum : num2 = 1 + 2 = 3 => numn2 =3 pnum = &num2, now pnum is pointing to num2 ++*pnum, now content of num2 will be incrimented by 1 so that the result will be 4 in num2, here * and ++ have same precedence and they are evaluated right to left finally num1 = 2 num2 = 4;

PR Attribute ID Marks New MarksC_9 4 4

(4)

int main(void) { char ch; int i; ch = 'd'; ch = ~ch; for( i = 128; i > 0; i = i / 2 ) if(i & ch) printf("1 "); else printf("0 "); } What does this program do? Explain. ?

Page 3 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 4: c1

the initial value of ch 0; ~ch will be -128 for is repeated for 8 times and every time we are doing the following operation i------>100000000000000 ch----->100000000000000 each time we are righ shifting i, if failes every time and 0 will be printed The output of the program is 1 0 0 0 0 0 0 0

PR Attribute ID Marks New MarksC_6 4 2

(5)

Explain the output of the following program? Is it better to use a fuction rather than macro here? #define swap(a,b,c) c = a; a = b; b = c; int main() { int a = 3,b = 5; int temp; swap(a,b,temp); printf("%d %d %d\n",a,b,temp); } ?

Page 4 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 5: c1

after preprocessing, the content will be int main() { int a = 3, b = 5; int temp; temp = a; a= b; b= temp; printf("%%d%d%d",\n", a,b,temp); the output will be 5 3 3 According to program the contents will be swaped, but it is beter to have function because there is code like if(cond) swap(a,b,temp) else ... in this situation we may get compilation errrors as well as runttime errors, it is beter avoid macro in this situation

PR Attribute ID Marks New MarksC_1 1 1C_5 2 1.5

(6)

void fun1(void) { static int dummy = 5; dummy = dummy + 10; printf("%d",dummy); } void fun2(char *c) { *c = *c + 2; printf("%d",dummy); printf("%c", *c); } int main() { char ch = 'A'; fun1(); fun2(&ch); printf("%c", ch); } The above program gives a compilation error. What is that? If the line that gives the error is commented, what would the output? ?

Page 5 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 6: c1

Yes, the above program will gives the compilation error, it will be in fun2 at statement printf("%d", dummy); the reason is dummy is not accessable to fun2, it is static variable. the scope is local and memory is allcated only once. if we commented that particular statement, the output will be 15CC 15 -> from fun1 C -> from fun2 C -> from main since in fun2 we are passing refernce but m=not value

PR Attribute ID Marks New MarksC_3 3 3C_5 1 1

(7)

Explain the output of the following program. int DoSomething (int a, int b) { if ( b == 0 ) return (a); else return (DoSomething(a-1, b-1)); } int main() { printf("%d\n",DoSomething(20,23)); } ?

Page 6 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 7: c1

The output of the prigram is -10 form the main we are calling DoSomething(20,30), in the function it will call DoSomething(19,29), since the condition is false, again DoSomthing(18,28); ................... .................. DoSomething(-9,1); DoSomething(-10,0); now b =0 the condition is true, return a= -10 All the stack frames are removed and the result will be returned to main stake frame. here we are displayed -10, In the process of evaluation, stack grous up to 31 frames

PR Attribute ID Marks New MarksC_8 4 2

(8) Place holder for Large Question (To de done on server) ?

PR Attribute ID Marks New MarksC_12 2 2C_14 2 1

Page 7 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 8: c1

C_17 4 3.5C_19 6 4.5C_20 2 1

(9)

If the content of a file (input) is abc def and the following program is executed to take input from the file: char input[100],ch; void main( void ) { FILE *fp; fp = fopen("input","rb"); fscanf(fp, "%s" ,&input); fscanf(fp, "%c" ,&ch); } What will be the value of input and ch? Reason your answer. ?

We have opened a file as binary file, having name input fp is pointing to fle input initially fscanf reads the data till we get space or tab or newline and depends on format specification first fscanf reads abc from the file, since the specification is %s, input content will be "abc" second fscanf reads one char, since the specification is %c,the content 'd' will be sent to ch. finally input contains "abc" ch contains 'd'

PR Attribute ID Marks New MarksC_11 4 2

#define SIZE 3 void fun(int *ptr) {

Page 8 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 9: c1

(10)

*ptr = *ptr + 1; } int main() { int oldval; const int newval = 0; static int temp = 99; int i; for (i=0;i< SIZE;i++) { oldval = newval; newval = i; fun(&temp); } printf("%d %d %d",oldval,newval,temp); } There is an error in this program. Identify that. If the line containing error is commented, what would be the output? ?

yes there is an error in the program, it was newval = i; we are trying to change the constant val, which gives compilation error If it is commented. The for loop is repeated for 3 times and every time temp will be inctemented by 1; it prints 0 0 102 since old val = 0; newval = 0; temp = 102;

PR Attribute ID Marks New MarksC_1 2 2C_3 3 3

#define MAX 10 int main() {

Page 9 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 10: c1

(11)

int low[MAX]; int index; int *high[3]; for(index = 0; index < MAX; index++) high[index] = low + index; } What is this program trying to do? What could be a bug here? ?

Here two arrays are created low[10], which can hold 10 integer values *high[3], which can hold 3 pointers which are refering to integers when the for loop is executed for 10 times, it will be like this high[0] = start addr (low) + 0; high[1] = start addr (low) + 1; high[2] = start addr (low) + 2; high[3] = start addr (low) + 3; ............................... .............................. high[9] = start addr (low) + 9; The program does not raise any compilation error, but for high we have only 3 cells, butt we are accessing high[3], high[4], .......high[9] which is not supposed to do. If memory is free in ram. If that memory allocated to other symbols, unexpected results will be obtained. Bug is out of bounds Exception

PR Attribute ID Marks New MarksC_2 2 2C_6 2 2

(12)

Compare and constrast following two functions. Also point out the bugs, if any. #define MAX 50 int fun1() { char *s; s=(char *) malloc (MAX*sizeof(char)); s="hello"; free(s); } int fun2() { char *s; s=(char *) malloc (MAX*sizeof(char));

Page 10 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532

Page 11: c1

strcpy(s,"hello"); free(s); } ?

The two functions are not same, there is a bug in fun1(). and fun2() will work properly. Actually the intention of the functions are to allocate a memory of 50 bytes and copy tge string hello in to the allocated memory. in fun1(), s can hold only the address of char. but "hello" constant symbol is resolved in compilation time. It will raise an error. "hello" is placed in code sengment. It is not possible to free the content the code segment, even we cant do modification. In fun2(), we are trying to copy hello in to the memory which is allocated dynamically, it will work properly.

PR Attribute ID Marks New MarksC_7 4 3C_10 2 2

Page 11 of 11

2/6/2009http://10.203.161.13/OES/take1.jsp?s1=431&s2=20532