C++ multiple choice questionaire

Could you check if the answers I chose are correct? I want to double check that I am understanding these concepts correctly.
Thanks in advance!
-Gluttons

Last edited on
Vector iterators are something you can use to access vectors.
Also, I think they want answer a for question 3. (The question isn't very well-phrased.)
1. C
2. Dont know vectors sorry
3. I would say false
4. No vector knowledge.
Thanks for the help, can any1 else chime in for Q3 & 4? im still not 100% positive which answer it should be
3. When passing multi-dimensional arrays ...
I chose b) cause the [] are empty when i used them in functions

Example, please.
I would answer as follows:

1. C
2. C - you can access a vector element with square brackets as well as an iterator of the required type where you iterate through each element sequentially, you can use the at() function with the index to the required element which is effectively the same as with angled brackets.
3. True
4. C - debatable, although you specify the type for the vector to hold, you could specify it as void* and then store pointers to different types, so that is why I say both are true
i've never heard of vector iterators, so i chose a)

Because, of course, you've heard of everything that exists in the world. So if you've never heard of it, it can't possibly exist.

Seriously, it would have taken you less effort to type "C++ vector iterators" into Google than it did to post the question here.

I chose b) cause the [] are empty when i used them in functions

Really? When you pass multidimensional arrays into functions, all the brackets are empty? And it compiles?

I chose a) i dont think vectors can carry different elements

Yeah, that's correct. A vector holds a single data type.

although you specify the type for the vector to hold, you could specify it as void* and then store pointers to different types, so that is why I say both are true

No, not if you're being precise. If you specify the type as void*, then type of data you are storing in it are void* - nothing more. It may be possible to reinterpret (i.e. cast) the values stored in those pointers as pointers to data of other types - but the type stored in the vector is a void*.

The same is true if you're thinking of polymorphism. If you store base class pointers in a vector, then it's possible to reinterpret those values as pointers to derived class objects, but the type of data actually in the vector is a base class pointer.


Last edited on
1. -
Why do people believe C is invalid?
 - The ++ operator should a to be incremented after it has been read by the array to create a local stack array. Right?

For me, all of them are valid and I tested them and the compiler did not complain. What did I do wrong? I declared a variable a btw, the syntax shouldn't be wrong, but one should perhaps declare a, is that why the answer should be C?


2. C
I suppose all of the above. Not to mention .at(unsigned) member function.


3. B
GNUGCC wrote:
multidimensional array must have bounds for all dimensions except the first


4. A
I've used vectors quite a bit... I believe.
1. -
Why do people believe C is invalid?
- The ++ operator should a to be incremented after it has been read by the array to create a local stack array. Right?

For me, all of them are valid and I tested them and the compiler did not complain. What did I do wrong?

In order to use it as the size of an array in a declaration, a must be a constant. (In C and traditional C++ anyway. Has this changed in C++11?)

If it's a constant, then it cannot be incremented using a++. So we can infer that the a in the question is not a constant.
Last edited on
Well this compiles fine, g++ -std=c++0x.

1
2
3
4
5
6
7
8
9
10
11
12
13
int f(int n)
{
    return 3 * n;
}

int main()
{
    int a = 3;
    int b[++a];
    int c[a++];
    int d[f(a)];
    return 0;
}
On the variable-length array and gcc - you might want to look here:
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
Those are const in the sense that compiler can evaluate them all, just like template parameters. Try using argc there.

VLA, variable length arrays are supposedly only in C99, and hopefully never in C++.
The GNU compiler allows them by default in C++. You need -pedantic to switch them off.

Well this compiles fine, g++ -std=c++0x.
Just because some compiler allows something, doesn't make it right.
So, to summarize: According to the C++ standard, answer C to question 1 is the syntactically invalid one.

I'm sure if the tutor had been asking about gcc-specific extensions, s/he would have mentioned it.
variable length arrays are supposedly only in C99, and hopefully never in C++.

They were approved for C++14 (from this proposal: http://isocpp.org/files/papers/N3639.html )
Topic archived. No new replies allowed.