Interview for C++ development positions, if you don’t know this, what else? (C/c++ interview questions and answer analysis)

C++ engineer school recruitment interview question bank guide

1. Learning instructions

The interview question bank does not include the items asked in the interview, hr face and personal technical development.
⚫ The project is more personalized, there is no way to use it as a question bank for your reference, but if you have a very gold-rich project, it is a very bonus, and your project may be asked more;
⚫ hr.
Generally speaking , if you pass the technical side, you can pass without too many problems that are not compatible with the company;
⚫ For technical development, this is completely up to you. The main test will be your love of technology and learning ability , For example, you will ask how you learned xxx technology, or where you can express your love for technology, etc. Do not repeat them here.

So apart from these, the proportion of the technical aspects of C++ engineers is as follows: It
Insert picture description here
should be noted that this picture is not absolute, because the interviewer will ask according to your resume in the actual interview. For example, you may ask for more projects. There are more projects, or the interviewer will ask you more if you are proficient. And this picture is sorted out based on the question bank data, not based on the actual single interview. For example, the basic part will not be tested so many, and will be taken from it.

2. Interview skills

Interviews are generally divided into technical aspects and hr aspects. In terms of format, there are few group aspects, and a small number of companies may have an intersection. However, in general, the technical aspect is basically to examine your professional and technical level, and the hr aspect is mainly It depends on the person’s comprehensive quality and family situation that does not meet the company’s requirements. Generally speaking, as long as the technology passes the technical aspect, there is basically no problem (there are also a few companies who will brush a lot of people), then we mainly talk about technology In terms of technical aspects, it is mainly to examine professional technical knowledge and level. We can have certain skills, but it must be based on a certain level of ability. Therefore, I also carefully tell everyone that skills are not opportunistic, but have auxiliary effects. The most important technical aspect is strength. This is based on the skills above the level of strength.

Here are a few tips in interviews:

1. Make a guide on your resume:
make a distinction in terms of vocabulary. For example, if you are familiar with Java, understand python, and be proficient in c language
, you have a distinction between your own mastery, so that the interviewer can focus on asking, python was originally written It's just to understand, so naturally I won't ask you more in-depth things.

2.
Give a guide during the interview: try to guide you to a field that you know well during the interview. For example, if you ask about DNS addressing, and then you simply answer (even this step can be omitted), you can say something, I may not be very familiar with this area, but I am familiar with the transportation layer in the computer network. If there is a specific one, you can even add another sentence, such as TCP and UDP. In this way, you can guide the entire interview process to a place you know well. Can be more inclined to reflect your strengths rather than weaknesses, but this method is limited to mastering the appropriate degree. For example, some knowledge points are bound to be known, but if you want to introduce them elsewhere, it is a bit unreasonable. For example, let you say a few c++ If you can’t say one of the keywords, it may be really out of place.

3. Make a guide in the self-introduction:
Generally, there will be a self-introduction at the beginning of the interview. In this position, you can also guide yourself to your own advantages.

4. Demonstrate self-confidence
during the interview : You must also master your attitude during the interview. Don’t be inferior or arrogant. Answer every question confidently, especially when you encounter problems that you don’t know, or do some guidance. Guidance can also be played first, and communicate with the interviewer about the question. It seems that you did not understand the meaning of the question. You can also think about this process by yourself. If you think this process can be avoided, you can also directly indicate that this place is not good. Do not force an answer if you are familiar with or have not mastered it.

Preparation before the interview: The
most important thing is to learn systematically, to have a knowledge framework, and the solidity of basic knowledge. Among them, the algorithm is especially important. More and more companies will let you hand-write the code in the live or video interview;

Another important and bonus item is the project. Before the interview, you must practice answering the three questions of your own project:
⚫ What kind of project is this
⚫ What technology is used, why this technology (and each technology) Very detailed points and extensions)
⚫ What problems were encountered during the process and how to solve them.

What to do after the interview:

After the interview, you don’t need to care too much about the result. You should do more with less time within a limited time. Of course, keep the phone and mailbox open, or you don’t even know if you don’t send you an offer.

Aside from these, what we need to do is to record the questions in the interview in time, especially the questions that we did not answer well enough, we must take time to research and solve these problems, and we will encounter the same questions next time. It can be solved very well. Of course, even if you don’t encounter it, you can stick to this habit, and you can tell the interviewer as an experience later. You can show your love for technology and an attitude of delving into it. At the same time, after each interview You will find your own shortcomings, a good opportunity to check for deficiencies, make timely adjustments, and you will get better and better in the process of constant adjustments and deficiencies.

Three, interview test site map

Insert picture description here

Four, interview technical exchanges, click here

If you are confused about school recruitment or career development, you can join the group to discuss and learn from each other. There are also siege lions who have worked for many years.

Interview test center

1. Basic knowledge

1. Basic language

1. Talk about the role of the static keyword.
Inspection point: C/C++

Reference answer:

1. Global static variables

Add the keyword static before the global variable, the global variable is defined as a global static variable. The static storage area always exists during the entire program running.

Initialization: Uninitialized global static variables will be automatically initialized to 0 (the value of the automatic object is arbitrary, unless it is explicitly initialized);

Scope: Global static variables are invisible outside of the file where they are declared. To be precise, they start at the point of definition and reach the end of the file.

2. Local static variables

Add the keyword static before the local variable, the local variable becomes a local static variable. Location in memory: static storage area

Initialization: Uninitialized global static variables will be automatically initialized to 0 (the value of the automatic object is arbitrary, unless it is explicitly initialized);

Scope: The scope is still a local scope. When the function or statement block that defines it ends, the scope ends. But when the local static variable leaves the scope, it is not destroyed, but still resides in the memory, but we can no longer access it until the function is called again, and the value remains unchanged;

3. Static function

Add static before the function return type, and the function is defined as a static function. The definition and declaration of functions are extern by default, but static functions are only visible in the file where they are declared, and cannot be used by other files.
The implementation of the function uses static modification, then this function can only be used in this cpp, and will not cause conflicts with functions of the same name in other cpp;

Warning: Do not declare static global functions in the header file, and do not declare non-static global functions in cpp. If you want to reuse the function in multiple cpp, put its declaration in the header file, otherwise cpp The internal declaration needs to be modified with static;

4. Static members of the class

In the class, static members can realize data sharing between multiple objects, and the use of static data members will not break the principle of hiding, which guarantees safety. Therefore, static members are members shared among all objects of the class, not members of an object. For multiple objects, static data members are only stored in
one place for all objects to share

  1. Static functions of classes
    Static member functions are the same as static data members. They are all static members of the class, and they are not object members. Therefore, references to static members do not require object names.

In the implementation of a static member function, you cannot directly refer to the non-static members described in the class, but can refer to the static members described in the class (this is very important). If non-static members are to be referenced in static member functions, they can be referenced by objects. It can be seen that calling static member functions uses the following format: <class name>::<static member function name>(<parameter list>)

2. Tell me about the difference between C++ and C.
Inspection point: C basic
reference answer:

In design philosophy:
C++ is an object-oriented language, and C is a process-oriented structured programming language

Syntactically:
C++ has three characteristics : overloading, inheritance, and polymorphism.
Compared with C, C++ adds many more type-safe functions, such as forced type conversion,

3. Talk about the role of the static keyword in C++
Test point: static keyword
reference answer:

For function definitions and variable declarations outside the code block, static modifies the link attribute of the identifier from the default external to internal, and the scope and storage type remain unchanged. These symbols can only be accessed in the source file where they are declared.

For variable declarations inside the code block, static modifies the storage type of the identifier, from automatic variables to static variables, and the scope and link properties remain unchanged. This kind of variable is created before the program is executed and exists throughout the cycle of program execution.

For ordinary functions modified by static, they can only be used in the source file that defines it, and cannot be referenced in other source files

For class member variables and member functions modified by static, they belong to the class, not an object, and all objects share a static member. Static members are used by <class name>::<static members>.

4. Please talk about the role of static.
Inspection point: C/C++
reference answer:

  1. Global static variable
    Add the keyword static before the global variable, the global variable is defined as a global static variable.

The static storage area always exists during the entire program operation.

Initialization: Uninitialized global static variables will be automatically initialized to 0 (the value of the automatic object is arbitrary, unless it is explicitly initialized).

Scope: Global static variables are invisible outside of the file where they are declared. To be precise, they start at the point of definition and reach the end of the file.

2. Local static variable
Add the keyword static before the local variable, the local variable becomes a local static variable.
Location in memory: static storage area.
Initialization: Uninitialized global static variables will be automatically initialized to 0 (the value of the automatic object is arbitrary, unless it is explicitly initialized).

Scope: The scope is still a local scope. When the function or statement block that defines it ends, the scope ends. But when the local static variable leaves the scope, it is not destroyed, but still resides in the memory, but we can no longer access it until the function is called again and the value remains unchanged.

  1. Static function
    Add static before the function return type, and the function is defined as a static function. The definition and declaration of functions are extern by default, but static functions are only visible in the file where they are declared, and cannot be used by other files.

The implementation of the function uses static modification, then this function can only be used in this cpp, and will not cause conflicts with functions of the same name in other cpp.

Warning: Do not declare static global functions in the header file, and do not declare non-static global functions in cpp. If you want to reuse the function in multiple cpp, put its declaration in the header file, otherwise cpp The internal declaration needs to be modified with static.

  1. Static members
    of a class In a class, static members can realize data sharing between multiple objects, and the use of static data members will not break the principle of hiding, which guarantees security. Therefore, static members are members shared among all objects of the class, not members of an object. For multiple objects, the static data member is only stored in one
    place for all objects to share.

  2. Static functions of classes
    Static member functions are the same as static data members. They are all static members of the class, and they are not object members. Therefore, references to static members do not require object names.

In the implementation of a static member function, you cannot directly refer to the non-static members described in the class, but can refer to the static members described in the class (this is very important). If non-static members are to be referenced in static member functions, they can be referenced by objects. It can be seen that calling a static member function uses the following format: <class name>::<static member function name>(<parameter list>);

5. Talk about the four types of cast conversions in C++.
Investigation point: C/C++
reference answer: The
four types of conversions in C++ are:
static_cast, dynamic_cast, const_cast, reinterpret_cast

1. const_cast is
used to convert const variables to non-const

2. Static_cast is
used for various implicit conversions, such as non-const to const, void* to pointer, etc., static_cast can be used for polymorphic upward conversion, if the downward conversion can be successful but not safe, the result is unknown;

3. dynamic_cast is
used for dynamic type conversion. It can only be used for classes that contain virtual functions for upward and downward transformations between class levels. Only pointers or references can be transferred. When converting downwards, if it is illegal, NULL is returned for the pointer, and an exception is thrown for the reference. To deeply understand the principles of internal conversion.

Up conversion: refers to the conversion from a subclass to a base class
Down conversion: refers to the conversion from a base class to a subclass.
It determines whether the runtime type of the variable is the same as the type to be converted when the statement is executed. Determine whether down conversion is possible.

4. Reinterpret_cast
can transfer almost anything, such as transferring int to pointer, it may cause problems, so use it as little as possible;

5. Why not use C forced conversion?
On the surface, the forced conversion of C seems to be powerful and can transfer anything, but the conversion is not clear enough, error checking cannot be performed, and errors are prone to errors.

6. Please tell me the difference between pointer and reference in C/C++?
Investigation point: C/C++ basic knowledge
reference answer:

1. The pointer has its own space, and the reference is just an alias;
2. Use sizeof to see that the size of a pointer is 4, and the reference is the size of the referenced object;
3. The pointer can be initialized to NULL, and the reference must be Initialization and must be a reference to an existing object;
4. When passing as a parameter, the pointer needs to be dereferenced to operate on the object, and direct modification of the reference will change the object pointed to by the reference;
5. There can be a const pointer , But there is no const reference;
6. The pointer can point to other objects in use, but the reference can only be a reference to an object and cannot be changed;
7. The pointer can have multiple levels of pointers (**p), ​​and the reference is one level ;
8. The meaning of using the ++ operator for pointers and references is different;

7. Given a triangle ABC and a point P(x,y,z), judge whether the point P is within ABC, give ideas and write the code

Reference answer:
According to the area method, if P is inside triangle ABC, then the area of ​​triangle ABP + the area of ​​triangle BCP + the area of ​​triangle ACP should be equal to the area of ​​triangle ABC. The algorithm is as follows:

#include <iostream> 
#include <math.h> using 
namespace std; 
#define ABS_FLOAT_0 0.0001 
struct point_float 
{
float x; 
float y; 
}; 
/** 
*@brief 计算三角形面积 
*/ 
float GetTriangleSquar(const point_float pt0, const point_float pt1, const point_float pt2) 
{ 
point_float AB, BC; 
AB.x = pt1.x - pt0.x; 
AB.y = pt1.y - pt0.y; 
BC.x = pt2.x - pt1.x; 
BC.y = pt2.y - pt1.y; 
return fabs((AB.x *BC.y - AB.y *BC.x)) / 2.0f; 
} 
/** 
*@brief 判断给定一点是否在三角形内或边上 
*/ 
bool IsInTriangle(const point_float A, const point_float B, const point_float C, const point_float D) 
{ 
float SABC, SADB, SBDC, SADC; 
SABC = GetTriangleSquar(A, B, C);
SADB = GetTriangleSquar(A, D, B); SBDC 
= GetTriangleSquar(B, D, C); SADC = 
GetTriangleSquar(A, D, C); 
float SumSuqar = SADB + SBDC + SADC; 
if ((-ABS_FLOAT_0 < (SABC - SumSuqar)) && ((SABC - SumSuqar) < ABS_FLOAT_0)) 
{ 
return true; 
} 
else 
{ 
return false; 
}
}

8. Please tell me what you understand about the smart pointers in C++. Four smart pointers:
shared_ptr, unique_ptr, weak_ptr, auto_ptr
Inspection point: C++ smart pointer
reference answer:

There are four smart pointers in C++: auto_ptr, shared_ptr, weak_ptr, and unique_ptr. The last three are supported by C++11, and the first one has been deprecated by 11.

Why use smart pointers:

The role of a smart pointer is to manage a pointer, because there is the following situation: the requested space forgot to release at the end of the function, causing a memory leak. Using smart pointers can avoid this problem to a large extent, because a smart pointer is a class. When it exceeds the scope of the class, the class will automatically call the destructor, and the destructor will automatically release resources. So the working principle of the smart pointer is to automatically release the memory space at the end of the function, without manually releasing the memory space.

  1. auto_ptr (c++98 scheme, cpp11 has been abandoned)
    adopts the ownership model.
    auto_ptr< string> p1 (new string ("I reigned lonely as a cloud.”));
    auto_ptr p2;
    p2 = p1; //auto_ptr will not report an error.
    No error will be reported at this time, p2 deprives the ownership of p1, but when An error will be reported when accessing p1 while the program is running. So the disadvantage of auto_ptr is: there is a potential memory corruption problem!

  2. unique_ptr (replace auto_ptr)
    unique_ptr implements the concept of exclusive ownership or strict ownership, ensuring that only one smart pointer can point to the object at the same time. It is particularly useful for avoiding resource leaks (such as "forgetting to call delete because of an exception after creating an object with new").

Using the ownership mode, or the example above
unique_ptr p3 (new string ("auto")); //#4
unique_ptr p4; //#5
p4 = p3;//An error will be reported at this time! !

The compiler considers p4=p3 illegal, which avoids the problem that p3 no longer points to valid data. Therefore, unique_ptr is safer than auto_ptr.

In addition, unique_ptr is smarter: when a program tries to assign a unique_ptr to another, if the source unique_ptr is a temporary rvalue, the compiler allows it; if the source unique_ptr will exist for a period of time, the compiler will prohibit it.
For example:
unique_ptr pu1(new string ("hello world"));
unique_ptr pu2;
pu2 = pu1; // #1 not allowed
unique_ptr pu3;
pu3 = unique_ptr(new string ("You")); // #2 allowed

Among them #1 leaves the unique_ptr(pu1) hanging, which may cause harm. And #2 will not leave the dangling unique_ptr, because it calls the unique_ptr constructor, and the temporary object created by the constructor will be destroyed after its ownership is transferred to pu3. This circumstantial behavior shows that unique_ptr is better than auto_ptr, which allows two assignments.

Note: If you really want to perform an operation similar to #1, you must safely reuse this pointer and assign a new value to it. C++ has a standard library function std::move() that allows you to assign one unique_ptr to another.

例如:
unique_ptr ps1, ps2;
ps1 = demo(“hello”);
ps2 = move(ps1);
ps1 = demo(“alexia”);
cout << *ps2 << *ps1 << endl;

  1. shared_ptr
    shared_ptr implements the concept of shared ownership. Multiple smart pointers can point to the same object, and the object and its related resources will be released when "the last reference is destroyed". It can be seen from the name share that resources can be shared by multiple pointers. It uses a counting mechanism to indicate that resources are shared by several pointers. You can view the number of resource owners through the member function use_count(). In addition to constructing by new, it can also be constructed by passing in auto_ptr, unique_ptr, weak_ptr. When we call release(), the current pointer will release the ownership of the resource and the count will decrease by one. When the count is equal to 0, the resource will be released.

shared_ptr is to solve the limitations of auto_ptr in object ownership (auto_ptr is exclusive), and provides a smart pointer that can share ownership on the mechanism of using reference counting.

Member function:
use_count returns the number of reference counts
unique returns whether it is exclusive ownership (use_count is 1)
swap exchanges two shared_ptr objects (ie exchanges owned objects)
reset abandons the ownership of internal objects or changes in the ownership of objects will cause the original Decrease the reference count of objects

get returns the internal object (pointer). Because the () method has been overloaded, it is the same as using the object directly. For example, shared_ptr sp(new int(1)); sp and sp.get() are equivalent

  1. weak_ptr
    weak_ptr is a smart pointer that does not control the life cycle of an object. It points to an object managed by shared_ptr. The memory management of the object is the strongly referenced shared_ptr. weak_ptr only provides a means of access to the managed object.

The purpose of weak_ptr design is to cooperate with shared_ptr and introduce a kind of smart pointer to assist shared_ptr work. It can only be constructed from a shared_ptr or another weak_ptr object. Its construction and destruction will not increase or decrease the reference count. Weak_ptr is used to solve the deadlock problem when shared_ptr references each other. If two shared_ptr references each other, then the reference count of the two pointers can never drop to 0, and the resource will never be released. It is a kind of weak reference to the object, it does not increase the reference count of the object, and can be converted to shared_ptr. Shared_ptr can be directly assigned to it, and it can obtain shared_ptr by calling the lock function.

class B; 
class A 
{ 
public: 
shared_ptr<B> pb_; 
~A() 
{ 
cout<<"A delete\n"; 
} 
}; 
class B 
{ 
public: 
shared_ptr<A> pa_; 
~B() 
{ 
cout<<"B delete\n"; 
} 
}; 
void fun() 
{
shared_ptr<B> pb(new B()); 
shared_ptr<A> pa(new A()); 
pb->pa_ = pa; 
pa->pb_ = pb; 
cout<<pb.use_count()<<endl; 
cout<<pa.use_count()<<endl; 
} 
int main() 
{ 
fun(); 
return 0; 
} 

It can be seen that pa and pb in the fun function refer to each other, and the reference count of the two resources is 2. When the function is to jump out, the reference count of the two resources will be reduced by one when the smart pointer pa and pb are destructed, but the two references The count is still 1, resulting in that the resource is not released when the function is jumped out (the destructor of AB is not called), if one of them is changed to weak_ptr, we can change the shared_ptr pb_; in class A to weak_ptr pb_; run The result is as follows. In this case, the reference of resource B is only 1 at the beginning. When pb is destroyed, the count of B becomes 0, and B is released. When B is released, the count of A is decremented by one. At the same time, when pa is destroyed Decrease the count of A by one, then the count of A is 0, and A is released.

Note that we cannot directly access the method of the object through weak_ptr. For example, there is a method print() in the B object, we can't access it like this, pa->pb_->print(); English pb_ is a weak_ptr, it should be used first Converted to shared_ptr, such as: shared_ptr p = pa->pb_.lock(); p->print();

9. How to judge whether a number is a multiple of two, how to find how many 1s in a number, talk about your ideas and write the code.
Inspection point: Algorithm
reference answer:
1. Judge whether a number is a multiple of two, that is, judge it Is the last bit of the binary number 0:
a% 2 == 0 or a & 0x0001 == 0.

2. To find the digits of 1 in a number, you can directly divide the digits to determine the remainder:

int fun(long x) 
{ 
int _count = 0; 
while(x) 
{ 
if(x % 10 == 1) 
++_count; 
x /= 10; 
} 
return _count; 
} 
int main() 
{ 
cout << fun(123321) << endl; 
return 0; 
}

10. Please answer the difference between array and pointer
Knowledge point: Array pointer
reference answer:
The main difference between pointer and array is as follows:
Insert picture description here

11. Please answer what is the wild pointer?
Test point: Pointer
reference Answer: A
wild pointer is a pointer that points to a deleted object or has not requested access to a restricted memory area

12. Could you please introduce smart pointers in C++
Test point: shared_ptr
reference answer:
Smart pointers are mainly used to manage memory allocated on the heap, and it encapsulates ordinary pointers into a stack object. When the life cycle of the stack object ends, the allocated memory will be released in the destructor to prevent memory leaks. The most commonly used smart pointer type in C++ 11 is shared_ptr, which uses a reference counting method to record how many smart pointers the current memory resource is referenced. The reference counted memory is allocated on the heap. When a new one is added, the reference count is increased by 1, and when it expires, the reference count is decreased by one. Only when the reference count is 0, the smart pointer will automatically release the referenced memory resources. When initializing shared_ptr,
an ordinary pointer cannot be directly assigned to a smart pointer, because one is a pointer and the other is a class. You can pass in ordinary pointers through the make_shared function or through the constructor. And can pass.

Regarding interviews and techniques, you need to communicate here .
Insert picture description here

Interviews, technology, and job information are covered by the entire network ~
everything is just for a better self!

Guess you like

Origin blog.csdn.net/qq_28581269/article/details/113138958