Sample C++ Interview Questions

C++ Interview Questions
1.Why does main function in C,C++ have parameters? What are they used for?
int main(argc, char* argv[]).
A.argc and argv are the arguments that are passed from the command line when running a program. argc is the number, argv is an array of values.
2. What is the difference between a pointer and a reference?
A.A pointer is a variable that stores the address of another variable. It must be dereferenced before it can access the value held by the other variable. A reference is an alias for another variable. It can be used the same way that the variable can (unless the reference was declared to be const).
3. What is a stream? What kinds of streams are present in standard C++?
A.A stream is an object which you can use for input or output. It accepts other objects via the insertion << or output operators >> .
A stream can be attached to a file.
An input stream can be attached to the keyboard (such as standard input, by default).
An output stream can be attached to the program console (as standard output and standard error are, by default).
A stream can also be attached to a string.
4.What is the need for istrstream,ostrstream in C++?
A. ostrstream directly writes to the strings in memory, without sending to standard output/error.
istrstream directly reads the characters from an array in memory

5. #include
int main() {
QTextStream cout(stdout);
int i = 5;
int j=6;
int* pointer = &i;
int& reference=i;
int& reference_to_pointer=(*pointer);
i = 10;
pointer = &j;
reference_to_pointer = 7;
reference_to_pointer = 8;
cout << "i=" << i << " j=" << j << endl;
A. I = 8, j = 6
6.What is ‘Late-Binding’,in which type of functions this occurs?
A.Virtual functions are resolved dynamically depending on the type of object during the runtime .this is called dynamic dispatch or late-binding.which occurs only in virtual functions.
Dynamic_cast operator uses the runtime information about the type of object. this is exclusively used for RunTimeTypeIdentification.
7.What is a smart pointer to the class?
A smart pointer is an object that stores a pointer to object created on heap.
int* iptr = new int;
int* jptr = new int(10);
The standard library has std::auto_ptr as smart pointer
The boost library has s hared_ptr as smart pointer.
It behaves much like an ordinary pointer except that it automatically deletes the heap object at the right time.

No comments:

Top Blogs