Pointers and References – C++ programming interview questions

(Last Updated On: February 26, 2019)

C ++ programming interview questions on pointers, references and memory with answers and explanations. This list contains conceptual and tricky coding / output questions on pointers in c ++.  These questions are real programming / quiz asked in software industries to freshers  and experienced professionals in technical interviews that we should practice.

Topics – Pointer, Reference, new and delete operator, dynamic memory allocation C++

 


Q – Find Crash in below C++ program with reason?

Answer: Program will crash when “delete this” in the function bookInfo() is called using the statically allocated object i.e. book.bookInfo();. Note that “Book book” object has been created statically.

this refers to current class object. Means, if you delete this current class object will be destroyed.

If we call a function using pointer to an object(b->bookInfo();) which is allocated dynamically(Book *b = new Book();), then the object will get destroyed using delete this statement. But,if we call function using statically allocated object then run time error will occur.

Note that delete operator is always applied on dynamically allocated object in C++.