Sunteți pe pagina 1din 27

1) #include<stdio.

h> int main(){ int a=2; if(a==2){ a=~a+2<<1; printf("%d",a); } else{ break; } return 0;

} ANS: (A) It will print nothing. (B) -3 (C) -2 (D) 1 (E) Compiler error ANS(Break is not part of if)

2) #include<stdio.h>

int main(){ char c=125; c=c+10; printf("%d",c); return 0;

(A) 135 (B) +INF (C) -121( CORRECT ANS) (D) -8 (E) Compiler error

3)

A binary tree having 20 nodes have hw many null branches?. ANS 21

4) void main() { while(1){ if(printf(%d,printf(%d))) break; else continue; }

Answer: Garbage values Explanation: The inner printf executes first to print some garbage value. The printf returns no of characters printed and this value also cannot be predicted. Still the outer printf prints something and so returns a non-zero value. So it encounters the break statement and comes out of the while statement.

5)

Tiem complexict in inerting element in queue A binary tree with n nodes has exactly n+1 null nodes.

6) What is the output of the following code? #include void main() { int s=0; while(s++<10)> # define a 10 main() { printf("%d..",a); foo(); printf("%d",a); } void foo()

{ #undef a #define a 50 }

1) 10..10 2) 10..50 3) Error (CORRECT ANS) chekc once again 4) 0

7) #include void func() { int x = 0; static int y = 0; x++; y++; printf( "%d -- %d\n", x, y ); }

int main() { func(); func(); return 0;

What will the code above print when it is executed? 1) 1 -- 1 1 -- 1 2) 1 -- 1 2 -- 1 3) 1 -- 1 2 -- 2 4)CORRECT ANS (1..1,1..2) 1 -- 1 1 -- 2

8) Code:

struct node *nPtr, *sPtr; /* pointers for a linked list. */ for (nPtr=sPtr; nPtr; nPtr=nPtr->next) { free(nPtr);

The sample code above releases memory from a linked list. Which of the choices below accurately describes how it will work? Choice 1 It will work correctly since the for loop covers the entire list. Choice 2 It may fail since each node "nPtr" is freed before its next address can be accessed. (CORRECT ANS) Choice 3 In the for loop, the assignment "nPtr=nPtr->next" should be changed to "nPtr=nPtr.next". Choice 4 This is invalid syntax for freeing memory. Choice 5 The loop will never end. ------------------

9)

penny = one nickel = five dime = ten quarter = twenty-five How is enum used to define the values of the American coins listed above? Choice 1 enum coin {(penny,1), (nickel,5), (dime,10), (quarter,25)}; Choice 2 enum coin ({penny,1}, {nickel,5}, {dime,10}, {quarter,25});

Choice 3 enum coin {penny=1,nickel=5,dime=10,quarter=25};[Ans] Choice 4 enum coin (penny=1,nickel=5,dime=10,quarter=25); Choice 5 enum coin {penny, nickel, dime, quarter} (1, 5, 10, 25);

10)

Code:

int i = 4; int x = 6; double z; z = x / i; printf("z=%.2f\n", z);

What will print when the sample code above is executed? Choice 1 z=0.00 Choice 2 z=1.00[Ans] Choice 3 z=1.50

Choice 4 z=2.00 Choice 5 z=NULL

11) Code:

int fibonacci (int n) { switch (n) { default: return (fibonacci(n - 1) + fibonacci(n - 2)); case 1: case 2: } return 1; }

The function above has a flaw that may result in a serious error during some invocations. Which one of the following describes the deficiency illustrated above? Choice 1 For some values of n, the environment will almost certainly exhaust its stack space before the calculation completes.(CORRECT ANS) Choice 2

An error in the algorithm causes unbounded recursion for all values of n. Choice 3 A break statement should be inserted after each case. Fall-through is not desirable here. Choice 4 The fibonacci() function includes calls to itself. This is not directly supported by Standard C due to its unreliability. Choice 5 Since the default case is given first, it will be executed before any case matching n.

12)

What is the output of this program?

Void main() { char thought[2][30]={"Don`t walk in front of me..","I am not follow"}; printf("%c%c",*(thought[0]+9),*(*(thought+0)+5)); }

kk Don`t walk in front of me I may not follow K[ANS]

13) (1) What will be output if you will compile and execute the following c code? (refernce:http://cquestionbank.blogspot.in/2012/02/tricky-c-questions-and-answers.html)

struct marks{

int p:3; int c:3; int m:2; }; void main(){ struct marks s={2,-6,5}; printf("%d %d %d",s.p,s.c,s.m); }

(a) 2 -6 5 (b) 2 -6 1 (c) 2 2 1 (d) Compiler error (e) None of these

Answer: (c) Explanation: Binary value of 2: 00000010 (Select three two bit) Binary value of 6: 00000110 Binary value of -6: 11111001+1=11111010 (Select last three bit) Binary value of 5: 00000101 (Select last two bit)

Complete memory representation:

14)

What will be output if you will compile and execute the following c code?

void main(){ printf("%s","c" "question" "bank"); }

(a) c question bank (b) c (c) bank (d) cquestionbank (e) Compiler error

Answer: (d) Explanation: In c string constant xy is same as x y

15) What will be output if you will compile and execute the following c code?

void main(){ int a=25; clrscr(); printf("%o %x",a,a); getch();

(a) 25 25 (b) 025 0x25 (c) 12 42 (d) 31 19 (e) None of these

Answer: (d) Explanation: %o is used to print the number in octal number format. %x is used to print the number in hexadecimal number format. Note: In c octal number starts with 0 and hexadecimal number starts with 0x.

16) What is error in following declaration?

struct outer{ int a; struct inner{ char c; }; };

(A)

Nesting of structure is not allowed in c.

(B) It is necessary to initialize the member variable. (C) (D) (E) Inner structure must have name. Outer structure must have name. There is not any error.

Explanation: It is necessary to assign name of inner structure at the time of declaration other wise we cannot access the member of inner structure. So correct declaration is: struct outer{ int a; struct inner{ char c; }name; };

17) What will be output of the following c program?

#include<stdio.h> int main(){ int _=5; int __=10; int ___; ___=_+__;

printf("%i",___); return 0; }

(A) (B) (C) (D) (E)

5 10 15 Compilation error None of these

Explanation: (C) 15 Variable name can have only underscore.

18) What will be output of the following c program?

#include<stdio.h> int main(){ int ABC=10; printf("%d",abc); return 0; }

(A)

10

(B) (C) (D) (E)

0 5 Compilation error None of these

Explanation: (D) Compilation error Variable name is case sensitive.

19)

What will be output of following c code?

#include<stdio.h> extern int x; int main(){ do{ do{ printf("%o",x); } while(!-2); } while(0); return 0; } int x=8;

Explanation

Output: 10 Explanation: Here variable x is extern type. So it will search the definition of variable x. which is present at the end of the code. So value of variable x =8 There are two do-while loops in the above code. AS we know do-while executes at least one time even that condition is false. So program control will reach at printf statement at it will print octal number 10 which is equal to decimal number 8. Note: %o is used to print the number in octal format. In inner do- while loop while condition is ! -2 = 0 In C zero means false. Hence program control will come out of the inner dowhile loop. In outer do-while loop while condition is 0. That is again false. So program control will also come out of the outer do-while loop.

20) What will be output if you will execute following c code?

#include<stdio.h> #include<conio.h> void main(){ int i=3,val; val=sizeof f(i)+ +f(i=1)+ +f(i-1); printf("%d %d",val,i);

} int f(int num){ return num*5; }

(A) (B) (C) (D) (E)

20 71 17 0 21 Compilation error

Correct answer is :(B) Runniing Prog in Dev C gives

91

21) What will be output if you will execute following c code?

#include<stdio.h> #include<conio.h> void main(){ int i=3,val; val=sizeof (f(i)+ +f(i=1)+ +f(i-1)); printf("%d %d",val,i); } int f(int num){ return num*5;

} (A)2 3 (B)4 3 (C)3 2 (D)Compilation error

Correct answer is :(B) 4 3 Explanation: Turbo C 3.0 and Turbo C 4.5 compiler: 23 Linux GCC complier 43 Any expression inside sizeof operator is never changed the value of the any variable. So value of variable i will remain 3. After the evaluation of expression inside sizeof operator we will get an integer value. So value of variable val will be sizeof int data type.

Note: Size of into in turbo C 3.0 and 4.5 is two byte while Linux gcc complier is four byte

22)

What will be output of following c program?

#include "string.h" typedef struct stu1{ char name1[6]; char name2[6]; double marks; }STU1;

void main(){ STU1 s1={"rohit","kumar",87.43},*p1; char *p; p1=&s1; p=memchr(p1,'u',sizeof(STU1)); printf("%s",p); } (A) (B) (C) (D) (E) r rohit umar rohit kumar Compilation error

ANS:- (C) umar

23)

What will be output of following c program?

#include<stdio.h> int main(){ float x; x=(int)1.1,(float)2.2,(int)3.3 ,5.4; printf("%f",x);

return 0; }

(A) (B) (C) (D) (E)

1.000000 5.400000 2.200000 3.300000 Compilation error

ANS:-(A) 1.000000

24) What will be output of following c program?

#include<stdio.h> #include<conio.h> float avg(float,float,float); void main(){ float p=1,q=2,r=-2,a; a=avg(p,(q=4,r=-12,q),r); printf("%f",a); } float avg(float x,float y,float z){ return (x+y+z)/3; }

(A) (B) (C) (D) (E)

0.111111 1.000000 -0.777777 -1.000000 Compilation error

ANS:- (B)

1.000000

25) What will be output of following c program?

#include<stdio.h> #include<conio.h> void main(){ int i=3,val; val=f(i)+ +f(i=1)+ +f(i-1); printf("%d",val); } int f(int num){ return num*5; } (A) (B) (C) (D) 30 20 21 31

(E)

Compilation error

ANS:- (B)

20

26) What will be output of following c program?

#include<stdio.h> int main(){ int a=5; static int b=a; printf("%d %d",a,b); return 0; } (A) (B) (C) (D) (E) 5 5 5 0 5 null 5 Garbage Compilation error

ANS:- (E)

Compilation error

27)

Miscellaneous

1) What will be output if you will compile and execute the following c code?

int extern x; void main() printf("%d",x); x=2; getch(); } int x=23;

(a) 0 (b) 2 (c) 23 (d) Compiler error (e) None of these

Answer: (c) Explanation: extern variables can search the declaration of variable any where in the program.

2) What will be output if you will compile and execute the following c code?

void main(){ int i=10; static int x=i; if(x==i) printf("Equal"); else if(x>i) printf("Greater than"); else printf("Less than"); }

(A) (B) (C) (D) (E)

Equal Greater than Less than Compiler error None of above

Explanation: static variables are load time entity while auto variables are run time entity. We can not initialize any load time variable by the run time variable. In this example i is run time variable while x is load time variable.

------------------------------------------------------------------------------------------

Static functions in C May 5, 2010

In C, functions are global by default. The static keyword before a function name makes it static. For example, below function fun() is static. static int fun(void) { printf("I am a static function "); }

Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.

For example, if we store following program in one file file1.c /* Inside file1.c */ static void fun1(void) { puts("fun1 called"); }

And store following program in another file file2.c /* Iinside file2.c */ int main(void) { fun1();

getchar(); return 0; }

Now, if we compile the above code with command gcc file2.c file1.c, we get the error undefined reference to `fun1' . This is because fun1() is declared static in file1.c and cannot be used in file2.c.

Please write comments if you find anything incorrect in the above article, or want to share more information about static functions in C.

------------------------------------------------------------------------------------------------------------------------------------------

What is the purpose of a function prototype? August 12, 2009

The Function prototype serves the following purposes -

1) It tells the return type of the data that the function will return. 2) It tells the number of arguments passed to the function. 3) It tells the data types of the each of the passed arguments. 4) Also it tells the order in which the arguments are passed to the function.

Therefore essentially, function prototype specifies the input/output interlace to the function i.e. what to give to the function and what to expect from the function.

Prototype of a function is also called signature of the function.

What if one doesnt specify the function prototype? Output of below kind of programs is generally asked at many places. int main() { foo(); getchar(); return 0; } void foo() { printf("foo called"); }

If one doesnt specify the function prototype, the behavior is specific to C standard (either C90 or C99) that the compilers implement. Up to C90 standard, C compilers assumed the return type of the omitted function prototype as int. And this assumption at compiler side may lead to unspecified program behavior.

Later C99 standard specified that compilers can no longer assume return type as int. Therefore, C99 became more restrict in type checking of function prototype. But to make C99 standard backward compatible, in practice, compilers throw the warning saying that the return type is assumed as int. But they go ahead with compilation. Thus, it becomes the responsibility of programmers to make sure that the assumed function prototype and the actual function type matches.

To avoid all this implementation specifics of C standards, it is best to have function prototype

S-ar putea să vă placă și