Top 10 C++ Interview Questions and Answers from Investment Banks

Today, for a change I am not sharing any Java interview question, but I am sharing a few C++ interview questions. There is a good demand for developers who know both Java and C++, particularly in investment banking domain, as a lot of high-performance application like client connectivity, exchange connectivity, and order management systems (OMS) are written in C++. If you know both Java and C++ then you have a great chance to get a job in big investment banks like Barclays, Nomura, Deutsche Bank, and other Wall Street Firms.

 In the past, I have shared a lot of Java questions which you can use for preparing Java part and today's C++ questions are to cover the C++ part. Though this is not an exhaustive list, it will give you some basic idea of what to expect in terms of C++ questions on Investment banks.



10 Frequently asked C++ Interview Questions with Answers

Here are my 10 most frequently asked C++ interview questions for both beginners and experienced programmers. You would find many of these questions on telephonic interviews or on the first few rounds, which are very important if you want to progress further.

10 Basic C++ Interview Questions and Answers from Investment Banks


1) What would be the value of i and j after the following code is executed? 
int i = 5;
int j = i++;

After the above code executes, i will equal 6, but j will equal 5.

Understanding the reason for this is fundamental to understanding how the unary increment (++) and decrement (--) operators work in C++.

When these operators precede a variable, the value of the variable is modified first and then the modified value is used. For example, if we modified the above code snippet to instead say int j = ++i; then i would be incremented to 6 and then j would be set to that modified value, so both would end up being equal to 6.

However, when these operators follow a variable, the unmodified value of the variable is used and then it is incremented or decremented. That’s why, in the statement int j = i++; in the above code snippet, j is first set to the unmodified value of i (i.e., 5) and then i is incremented to 6.


2) Can you have a recursive inline function in C++?
This is a tricky C++ question which may not be easy to answer and I have seen many intermediate and experienced C++ programmer with 2 to 3 years of experience got this question wrong. Although you can call an inline function from within itself, the compiler will not generate inline code since the compiler cannot determine the depth of recursion at compile time. See Learn Advanced C++ Programming to learn more.

Can we have a recursive inline function in C++?

And, if you like free resources, then this list of 5 Free C++ Courses on HackerNoon is also a good resource to learn recursive inline fucntion in C++.



3) What is the difference between a class and struct in C++?
Another C++ interview questions for beginners or junior developers. The only difference between a class and struct are the access modifiers. Struct members are public by default; class members are private. It is good practice to use classes when you need an object that has methods and structs when you have a simple data object. In other words, the class is used for OOP programming and struct is used in procedural programming.


4)  What does the auto keyword do in C++?
Similar to var of Java 10, auto keyword in C++ provides dynamic typing which means you don't need to declare the type of variable while declaring and assigning value to it. Just make it auto and compiler will figure out its type depending upon which value you assign to it.


5)  What would the following  C++ program will print?
#include <iostream.h>

int main(int argc, const char * argv[]) {
int a[] = {1, 2, 3, 4, 5, 6};
std::cout << (1 + 3)[a] - a[0] + (a + 1)[2];
}

The above will output 8, since:

(1+3)[a] is the same as a[1+3] == 5

a[0] == 1

(a + 1)[2] is the same as a[3] == 4

This question is testing pointer arithmetic knowledge and the magic behind square brackets with pointers.

While some might argue that this isn’t a valuable question as it appears to only test the capability of reading C constructs, it’s still important for a candidate to be able to work through it mentally; it’s not an answer they’re expected to know off the top of their head, but one where they talk about what conclusion they reach and how.


6) What is a pure virtual function in C++ Programming language?
This is another interesting C++ interview question which is often asked beginners on Investment banks. A virtual function with no function body and assigned with a value zero is called as a pure virtual function.


7) What is an inline function in C++?
Another beginner-level C++ function for freshers or C++ developers with 1 to 2 years of experience. A function prefixed with the keyword inline before the function definition is called as an inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros. See C++: From Beginner to Expert to learn more about inline function in C++.

What is the difference between i++ and ++i in CPP?



8) What is the difference between i++ and ++i in CPP?
This is one of the tricky C++ questions and if you haven't worked in C++ then you won't be able to answer it, but it's very much similar to what i++ and ++i do in Java. If you don't know the, i++ is known as postfix operator and ++i is prefix operator.

If you use prefix operator then it will first increment the value and then assign while in case of postfix operator, an assignment will happen first and then the increment.

For example, what would be the value of i, j, and k after running this code?

int i = 2;
int j = i++;
int k = ++i;



9) What is a friend class in C++?
I remember this question was actually asked to me during my first interview. I didn't know the answer but I guessed it by seeing the word friend on it :-) A class member can gain accessibility over another class member by placing the class declaration prefixed with the keyword ‘friend’ in the destination class. This is like a friend of you can access some of your stuff :-) If you are new to C++, I suggest you to go through Learn How to Program with C++ course on Pluralsight to learn from scratch.



10) What is the difference between the actual and formal parameters in C++?
This is another fundamental C++ interview question which is often asked during telephonic interviews or face-to-face interviews with beginner C++ programmers. The answer to this question is simpler, the parameters sent to the function by calling end are called actual parameters while at the receiving of the function definition called formal parameters.


11) What is the difference between volatile and mutable keywords in C++?
This is a bonus question as I had only planned for 10 questions but I found this interesting enough to share with you guys.  The volatile keyword informs the compiler that a variable will be used by multiple threads.

Variables that are declared as volatile will not be cached by the compiler to ensure the most up-to-date value is held.  The mutable keyword can be used for class member variables. Mutable variables are allowed to change from within const member functions of the class


That's all about C++ interview Questions and answers for both beginners and experienced programmers. If you have any other interesting question which was asked to you in any investment banks like Citi, Goldman Sachs, Barclays, Nomura, Deutsche Bank,  Credit Suisse or any other financial service institution then please share with us.

Other Programming Interview Questions and Resources to do well
C++: From Beginner to Expert
Learn Advanced C++ Programming 
75 Coding Interview Questions to Crack any Programming Job Interview
20 System Design Interview Questions for Programmers
18 Java Design Pattern Questions
50 Programming Phone Interview Questions with Answers
17 SQL and UNIX Interview Questions for Programmers
5 Free Courses to Learn C++ for Beginners

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any doubt or feedback, please drop a note.

No comments:

Post a Comment