Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Top 10 C++ Interview Questions with Answers for 1 to 2 years Experienced

Today, for a change I am not sharing any Java interview questions, but I am sharing a few C++ interview questions. There is a good demand for developers who know both Java and C++, particularly in the investment banking domain, as a lot of high-performance applications 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 that you can use for preparing the Java part and today's C++ questions are to cover the C++ part. Though this is not an exhaustive list, it will give you a basic idea of what to expect in terms of C++ questions on Investment banks.

Btw, C++ is considered a tough language and when you get interviewed by someone who is really a C++ expert then it will be really tough to find your way when asked questions about pointers, memory management, and concurrency.

That's why it's very important that you prepare well in advance, even if you are working in C++, it makes sense to prepare because Interviews are completely different things, you will be asked questions from topics which may not be important for you but for them. 

I think you should refresh your C++ concepts by reading a good book or joining a course like I have shared in these best CPP courses for beginners, those will actually help you to fill gaps in your learning as well.

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 that may not be easy to answer and I have seen many intermediate and experienced C++ programmers 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.  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 functions in C++.

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

3. What is the difference between a class and a struct in C++? 
Another C++ interview question for beginners or junior developers. The only difference between a class and a struct is 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 the 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 the 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 the C++ Programming language? 
This is another interesting C++ interview question that is often asked beginners on Investment banks. A virtual function with no function body and assigned with a value zero is called 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 an inline function. The inline functions are faster in execution when compared to normal functions as the compiler treats inline functions as macros. See these C++ Courses 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 a postfix operator and ++i is a prefix operator.

If you use a prefix operator then it will first increment the value and then assign it while in the case of the 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 Plus Plus? 
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 :-) 

10. What is the difference between the actual and formal parameters in C++? 
This is another fundamental C++ interview question that 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
75 Coding Interview Questions to Crack any Programming Job Interview
20 System Design Interview Questions for Programmers
10 Courses to Learn C++ for Beginners
18 Java Design Pattern Questions
My favorite courses to learn C++ from scratch
50 Programming Phone Interview Questions with Answers
17 SQL and UNIX Interview Questions for Programmers

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

P. S. - If you are new to C++ and just preparing interview questions to get your first job then I also suggest you join a C++ online course to learn first. If you need free resources, you can also check out 5 Free Courses to Learn C++ for Beginners which is a great starting point for any beginner.

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.