I just came across Washu's
C++ quiz, when I skimmed through
this GDNet thread, and I just finished answering the questions. Can someone check my answers? (He didn't provide the answers for the first quiz)
Quote:Given the following three lines of code, answer these questions.int* p = new int[10];
int* j = p + 11;
int* k = p + 10;
1.A) Is the behavior of the second line well defined?
1.B) If the behavior second line is well defined, where does the pointer point to?
1.C) What are some of the legal operations that can be performed on the third pointer?
1.A) Yes
1.B) It points to the location immediately after the int array (The address of 'p' + sizeof(int)*10). What's contained there, on the other hand, could be utter gibberish.
1.C) You can use any pointer manipulation operators on it, like any other pointer. (add, subtract, multiply, divide, etc...)
Quote:2) What output should the following line of code product?
int a = 10;
std::cout<<a<<a++<<--a;
2) 10 9 9
Quote:3) Assuming the function called in the following block of code has no default parameters, how many parameters does it take? Which objects are passed to it?
f((a, b, c), d, e, ((g, h), i));
3) It takes four parameters. (a, d, e, and g)
Quote:4) Assuming the function called in the following block of code takes an A* and a B*, what is bad about the code?f(new A(), new B());
4) What's bad is that you'll leak memory unless 'f' knows to free both 'A' and 'B', because the scope of A and B will end immediately following the function call, and so the user has no chance to free it themselves. 'f' must do the freeing (or else keep pointers to them to free later, depending on what the function is used for), and 'f' must also do the error checking, in case 'new' fails.
I know I made a few mistakes, from reading
this, but did I get any of the other questions right?