Tricky C++ questions

Started by
24 comments, last by ZQJ 17 years, 1 month ago
Quote:Original post by King Mir
I guess that's tricky because you have to know that it's undefined.


correct ;) VC will give you 12. borland & gcc 10.
Advertisement
As an extra data point, ICC gives 9. With that kind of question, you can't just work out an answer - the trick is to make sure you step back and think about it at a higher level, to determine that the code in the question is invalid, rather than immediately pushing yourself into the details and trying to work out what the execution would be.

(But you have to hope the interviewer is sufficiently competent and won't listen to your explanation of undefined behaviour and sequence points and then just say "no, the answer is 10, I tried it last night" [smile])
Quote:Original post by mrbastard
Quote:Original post by blaze02
9


I was going to laugh, but I got it wrong as well... I said 6. Will have to read the spec on preincrement and brackets I think.


It prints whatever the compiler wants to print. This statement has undefined behavior (google for sequence points for more information).

Edit: Or read Nathan's post :)
the part that I found tricky was that I forgot that preincrement returns a reference rather than a value. If it returned a value then it would be undefined behavior and yet any reasonable compiler would return a 9 (2+3+4 or 2+4+3 or some other permutation)
1. How do you add with out using addition, subtraction, modulus, multiply, divide operators.

2. How do you multiply without multiply operators

3. Whats so bad about iterating through an array of abstract objects?

4. Whats slower casting from float -> int or int -> float or are they the same?

5. Whats negative zero?

6. Whats malloc? Whats new operator? Is there a difference? Why?


lolz enjoy.


Ugh. May C++ rest in peace one of these days.
Quote:Original post by Riekistyx
1. How do you add with out using addition, subtraction, modulus, multiply, divide operators.

2. How do you multiply without multiply operators

3. Whats so bad about iterating through an array of abstract objects?

4. Whats slower casting from float -> int or int -> float or are they the same?

5. Whats negative zero?

6. Whats malloc? Whats new operator? Is there a difference? Why?


lolz enjoy.


Hmm..

1. You could use and's and or's just like the computer hardware does to do addition and subtraction.

2. bit shift

3... there's something bad about that?

4. i'm gona guess float->int

that problem with undefined behavior was very interesting... i originally guessed 12 but dev-c++ gave 10.
1. How do you add with out using addition, subtraction, modulus, multiply, divide operators.

// Originalint result = a + b; // Silly answer 1int result = a; result += b;// Silly answer 2int result = a, x = 0;while (x < b) { ++x; ++result; }while (x > b) { --x; --result; }// Useful answerstd::sum<int> op;int result = op(a,b);


2. How do you multiply without multiply operators

Same as above, with std::multiplies.

3. Whats so bad about iterating through an array of abstract objects?

Trick question, as "abstract object" is a vague expression. If you meant "abstract class instances", then the question is silly as there is no such thing as an abstract class instance.

4. Whats slower casting from float -> int or int -> float or are they the same?

Trick question. This behaviour is platform-dependent, and may even depend on the current pipeline status on a given platform.

5. Whats negative zero?

Trick question. C++ mentions no such thing as a "negative zero". However, IEEE 754 (one of the floating-point specifications) does mention negative zero (refer to that specification about the meaning of that expression). Of course, C++ has nothing to do with IEEE 754, as the specification of floating-point numbers in C++ is implementation-dependent. So, negative zero might well not exist in C++ on your platform!

6. Whats malloc? Whats new operator? Is there a difference? Why?

std::malloc is a memory allocation function from the C standard library (#include <cstdlib>), which operates in approximately the same way as the default void* ::operator new(std::size_t) overload of operator new. The differences are the return value (by default, new never returns a null pointer, and throws std::bad_alloc when out of memory), and the fact that std::malloc is used naked while ::operator new() almost never is. As with all C legacy allocators, the corresponding release function is std::free.

Operator new is a combination of two effects: operator new is a memory allocation function which may be overloaded by the programmer (although it must respect alignment and size constraints). A new-expression, when evaluated, calls the corresponding version of operator new (which may be overloaded for individual class hierarchies, and may receive additional arguments) to allocate memory, and then intializes an object at that location using the object's constructor. To each operator new corresponds an operator delete, which is used to release memory. In particular, when an exception occurs in a new-expression (for instance, thrown in the constructor), the corresponding operator delete is called on the allocated memory.

The main difference is that std::malloc does not initialize objects (although it is possible to use placement new post-allocation) and is not typesafe (as would obviously be expected from a raw memory allocator).
Thanks for all the ideas!
I just finished a few hours ago and they didn't really ask anything hard. the "hardest" thing that they asked was "what's the difference between overloading and overwriting":)
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the internet, we know this is not true." -- Professor Robert Silensky
Quote:Original post by Riekistyx
1. How do you add with out using addition, subtraction, modulus, multiply, divide operators.

I don't.
Quote:
2. How do you multiply without multiply operators

I don't.
Quote:
3. Whats so bad about iterating through an array of abstract objects?

There's no such thing.
Quote:
4. Whats slower casting from float -> int or int -> float or are they the same?

Mu.
Quote:
5. Whats negative zero?

A convenient fiction.
Quote:
6. Whats malloc? Whats new operator? Is there a difference? Why?

A function. An operator. Yes. Because.

This topic is closed to new replies.

Advertisement