- This topic has 0 replies, 1 voice, and was last updated 5 months, 3 weeks ago by
Pavatharni . S.
-
AuthorPosts
-
June 10, 2023 at 5:10 am #25069
Pavatharni . S
Participant®
24 Essential C++ Interview Questions *
Toptal sourced essential questions that the best C++ developers and engineers can answer. Driven from our community, we encourage experts to submit questions and offer feedback.
Toptal logois an exclusive network of the top freelance software developers, designers, finance experts, product managers, and project managers in the world. Top companies hire Toptal freelancers for their most important projects.
INTERVIEW QUESTIONS
1.
Is there a difference between class and struct?View answer
2.
What will the line of code below print out and why?#include <iostream>
int main(int argc, char **argv)
{
std::cout << 25u – 50;
return 0;
}
View answer3.
What is the error in the code below and how should it be corrected?my_struct_t *bar;
/* … do stuff, including setting bar to point to a defined my_struct_t object … */
memset(bar, 0, sizeof(bar));
View answerApply to Join Toptal’s Development Network
and enjoy reliable, steady, remote Freelance C++ Developer Jobs
4.
What will i and j equal after the code below is executed? Explain your answer.int i = 5;
int j = i++;
View answer5.
Assuming buf is a valid pointer, what is the problem in the code below? What would be an alternate way of implementing this that would avoid the problem?size_t sz = buf->size();
while ( –sz >= 0 )
{
/* do something */
}
View answer6.
Consider the two code snippets below for printing a vector. Is there any advantage of one vs. the other? Explain.Option 1:
vector vec;
/* … .. … */
for (auto itr = vec.begin(); itr != vec.end(); itr++) {
itr->print();
}
Option 2:vector vec;
/* … .. … */
for (auto itr = vec.begin(); itr != vec.end(); ++itr) {
itr->print();
}
View answer7.
Implement a template function IsDerivedFrom() that takes class C and class P as template parameters. It should return true when class C is derived from class P and false otherwise.View answer
8.
Implement a template boolean IsSameClass() that takes class A and B as template parameters. It should compare class A and B and return false when they are different classes and true if they are the same class.View answer
9.
Is it possible to have a recursive inline function?Hide answer
Although you can call an inline function from within itself, the compiler may not generate inline code since the compiler cannot determine the depth of recursion at compile time. A compiler with a good optimizer can inline recursive calls till some depth fixed at compile-time (say three or five recursive calls), and insert non-recursive calls at compile time for cases when the actual depth gets exceeded at run time.10.
What is the output of the following code:#include <iostream>
class A {
public:
A() {}
~A() {
throw 42;
}
};int main(int argc, const char * argv[]) {
try {
A a;
throw 32;
} catch(int a) {
std::cout << a;
}
}
View answer11.
You are given library class Something as follows:class Something {
public:
Something() {
topSecretValue = 42;
}
bool somePublicBool;
int somePublicInt;
std::string somePublicString;
private:
int topSecretValue;
};
Implement a method to get topSecretValue for any given Something* object. The method should be cross-platform compatible and not depend on sizeof (int, bool, string).View answer
12.
Implement a void function F that takes pointers to two arrays of integers (A and B) and a size N as parameters. It then populates B where B[i] is the product of all A[j] where j != i.For example: If A = {2, 1, 5, 9}, then B would be {45, 90, 18, 10}.
Hide answer
This problem seems easy at first glance so a careless developer might write something like this:void F(int* A, int* B, int N) {
int m = 1;
for (int i = 0; i < N; ++i) {
m *= A[i];
}for (int i = 0; i < N; ++i) {
B[i] = m / A[i];
}
}
This will work for the given example, but when you add a 0 into input array A, the program will crash because of division by zero. The correct answer should take that edge case into account and look like this:void F(int* A, int* B, int N) {
// Set prod to the neutral multiplication element
int prod = 1;for (int i = 0; i < N; ++i) {
// For element “i” set B[i] to A[0] * … * A[i – 1]
B[i] = prod;
// Multiply with A[i] to set prod to A[0] * … * A[i]
prod *= A[i];
}// Reset prod and use it for the right elements
prod = 1;for (int i = N – 1; i >= 0; –i) {
// For element “i” multiply B[i] with A[i + 1] * … * A[N – 1]
B[i] *= prod;
// Multiply with A[i] to set prod to A[i] * … * A[N – 1]
prod *= A[i];
}
}
The presented solution above has a Big O complexity of O(N). While there are simpler solutions available (ones that would ignore the need to take 0 into account), that simplicity has a price of complexity, generally running significantly slower.13.
When you should use virtual inheritance?Hide answer
While it’s ideal to avoid virtual inheritance altogether (you should know how your class is going to be used) having a solid understanding of how virtual inheritance works is still important:So when you have a class (class A) which inherits from 2 parents (B and C), both of which share a parent (class D), as demonstrated below:
#include <iostream>
class D {
public:
void foo() {
std::cout << “Foooooo” << std::endl;
}
};class C: public D {
};class B: public D {
};class A: public B, public C {
};int main(int argc, const char * argv[]) {
A a;
a.foo();
}
If you don’t use virtual inheritance in this case, you will get two copies of D in class A: one from B and one from C. To fix this you need to change the declarations of classes C and B to be virtual, as follows:class C: virtual public D {
};class B: virtual public D {
};
14.
What is the output of the following code:#include <iostream>
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];
}
Hide answer
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.
15.
What is the output of the following code:#include <iostream>
class Base {
virtual void method() {std::cout << “from Base” << std::endl;}
public:
virtual ~Base() {method();}
void baseMethod() {method();}
};class A : public Base {
void method() {std::cout << “from A” << std::endl;}
public:
~A() {method();}
};int main(void) {
Base* base = new A;
base->baseMethod();
delete base;
return 0;
}
View answer16.
How many times will this loop execute? Explain your answer.unsigned char half_limit = 150;
for (unsigned char i = 0; i < 2 * half_limit; ++i)
{
// do something;
}
View answer17.
How can you make sure a C++ function can be called as e.g. void foo(int, int) but not as any other type like void foo(long, long)?Hide answer
Implement foo(int, int)…void foo(int a, int b) {
// whatever
}
…and delete all others through a template:template <typename T1, typename T2> void foo(T1 a, T2 b) = delete;
Or without the delete keyword:template <class T, class U>
void f(T arg1, U arg2);template <>
void f(int arg1, int arg2)
{
//…
} -
AuthorPosts
You must be logged in to reply to this topic. Login here