Some experience and knowledge accumulation in C++ brushing questions

Background: When scoring multiple-choice questions, some of the big guys' answers are quite good. Collect them and it's convenient for everyone.

1. Regarding the conversion between object inheritance. 20201101
Answer: Conversion refers to changing the representation of a variable by changing the type of a variable to another type.
The C++ standard defines four new conversion symbols: reinterpret_cast, static_cast, dynamic_cast and const_cast, the purpose of which is to control type conversion between classes.
1) reinterpret_cast: used to handle conversions between unrelated types, such as conversion between integers and pointers.

2) static_cast: can be used to convert pointers or references between base classes and subclasses in the class hierarchy. It is safe to convert a pointer or reference of a subclass to a base class, but it is not safe to convert a base class to a subclass because of the lack of dynamic type detection.

3) dynamic_cast: The use of conversion symbols for dynamic type conversion of base classes and subclasses. When used for polymorphic type conversion, implicit conversion and opposite operations are allowed, and it will check the validity of the operation.

4) const_cast: Used to modify the const or volatile attribute of the type. Specifically, it can manipulate the const attribute of the object, set or move the attribute.

2. Features of atomic operations in multithreading in C++. 20201102
Answer: When multi-threaded, atomic operations do not need to be synchronized. The purpose of synchronization is to avoid being interrupted by other threads during the operation. The atomic operation is in place in one step, and there is no possibility of being interrupted by other processes, so synchronization is not required.
For example, the operation of multi-threaded and multi-int type variable x, x=1 is an atomic operation and does not need to be synchronized; while ++x, x=y, and x++ all go through the two stages of value evaluation and re-assignment, and are not atomic Operation, so it needs to be synchronized.

3. Idiomatic usage in C++, understanding of IMPL and RALL. 20201102
Answer:
1) IMPL is a bridge design pattern in C++. It is mainly used to hide data and reduce the pressure during compilation. The usual practice is to define a new class whose interface is the same as the original class, but all its data is hidden In the new class. For example, define a class A, define a new class B and it has the same interface, and its data members are in the new class. There is no need to recompile to reference the A code.

2) RALL, which manages limited resources. RALL only requires a small amount of management code (for classes rather than objects), can be universally applied to the use of various resource objects, can be controlled and predicted in time, and can provide a unified mode for resource management.

3. Precautions for using inline functions. 20201102
Answer:
1) Recursive functions cannot be defined as inline functions;
2) Generally applicable to small functions that do not have complex structures such as while and switch and only have 1 to 5 statements, otherwise the compiler system treats the function as an ordinary function ;
3) The two inner functions need to be defined before being used, otherwise the system will consider it to be a normal function.
4) The inline function cannot be abnormal interface description.

4. Some calculation considerations about the sizeof of the class. 20201102
Answer: Empty class, sizeof is 1 (it has nothing to do with whether there is a constructor), but if only one virtual function is added to the empty class (no other variables or functions are added), because a pointer is needed to point to the virtual function Table, so to increase the size of a pointer, the specific size depends on the system.

20201109
5. The characteristics of the class, the three basic elements, and the five basic principles.
Answer:
1) Three basic elements: encapsulation, inheritance, and polymorphism.
2) Five basic principles:
a. Single responsibility principle: It is best to do only one thing for one category. The single principle can be regarded as an extension of the object-oriented principle of low coupling and high cohesion.

b. The principle of open and closed: software entities should be expandable, not modifiable. That is, it is closed to expansion and development and modification.

c. Liskov replacement principle: the subclass must be able to replace its base class. Only when the subclass can replace the base class can the system recognize the subclass during operation, which is the basis for ensuring inheritance and reuse.

d. Dependency inversion principle: Specifically, high-level modules do not depend on low-level modules, and both rely on abstraction; abstraction does not depend on concrete, but concrete depends on abstraction.

e. The principle of interface isolation: use multiple small dedicated interfaces instead of one big total interface.

6. Dynamically typed languages ​​and statically typed languages.
Answer:
1) Dynamically typed language: refers to a language that does data type checking during runtime. In other words, when programming in a dynamically typed language, you never need to assign a data type to any variable. The language will record the data type internally when you assign a value to the variable for the first time.
2) A statically typed language is opposite to a dynamically typed language. Its data type is checked during compilation, that is to say, the data types of all variables need to be declared when writing a program.
C/C++, C#, and JAVA are all representatives of static typing.

7. The break and continue in the loop body.
Answer:
1) Break refers to jumping out of the entire loop body, generally jumping out of multiple loops;
2) Continue, refers to jumping out of this loop, executing the next loop in the loop body, but jumping out of a single loop.

20201110
8. Some static classes, non-static classes, internal and external access properties.
Answer: The
static inner class cannot directly access the non-static data of the outer class, but can access the static data of the outer class; the non-static inner class can directly access the data of the outer class, including private data.
Peripheral class and inner class refer to a class in which there is also a class in the member. The former class is the outer class, and the contained class is called the inner class.

9. List, Set, Map about interface issues.
Answer: Collection objects such as List and Set inherit the Collection interface; Map is a top-level interface with the same level as Collection.

10. Regarding the formal parameters of the main function.
Answer: The first parameter is an integer variable, and the second parameter is an array of pointers whose elements point to character data.

11. Some features of null.
Answer: Null means that there is no address; null can be assigned to a reference variable, but cannot be assigned to a basic type variable, such as int, double, float, boolean.

12. Static member data can be defined within the class, but static member functions can only be defined outside the class.

13. The reason why the static member function does not have this pointer.
Answer:
1) The static member function is not for a certain instance object of the class, but belongs to the entire class and is common to all object instances. The scope is global and independent of the class object.
2) This pointer is equivalent to an instance pointer of a class and is used to manipulate the content of an object instance.
3) Since the static member function is independent of the instance object of the class, the this pointer cannot operate the static member function.

20201111
14. For the definition statement: char a='\82'; What is the value of a?
Answer: a=2; \ means escape character, but the following 8 can not be regarded as xxx hexadecimal here. At this time, it should be considered that from right to left, the corresponding character is intercepted according to the type of a and given to a, where a is of char type, so a 2 is intercepted and given to a. Or simply understand that the assignment is from right to left.

15. What are the similarities and differences between references and pointers?
Answer:
A. The same points:
1). Both are the concept of addresses;
2) The pointer points to a block of memory, and its content is the address of the pointed memory; the alias of a block of memory when it is referenced.

B. Differences:
1) A pointer is an entity, and a reference is only an alias;
2) There is no need to dereference (*) when using a reference, but a pointer needs to be used;
3) A reference can only be initialized once at the time of definition, and then immutable (note , There must be a referenced amount to initialize the reference); the pointer is variable.
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 pointer itself size;
7) and a pointer increment (+) is not the same meaning cited operation, after incrementing a pointer memory represents a move, and cited references is the target value plus 1; if: int a = 0 ; int b=&a;int *p=&a;
b++, means a++, the value is 1; and p++, means p points to the memory behind a.
8) From the memory point of view, the program will allocate a memory area for the pointer, and the reference does not need to be allocated.

20201112
16. How to understand the strcmp(a,b) function?
Answer:
Compare the two strings character by character from left to right (compare according to the size of the ASCII code value) until a different character or'\0' appears. If all the characters are the same, they are considered equal; if there are different characters or some are over, but a little unfinished, then they can be regarded as not equal.

17. What is the definition of a heterogeneous linked list? How to understand?
Answer:
1) (The node types in a general linked list are all the same). In the linked list, the nodes or objects are not all of the same type. This kind of linked list is called a heterogeneous linked list;
2) Think about it first, if different types of Objects are directly linked by a linked list, which is obviously not easy to operate (you cannot use the for operation). Another way of thinking is to abstract the common points of these objects, construct these common points into nodes, and then connect these nodes in series, and you need to ensure that through These nodes can access the corresponding objects, that is, use abstract class pointers to construct a linked list of derived class objects.

18. The process from source code to machine-recognizable code (.cpp->.exe).
Answer:
Source Code -> Compilation Preprocessing -> Compilation -> Optimizer -> Assembler -> Linker -> Executable File

20201113
19. What is the representation of escape characters in octal and hexadecimal systems?
Answer:
\0dd means octal, d ranges from 0 to 8;
\xhh means hexadecimal, h ranges from 0 to f.

20. What is the operator precedence?
Answer: Refer to https://www.cnblogs.com/home123/p/7519655.html
Insert picture description here
Insert picture description here
21. What are the common escape characters?
Answer: Refer to https://www.cnblogs.com/seer/archive/2013/06/08/3127055.html
\a Bell
\b Backspace
\f Page feed
\n Line feed
\r Carriage return
\t Horizontal tabulation Character
\v vertical tab character
\ backslash
\? Question mark character
\ 'single quote character
\ "double quote character
\0 null character (NULL)
\ddd any character
\xdd any character

20201118
22. When does the inline function start to take effect?
Answer: It embeds the function body into each call place when it is recompiled, which is similar to the function of macro definition at compile time. (Note that inline functions do not transfer control when they are called.)
In addition, inline functions are suitable for small functions (lines 1-5) and are frequently called.

23. How to understand & (and operation), | (or operation), ^ (exclusive OR operation)?
Answer:
1) & operation: equivalent to 1/2 of addition of the same bit in decimal
0101 & 0011 Result: binary 0001 decimal (2 ^ 0 +2 ^ 0) / 2 where "^" stands for power

2) | Operation: equivalent to 1/2 of addition of the same digit in decimal and addition of different digits to sum
0101 | 0011 Result: binary 0111 decimal (2 ^ 0 +2 ^ 0)/2 +(2^ 2 +2^ 1)

3) ^ operation: equivalent to adding
0101 ^ 0011 to different digits of the decimal system. The result: binary 0110 decimal system (2^ 2 + 2^1)

Personal understanding is: write the position of the binary number: &, the same, take 1, otherwise it is 0; if there is 1, take 1, and two 0s are 0; if ^, take 0 for the same, otherwise take 1.
Verification applet:

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

int main()
{
    
    
	int m = 123, n = 21;
	char t[10];
	_itoa_s(m, t, 2);
	printf("m二进制:%s\n", t);
	_itoa_s(n, t, 2);
	printf("n二进制:%s\n", t);

	printf("m&n:%d\n", m & n);
	printf("m|n:%d\n", m | n);
	printf("m^n:%d\n", m ^ n);
	system("pause");
	return 0;
}

Reference: https://blog.csdn.net/weixin_41074793/article/details/84943410

20201120
24. What does the final keyword mean?
Answer: The essence of final modified variables is that the modified variable value (address or content) cannot be changed. If the modification is a reference, the address cannot be changed, and the content on the address can still be changed; if the modification is a variable, the value of the variable cannot be modified.

20201125
25. What about the structs framework?
Answer:
1) structs can upload files;
2) structs is based on the MVC model, which is a model, view, and controller, which is a design pattern;
3) structs framework can make the process structure clearer;
4) structs requires a lot of action classes , Will increase the number of files.

26. Some members in the class are static variables.
Answer:
1) The static variable cannot be accessed through this;
2) The static variable can be accessed through the class name +.

27. Small knowledge of stack and heap operation.
Answer:
1) If the stack memory is not initialized, it will appear "hot and hot";
2) If the heap memory is not initialized, it will appear "tun Tun Tun".

20201128
28, comma expression characteristics.
Answer: If there is a = expression 1, expression 2, ..., expression n, then the final result of a is expression n.

29, while, for conditional judgment attention points.
Answer:
When the condition is judged to be an assignment statement, such as while(x=y), for(;x=y;), if the assignment is not 0, the conditions are all 1, and the loop body will execute; but when the assignment is 0, Indicates termination, and the loop body will not be executed.

20201202
30. Keyword super
Answer:
1) The subclass constructor calls the super class constructor to use super;
2) After the subclass rewrites the parent class method, if you want to call the rewritten method of the parent class, you need to use super; note , The method that has not been overridden can be called directly.

###########################
Most of them are sorted out from Niuke.com during the process of brushing the questions, thanks to the original answerer for providing answers ~
no accumulation Silicon step, no more than a thousand miles

Guess you like

Origin blog.csdn.net/qq_45701501/article/details/109411308