C++ Interview Questions & Answers part 4

Q: – What does #include do

This is a directive to the preprocessor, which runs when you call your compiler. This specific directive causes the file named after the word include to be read, as if it were typed in at that location in your source code.

Q: – When should the keyword inline is used in a function prototype?

If the function is very small, no more than a line or two, and won’t be called from many places in your program, it is a candidate for inlining.

Q: – Why aren’t changes to the value of function arguments reflected in the calling function?

A Arguments passed to a function are passed by value. That means that the argument in the function is actually a copy of the original value.

Q: – What is the difference between int main() and void main(); which one should I use? I have used both and they both worked fine, so why do I need to use int main(){ return 0;}?

Both will work on GNU and most other compilers, but only int main() is ANSI compliant, and thus only int main() is guaranteed to continue working in the future. The difference is that int main() returns a value to the operating system. When your program completes, that value can be captured, for example, by batch programs.

Q: – What is Recursion?

A function can call itself. This is called recursion, and recursion can be direct or indirect. Recursion is direct when a function calls itself; it is indirect when a function calls another function that then calls the first function.

Q: – If a short int can run out of room and wrap around, why not always uses long integers?

Both short integers and long integers will run out of room and wrap around, but a long integer will do so with a much larger number. For example, an unsigned short int will wrap around after 65,535, where as an unsigned long int will not wrap around until 4,294,967,295. However, on most machines, a long integer takes up twice as much memory every time you declare one (4 bytes versus 2 bytes), and a program with 100 such variables will consume an extra 200 bytes of RAM. Frankly, this is less of a problem than it used to be because most personal computers now come with many thousands (if not millions) of bytes of memory.

Q: – What is the difference between declaring and defining?

A declaration introduces a name of something but does not allocate memory. A definition allocates memory.

Q: – Why not make all variables global?

At one time, this was exactly how programming was done. As programs became more complex, however, it became very difficult to find bugs in programs because data could be corrupted by any of the functions—global data can be changed anywhere in the program. Years of experience have convinced programmers that data should be kept as local as possible, and access to changing that data should be narrowly defined.

Q: – Why not use literal constants; why go to the bother of using symbolic constants?

If you use the value in many places throughout your program, a symbolic constant allows all the values to change just by changing the one definition of the constant. Symbolic constants also speak for themselves. It might be hard to understand why a number is being multiplied by 360, but it’s much easier to understand what’s going on if the number is being multiplied by degreesInACircle

Submitted By:-Ankur Goyal            Email-ID: – goyal.ankur30@yahoo.in