C++ multiple choice questionaire

May 14, 2013 at 5:11am
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 May 15, 2013 at 1:25am
May 14, 2013 at 5:13am
Vector iterators are something you can use to access vectors.
May 14, 2013 at 5:27am
Also, I think they want answer a for question 3. (The question isn't very well-phrased.)
May 14, 2013 at 5:38am
1. C
2. Dont know vectors sorry
3. I would say false
4. No vector knowledge.
May 14, 2013 at 6:56am
Thanks for the help, can any1 else chime in for Q3 & 4? im still not 100% positive which answer it should be
May 14, 2013 at 11:59am
3. When passing multi-dimensional arrays ...
I chose b) cause the [] are empty when i used them in functions

Example, please.
May 14, 2013 at 12:24pm
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
May 14, 2013 at 1:26pm
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 May 14, 2013 at 1:27pm
May 14, 2013 at 2:01pm
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.
May 14, 2013 at 2:05pm
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 May 14, 2013 at 2:06pm
May 14, 2013 at 3:08pm
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;
}
May 14, 2013 at 3:29pm
On the variable-length array and gcc - you might want to look here:
http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
May 14, 2013 at 3:32pm
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++.
May 14, 2013 at 3:49pm
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.
May 14, 2013 at 4:56pm
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.
May 14, 2013 at 5:07pm
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.