435,445 Members | 3,172 Online
Need help? Post your question and get tips & solutions from a community of 435,445 IT Pros & Developers. It's quick & easy.

# How well did I do??? Quiz 2

 P: n/a Here are my answers for quiz 2 - how well did I do? 1. Which of the following types cannot be used to subscript an array? a. short b. char c. float d. all of the above types may be used to subscript an array. D 2. Given the following declaration: int a[10]; which of the following is a pointer to the second element of a? a. a+1 b. a[2] c. a+2 d. &(a+1) A 3. Given the following declarations: int a[10], *p=a; which of the following is equivalent to a[1]? a. *(p+1) b. p(1) c. *(a+1) d. Both a and c. D 4. Given the following two-dimensional array declaration and initialization: int a[ ][4] = {{1,2,3,4},{5,6,7,8}}; what is the value of the expression *(a[1]+2)? a. 3 b. 6 c. 7 d. The subscript exceeds array bounds. A 5. What is the output of the following code? int a[5], i; for (i=0; i<5; ++i) a[(i+3) %5] = 2*i; for (i=0; i<5; ++i) printf("%d " , a[i]); a. 2 4 6 8 0 b. 4 6 8 0 2 c. 0 2 4 6 8 d. 4 6 8 0 2 C 6. Given the following two-dimensional array declaration: int A[2][5]; what is the type of A[1]? a. int b. int * c. int ** d. The expression has no type. A 7. Which of the following string declarations contains an error? a. char String1[3] = "xyz"; b. char String2[5] = "abcd"; c. char String3[7] = "hello"; d. char String4[ ] = "world"; A 8. Given the following declaration, char S[ ]= "abcdefg"; What is the output of the following statement? printf("%s", S+3); a. abcdefg b. abc c. defg d. cdefg C 9. Given the following declaration, char a[ ] = "cba", *p=a; What is the difference between the values of the expression ++*p and *++p? a. The first is equal to d, and the second one is equal to b. b. The first is equal to c, and the second one is equal to a. c. The first causes a compiler error, while the second does not. d. The two expressions have the same value. C 10. If S is a string, what can you say about the expression: strlen(S+strlen(S)) a. It has the value 0. b. It has a value of 1. c. It will generate an error. d. The value of the expression will depend on the contents of S. A 11. Given the following declarations: char a[ ] = "abc", *p = "def"; what is the effect of the following statement? p=a; a. It copies the string "abc" into the string p b. It changes p to point to string "abc" c. It copies the first character of a to the first character of p d. It generates an error. B 12. How is the length of a string determined in C? a. From the declared size of the array which contains it. b. By a byte stored at the beginning of the string. c. By a null byte stored at the end of the string. d. By a byte stored at the end of the string. C 13. What is the effect of the following recursive function? void Mystery(int A[ ], int size) { if (size > 0) {A[0] = 0; Mystery (A+1, size -1);} } a. It sets the first element of the array A to zero. b. It sets the last element of the array A to zero. c. It sets all elements of the array A to zero. d. It generates an infinite recursion loop. C 14. What is the error in the following recursion function? void Fill_Ary(int A[ ], int N) { if (N != 0) { --N; A[N] = N; Fill_Ary(A,N); } } a. It will always result in an infinite recursion loop. b. It will cause an infinite recursion loop for some values of N but not for others. c. There is a syntax error in the function. d. There is no error in the function. B 15. What is the effect of the following recursive function? int Count (float A[ ], int Size, float X) { if( Size <=0) { return 0; } else return (A[0] == X) + Count(A+1, Size-1, X); } a. It returns the number of elements in A. b. It sets each element of A to X. c. It returns the number of elements in A equal to X. d. It will result in an error. C 16. What is the output of the following code? void Mystery(int n) { if (n >0) Mystery(n-1); printf(" %d", n); } Mystery(9); a. 0 1 2 3 4 5 6 7 8 9 b. 1 2 3 4 5 6 7 8 9 c. 9 8 7 6 5 4 3 2 1 0 d. 9 8 7 6 5 4 3 2 1 A 17. What is the effect of the following code? int Mystery (int N) { if (N <= 0) return 0; else return (1 + Mystery (N/10) ); } a. It computes N/10 b. It computes the number of decimal digits in N c. It computes N*2 d. It produces an error D 18. What is the output of the following code? void Mystery (char *Str) { if (*Str != '\0') { Mystery(Str + 1); printf("%s\n", Str); } } Mystery("abcd"); a. d cd bcd abcd b. "null string" d cd bcd abcd c. abcd bcd cd d d. d cd bcd abcd A 19. The main difference between a structure and an array is: a. structures are fixed size, while arrays can vary in size. b. structures are made up of dissimilar components, while arrays are made up of identical components. c. structures can contain pointers, while arrays cannot. d. There is no real difference; whether a struct or an array is used is a matter of programming style. B 20. The -> operator has the effect of which other two operators? a. * and ++ (dereference and then increment pointer) b. - and > (decrement and then check for greater than) c. [] and . (get array element and then get number) d. * and . (dereference and then get member) D 21. What is the effect of dereferencing a pointer variable, which has the value NULL? a. Zero is returned. b. The operation is ignored. c. The compiler detects the error and issues an error message. d. A runtime error results. D 22. What is the advantage of using a pointer to a structure as a parameter to a function, instead of the structure itself? a. The code is easier to read. b. It is more efficient because the structure is not copied. c. There is no difference; it is a matter of style which is used. d. Passing a structure as a parameter is not allowed. B Questions 23 and 24 are based on the following program: struct vector { float x; float y; float z; }; void main(void) { struct vector a = {2.3, 4.2, 6.5}; struct vector b = {-1.2, 3.5, 5.1}; float scalar(vector *, vector *); printf("The answer is %.2f", scalar(&a, &b)); } float scalar(struct vector*po1, struct vector *po2) { float s; s=po1->x*po2->x + po1->y*po2->y + po1->z*po2->z; return(s); } 23. After running this program, the screen will show: a. 45.0900000 b. 45.09 c. the answer is 45.09 d. the answer is 45.090000 C 24. The scalar() function uses po1->x to access the first member of the a structure. Which of the following constructs can be equally used within the function to access the same member? a. po1.x b. *po1.x c. (*po1).x d. a.x C 25. Given the structure and pointer declarations shown below, choose the assignment statement which sets the Price member of the structure pointed to by PC to 1000. struct Computer { char Manufacturer[30]; float Price; int Memory; } *PC; a. PC->Price = 1000.0; b. PC.Price = 1000.0; c. *PC.Price = 1000.0; d. Computer.Price = 1000.0; A Nov 15 '05 #1
8 Replies

 P: n/a DJ wrote: Here are my answers for quiz 2 - how well did I do? 6. Given the following two-dimensional array declaration: int A[2][5]; what is the type of A[1]? a. int b. int * c. int ** d. The expression has no type. A None of the above. The type of A[1] is int[5]. 7. Which of the following string declarations contains an error? a. char String1[3] = "xyz"; b. char String2[5] = "abcd"; c. char String3[7] = "hello"; d. char String4[ ] = "world"; A Since you posted to both C and C++ groups for some reason, all of the above are legal in C but choice A is illegal C++. 21. What is the effect of dereferencing a pointer variable, which has the value NULL? a. Zero is returned. b. The operation is ignored. c. The compiler detects the error and issues an error message. d. A runtime error results. D Any of the above are legal under undefined behavior. 22. What is the advantage of using a pointer to a structure as a parameter to a function, instead of the structure itself? a. The code is easier to read. b. It is more efficient because the structure is not copied. c. There is no difference; it is a matter of style which is used. d. Passing a structure as a parameter is not allowed. B None of these answers are right (D was right in an ancient version of C) and A is highly subjective. The only advantage you can say is when you need to modify the structure directly or need know the address of it for some reason. B is highly implementation specific because passing a struct by pointer may require it to be written to a memory location and the compiler now has to deal with aliasing inside the function where otherwise it could be held in register(s) and no aliasing or overhead of writing to a memory location would exist. For small structs (say <= sizeof(float[4]) at least), I've found that passing it by value is much quicker than passing it by reference/pointer in certain cases. Nov 15 '05 #2

 P: n/a [Reply relates to C, and this matters in at least one question; followups set to comp.lang.c] DJ wrote: Here are my answers for quiz 2 - how well did I do? You got 4 out of the first 10 right. I didn't check the rest. Of those first ten questions, neither (6) nor (7) has a correct answer available in the multiple-choice, so you were kind of doomed, really. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk mail: rjh at above domain Nov 15 '05 #3

 P: n/a DJ wrote: Here are my answers for quiz 2 - how well did I do? There are so many mistakes in the quiz questions.. Even if I'm doing your homework, the schoolteacher deserves telling off! Also you are posting in c.l.c and c.l.c++, and some of the answers differ depending on whether the code is C or C++. 4. Given the following two-dimensional array declaration and initialization: int a[ ][4] = {{1,2,3,4},{5,6,7,8}}; what is the value of the expression *(a[1]+2)? a. 3 b. 6 c. 7 d. The subscript exceeds array bounds. A Hint: *(A+B) means A[b] , so the answer is a[1][2] (which is not 3) 5. What is the output of the following code? int a[5], i; for (i=0; i<5; ++i) a[(i+3) %5] = 2*i; for (i=0; i<5; ++i) printf("%d " , a[i]); a. 2 4 6 8 0 b. 4 6 8 0 2 c. 0 2 4 6 8 d. 4 6 8 0 2 C No. The program is of course equivalent to: a[0] = 4; a[1] = 6; a[2] = 8; a[3] = 0; a[4] = 2; printf("%d %d %d %d %d ", a[0], a[1], a[2], a[3], a[4]); In C, the answer is that it's implementation-defined as to whether it prints anything at all, since there was no newline character. I'm not sure if this also applies to C++. 6. Given the following two-dimensional array declaration: int A[2][5]; what is the type of A[1]? a. int b. int * c. int ** d. The expression has no type. A None of the above. The type is array[5] of int. (NB. Borland C++ gets this wrong sometimes) 7. Which of the following string declarations contains an error? a. char String1[3] = "xyz"; b. char String2[5] = "abcd"; c. char String3[7] = "hello"; d. char String4[ ] = "world"; A In C++, A is an error, but in C, they are all allowed. 14. What is the error in the following recursion function? void Fill_Ary(int A[ ], int N) { if (N != 0) { --N; A[N] = N; Fill_Ary(A,N); } } a. It will always result in an infinite recursion loop. b. It will cause an infinite recursion loop for some values of N but not for others. c. There is a syntax error in the function. d. There is no error in the function. B Bad question; if the function is always called with A being an array and N being the dimenson of the array, then it will operate correctly. If you call the function with garbage then it might write past the bounds of A (which isn't one of the options). 17. What is the effect of the following code? int Mystery (int N) { if (N <= 0) return 0; else return (1 + Mystery (N/10) ); } a. It computes N/10 b. It computes the number of decimal digits in N c. It computes N*2 d. It produces an error D What error were you expecting? 21. What is the effect of dereferencing a pointer variable, which has the value NULL? a. Zero is returned. b. The operation is ignored. c. The compiler detects the error and issues an error message. d. A runtime error results. D The behaviour is undefined: any of the above could happen (or anything else), although B, C, and D are all relatively common results. Questions 23 and 24 are based on the following program: struct vector { float x; float y; float z; }; void main(void) This line is illegal in C++. It must be: int main() or int main(int argc, char **argv) or equivalent. In C it may be legal depending on your compiler, but is not portable. { struct vector a = {2.3, 4.2, 6.5}; struct vector b = {-1.2, 3.5, 5.1}; float scalar(vector *, vector *); This line causes a compiler error in C, but not in C++ . printf("The answer is %.2f", scalar(&a, &b)); } float scalar(struct vector*po1, struct vector *po2) { float s; s=po1->x*po2->x + po1->y*po2->y + po1->z*po2->z; return(s); } 23. After running this program, the screen will show: a. 45.0900000 b. 45.09 c. the answer is 45.09 d. the answer is 45.090000 C E. Compiler error before running the program (in both C and C++). Nov 15 '05 #4

 P: n/a Old Wolf wrote: 7. Which of the following string declarations contains an error? a. char String1[3] = "xyz"; b. char String2[5] = "abcd"; c. char String3[7] = "hello"; d. char String4[ ] = "world"; A In C++, A is an error, but in C, they are all allowed. Since we are specifically talking about strings as opposed to mere character arrays, A is an error in C too: " A string is a contiguous sequence of characters terminated by and including the first null character." String1 will not contain the null character after the initialization and will therefore not be a string. Operations relying on the string property (such as the str* functions from the standard library) will cause undefined behavior for String1. Christian Nov 15 '05 #5

 P: n/a Christian Kandeler wrote: Old Wolf wrote: 7. Which of the following string declarations contains an error? a. char String1[3] = "xyz"; b. char String2[5] = "abcd"; c. char String3[7] = "hello"; d. char String4[ ] = "world"; A In C++, A is an error, but in C, they are all allowed. Since we are specifically talking about strings as opposed to mere character arrays, A is an error in C too: " A string is a contiguous sequence of characters terminated by and including the first null character." String1 will not contain the null character after the initialization and will therefore not be a string. String1 would not be a string even if it /were/ big enough to contain the null terminator. (See below.) Operations relying on the string property (such as the str* functions from the standard library) will cause undefined behavior for String1. True enough, but the question is still broken, because - in C at least - you *cannot* declare a string. The case in point, String1, is not a string declaration, nor would it be a string declaration even if it were written with [4] instead of [3]. It is a declaration (and definition) of an array of char. C has no string type (although it does have a string data format). And you can't declare instances of a non-existent type. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk mail: rjh at above domain Nov 15 '05 #6

 P: n/a 6. It does have a type, just not any of those on the list. Those who set the quiz probably think it's int * but it isn't. However you can point an int * to it in C++ without a cast, which you cannot do with any of the other types. 1. I don't think you can subscript an array with a float. I don't think compiler will automatically cast it to size_t for you. 9. *++p and ++*p are both valid statements. The operator precedence here is that closest to the p. 14. Will err when N is negative any value bigger than sizeof(A)/sizeof(int) (technically will produce undefined behaviour). For other high values you might get stack overflow. 17. Where N is positive it will give (b) - number of decimal digits. Nov 15 '05 #7

 P: n/a In comp.lang.c Richard Heathfield wrote: Of those first ten questions, neither (6) nor (7) has a correct answer available in the multiple-choice, so you were kind of doomed, really. 6 (which was "Given int A[2][5];, what is the type of A[1]" is int (*)[5], correct? 7 would really depend on how one defined "error", as char foo[3]="foo"; is legal but probably not what one would intend. Given that #23 and #24 refer to a void-returning main(), I would presume that whoever wrote the test could use a remedial course him/herself. -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome. Nov 15 '05 #8

 P: n/a Christopher Benson-Manica wrote: In comp.lang.c Richard Heathfield wrote: Of those first ten questions, neither (6) nor (7) has a correct answer available in the multiple-choice, so you were kind of doomed, really. 6 (which was "Given int A[2][5];, what is the type of A[1]" is int (*)[5], correct? No, the type is int[5]. This can be seen in the popular macro for determining the dimension of an array... #include #define countof(x) (sizeof (x) / sizeof *(x)) #define DUMP_U(x) \ printf("%s == %u\n", #x, (unsigned) x) int main(void) { int a[2][5]; puts("given: int a[2][5]"); DUMP_U( sizeof a ); DUMP_U( sizeof *a ); DUMP_U( sizeof **a ); DUMP_U( countof(a) ); DUMP_U( countof(a[1]) ); DUMP_U( countof(*a) ); return 0; } Note that if you were to pass a[1] as an argument to a function, it would decay (in the usual manner) to a pointer to the first element, thus becoming an int *. 7 would really depend on how one defined "error", as char foo[3]="foo"; is legal but probably not what one would intend. It's an error in C++. -- Peter Nov 15 '05 #9

### This discussion thread is closed

Replies have been disabled for this discussion.