[C++ Interview Questions] 20 must-master C++ interview questions

20 must-master C++ interview questions

When interviewing for C++ jobs, I often encounter various interview questions, which require higher knowledge mastery of the recruited person. This article will bring you 20 C++ interview questions that must be mastered, don't miss it!

Question 1: Please tell me in simple language what is C++?

Answer: C++ is an object-oriented programming language developed on the basis of C language, which is widely used. C++ supports multiple programming paradigms-object-oriented programming, generic programming and procedural programming. It has a wide range of programming fields and is often used in application fields such as system development and engine development. It is one of the most powerful programming languages ​​used by programmers. It supports classes: classes, encapsulation, overloading and other features!

Question 2: What is the difference between C and C++?

Answer: C++ adds classes on the basis of C. C is a structured language whose focus is on algorithms and data structures. The first consideration in the design of C programs is how to process input (or environmental conditions) to obtain output (or realize process (transaction) control) through a process. For C++, the first consideration is how to construct an object model so that This model can fit the corresponding problem domain, so that the output or process (transaction) control can be achieved by obtaining the state information of the object.

Question 3: What is object-oriented (OOP)?

Answer: Object-oriented is a method and idea to understand and abstract the real world, and an idea to deal with problems by transforming demand elements into objects.

Question 4: What is polymorphism?

Answer: Polymorphism means that the same operation or function or process can act on multiple types of objects and obtain different results. Different objects can produce different results when they receive the same message. This phenomenon is called polymorphism.

Question 5: Do you understand design patterns, just give me a brief example?

Answer: Design pattern is a set of code design experience that has been used repeatedly and is known to most people.
For example, the singleton mode ensures that a class has only one instance and provides a global access point to access it.
Applicable to: when the class can only have one instance and the client can access it from a well-known access point; when the only instance should be extensible through subclassing, and the client should be able to use an extended instance without changing the code Time.
For example, the factory pattern defines an interface for creating objects, and lets subclasses decide which class to instantiate. Factory Method delays the instantiation of a class to its subclasses.
Applicable to: when a class does not know the class of the object it must create; when a class wants its subclasses to specify the objects it creates; when the class delegates the responsibility of creating objects to multiple helpers When one of the sub-categories, and you want to localize the information about which helper sub-category is the agent.

Question 6: Has the STL library been used? What are the common STL containers? Which algorithms have been used?

Answer: STL includes two parts: containers and algorithms. (It is also important to integrate the two iterators)

The container is the place where the data is stored. Such as array and so on.

In STL, containers are divided into two categories: sequential containers and associative containers.

Sequence container, the elements are not necessarily in order, but all can be sorted. Such as: vector, list, deque, stack, queue, heap, priority_queue, slist;

Associative container, the internal structure is basically a balanced binary tree. The so-called association means that each element has a key value and a real value, and the elements are stored according to certain rules. Such as: RB-tree, set, map, multiset, multimap, hashtable, hash_set, hash_map, hash_multiset, hash_multimap.

Choose one of each as an explanation below.

vector: It is a container that dynamically allocates storage space. Different from array in C++, the space allocated by array is static and cannot be changed after allocation, while vector will automatically reallocate (expand) space.

set: Its internal elements will be automatically sorted according to the key value of the element. Different from map, its key value is real value, and map can have different key value and real value at the same time.

Algorithms, such as sorting, copying... and a container-specific algorithm. This point does not need to be introduced too much, mainly look at the content of the iterator below.

Iterators are the essence of STL, and we describe it as follows: Iterators provide a way to enable it to access the various elements contained in a container in order without exposing the internal structure of the container. It separates the container and the algorithm so that the two can be designed independently.

Question 7: Will the data structure be? Which ones are mainly used in the project development process?

Answer: The data structure mainly uses arrays, linked lists, and trees (less), as well as the idea of ​​stacks and queues.

Question 8: Does const know? Explain its role.

Answer:
1. const modifies the member variables of the class, which means member constants and cannot be modified.

2. The const modified function promises that it will not modify the data members of the class within this function, and will not call other non-const member functions.

3. If const constitutes a function overload, const objects can only call const functions, and non-const objects call non-const functions first.

4. The const function can only call the const function. Non-const functions can call const functions.

5. The const member function defined outside the class requires a const modifier at both the definition and declaration.

Question 9: When is the static variable of the class initialized? When is the static variable of a function initialized?

Answer: The static member variables of the class already exist before the class is instantiated, and memory is allocated. The static variable of the function is initialized when the function is executed.

Question 10: What is the difference between heap and stack? The life cycle of the heap and stack?

Answer:
1. The difference in stack space allocation:

1. Stack (operating system): automatically allocated and released by the operating system, storing function parameter values, local variable values, etc. Its operation mode is similar to the stack in the data structure;

2. Heap (operating system): Generally, it is allocated and released by the programmer. If the programmer does not release it, the OS may reclaim it at the end of the program. The allocation method is similar to a linked list.

Two, the difference between stack caching methods:

1. The stack uses the first level cache. They are usually in the storage space when they are called, and they are released immediately after the call;

2. The heap is stored in the second-level cache, and the life cycle is determined by the garbage collection algorithm of the virtual machine (not that it can be recycled once it becomes an orphan). Therefore, the speed of calling these objects is relatively low.

Three, the stack data structure difference:

Heap (data structure): Heap can be regarded as a tree, such as: heap sort;

Stack (data structure): a first-in-last-out data structure.

Question 11: What is the difference between C and C++?

Answer:
C++ adds classes on the basis of C

C is a structured language, and its focus is on algorithms and data structures.

The first consideration in the design of C programs is how to process inputs (or environmental conditions) to obtain output (or realize process (transaction) control) through a process. For C++, the first consideration is how to construct an object model so that This model can fit the corresponding problem domain, so that the output or process (transaction) control can be achieved by obtaining the state information of the object.

Question 12: Explain encapsulation, inheritance and polymorphism?

answer:

1. Package:

Encapsulation is the first step to realize object-oriented programming. Encapsulation is to gather data or functions in individual units (we call them classes).

The meaning of encapsulation is to protect or prevent code (data) from being unintentionally destroyed by us.

2. Inheritance:

Inheritance mainly realizes the reuse of code and saves development time.

The child class can inherit some things from the parent class.

Three, polymorphism

Polymorphism: The same operation acts on different objects, can have different interpretations, and produce different execution results. At runtime, the method in the derived class can be called through the pointer to the base class.

Question 13: What is the difference between pointer and reference?

answer:

  1. A pointer is a variable, but this variable stores an address, pointing to a storage unit in the memory; and the reference is only an alias;

  2. No need to dereference (*) when reference is used, pointers need to be dereferenced;

  3. References can only be initialized once at the time of definition, and then immutable; pointers are mutable;

  4. References are not const, pointers are const;

  5. References cannot be null, pointers can be null;

  6. “Sizeof reference” gets the size of the pointed variable (object), and “sizeof pointer” gets the size of the pointer itself;

  7. The self-increment (++) operation meaning of pointer and reference is different;

  8. The pointer can have multiple levels, but the reference can only be one level (int **p; legal but int &&a is illegal)

9. From the point of view of memory allocation: the program allocates a memory area for pointer variables, and the reference does not need to allocate the memory area.

Question 14: What is a memory leak? In the face of memory leaks and pointer out-of-bounds, what methods do you have? What methods do you usually use to avoid and reduce such errors?

Answer: The space dynamically opened up by the dynamic storage allocation function is not released after use. As a result, the memory unit is always occupied, which is a memory leak.

Remember the length of the pointer when using it.

Make sure to be free there when malloc.

When assigning pointers, it should be noted that the assigned pointer does not need to be released.

It is best not to assign a pointer to dynamically allocated memory.

Question 15: What are the commonly used sorting algorithms? Briefly describe the advantages and disadvantages of several sorting algorithms?

Answer: select, bubbling, fast, hill, merge, stacking, etc.

1. Fast sorting: It is an improvement of bubble sorting.

Advantages: fast, less data movement

Disadvantages: insufficient stability

2. Merge: Divide-and-conquer sorting, a stable sorting algorithm, generally used for sequence of numbers that are disordered in the whole but ordered locally.

Advantages: high efficiency O(n), stable

Disadvantages: more memory usage

Question 16: What is the difference between new and malloc?

answer:

1. Malloc and free are standard library functions of C++/C language, and new/delete are C++ operators. They can be used to apply for dynamic memory and release memory.

2. For objects of non-internal data types, malloc/free alone cannot meet the requirements of dynamic objects. The object must automatically execute the constructor when it is created, and the object must automatically execute the destructor before it dies.

3. Because malloc/free is a library function rather than an operator, it is not within the control of the compiler, and the task of executing the constructor and destructor cannot be imposed on malloc/free. Therefore, the C++ language needs an operator new that can complete dynamic memory allocation and initialization, and an operator delete that can complete cleanup and release of memory. Note that new/delete is not a library function.

4. C++ programs often call C functions, and C programs can only use malloc/free to manage dynamic memory.

5. New can be considered as the execution of malloc plus constructor. The new pointer directly carries type information. And malloc returns void pointers.

Question 17: What is the difference between TCP and UDP communication? What is IOCP?

answer:

1. TCP is connection-oriented, UDP is connectionless

2. TCP is guaranteed, UDP transmission is not guaranteed

3. TCP is low efficiency, UDP is high efficiency

4. TCP is based on flow, UDP is based on data message

5. TCP transmits important data, UDP transmits unimportant data

The full name of IOCP is I/O Completion Port, which is translated into I/O Completion Port in Chinese.

IOCP is an asynchronous I/O API, which can efficiently notify I/O events to applications.

Unlike using select() or other asynchronous methods, a socket [socket] is associated with a completion port, and then normal Winsock operations can be continued. However, when an event occurs, the completion port will be added to a queue by the operating system. Then the application can query the core layer to get this completion port.

Question 18: What is the difference between synchronous IO and asynchronous IO?

answer:

A. Synchronization

The so-called synchronization means that when a function call is issued, the call does not return until the result is not obtained.

According to this definition, in fact, most functions are called synchronously (for example, sin isdigit, etc.).

But generally speaking, when we talk about synchronous and asynchronous, we specifically refer to tasks that require other components to collaborate or take a certain amount of time to complete.

The most common example is SendMessage.

This function sends a message to a certain window. This function does not return until the other party finishes processing the message.

After the other party has finished processing, the function returns the value returned by the message processing function to the caller.

B. Asynchronous

The concept of asynchrony is the opposite of synchronization.

When an asynchronous procedure call is issued, the caller will not get the result immediately.

The component that actually handles this call is to notify the caller through status and notification after the call is issued, or process the call through a callback function.

Question 19: Explain static functions and static variables in C++?

answer:

(1) Class static data members are created and initialized at compile time: they exist before any object of the class is established and do not belong to any object, while non-static class member variables belong to the object. There is only one copy of a class static data member, which is shared by all objects of this class.

(2) Class static member functions belong to the entire class, not to an object, and are shared by all objects of that class.

1. The static member variable realizes the sharing of information among similar objects.

2. The static member is stored outside the class, and the class size is calculated, and it is not included.

3. The static member is a global variable whose namespace belongs to the class and is stored in the rw section of the data area.

4. Static members can only be initialized outside the class.

5. It can be accessed through the class name (it is also possible when there is no object generated) or through the object.

Question 20: What do you know about memory?

answer:

1. Stack-automatically allocated and released by the compiler

2. Heap-Generally allocated and released by the programmer, if the programmer does not release, it may be reclaimed by the OS at the end of the program

3. Global area (static area), the storage of global variables and static variables are put together, initialized global variables and static variables are in one area, uninitialized global variables and uninitialized static variables are in another adjacent block area. -Release at the end of the program

4. There is also a special place for constants. -Release at the end of the program

5 Program code area, storing binary codes.

The variables defined in the function body are usually on the stack, and the functions that allocate memory using malloc, calloc, realloc, etc. are allocated on the heap. The global variables defined outside of all functions are stored in the global area (static area) wherever the static modifier is added. The static variables defined outside of all functions are valid in this file and cannot be externed to other files. Use, static defined in the function body means that it is only valid in the function body. In addition, strings such as "adgfdf" in the function are stored in the constant area.

Guess you like

Origin blog.csdn.net/m0_37882192/article/details/109440125
Recommended