/*question number 1*/ int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a; What number will z in the sample code above contain? Choice 1 5 Choice 2 6 Choice 3 10 Choice 4 11 Choice 5 12 _______________________________________________________________________ /*question number 2*/ With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed? Choice 1 unalloc() Choice 2 dropmem() Choice 3 dealloc() Choice 4 release() Choice 5 free() _______________________________________________________________________ /*question number 3*/ void *ptr; myStruct myArray[10]; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? Choice 1 ptr = ptr + sizeof(myStruct); Choice 2 ++(int*)ptr; Choice 3 ptr = ptr + sizeof(myArray); Choice 4 increment(ptr); Choice 5 ptr = ptr + sizeof(ptr); _______________________________________________________________________ /*question number 4*/ char* myFunc (char *ptr) { ptr += 3; return (ptr); } int main() { char *x, *y; x = "HELLO"; y = myFunc (x); printf ("y = %s \n", y); return 0; } What will print when the sample code above is executed? Choice 1 y = HELLO Choice 2 y = ELLO Choice 3 y = LLO Choice 4 y = LO Choice 5 x = O _______________________________________________________________________ /*question number 5*/ 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. 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. _______________________________________________________________________ /*question number 6*/ What function will read a specified number of elements from a file? Choice 1 fileread() Choice 2 getline() Choice 3 readfile() Choice 4 fread() Choice 5 gets() _______________________________________________________________________ /*question number 7*/ "My salary was increased by 15%!" Select the statement which will EXACTLY reproduce the line of text above. Choice 1 printf("\"My salary was increased by 15/%\!\"\n"); Choice 2 printf("My salary was increased by 15%!\n"); Choice 3 printf("My salary was increased by 15'%'!\n"); Choice 4 printf("\"My salary was increased by 15%%!\"\n"); Choice 5 printf("\"My salary was increased by 15'%'!\"\n"); _______________________________________________________________________ /*question number 8*/ What is a difference between a declaration and a definition of a variable? Choice 1 Both can occur multiple times, but a declaration must occur first. Choice 2 There is no difference between them. Choice 3 A definition occurs once, but a declaration may occur many times. Choice 4 A declaration occurs once, but a definition may occur many times. Choice 5 Both can occur multiple times, but a definition must occur first. _______________________________________________________________________ /*question number 9*/ int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; What value does testarray[2][1][0] in the sample code above contain? Choice 1 3 Choice 2 5 Choice 3 7 Choice 4 9 Choice 5 11 _______________________________________________________________________ /*question number 10*/ int a=10,b; b=a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a); what will be the output when following code is executed choice 1 12,10,11,13 choice 2 22,10,11,13 choice 3 22,11,11,11 choice 4 12,11,11,11 choice 5 22,13,13,13
*question number 1*/ int z,x=5,y=-10,a=4,b=2; z = x++ - --y * b / a; What number will z in the sample code above contain? Choice 3 10 __________________________________________________ _____________________ /*question number 2*/ With every use of a memory allocation function, what function should be used to release allocated memory which is no longer needed? Choice 5 free() __________________________________________________ _____________________ /*question number 3*/ void *ptr; myStruct myArray[10]; ptr = myArray; Which of the following is the correct way to increment the variable "ptr"? soory i couln't find the answer __________________________________________________ _____________________ /*question number 4*/ char* myFunc (char *ptr) { ptr += 3; return (ptr); } int main() { char *x, *y; x = "HELLO"; y = myFunc (x); printf ("y = %s \n", y); return 0; } What will print when the sample code above is executed? Choice 3 y = LO __________________________________________________ _____________________ /*question number 5*/ 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 2 It may fail since each node "nPtr" is freed before its next address can be accessed. __________________________________________________ _____________________ /*question number 6*/ What function will read a specified number of elements from a file? Choice 4 fread() __________________________________________________ _____________________ /*question number 7*/ "My salary was increased by 15%!" Select the statement which will EXACTLY reproduce the line of text above. / Choice 4 printf("\"My salary was increased by 15%%!\"\n"); / __________________________________________________ _____________________ /*question number 8*/ What is a difference between a declaration and a definition of a variable? acorrding to C++ Choice 1 Both can occur multiple times, but a declaration must occur first. according to C Choice 3 A definition occurs once, but a declaration may occur many times. __________________________________________________ _____________________ /*question number 9*/ int testarray[3][2][2] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; What value does testarray[2][1][0] in the sample code above contain? Choice 5 11 __________________________________________________ _____________________ /*question number 10*/ int a=10,b; b=a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a); what will be the output when following code is executed choice 5 22,13,13,13 I think i solved most of question,but think my c knowledge need more brush up
Added [thread=1373]100 Multiple choice questions in C[/thread] but some of them may be repeated from shab's post.
Hi, One of the solution which you have posted in "Multiple choice C++ question" is wrong. Question is: /*question number 10*/ Code: int a=10,b; b=a++ + ++a; printf("%d,%d,%d,%d",b,a++,a,++a); what will be the output when following code is executed Choice 1 12,10,11,13 Choice 2 22,10,11,13 Choice 3 22,11,11,11 Choice 4 12,11,11,11 Choice 5 22,13,13,13[Ans] The correct answer will be 22,13,14,14 Because if you look at the operator precedence then PreIncerement operator has higher precedence than print operator. Therefore, when we write printf("%d,%d,%d,%d",b,a++,a,++a); so here 'a++' then '++a' gets executed and then 'a' gets executed therefore we get result as 22,13,14,14. Hope you will correct this. Thanks ~Gaurav
Performing an action in an argument list that has a side-effect on another argument in the list is ill-advised since the order that the arguments are processed is undefined in C. Code: int a = 1; func( a, ++a ); The arguments can be processed in either order, so func could get either 1, 2 or 2, 2. Isn't that correct? Having said that, mingw/gcc prints 22,13,13,13.
> The correct answer will be 22,13,14,14 That's no more correct than any other explanation. http://c-faq.com/expr/evalorder2.html If you modify the same variable twice with either ++ or -- between sequence points, then the whole program is UNDEFINED. So what you get is entirely at the whim of the compiler and no guarantee that anyone else will have the same result (or any result at all for that matter).
Yes that's correct. However, if you look at it from the C++ point of view then pre increment has higher precedence. And since we are doing multiple mathematical operations in the ouput statement, so when it comes to pre increment and << (which is bitwise left shift) it gives higher precedence to pre-increment and therefore it first executes ++ and then it outputs a. Therefore, it will always give result ..,..,14,14
I just said that my system gives ...,...,13,13. So clearly it doesn't always give 14,14. Here is a piece of *bad* code and my output. Try it for yourself? Code: /* Caveat utilitor: broken code ahead! * For demonstration purposes only. */ #include <stdio.h> void f(int a, int b) { printf("%d %d\n", a, b); } int main() { int a; a = 1; printf("Pre: f(++a, a) --> "); f(++a, a); a = 1; printf(" f(a, ++a) --> "); f(a, ++a); a = 1; printf("Post: f(a++, a) --> "); f(a++, a); a = 1; printf(" f(a, a++) --> "); f(a, a++); } Output: Pre: f(++c, c) --> 2 1 f(d, ++d) --> 2 2 Post: f(a++, a) --> 1 1 f(b, b++) --> 2 1
Evaluation of precedence and evaluation of side effects are NOT synchronised in any way. Read the FAQ and try to get to grips with "before", "after" and "sequence points". If you believe that given a * b + ++c; that the increment of c only happens between the multiply of a*b and before the addition of the modified value of c, then you're very much mistaken. The language makes no such guarantees about such behaviour, and if you assume it does, you're going to get burned. Simply saying "this works for me" is no substitute for understanding these basic rules. And if you don't understand them, then you're laying yourself open to being burnt with some pretty obscure effects if you use them in a large program. Now whilst the FAQ I posted refers to C, you can be sure that the same rules apply to C++ as well. And now, some results from the test program. Code: $ gcc -W -Wall -ansi -pedantic -O2 foo.c foo.c: In function `main': foo.c:7: warning: operation on `a' may be undefined foo.c:8: warning: operation on `a' may be undefined foo.c:9: warning: operation on `a' may be undefined foo.c:10: warning: operation on `a' may be undefined $ ./a.exe Pre: f(++a, a) --> 2 1 f(a, ++a) --> 2 2 Post: f(a++, a) --> 1 1 f(a, a++) --> 2 1 $ gcc -W -Wall -ansi -pedantic -O3 foo.c foo.c: In function `main': foo.c:7: warning: operation on `a' may be undefined foo.c:8: warning: operation on `a' may be undefined foo.c:9: warning: operation on `a' may be undefined foo.c:10: warning: operation on `a' may be undefined $ ./a.exe Pre: f(++a, a) --> 2 2 f(a, ++a) --> 1 2 Post: f(a++, a) --> 1 2 f(a, a++) --> 1 1 Well how about that then!? Changing the optimisation level changes the results. Which is pretty obvious really. The compiler warned about possible undefined operations, and it came through in spades with different results. Optimisation does not affect the results of a correct program, but will generally play havoc with an undefined program. Lets try with VC6 Code: > cl /nologo foo.c foo.c > foo.exe Pre: f(++a, a) --> 2 1 f(a, ++a) --> 2 2 Post: f(a++, a) --> 1 1 f(a, a++) --> 1 1 > cl /O2 /nologo foo.c foo.c > foo.exe Pre: f(++a, a) --> 2 2 f(a, ++a) --> 2 2 Post: f(a++, a) --> 1 2 f(a, a++) --> 2 1 Wow, even more fun from the broken code. 4 different compilations, 4 different sets of results. This is NOT GOOD CODE. That is, after all, what my signature line means.
Question NO: 1 Choice 3: 10 Question No: 2 Choice 5: free() Question 3: Choice 4: By providing function by user can increment that pointer. Question 4: Choice 4 y = LO QUESTION 5: Choice 2 It may fail since each node "nPtr" is freed before its next address can be accessed. QUESTION
Question 6: Choice 4 fread() Note : Protype of this function is as : size_t fread(void *ptr, size_t size, size_t nitems, FILE *stream); Question 7: Choice 4 printf("\"My salary was increased by 15%%!\"\n"); Question 8: Choice 4 A declaration occurs once, but a definition may occur many times. Question 9: Choice 5 11 Question 10: 21 , 11, 13, 13
Question 10 : Depends on compiler also. Expression evaluatio starting from left side or right side.Depernds on parse tree provided by compiler. If expression is evaluating from left side then output will be 21 11 13 13
I should have made it more evident that I _meant_ for the code to be broken. It was just for humhain... to run and see the problem, that it gets different results. I've edited the post.
#include <stdio.h> void f(int a, int b) { printf("%d %d\n", a, b); } int main() { int a; a = 1; printf("Pre: f(++a, a) --> "); f(++a, a); a = 1; printf(" f(a, ++a) --> "); f(a, ++a); a = 1; printf("Post: f(a++, a) --> "); f(a++, a); a = 1; printf(" f(a, a++) --> "); f(a, a++); } ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ :!./a.out Pre: f(++a, a) --> 2 2 f(a, ++a) --> 2 2 Post: f(a++, a) --> 1 2 f(a, a++) --> 2 1 Press ENTER or type command to continue ------------------------------------------------------------------------------------------------- See dude my compiler is still giving different output then yours. Well, I am using UNIX machine I don't know if that matters. But oh well here is the output from my system .
hi need immediately ans for the following questions in c plz help me void freeList( struct node *n ) { while( n ) { ???? } } Which one of the following can replace the???? for the function above to release the memory allocated to a linked list? a)n = n->next; b) node m = n; c)struct node m = n; free( n ); n = n->next; free( n ); free( m ); n = m->next; d)free( n ); e)struct node m = n; n = n->next; free( m ); n = n->next; What are two predefined FILE pointers in C? a)stdout and stderr c)stdout and stdio b)console and error d)stdio and stderr e)errout and conout u32 X (f32 f) { union { f32 f; u32 n; } u; u.f = f; return u.n; } Given the function X(), defined above, assume that u32 is a type-definition indicative of a 32-bit unsigned integer and that f32 is a type-definition indicative of a 32-bit floating-point number. Which one of the following describes the purpose of the function defined above? a)X() effectively rounds f to the nearest integer value, which it returns. b)X() effectively performs a standard typecast and converts f to a roughly equivalent integer. c)X() preserves the bit-pattern of f, which it returns as an unsigned integer of equal size. d)Since u.n is never initialized, X() returns an undefined value. This function is therefore a primitive pseudorandom number generator. e)Since u.n is automatically initialized to zero (0) by the compiler, X() is an obtuse way of always obtaining a zero (0) value. Which one of the following is a true statement about pointers? a)They are always 32-bit values. b)For efficiency, pointer values are always stored in machine registers. c)With the exception of generic pointers, similarly typed pointers may be subtracted from each other. d)A pointer to one type may not be cast to a pointer to any other type. e)With the exception of generic pointers, similarly typed pointers may be added to each other All of the following choices represent syntactically correct function definitions. Which one of the following represents a semantically legal function definition in Standard C? a)Code: b)Code: c)Code: int count_digits (const char * buf) { int count_digits (const char * buf) { int count_digits (const char * buf) { assert(buf != NULL); int cnt = 0; int cnt = 0, i; int cnt = 0, i; assert(buf != NULL); assert(buf != NULL); for (i = 0; buf != '\0'; i++) for (int i = 0; buf != '\0'; i++) for (i = 0; buf != '\0'; i++) if (isdigit(buf)) if (isdigit(buf)) if (isdigit(buf)) cnt++; cnt++; cnt++; return cnt; return cnt; return cnt; } } } d)Code: e)Code: int count_digits (const char * buf) { int count_digits (const char * buf) { assert(buf != NULL); assert(buf != NULL); for (i = 0; buf != '\0'; i++) int cnt = 0; if (isdigit(buf)) for (int i = 0; buf != '\0'; i++) cnt++; if (isdigit(buf)) return cnt; cnt++; } return cnt; }