Summary of common knowledge points in C + + and data structure written interview

Source: Internet
Author: User
Tags finally block function prototype

Some of the basic knowledge of common test points of personal summary, the great God do not spray, welcome correction.

1. The footer of a generalized table is a table of elements that are left behind when the header is dropped, and the header can be a table or a single-element value. The footer or table, or empty table.

2. Constructors cannot be declared as virtual functions.

Why can't a constructor be a virtual function?

1. From the storage space angle, the virtual function corresponds to a pointer to the vtable virtual function table, which we all know, but this pointer to vtable is actually stored in the object's memory space. The problem comes out, if the constructor is virtual, it needs to be called through vtable, but the object is not instantiated, that is, the memory space is not yet, how to find vtable it? So the constructor cannot be a virtual function.

2. From the perspective of Use, virtual function is mainly used in the case of incomplete information, can make the overloaded function to get the corresponding call. The constructor itself is to initialize the instance, and it has no practical meaning to use virtual functions. So the constructor is not necessarily a virtual function. The function of a virtual function is to call it through a pointer or reference to the parent class to become the member function that invokes the subclass. The constructor is called automatically when the object is created and cannot be called by a pointer or reference to the parent class, so the constructor cannot be a virtual function.

3. The constructor does not need to be a virtual function, nor is it allowed to be a virtual function, because we always explicitly specify the type of object when creating an object, although we may access it through a pointer or reference to a base class but not necessarily, we often destroy objects by pointers to the base class. At this point, if the destructor is not a virtual function, the object type cannot be correctly identified and the destructor cannot be called correctly.

4. From the implementation perspective, VBTL is established after the constructor call, so the constructor cannot be a virtual function from the actual meaning, when the constructor is called to determine the real type of the object (because the subclass will call the constructor of the parent class), and the function of the constructor is to provide initialization, only once in the object lifetime, is not a dynamic behavior of an object, nor is it necessary to become a virtual function.

5. When a constructor is called, one of the first things it does is initialize its vptr. Therefore, it can only know that it is the "current" class, and completely ignores whether there are successors behind the object. When the compiler generates code for this constructor, it generates code for the constructor of the class-neither for the base class nor for its derived class (because the class does not know who inherits it). So the vptr that it uses must be for this class of vtable. And, as long as it is the last constructor call, the vptr will remain initialized to point to this VTABLE during the lifetime of the object, but if there is a later derived constructor called, the constructor sets Vptr to its VTABLE, And so on. Until the end of the constructor function. The state of the vptr is determined by the constructor that was last called. This is another reason why constructor calls are from the base class to the order of the more derived classes. However, when this series of constructor calls is occurring, each constructor has been set vptr points to its own vtable. If a function call uses a virtual mechanism, it will only produce a call through its own vtable, not the last vtable (all constructors are called after the last vtable).

3. Two cross-tree related concepts:

Depth: The number of layers of the tree;

Degree: (to the node) the number of children of a node, the maximum of the degree of its node (for the whole tree).

Full binary tree: A full binary tree is the two fork tree that is removed from the right-to-left node of a two-fork tree.

If a tree has n leaf nodes, then the node with a degree of 2 is n-1. If we know that a complete binary tree has n leaf nodes, then it has a maximum of 2n nodes.

Any node with a binary tree with a 0 node ratio of 2 is one more.

4. The insert element of the heap is inserted at the last, then the heap is adjusted, and the delete element is the place where the last element is deleted and then the heap is adjusted.

5. The first sequence traversal and the middle sequence traversal can uniquely determine the two-fork tree, the method is: the root node is determined by the ordinal sequence, the sequence is divided into two ends by the root node, the left sub-tree is in front, the right subtree is followed, and the tree structure is restored by repeating the previous steps on the left and next subtree.

6.PRINTF is pressed from right to left into the stack, run the right item first, and then run the left item. For example, printf ("%d,%d", a,++a); the output should be two equal (all values after a+1).

7. When writing a judgment statement, it is best to place a right value on the left side, and put the left value on the right side, as this will check the case where the assignment operator was mistakenly written.

8. All ASCII codes can be represented by "\" plus numbers (typically 8 digits). So see \ followed by three digits to understand that this is a character.

9. In the sequence of the binary tree nodes, the sequence of the first sequence, the middle sequence, and the sequential order are the exact same sequence of all leaf nodes.

10. A type A is a subtype of type B, providing at least one behavior of another type T. A derived class of public inheritance is a subtype of a base class. Class A is a subtype of Class B, which means that class A adapts to Class B, that is, where class A objects can be used as well as for Class B objects.

11. Topological ordering is often used to determine the order in which things occur in a dependency set. A linear sequence of topological sorts, as long as the first task can be satisfied, such as V1→V2, the sequence as long as the V1 in front of the V2 (so the resulting sequence may not be unique).

12. The first sequence traversal sequence of a non-empty two-fork tree is exactly the opposite of the sequential traversal sequence, then the two-fork tree must satisfy only one leaf node.

13.++ is a unary operator, a self-increment operation that can be used only for one variable, that is, the value of the variable is increased by 1 and cannot be used in an expression. The + + operator has a very interesting feature, the predecessor + + returned as an Lvalue object, can be used in an expression, such as ++++i, but the Post + + returns the right value, can not be used in an expression multiple times after + + + (for-the same), because the right value cannot be self-increment operation, As a result, an object can no longer be self-growing in the current style once it has been added to its own. (can be added next time) This is understood: the predecessor operation returns the object, the value of the object is self-increment, and the subsequent operation returns a value instead of an object, the object itself is self-increment, but it does not return.

14. For any tree, set it to have n nodes, and the sum of the degrees of the N nodes is n-1.

15. A multi-type data type is not determined by the type of the contained data element.

16. A char occupies a byte, a short occupies 2 bytes, and an int occupies 4 bytes.

17. Storage of three kinds of data: Local variables are stored in stacks, global variables and static variables are stored in static storage, and dynamically allocated objects are stored in the heap area (dynamic storage).

18. Use a fetch address symbol for an array, return a pointer to the entire array (not the first element), and for pointers to the first element of the array, simply assign the array to the pointer directly. For example, int a[]={1,2,3,4};&a returns an int (*) [4] that is the array pointer type.

19. The character type occupies 1 bytes, can start from any address, the short type occupies 2 bytes, must start from the 2-byte multiple address, the int type occupies 4 bytes, must start from the 4-byte multiple address.

20. The structure is sequential in the memory organization, the Union is overlapping, each member shares a piece of memory, so sizeof, the entire consortium, is the maximum value of each member sizeof. (You need to consider alignment).

21. In undirected graphs, if the VJ has a path from vertex VI to vertex, it is called VI and VJ connected. If any of the two vertices in the graph are connected, then the graph is connected, otherwise, the graph is a non-connected graph, and the maximal connected sub-graph is called the connected component, where the so-called maximal refers to the maximum number of vertices contained in the sub-graph.

22. A pure virtual function is a virtual function declared in a base class that is not defined in the base class, but requires that the derived class define its own implementation method. The method of implementing a pure virtual function in a base class is to add "= 0" after the function prototype

23. Such as virtual void f () = 0;

24. Whereas a class containing a pure virtual function in C + + is called an abstract class, an object of an abstract class cannot be defined because the abstract class contains a pure virtual function that is not defined. Summary: 1. Abstract classes can only be used as base classes for other classes, and objects of abstract classes cannot be defined. 2. Abstract classes cannot be used for parameter types, function return values, or type 3 for display conversions. Abstract classes can define pointers and references to abstract classes, which can point to its derived classes for polymorphism.

25. Heap memory and Stack memory differences: 1. Stack memory is allocated by the operating system, and heap memory is allocated by the programmer itself. The size of the 2 stack memory space is fixed, and the heap size is limited by the effective virtual memory in the system. The 3 stack of space is determined by the system to be free, and the heap memory needs to decide when to release. The use of 4 stacks is prone to fragmentation, but easy to use.

26. Macro definition Problem: #define是宏定义, the definition simply converts, does not perform advanced mathematical operations including arithmetic, such as #definea 2, #define B a+1; #define C (b+1) *B/2 will perform the operation (2+1+1) *2+1/2= 8, rather than following the normal math priority.

27. The difference between the 32-bit and 64-bit bytes in different data types

32-bit compilers:

Char:1 bytes

char* (i.e. pointer variable): 4 bytes (32-bit addressing space is 2^32, which is 32 bit, or 4 bytes. Similarly 64-bit compilers)

Short Int:2 bytes

Int:4 bytes

Unsigned int:4 bytes

Float:4 bytes

Double:8 bytes

Long:4 bytes

A long long:8 bytes

Unsigned long:4 bytes

64-bit compilers:

Char:1 bytes

char* (i.e. pointer variable): 8 bytes

Short Int:2 bytes

Int:4 bytes

Unsigned int:4 bytes

Float:4 bytes

Double:8 bytes

Long:8 bytes

A long long:8 bytes

Unsigned long:8 bytes

28. After converting the tree T to binary tree B, then for a node n in B:

(1) If there is a left subtree, the left subtree must come from the eldest son of N in the tree T;

(2) If there is a right subtree, the right subtree must come from n adjacent to the right sibling in the tree T;

29. Automatic variables are created only when they are defined, and the system reclaims the storage space that the variable occupies when the function that defines them returns. The allocation and recycling of these variable storage spaces is done automatically by the system. In general, local variables that are not specifically described are automatic variables. Automatic variables can also be described using the keyword Auto.

30. For the heap, the direction of growth is upward, that is, toward the memory address of the direction of increase, for the stack, its growth direction is downward, is to reduce the memory address in the direction of growth.

31. The Clue Binary tree utilizes a two-fork species with a node number of N to have a n-1 character, pointing the null pointer field to a specific type of precursor or successor (preface, middle order, post order) if a node left subtree is empty, then the left subtree is pointed to its precursor, if the right subtree is empty, The right subtree pointer is pointed to its successor (as required by the order).

If a new node is inserted into the balanced binary tree, the balance of the balanced binary tree is destroyed. The first thing to find is the pointer to the root node that loses balance after inserting a new node. Then adjust the link relationship between the nodes in this subtree to make it a new balance subtree. When the lost balance of the most small tree is adjusted to balance the subtree, the original other unbalanced subtree without adjustment, the entire binary sorting tree becomes a balanced binary tree.

The least-balanced tree is a subtree that is closest to the insertion node and has an absolute value greater than 1 of the balance factor as the root. Assuming that a represents the root node of the least-balanced tree, the action of adjusting the subtree can be summarized in the following four cases.

(1) ll-type balanced rotation method

Because the node f is inserted on the left child B of a, the balance factor of a increases from 1 to 2 and loses balance. Therefore, a clockwise rotation operation is required. The left child B of a will rotate to the right to replace a as the root node, and a to the right down to the root node of the right subtree of B. and the right sub-tree of B turns into a's left subtree.

(2) RR type Balanced rotation method

Because the node f is inserted in right child C of a, the balance factor of a is reduced from 1 to 2. Therefore, a counterclockwise rotation operation is required. The right child c of a will rotate to the left up instead of a as the root node, and a to the left is the root node of the left subtree of c. The original C's left subtree becomes the right subtree of a.

(3) LR-Type balanced rotation method

Due to the insertion of node F on the right number of the left child B of a, the balance factor of a increases from 1 to 2 and loses balance. It is necessary to perform two rotation operations (first counterclockwise, then clockwise). That is, first, the right subtree of the left child B of the A node, the root node D to the left up to the position of the B node, and then the D node to the right up to the point of rotation to the position of a node. That is, first make it a ll type, and then the LL-type treatment.

, that is, the circle is first adjusted to the balance tree, and then to the root node to the left of a subtree, at this time become the ll type, and then by the LL-type treatment into a balanced type.

(4) RL type Balanced rotation method

Because the node f is inserted on the left subtree of right child c of a, the balance factor of a is reduced from 1 to 2. It is necessary to perform two rotations (clockwise, counterclockwise), i.e. the root node d of the right child C of the a node is lifted to the right up to the C node position, and then the D node is rotated to the left up to the position of a node. The first is to make it a RR type, and then the RR type processing.

, that is, the circle is first adjusted to the balance tree, and then to the root node to the left of a subtree, at this time become RR type, and then processed into a balanced type RR.

The balance depends on the rotation. There are 3 nodes involved in the rotation (one of which may be the outer node null), and the rotation is to move the 3 nodes to a position. Note that when the left-hand p->right must not be empty, right-handed when the p->left must not be empty, it is obvious.

If you start from an empty tree and maintain balance at all times, the imbalance will only occur on the insert delete operation, and the unbalanced flag is the one that appears bf = = 2 or BF = =-2 nodes.

32. The stack has two storage methods: One is the sequential stack, the other is the chain stack. The sequential storage structure of a stack is a data element that is stored sequentially from the stack to the top of the stack using a contiguous set of storage units, with a pointer top indicating the position of the top element of the stack. Because the operation of the stack is a special case of linear table operation, the operation of the chain stack is easier to implement.

33. Note the rules of logic and OR operation, and if the left operand is false, the right operand (directly returning false) is no longer executed, and if the left operand of the operation is true, the right operand is no longer executed (directly returning true).

34. Sorting algorithm Features Summary:

35.final modifier

If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent class. Therefore, a class cannot be declared abstract and declared final. Declaring variables or methods as final ensures that they are not changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read in subsequent references and cannot be modified.

Finally

Provides a finally block to perform any cleanup operations when exception handling occurs. If an exception is thrown, the matching catch clause executes, and the control enters the finally block, if any. The generic exception handling block requires a method name.

Finalize

Java technology allows the use of the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector to this object when it determines that the object is not referenced. It is defined in the object class, so all classes inherit it. Subclasses override the Finalize () method to organize system resources or perform other cleanup work. The Finalize () method is called on the object before the object is deleted by the garbage collector. All classes in Java inherit the Finalize () method from the object class. When the garbage collector (garbage Colector) decides to reclaim an object, it runs the Finalize () method of the object.

36. Note that free one pointer and delete one pointer is different, the former releases a pointer, the pointer will still point to the original address. The meaning of free is to tell the system that the destination address can be recycled. Delete, however, removes the pointer.

37. Even the simplest virtual function call, the compiler's inline handler does not help it. (This is not surprising.) Virtual means "Wait until runtime to decide which function to invoke", inline means "replace the call with the called function during compilation", if the compiler does not even know which function will be called, of course it cannot be blamed for rejecting the build inline call.

38. The difference between a macro definition and a const:

(1) Compiler processing method is different, define macro is in the preprocessing phase of the expansion, const constant in the compilation run phase.

(2) Different types and security checks, define macros do not have a type, do not do any checks, just expand, const constants have a specific type, during the compilation phase will perform type checking.

(3) Different storage methods, define macro is only expanded, how many places to expand how many times, const constants are stored in memory allocation, only generated once. (const can save space, avoid unnecessary memory allocations, and improve efficiency).

Only const constants are used in C + + programs without the use of macro constants, that is, const constants completely supersede macro constants.

39. The function reserve (size) sets the capacity of the string to at least size. If size specifies a value that is less than the number of characters in the current string, the capacity is set to the numeric value that holds the character exactly. Reserve () runs at a linear time. Its greatest use is to avoid the loss of buffer memory over and over again, or to ensure that the buffer is large enough before some STL operations are used.

40. Important points of knowledge about final;

(1) The final keyword can be used for member variables, local variables, methods, and classes.

(2) The final member variable must be initialized at the time of declaration or initialized in the constructor, or a compilation error will be reported.

(3) You cannot assign a value to the final variable again.

(4) A local variable must be assigned a value at the time of declaration.

(5) All variables in an anonymous class must be final variables.

(6) The final method cannot be overridden.

(7) The final class cannot be inherited.

(8) The final variable is not initialized at the time of declaration (blank final variable), which must be initialized in the constructor or called this () initialization. If you do not, the compiler will say "final variable (variable name) needs to be initialized".

41. As long as one of the two operands is of type double, the other is converted to a double type, and the result is a double type, and if one of the two operands is of type float, the other is converted to float type, and the result is a float type. If one of the two operands is of type long, the other is converted to a long type, and the result is a long type, otherwise (operands: byte, short, int, char), two numbers will be converted to int type, and the result is int type.

42. The illegal pointer is pointing to the memory that has been reclaimed, or pointing to an incorrect address.

43. Classes that have virtual functions are stored with the first address stored as virtual function pointers, followed by member variables.

44. General when you convert a class object pointer to another type pointer, the pointer itself remains the same, just the way the address is read, pointing to the previous position.

45. When overloading an operator as a member function, there are no parameters in its parameter table, which means that the operator is a prefix unary operator and a suffix unary if there is an int in the argument list.

46. When sizeof is performed on a string, the terminator of the string is computed, and the strlen operation is performed to find the length of the string.

The definition of B-tree is like this, the B-Tree of an M-order satisfies the following conditions: (1) Each node has at most m subtrees Tree, (2) root node, each other non-leaf node has at least m/2 subtrees tree; (3) Joghen node is not a leaf node, at least two subtrees tree; (4) All leaf nodes are on the same layer. The leaf nodes of B-tree can be regarded as an external node, without any information; (5) All non-leaf nodes contain information data: (N,P0,K1,P1,K2,P2,..., kj-1,pj-1) wherein Ki is the keyword, and satisfies kiki+1;pi as a pointer to the subtree node, And the keywords of all the nodes in the subtree referred to by PI-1 are smaller than the keywords of all the nodes in the subtree referred to by ki,pj-1 are more than kj-1. B + Tree is a variant tree that appears in the file system, the main difference is that a non-leaf node has n sub-tree has n keywords, the role of these keywords is index; all the leaf nodes contain information about all the keywords and pointers to these key records. And the key of the leaf node itself is the size of a large sequential link. From the above characteristics, we know that the two trees are balanced multi-branch tree, they can be used for the index structure of the file, but B-tree can only support random retrieval, and the second-level tree is an ordered tree, can support both random retrieval and sequential retrieval.

48. Using Post + + for the same variable in the same expression, the object before the change is called in the current expression, regardless of the number of times it is used. and the predecessor + + will change the variable every time. But at the end of the current expression, the original variable is changed. For example x=1;y= (x + +) + (x + +); the resulting y=2;x=3

49. In C++stl, the list uses a chained data structure, and map uses a red-black tree.

. Byte is a byte and bit is a bit.
51. The byte alignment rules indicate that for a struct, its starting address and size must be positive multiples of the alignment number, where the member's occupied space satisfies its own rules and, if necessary, fills it as needed.

Summary of common knowledge points in C + + and data structure written interview

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.