# C++ / General Questions

This topic is 4158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, i recently recieved a sample set of questions for a job related test. There were a few questions i am unsure of. Any help would be appreciated... Q1. Name something that happens before a c/c++ program enters main()? Q2. Whats the quickest, simplest way to determine in an unsigned integer is a power of two? Q3. Why wouldn't you want to put code for a non-trivial function in a header file? Thanks in advance

##### Share on other sites
Q1, Q2, Q3: (see Code Bloat, Compilation Process)

##### Share on other sites
1. Function variables class are declared and headers are included
2. if( x & (x-1) == 0 )
3. This I'm not completely sure about.

##### Share on other sites
2)
bool isPowerOfTwo(unsigned int x){    return !(x & (x-1));}

*edit* beaten to it :p

##### Share on other sites
Quote:
 Original post by chubbc1. Function variables class are declared and headers are included

By that standard, we could also say that the computer is turned on [wink]

Q1 is interesting to determine if the interviewee believes that global and static variables are initialized before main (they are only guaranteed to be initialized before their first use, so side-effects of their initialization should not be relied on). I would definitely answer that the command-line arguments are copied over to argv[][].

Q2 is, in my opinion, silly. It certainly is a "you know it or don't know it trick" of the "google if you don't know it" variety. If the person happens to know the trick, then you can't be sure whether they understand it, and when they don't know it, you can't be sure whether they'd understand it if they knew it. A better version of this question would be:

Does (x & (x-1)) == 0 determine whether x is a power of two? Why or why not?

Q3 is a very good question related to insight about the C++ compiling process. My answers would be reducing the compile time (recompile needed whenever the function is changed), reducing apparent coupling to allow mutual recursion of modules (due to the absence of a module system in C++) and reducing code bloat on compilers silly enough to inline every function definition they encounter.

##### Share on other sites
Q1. Static class constructors are called. You can't stick your life on it, but it's a fairly safe bet that this is what they're angling for ;-)

Q3. The reason why is because every time you change the function then any other code which references the function will also need to be recompiled. If you're using precompiled headers the whole project needs to be recompiled.

##### Share on other sites
Q1: As far as I can think only the main parameters are initalisised (argv[][]), global variables can be constructed on first-use much like anything else.

Q2:
Quote:
 Original post by ToohrVykDoes (x & (x-1)) == 0 determine whether x is a power of two? Why or why not?

It'll only work for integer values of x where x > 0.

##### Share on other sites
Quote:
 Original post by ToohrVykreducing code bloat on compilers silly enough to inline every function definition they encounter.

But if the function is in a header file, it has to be inline to avoid linker errors, or did I misunderstood you?

Quote:
 reducing apparent coupling to allow mutual recursion of modules (due to the absence of a module system in C++)

Could you please explain this a bit more?

##### Share on other sites
Quote:
 Original post by TheGilbQ1. Static class constructors are called. You can't stick your life on it, but it's a fairly safe bet that this is what they're angling for ;-)

If I was the one asking the question, I'd be trying to determine if you would actually make that mistake [wink]

Quote:
 Original post by Gage64But if the function is in a header file, it has to be inline to avoid linker errors, or did I misunderstood you?

It can also be static (although this would make matters worse). Also, the inline keyword is only optionally respected by compilers anyway. Still, my remark pertained mostly to member functions.

Quote:
 Original post by Gage64Could you please explain this a bit more?

Certainly. When two headers (which define types) recurse mutually, this clashes with the C and C++ requirement that types be defined before they are instantiated or inherited from, or their members are accessed. This means that, whenever you need to use a type (defined in a header) in this way, you need to include the corresponding header. Which means that if two headers each use the other's type in this way, circular inclusion will occur. The solution is to move one of the implementations to a non-header file.

##### Share on other sites
Thank you for the explanation. I'm actually familiar with this, I thought you meant something else because you mentioned that C++ lacks a module system. How would such a system solve this?

Quote:
 ORiginal post by ToohrvYkIt can also be static (although this would make matters worse). Also, the inline keyword is only optionally respected by compilers anyway. Still, my remark pertained mostly to member functions.

But again, if the member functions are not inlined won't this cause linker errors? In such a situation, isn't the compiler forced to respect the inline keyword? (Sorry, I'm a bit rusty on these things)

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013245
×