Introduction to common interview pen questions, interview pen questions
1. # include
# Include
Using namespace std;
Void main ()
{
Int c = 5;
If (c ++ = 5)
{
Printf ("% d \ n", c --);
Printf ("% d", c );
}
Else
{
Printf ("% d", c ++ );
}
System ("pause ");
} Answer: 6, 5
2. int x = 3, y = 4, z = 5, t = 2, n = 5;
Printf ("% d \ n", (t> 0 )(! (X + y) + (z-1 & y) + t): n );
Answer: 3.
Priority relationships between operators in C language:
To put it simply:!> Arithmetic Operators> Relational operators >&>||> value assignment operators
Detailed introduction:
1. Highest Level: When an operator of the same level is displayed, the Union direction is from left to right (when there is no union order at the following level, the Union direction is from left to right by default ).
() Parentheses
[] Subscript operator number
-> Pointer to struct member operators
. Struct member Operator
2,
Level 2: This level is a single object operator number! (Not logically ),~ (Bitwise inversion), ++ (auto-increment), -- (auto-subtraction),-(negative), (type) (type conversion operator), * (pointer), & (value), sizeof (length operator number ).
The combination direction of this level is from right to left.
For example, if * p ++ appears, then * And ++ are at the same level. Calculate the right and then the left.
Therefore, * p ++ is equivalent to * (p ++), rather than (* p) ++.
3,
Level 3: +,-, *,/, %
This level is an arithmetic operator. In combination with the sequence that is consistent with that in mathematics learning, we first multiply and divide the remainder, and then add or subtract.
4,
Level 4: <,>
This is the shift left and right operators, which may be used in bitwise operations.
5,
Level 5: <, <=,>,> = ,! =, =
These operators are also called Relational operators, where <, <=,>,> = is higher! =, =.
6. Level 6: & (bitwise AND), ^ (bitwise OR), | (bitwise OR)
These three symbols are also bit operators, with the inner priority, <^> |.
7,
Level 7: &, |
Logic and & priority are greater than logic or |.
(Expression 1 & Expression 2) If expression 1 is not 1, the logic returns 0 directly, skipping expression 2, even if expression 2 has an important operation.
(Expression 1 | expression 2) If expression 1 is 1, logic may return 1 directly, skip expression 2, even if expression 2 has an important operation.
8,
Level 8 :? :
It is also called the conditional operator number, which is the only three-object operator in C language. The combination sequence is from right to left.
9,
Level 9: =, + =,-+, * =,/=, % =
These operators are also called value assignment operators. In addition, >=, <=, ^=, and |= are within this level, the combination sequence is from right to left.
A = 3, B = 4, c = 5;
A + = B * = c;
The combination sequence is from right to left: calculate B, B = B * c = 20 first, and then calculate a = a + B = 23;
10.
Minimum :,
The comma operator is also called an ordered value operator. In C, the operation level is the lowest.
Comma expression = last expression
Int a = 1, B = 2, c = 3;
A = (a, c + = 5 );
Printf ("% d", a, B, c); 8, 2, 8
In general,Primary Operators(), [],->,. HigherSingle Object OperatorHigherArithmetic Operators(Multiplication, division, and addition and subtraction) higherRelational operatorsHigherLogical operators(Not including !) HigherConditional OperatorsHigherValue assignment operatorHigherComma operator.
Bitwise operatorsThe priority is relatively scattered.
In addition to the assignment operator, condition operator, and single object operator, the order of union between level operators is from right to left, and the other operators are from left to right.
3. If a function does not have a return statement when it is called, what will be returned after the function is called?
Even if the function does not have a return statement, if the function is not declared as void, the return value of int type can still be obtained when the function is called. But an uncertain value
4. The sequence of calling constructor and destructor in the derived class
When an object is created, the base class constructor is called first, and then the constructor of the derived class is called. When the Destructor is called, the order is the opposite.
# Include
Using namespace std; class Shape {public: void Draw () {cout <"Base: Draw ()" <
5. # include
# Include
Using namespace std; class A {public: A (int x) {cout <"";}~ A () {cout <"~ A ";}int a ;}; class B {public: B (int y) {cout <" B "; y = B ;}~ B () {cout <"~ B ";}int B ;}; class C {public: C () {cout <" C ";}~ C () {cout <"~ C ";}}; class D: public C {private: B B; A a; int t; public: D (int I, int j, int k ): a (I), B (j), t (k) {cout <"D ";}~ D () {cout <"~ D ";}}; int main () {D d (1, 2, 3); C * p = & d; delete p; p = NULL; system (" pause "); // return 0 ;}
Output result: CBAD ~ C ~ D ~ A ~ B ~ C
The Calling sequence of constructor: first the base class, then the class constructor of its own member, and finally its own. (First parent class, at the end of the guests)
6,
#include
#include
using namespace std;class A{public:A(int x){cout << "A";a = x;//cout << a << endl;}A(){A(0);}~A(){cout << "~A";}int a;};class B{public:B(int y){cout << "B";y = b;}~B(){cout << "~B";}int b;};class C{public:C(int aa=0){cout << "C";a = aa;}~C(){cout << "~C" << a << endl;;}int a;};class D: public C{private:public:D(int aa = 0, int bb = 0) :C(aa), b(bb){cout << "D";}~D(){cout << "~D" << b << endl;}int b;};void test(){D X(5);D Y(6, 7);}int main(){ A obj1;cout << obj1.a << endl;A obj(19);cout << obj.a << endl;test();system("pause");return 0;}
Output result: ~ A-858993460
A19 CDCD ~ D7 ~ C6 ~ D0 ~ C5
When A Class A object is created, if there is no parameter, an uncertain value is output. If there is A parameter function, the value of the variable depends on the shape parameter.
4. Relationship between Template functions and function templates and function Overloading
Template
T function name (T-parameter 1, T-parameter 2 ,.....){}
Template functions are a special case of function templates. A template function has the same name and number of parameters. The difference is only the parameter type.
According to the definition of function overload, you can use the function template to implement this function. Otherwise, it is not true.
Another difference between a template function and a function overload is that when a function is overloaded, each function can execute different actions. However, after the function template is instantiated, the same action must be executed for the template function.
5. The nature of private members, protected members, and public members in a derived class
6. Functions of constructor, destructor, and friend functions in the class
Constructor is mainly used to solve the problem of object initialization. constructor is a special type of function. Unlike other member functions, constructor does not need to be called by users, instead, it is automatically executed when an object is created.
The Destructor is used to clear the object, such as releasing the memory.
You can reduce system overhead and improve efficiency when sharing data between classes. That is, you can enable member functions of other classes to directly access private variables and protection variables of these classes, to achieve data sharing.
Disadvantages of using a friend function: the friend function breaks down the encapsulation mechanism and tries not to use the friend function.
Youyuan function parameters: Because youyuan function does not have this pointer, there are three parameters:
1. When you want to access non-static members, you need to set parameters for the object. 2. If you want to access static or global variables, you do not need to set parameters for the object. 3. If the global object is used as the parameter object, no parameter is required for the object;
Location of a friend function: because a friend function is a function outside the class, his declaration can be placed in the private or public segment of the class.
You can call functions directly without pointers or objects.
There are three types of user functions: normal functions are user functions, and all functions of class Y are user functions of class X. One function of class Y is user functions of class X.
Differences between membership functions:
The member function has the this pointer, but the member function does not; the member function cannot inherit.
The following two scenarios are often used:
1. Overload operators 2. data sharing between two classes.
8. const is used in C ++. If you use const to modify the shape parameters of a function, what is the difference between const to modify variables, modify pointers, and modify references?
9. The role of static in C ++.
10. Differences between pointers and references.
11. What is the role of indexes in the database?
12. What is the difference between left join and right join?
13. Joint database search?
14. What is the difference between vector and LIST?
15. How can we establish, traverse, and reverse Binary Trees to balance Binary Trees and red/Black Binary Trees?
16. Common sorting algorithms
17. What is the size of sizeof pointer and sizeof pointer?
18. How do I store images?
19. Can static member functions access non-static member functions?
20. What is the difference between heavy load and rewriting?
1. Overload occurs in the same scope, that is, the same class.
2. The function name is the same, but the parameter (indicating the number, type, and order of parameters must be different.
** The return value type of the overload function can be the same or different. You cannot determine whether a function is an overloaded function based on the type of the returned value. Because only different return value types cannot constitute a function overload, a redefinition error may occur.
** However, with one exception, you can use the const keyword to reload a function to convert a member function into a const function.
For example:
String greeting ()
{
} And
String greeting () const
{
}
Can constitute a function overload. The call mechanism is to call the const member function if the defined object is a constant object. If the defined object is a very object, it calls a non-const member function.
However:
String greeting () const
{
} And
Const string greeting () const
{
}
It cannot constitute a function overload. Only the return value types are different.
** Int f (int *){}
Int f (int const *) {} can constitute a function overload.
But int f (int *){}
Int f (int * const) {} cannot constitute a function overload. That is to say, the function overload cannot be implemented based on whether the pointer itself is const.
(2) rewrite is also called overwrite.
Subclass redefines virtual functions with the same name and parameters in the parent class. Function features are the same. However, different implementations mainly occur in the inheritance relationship.
(1) different scopes (located in the derived class and the base class respectively );
(2) functions with the same name and parameters;
(3) basic functions must have virtual keywords;
(3) Hide (also called redefinition)
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. In this case, the function of the base class is hidden (note not to confuse with overwrite) and overwrite when vitual exists. (The overwritten method has polymorphism and is hidden)