You are on page 1of 34

What is the output of the program? #include<stdio.h> int main() { union a { int i; char ch[2]; }; union a u; u.ch[0] = 3; u.

ch[1] = 2; printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); return 0; } A.3, 2, 515 B. 515, 2, 3 C.3, 2, 5 D.None of these Answer & Explanation Answer: Option A Explanation: printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); It prints the value of u.ch[0] = 3, u.ch[1] = 2 and it prints the value of u.i means the value of entire union size.

So the output is 3, 2, 515. #include<stdio.h> int main() {

unsigned int i = 65535; /* Assume 2 byte integer*/ while(i++ != 0) printf("%d",++i); printf("\n"); return 0; } A.Infinite loop B. 0 1 2 ... 65535 C.0 1 2 ... 32767 - 32766 -32765 -1 0 D.No output Answer & Explanation Answer: Option A Explanation: Here unsigned int size is 2 bytes. It varies from 0,1,2,3, ... to 65535. Step 1:unsigned int i = 65535; Step 2: Loop 1: while(i++ != 0) this statement becomes while(65535 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '1'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement) Loop 2: while(i++ != 0) this statement becomes while(1 != 0). Hence the while(TRUE) condition is satisfied. Then the printf("%d", ++i); prints '3'(variable 'i' is already increemented by '1' in while statement and now increemented by '1' in printf statement) .... .... The while loop will never stops executing, because variable i will never become '0'(zero). Hence it is an 'Infinite loop'. What will be the output of the program? #include<stdio.h> int main() { float a = 0.7; if(0.7 > a) printf("Hi\n"); else printf("Hello\n"); return 0; } A.Hi C.Hi Hello

B. Hello D.None of above

Answer & Explanation Answer: Option A Explanation: if(0.7 > a) here a is a float variable and 0.7 is a double constant. The double constant 0.7 is greater than the float variable a. Hence the if condition is satisfied and it prints 'Hi' Example: #include<stdio.h> int main() { float a=0.7; printf("%.10f %.10f\n",0.7, a); return 0; } Output: 0.7000000000 0.6999999881 #include<stdio.h> int main() { int i=3; switch(i) { case 1: printf("Hello\n"); case 2: printf("Hi\n"); case 3: continue; default: printf("Bye\n"); } return 0; } A.Error: Misplaced continue C.No output Answer & Explanation Answer: Option A

B. Bye D.Hello Hi

Explanation: The keyword continue cannot be used in switch case. It must be used in for or while or do while loop. If there is any looping statement in switch case then we can use continue. What will be the output of the program? #include<stdio.h> int main() { int i = 1; switch(i) { printf("Hello\n"); case 1: printf("Hi\n"); break; case 2: printf("\nBye\n"); break; } return 0; } Hello A. Hi C.Hi Answer & Explanation Answer: Option C Explanation: switch(i) has the variable i it has the value '1'(one). Then case 1: statements got executed. so, it prints "Hi". The break; statement make the program to be exited from switch-case statement. switch-case do not execute any statements outside these blocks case and default Hence the output is "Hi". Which of the following statements are correct about the below program? #include<stdio.h> int main() { int i = 10, j = 20; if(i = 5) && if(j = 10)

Hello Bye D.Bye B.

printf("Have a nice day"); return 0; } A.Output: Have a nice day B. No output C.Error: Expression syntax D.Error: Undeclared identifier if Answer & Explanation Answer: Option C Explanation: "Expression syntax" error occur in this line if(i = 5) && if(j = 10). It should be like if((i == 5) && (j == 10)). Assunming, integer is 2 byte, What will be the output of the program? #include<stdio.h> int main() { printf("%x\n", -2<<2); return 0; } A.ffff B. 0 C.fff8 D.Error Answer & Explanation Answer: Option C Explanation: The integer value 2 is represented as 00000000 00000010 in binary system. Negative numbers are represented in 2's complement method. 1's complement of 00000000 00000010 is 11111111 11111101 (Change all 0s to 1 and 1s to 0). 2's complement of 00000000 00000010 is 11111111 11111110 (Add 1 to 1's complement to obtain the 2's complement value). Therefore, in binary we represent -2 as: 11111111 11111110. After left shifting it by 2 bits we obtain: 11111111 11111000, and it is equal to "fff8" in hexadecimal system. Which of the following range is a valid long double (Turbo C in 16 bit DOS OS) ? A.3.4E-4932 to 1.1E+4932 B. 3.4E-4932 to 3.4E+4932 -4932 +4932 C.1.1E to 1.1E D.1.7E-4932 to 1.7E+4932

Answer & Explanation Answer: Option A Explanation: The range of long double is 3.4E-4932 to 1.1E+4932 #include<stdio.h> int main() { float f=43.20; printf("%e, ", f); printf("%f, ", f); printf("%g", f); return 0; } A.4.320000e+01, 43.200001, 43.2 B. 4.3, 43.22, 43.21 C.4.3e, 43.20f, 43.00 D.Error Answer & Explanation Answer: Option A Explanation: printf("%e, ", f); Here '%e' specifies the "Scientific Notation" format. So, it prints the 43.20 as 4.320000e+01. printf("%f, ", f); Here '%f' specifies the "Decimal Floating Point" format. So, it prints the 43.20 as 43.200001. printf("%g, ", f); Here '%g' "Use the shorter of %e or %f". So, it prints the 43.20 as 43.2. What will be the output of the program? #include<stdio.h> int reverse(int); int main() { int no=5; reverse(no); return 0; } int reverse(int no) { if(no == 0) return 0;

else printf("%d,", no); reverse (no--); } A.Print 5, 4, 3, 2, 1 C.Print 5, 4, 3, 2, 1, 0 Answer & Explanation Answer: Option D Explanation: Step 1: int no=5; The variable no is declared as integer type and initialized to 5. Step 2: reverse(no); becomes reverse(5); It calls the function reverse() with '5' as parameter. The function reverse accept an integer number 5 and it returns '0'(zero) if(5 == 0) if the given number is '0'(zero) or else printf("%d,", no); it prints that number 5 and calls the function reverse(5);. The function runs infinetely because the there is a post-decrement operator is used. It will not decrease the value of 'n' before calling the reverse() function. So, it calls reverse(5) infinitely. Note: If we use pre-decrement operator like reverse(--n), then the output will be 5, 4, 3, 2, 1. Because before calling the function, it decrements the value of 'n'. 5. What will be the output of the program? #include<stdio.h> void fun(int); typedef int (*pf) (int, int); int proc(pf, int, int); int main() { int a=3; fun(a); return 0; } void fun(int n) { if(n > 0) { fun(--n); printf("%d,", n); fun(--n); B. Print 1, 2, 3, 4, 5 D.Infinite loop

} } A.0, 2, 1, 0 C.0, 1, 0, 2 Answer & Explanation Answer: Option D What will be the output of the program? #include<stdio.h> int fun(int(*)()); int main() { fun(main); printf("Hi\n"); return 0; } int fun(int (*p)()) { printf("Hello "); return 0; } A.Infinite loop C.Hello Hi Answer & Explanation Answer: Option C #include<stdio.h> int main() { int a=10; void f(); a = f(); printf("%d\n", a); return 0; } void f() { printf("Hi"); } A.Error: Not allowed assignment B. Error: Doesn't print anything C.No error D.None of above Answer & Explanation B. 1, 1, 2, 0 D.0, 1, 2, 0

B. Hi D.Error

Answer: Option A Explanation: The function void f() is not visible to the compiler while going through main() function. So we have to declare this prototype void f(); before to main() function. This kind of error will not occur in modern compilers. #include<stdio.h> int main() { printf("%p\n", main()); return 0; } A.It prints garbage values infinitely B. Runs infinitely without printing anything C.Error: main() cannot be called inside printf() D.No Error and print nothing Answer & Explanation Answer: Option B Explanation: In printf("%p\n", main()); it calls the main() function and then it repeats infinetly, untill stack overflow. If return type for a function is not specified, it defaults to int A.True B.False Answer & Explanation Answer: Option A Explanation: True, The default return type for a function is int. #include<stdio.h> #define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2); int main() { char *str1="India"; char *str2="BIX"; JOIN(str1, str2); return 0; } A.str1=IndiaBIX str2=BIX B. str1=India str2=BIX

C.str1=India str2=IndiaBIX Answer & Explanation Answer: Option B #include<stdio.h> #define CUBE(x) (x*x*x) int main() { int a, b=3; a = CUBE(b++); printf("%d, %d\n", a, b); return 0; } A.9, 4 C.27, 6 Answer & Explanation Answer: Option C Explanation:

D.Error: in macro substitution

B. 27, 4 D.Error

The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.) Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3. Step 2: a = CUBE(b++); becomes => a = b++ * b++ * b++; => a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement. => a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6) Step 3: printf("%d, %d\n", a, b); It prints the value of variable a and b. Hence the output of the program is 27, 6. #include<stdio.h> int main() { static char *s[] = {"black", "white", "pink", "violet"};

char **ptr[] = {s+3, s+2, s+1, s}, ***p; p = ptr; ++p; printf("%s", **p+1); return 0; } A.ink C.ite Answer & Explanation Answer: Option A What will be the output of the program? #include<stdio.h> int main() { int arr[3] = {2, 3, 4}; char *p; p = arr; p = (char*)((int*)(p)); printf("%d, ", *p); p = (int*)(p+1); printf("%d", *p); return 0; } A.2, 3 C.2, Garbage value Answer & Explanation Answer: Option B 15. What will be the output of the program ? #include<stdio.h> int main() { printf("%c\n", 7["IndiaBIX"]); return 0; } A.Error: in printf B. ack D.let

B. 2, 0 D.0, 0

B. Nothing will print

C.print "X" of IndiaBIX Answer & Explanation Answer: Option C 20. What will be the output of the program ? #include<stdio.h> int main() { char str1[] = "India"; char str2[] = "BIX"; char *s1 = str1, *s2=str2; while(*s1++ = *s2++) printf("%s", str1); printf("\n"); return 0; } A.IndiaBIX C.India Answer & Explanation Answer: Option B 21. What will be the output of the program ? #include<stdio.h> #include<string.h> int main() { int i, n; char *x="Alice"; n = strlen(x); *x = x[n]; for(i=0; i<=n; i++) { printf("%s ", x); x++; } printf("\n", x); return 0; }

D.print "7"

B. BndiaBIdiaBIXia D.(null)

A.Alice C.Alice lice ice ce e Answer & Explanation Answer: Option D Explanation:

B. ecilA D.lice ice ce e

If you compile and execute this program in windows platform with Turbo C, it will give "lice ice ce e". It may give different output in other platforms (depends upon compiler and machine). The online C compiler given in this site will give the Option C as output (it runs on Linux platform). 22. What will be the output of the program ? #include<stdio.h> int main() { int i, a[] = {2, 4, 6, 8, 10}; change(a, 5); for(i=0; i<=4; i++) printf("%d, ", a[i]); return 0; } void change(int *b, int n) { int i; for(i=0; i<n; i++) *(b+1) = *(b+i)+5; } A.7, 9, 11, 13, 15 C.2 4 6 8 10 Answer & Explanation Answer: Option B Which of the statements is correct about the program? #include<stdio.h> int main() {

B. 2, 15, 6, 8, 10 D.3, 1, -1, -3, -5

float a=3.14; char *j; j = (char*)&a; printf("%d\n", *j); return 0; } It prints ASCII value of the binary number present in the first byte of a float variable A. a. B. It prints character equivalent of the binary number present in the first byte of a float variable a.

C.It will print 3 D.It will print a garbage value Answer & Explanation Answer: Option A In the following program add a statement in the function fact() such that the factorial gets stored in j. #include<stdio.h> void fact(int*); int main() { int i=5; fact(&i); printf("%d\n", i); return 0; } void fact(int *j) { static int s=1; if(*j!=0) { s = s**j; *j = *j-1; fact(j); /* Add a statement here */ } } A.j=s; C.*j=&s;

B. *j=s; D.&j=s;

Answer & Explanation Answer: Option B 1. What will be the output of the program ? #include<stdio.h> int main() { int a[5] = {5, 1, 15, 20, 25}; int i, j, m; i = ++a[1]; j = a[1]++; m = a[i++]; printf("%d, %d, %d", i, j, m); return 0; } A.2, 1, 15 C.3, 2, 15 Answer & Explanation Answer: Option C Explanation: Step 1: int a[5] = {5, 1, 15, 20, 25}; The variable arr is declared as an integer array with a size of 5 and it is initialized to a[0] = 5, a[1] = 1, a[2] = 15, a[3] = 20, a[4] = 25 . Step 2: int i, j, m; The variable i,j,m are declared as an integer type. Step 3: i = ++a[1]; becomes i = ++1; Hence i = 2 and a[1] = 2 Step 4: j = a[1]++; becomes j = 2++; Hence j = 2 and a[1] = 3. Step 5: m = a[i++]; becomes m = a[2]; Hence m = 15 and i is incremented by 1(i++ means 2++ so i=3) Step 6: printf("%d, %d, %d", i, j, m); It prints the value of the variables i, j, m Hence the output of the program is 3, 2, 15 What will be the output of the program If characters 'a', 'b' and 'c' enter are supplied

B. 1, 2, 5 D.2, 3, 20

as input? #include<stdio.h> int main() { void fun(); fun(); printf("\n"); return 0; } void fun() { char c; if((c = getchar())!= '\n') fun(); printf("%c", c); } A.abc abc C.Infinite loop Answer & Explanation Answer: Option D Explanation: Step 1: void fun(); This is the prototype for the function fun(). Step 2: fun(); The function fun() is called here. The function fun() gets a character input and the input is terminated by an enter key(New line character). It prints the given character in the reverse order. The given input characters are "abc" Output: cba 7. What will be the output of the program ? #include<stdio.h> int main() { printf("India", "BIX\n"); return 0;

B. bca D.cba

} A.Error C.India Answer & Explanation Answer: Option C Explanation:

B. India BIX D.BIX

printf("India", "BIX\n"); It prints "India". Because ,(comma) operator has Left to Right associativity. After printing "India", the statement got terminated. 12. What will be the output of the program ? #include<stdio.h> #include<string.h> int main() { static char s[] = "Hello!"; printf("%d\n", *(s+strlen(s))); return 0; } A.8 C.16 Answer & Explanation Answer: Option B 13. What will be the output of the program ? #include<stdio.h> int main() { static char s[25] = "The cocaine man"; int i=0; char ch; ch = s[++i]; printf("%c", ch); ch = s[i++]; printf("%c", ch); ch = i++[s]; printf("%c", ch); ch = ++i[s];

B. 0 D.Error

printf("%c", ch); return 0; } A.hhe! C.The c Answer & Explanation Answer: Option A 20. What will be the output of the program ? #include<stdio.h> int main() { char str[] = "India\0BIX\0"; printf("%d\n", sizeof(str)); return 0; } A.10 C.5 Answer & Explanation Answer: Option D Explanation: The following examples may help you understand this problem: 1. sizeof("") returns 1 (1*). 2. sizeof("India") returns 6 (5 + 1*). 3. sizeof("BIX") returns 4 (3 + 1*). 4. sizeof("India\0BIX") returns 10 (5 + 1 + 3 + 1*). Here '\0' is considered as 1 char by sizeof() function. 5. sizeof("India\0BIX\0") returns 11 (5 + 1 + 3 + 1 + 1*). Here '\0' is considered as 1 char by sizeof() function. 21. What will be the output of the program ? #include<stdio.h> B. he c D.Hhec

B. 6 D.11

int main() { char str[25] = "IndiaBIX"; printf("%s\n", &str+2); return 0; } A.Garbage value C.No output Answer & Explanation Answer: Option A Explanation:

B. Error D.diaBIX

Step 1: char str[25] = "IndiaBIX"; The variable str is declared as an array of characteres and initialized with a string "IndiaBIX". Step 2: printf("%s\n", &str+2); => In the printf statement %s is string format specifier tells the compiler to print the string in the memory of &str+2 => &str is a location of string "IndiaBIX". Therefore &str+2 is another memory location. Hence it prints the Garbage value. #include<stdio.h> void swap(char *, char *); int main() { char *pstr[2] = {"Hello", "IndiaBIX"}; swap(pstr[0], pstr[1]); printf("%s\n%s", pstr[0], pstr[1]); return 0; } void swap(char *t1, char *t2) { char *t; t=t1; t1=t2; t2=t; } A.IndiaBIX

B. Address of "Hello" and "IndiaBIX"

Hello C. Hello IndiaBIX D. Iello HndiaBIX

Answer & Explanation Answer: Option C Explanation: Step 1: void swap(char *, char *); This prototype tells the compiler that the function swap accept two strings as arguments and it does not return anything. Step 2: char *pstr[2] = {"Hello", "IndiaBIX"}; The variable pstr is declared as an pointer to the array of strings. It is initialized to pstr[0] = "Hello", pstr[1] = "IndiaBIX" Step 3: swap(pstr[0], pstr[1]); The swap function is called by "call by value". Hence it does not affect the output of the program. If the swap function is "called by reference" it will affect the variable pstr. Step 4: printf("%s\n%s", pstr[0], pstr[1]); It prints the value of pstr[0] and pstr[1]. Hence the output of the program is Hello IndiaBIX 27. What will be the output of the program (Turbo C in 16 bit platform DOS) ? #include<stdio.h> #include<string.h> int main() { char *str1 = "India"; char *str2 = "BIX"; char *str3; str3 = strcat(str1, str2); printf("%s %s\n", str3, str1); return 0; } A.IndiaBIX India

B. IndiaBIX IndiaBIX

C.India India Answer & Explanation Answer: Option B Explanation:

D.Error

It prints 'IndiaBIX IndiaBIX' in TurboC (in 16 bit platform). It may cause a 'segmentation fault error' in GCC (32 bit platform). 2. Which of the following statements are correct about the below declarations? char *p = "Sanjay"; char a[] = "Sanjay"; 1: There is no difference in the declarations and both serve the same purpose. 2: p is a non-const pointer pointing to a non-const string, whereas a is a const pointer pointing to a non-const pointer. The pointer p can be modified to point to another string, whereas the individual characters within array a can be changed.

3:

4: In both cases the '\0' will be added at the end of the string "Sanjay". A.1, 2 C.3, 4 Answer & Explanation Answer: Option B What will be the output of the program ? #include<stdio.h> int main() { struct value { int bit1:1; int bit3:4; int bit4:4; }bit={1, 2, 13}; printf("%d, %d, %d\n", bit.bit1, bit.bit3, bit.bit4); B. 2, 3, 4 D.2, 3

return 0; } A.1, 2, 13 C.-1, 2, -3 Answer & Explanation Answer: Option C Explanation: Note the below statement inside the struct: int bit1:1; --> 'int' indicates that it is a SIGNED integer. For signed integers the leftmost bit will be taken for +/- sign. If you store 1 in 1-bit field: The left most bit is 1, so the system will treat the value as negative number. The 2's complement method is used by the system to handle the negative values. Therefore, the data stored is 1. The 2's complement of 1 is also 1 (negative). Therefore -1 is printed. B. 1, 4, 4 D.-1, -2, -13

If you store 2 in 4-bits field: Binary 2: 0010 (left most bit is 0, so system will treat it as positive value) 0010 is 2 Therefore 2 is printed.

If you store 13 in 4-bits field: Binary 13: 1101 (left most bit is 1, so system will treat it as negative value) Find 2's complement of 1101: 1's complement of 1101 : 0010 2's complement of 1101 : 0011 (Add 1 to the result of 1's complement)

0011 is 3 (but negative value) Therefore -3 is printed. 10. What will be the output of the program ? #include<stdio.h> int main() { struct byte { int one:1; }; struct byte var = {1}; printf("%d\n", var.one); return 0; } A.1 C.0 Answer & Explanation Answer: Option B 12. If a char is 1 byte wide, an integer is 2 bytes wide and a long integer is 4 bytes wide then will the following structure always occupy 7 bytes? struct ex { char ch; int i; long int a; }; A.Yes Answer & Explanation Answer: Option B Explanation: A compiler may leave holes in structures by padding the first char in the structure with another byte just to ensures that the integer that follows is stored at an location. Also, there might be 2extra bytes after the integer to ensure that the long integer is stored at an address, which is multiple of 4. Such alignment is done by machines to improve the efficiency of accessing values.

B. -1 D.Error

B.No

What will be the output of the program (myprog.c) given below if it is executed from the command line? cmd> myprog one two three /* myprog.c */ #include<stdio.h> int main(int argc, char **argv) { printf("%c\n", **++argv); return 0; } A.myprog one two three C.o Answer & Explanation Answer: Option C 8. What will be the output of the program (sample.c) given below if it is executed from the command line? cmd> sample friday tuesday sunday /* sample.c */ #include<stdio.h> int main(int argc, char *argv[]) { printf("%c", **++argv); return 0; } A.s C.sample Answer & Explanation Answer: Option B What will be the output of the program (myprog.c) given below if it is executed from the command line? cmd> myprog friday tuesday sunday /* myprog.c */ #include<stdio.h> int main(int argc, char *argv[]) {

B. myprog one D.two

B. f D.friday

printf("%c", *++argv[1]); return 0; } A.r C.m Answer & Explanation Answer: Option A 10. What will be the output of the program (sample.c) given below if it is executed from the command line? cmd> sample one two three /* sample.c */ #include<stdio.h> int main(int argc, char *argv[]) { int i=0; i+=strlen(argv[1]); while(i>0) { printf("%c", argv[1][--i]); } return 0; } A.three two one C.eno Answer & Explanation Answer: Option C 15. What will be the output of the program (myprog.c) given below if it is executed from the command line? cmd> myprog one two three /* myprog.c */ #include<stdio.h> int main(int argc, char *argv[]) { int i; for(i=1; i<argc; i++) printf("%c", argv[i][0]); return 0; B. f D.y

B. owt D.eerht

} A.oot C.nwh Answer & Explanation

B. ott D.eoe

Answer: Option B 21. What will be the output of the program (myprog.c) given below if it is executed from the command line? cmd> myprog one two three /* myprog.c */ #include<stdio.h> #include<stdlib.h> int main(int argc, char **argv) { int i; for(i=1; i<=3; i++) printf("%u\n", &argv[i]); return 0; } If the first value printed by the above program is 65517, what will be the rest of output? A.65525 65531 B. 65519 65521 C.65517 65517 Answer & Explanation Answer: Option B . What will be the output of the program? #include<stdio.h> #include<math.h> int main() { float i = 2.5; printf("%f, %d", floor(i), ceil(i)); return 0; } A.2, 3 C.2.000000, 0 D.65521 65525

B. 2.000000, 3 D.2, 0

Answer: Option C Explanation: Both ceil() and floor() return the integer found as a double. floor(2.5) returns the largest integral value(round down) that is not greater than 2.5. So output is 2.000000. ceil(2.5) returns 3, while converting the double to int it returns '0'. So, the output is '2.000000, 0'. If the binary eauivalent of 5.375 in normalised form is 0100 0000 1010 1100 0000 0000 0000 0000, what will be the output of the program (on intel machine)? #include<stdio.h> #include<math.h> int main() { float a=5.375; char *p; int i; p = (char*)&a; for(i=0; i<=3; i++) printf("%02x\n", (unsigned char)p[i]); return 0; } [A].40 AC 00 00 [B]. 04 CA 00 00 [C].00 00 AC 40 [D].00 00 CA 04 Answer: Option C Binary equivalent of 5.375 in normalised form is 0100 -> 4 0000 -> 0 1010 -> A 1100 -> C 0000 -> 0 0000 -> 0 0000 -> 0 0000 -> 0

Since the PC's (intel processors) use " LITTLE ENDIAN" byte order, the higher order byte of number is stored in lowest address. The result is written from bottom to top.

#include<stdio.h> int main() { float a=0.7; if(a < 0.7) printf("C\n"); else printf("C++\n"); return 0; } A.C B. C++ C.Compiler error D.Non of above if(a < 0.7) here a is a float variable and 0.7 is a double constant. The float variable a is less than double constant 0.7. Hence the if condition is satisfied and it prints 'C' #include<stdio.h> #define CUBE(x) (x*x*x) int main() { int a, b=3; a = CUBE(b++); printf("%d, %d\n", a, b); return 0; } A.9, 4 C.27, 6 Answer & Explanation Answer: Option C Explanation: The macro function CUBE(x) (x*x*x) calculates the cubic value of given number(Eg: 103.) Step 1: int a, b=3; The variable a and b are declared as an integer type and varaible b id initialized to 3. Step 2: a = CUBE(b++); becomes

B. 27, 4 D.Error

=> a = b++ * b++ * b++; => a = 3 * 3 * 3; Here we are using post-increement operator, so the 3 is not incremented in this statement. => a = 27; Here, 27 is store in the variable a. By the way, the value of variable b is incremented by 3. (ie: b=6) Step 3: printf("%d, %d\n", a, b); It prints the value of variable a and b. Hence the output of the program is 27, 6. What will be the output of the program? #include<stdio.h> #define FUN(i, j) i##j int main() { int va1=10; int va12=20; printf("%d\n", FUN(va1, 2)); return 0; } A.10 C.1020 Answer & Explanation Answer: Option B Explanation: The following program will make you understand about ## (macro concatenation) operator clearly. #include<stdio.h> #define FUN(i, j) i##j int main() { int First = 10; int Second = 20; char FirstSecond[] = "IndiaBIX"; printf("%s\n", Fun(First, Second) );

B. 20 D.12

return 0; } Output: ------IndiaBIX

The preprocessor will replace Fun(First, Second) as FirstSecond. Therefore, the printf("%s\n", Fun(First, Second) ); statement will become as printf("%s\n", FirstSecond ); Hence it prints IndiaBIX as output. Like the same, the line printf("%d\n", FUN(va1, 2)); given in the above question will become as printf("%d\n", va12 );. Therefore, it prints 20 as output.

We can prevent the same file from getting included again by using a preprocessor directive called #ifndef (short for "if not defined") to determine whether we've already defined a preprocessor symbol called XSTRING_H. If we have already defined this symbol, the compiler will ignore the rest of the file until it sees a #endif (which in this case is at the end of the file).

Macro calls and function calls work exactly similarly. A.True B.False Answer & Explanation Answer: Option B Explanation: False, A macro just replaces each occurrence with the code assigned to it. e.g. SQUARE(3) with ((3)*(3)) in the program. A function is compiled once and can be called from anywhere that has visibility to the funciton.

A macro must always be defined in capital letters. A.True B.False Answer: Option B Explanation: FALSE, The macro is case insensitive. Macros have a local scope. A.True Answer & Explanation Answer: Option B

B.False

The scope of macros is globals and functions. Also the scope of macros is only from the point of definition to the end of the file. After preprocessing all the macro in the program are removed. It is necessary that a header files should have a .h extension? A.Yes B.No Answer & Explanation Answer: Option B Explanation: No, the header files have any kind of extension. Will the program compile successfully? #include<stdio.h> int main() { #ifdef NOTE int a; a=10; #else int a; a=20; #endif printf("%d\n", a); return 0;

} A.Yes B.No Answer & Explanation Answer: Option A Explanation: Yes, this program will compile and run successfully and prints 20. The macro #ifdef NOTE evaluates the given expression to 1. If satisfied it executes the #ifdef block statements. Here #ifdef condition fails because the Macro NOTE is nowhere declared. Hence the #else block gets executed, the variable a is declared and assigned a value of 20. printf("%d\n", a); It prints the value of variable a 20. Will it result in to an error if a header file is included twice? A.Yes B. No C.It is compiler dependent. Answer & Explanation Answer: Option C Explanation: Unless the header file has taken care to ensure that if already included it doesn't get included again. Turbo C, GCC compilers would take care of these problems, generate no error. What will be the output of the program if the array begins at 65472 and each integer occupies 2 bytes? #include<stdio.h> int main() { int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; printf("%u, %u\n", a+1, &a+1); return 0; } A.65474, 65476 B. 65480, 65496 C.65480, 65488 D.65474, 65488 Answer & Explanation Answer: Option B Explanation: Step 1: int a[3][4] = {1, 2, 3, 4, 4, 3, 2, 1, 7, 8, 9, 0}; The array a[3][4] is declared as an integer array having the 3 rows and 4 colums dimensions.

Step 2: printf("%u, %u\n", a+1, &a+1); The base address(also the address of the first element) of array is 65472. For a two-dimensional array like a reference to array has type "pointer to array of 4 ints". Therefore, a+1 is pointing to the memory location of first element of the second row in array a. Hence 65472 + (4 ints * 2 bytes) = 65480 Then, &a has type "pointer to array of 3 arrays of 4 ints", totally 12 ints. Therefore, &a+1 denotes "12 ints * 2 bytes * 1 = 24 bytes". Hence, begining address 65472 + 24 = 65496. So, &a+1 = 65496 Hence the output of the program is 65480, 65496 3. What will be the output of the program (sample.c) given below if it is executed from the command line (Turbo C in DOS)? cmd> sample 1 2 3 /* sample.c */ #include<stdio.h> int main(int argc, char *argv[]) { int j; j = argv[1] + argv[2] + argv[3]; printf("%d", j); return 0; } A.6 C.Error Answer & Explanation Answer: Option C Explanation: Here argv[1], argv[2] and argv[3] are string type. We have to convert the string to integer type before perform arithmetic operation. Example: j = atoi(argv[1]) + atoi(argv[2]) + atoi(argv[3]); http://www.eskimo.com/~scs/cclass/int/sx4ab.html

B. sample 6 D.Garbage value

Because of the nature of base-2 arithmetic, it turns out that shifting left and shifting right are equivalent to multiplying and dividing by two. These operations are equivalent for the same reason that tacking zeroes on to the right of a number in base 10 is the same as multiplying by 10, and deleting digits from the right is the same as dividing by 10. You can convince yourself that 0x56 << 2 is the same as 0x56 * 4, and that 0x56 >> 1 is the same as 0x56 / 2. It's also the case that masking off all but the low-order bits is the same as taking a remainder; for example, 0x56 & 0x07 is the same as 0x56 % 8. Some programmers therefore use <<, >>, and & in preference to *, /, and % when powers of two are involved, on the grounds that the bitwise operators are ``more efficient.'' Usually it isn't worth worrying about this, though, because most compilers are smart enough to perform these optimizations anyway (that is, if you write x * 4, the compiler might generate a left shift instruction all by itself), they're not always as readable, and they're not always correct for negative numbers. The issue of negative numbers, by the way, explains why the right-shift operator >> is not precisely defined when the high-order bit of the value being shifted is 1. For signed values, if the high-order bit is a 1, the number is negative. (This is true for 1's complement, 2's complement, and sign-magnitude representations.) If you were using a right shift to implement division, you'd want a negative number to stay negative, so on some computers, under some compilers, when you shift a signed value right and the high-order bit is 1, new 1 bits are shifted in at the left instead of 0s. However, you can't depend on this, because not all computers and compilers implement right shift this way. In any case, shifting negative numbers to the right (even if the high-order 1 bit propagates) gives you an incorrect answer if there's a remainder involved: in 2's complement, 16-bit arithmetic, -15 is 0xfff1, so -15 >> 1 might give you 0xfff8shifted which is -8. But integer division is supposed to discard the remainder, so -15 / 2 would have given you -7. (If you're having trouble seeing the way the shift worked, 0xfff1 is 1111111111110001<sub>2</sub> and 0xfff8 is 1111111111111000<sub>2</sub>. The low-order 1 bit got shifted off to the right, but because the high-order bit was 1, a 1 got shifted in at the left.)

You might also like