
Autor: 28.09.2023
C++ - Sample Interview Questions
In this article, we decided to take a closer look at what you can expect in job interviews related to the C++ language. We have prepared a compilation of 10 questions that regularly appear in interviews for C++ programmer positions.
If you want to fully prepare yourself, we invite you to explore our course "C++ Developer - 250 Interview Questions," where you will find an extensive database of questions and answers that may come up in job interviews.
10 Example Questions
String Class
What will the following code return?
#include <iostream>
#include <string>
int main(void) {
std::string word = "Hello";
int length = word.length();
std::cout << length;
return 0;
}
A) Hello
B) 5
C) 6
D) 7
Correct answer: B. We use the length() method to return the length of the character string.
Access Modifiers
What are the access modifiers in the C++ language?
A) public, private, protected
B) public, private, internal
C) public, protected, sealed
D) private, internal, sealed
Correct answer: A. This answer includes a set of three access modifiers present in the C++ language.
Member Functions of a Class
How do you define a class member function as static?
class MyClass {
public:
};
A) void MyClass::myFunction() static
B) static void MyClass::myFunction
C) static myFunction() in MyClass
D) void myFunction() static in MyClass
Correct answer: B. This is how you define a static function.
Constructor and Destructor
What will be the output of the program?
#include <iostream>
class Test {
public:
Test() {
std::cout << "Create";
}
~Test() {
std::cout << "Delete";
}
};
int main() {
Test * ptr = new Test();
delete ptr;
return 0;
}
A) DeleteCreate
B) CreateDelete
C) No output
D) Compilation error
Correct answer: B. We create an object, and the constructor is called. Then the object is deleted, and the destructor is called.
Structures
How do you initialize a Circle structure with a radius of 5?
struct Circle {
float radius;
};
A) Circle(5);
B) Circle c = Circle(5);
C) Circle c; c.radius = 5;
D) Circle c; c.radius(5);
Correct answer: C. This is the correct way to initialize a structure.
Pointers
What will be displayed on the screen?
#include <iostream>
int main() {
int* ptr = nullptr;
std::cout << ptr;
return 0;
}
A) The value of the ptr variable
B) Compilation error
C) The address of the ptr variable
D) Nothing, the program will not run
Correct answer: C. Our pointer points to the address of the variable.
Classes - Question 1
Which of the following statements about inheritance in the C++ language is true?
A) A derived class can inherit only one base class
B) A derived class can inherit multiple base classes
C) Inheritance in C++ is single inheritance only
D) A derived class cannot use the methods of the base class
Correct answer: B. In the C++ language, one derived class can inherit from multiple base classes.
Classes - Question 2
What is a characteristic feature of static methods?
A) Static methods are methods that do not require the creation of an instance of the class to be called
B) Static methods are always called first in a program
C) Static methods cannot be inherited by derived classes
D) Static methods cannot use local variables
Correct answer: A. Static methods can be called on the class itself without the need for an instance of the class.
Library cstdlib - Question 1
Which function from the cstdlib library converts a character string to an integer?
A) to_int()
B) str_to_int()
C) atof()
D) atoi()
Correct answer: D. The atoi() function can be used to convert a character string to an integer.
Library cstdlib - Question 2
What is the effect of calling the exit(0) function from the cstdlib library?
A) The program will terminate and return a code of 0
B) The program will terminate and return a code of 1
C) The program will throw an exception
D) The program will terminate without returning a code
Correct answer: A. The effect mentioned in the question can be achieved using the exit(0) function.
Summary
Every job interview is an opportunity to showcase your knowledge and skills. If you are keen on achieving success, you will undoubtedly appreciate the value of solid learning and practice.