Summary of C++ Interview Knowledge Points

Summary of C++ Interview Knowledge Points
Basic Concepts of Program Design

1int i=1;

void main(){

inti=i; //i is an undefined value

}

2、Type Conversion

C++ defines a set of standard conversions between built-in type objects:

  • If one operand’s type is long double, the other operand will be converted to long double

  • If neither is long double, if one is double, the other will be converted to double

  • Similarly, if one is float, the others will be converted to float

  • If neither operand is one of the 3 floating-point types, they must be some integer type, and before determining a common target promotion type, the compiler will apply a process called integral promotion to all integer types smaller than int.

3、Operator Issues

1) unsigned char a=0xA5;

uUnsignedchar b=~a>>4+1;

cout<<b; //The result is250, because the register is16 bits

2) Using an expression to determine if a numberX is a power of 2N, without using loops

!(X&(X-1))

3) The following code:

return(x&y)+((x^y)>>1) (729,271)=500

4)Without using if or other conditional statements, find the larger of two numbers

int max=((a+b)+abs(a-b))/2

5)How to swap two values without using any intermediate variables

a=a^b; b=a^b; a=a^b;

4C and C++ Relationship

1)In C++ programs, when calling functions compiled by the C compiler, why use extern “C”

C++ provides a linkage specification to resolve name mangling issues

2)Header file #ifndef/#define/#endif

Prevents the header file from being included multiple times

Summary of C++ Interview Knowledge Points
Preprocessing,const and sizeof

1、Macro Definition

1)Use a macro definition FIND to find the offset of a variable in a structure struc relative to struc:

#define FIND(struc,e) (size_t)&(((struc*)0)->e

Summary of C++ Interview Knowledge Points
Pointers and References

1class A{ public: int a; A(){ a=1;} void print(){cout<<a;};

class B:public A{public:int a;B(){ a=2 ;}};

B b; b.print(); cout<<b.a; //The result is1 2

2、Function Pointers

1) float(**def)[10] def is a double pointer pointing to a pointer of a one-dimensional array, the array elements are float

2) double*(*gh)[10] gh is a pointer pointing to a one-dimensional array, the array elements are all double*

3) double(*f[10])() Function pointer array, f is an array whose elements are function pointers

4) int*((*b)[10]) Pointer to a one-dimensional array

5) Long (*fun)(int) Function pointer

6) Int(*(*F)(int,int))(int) F is a pointer to a function whose type has two int parameters and returns a function pointer, the returned function pointer points to a function with one int parameter and returns int.

7) int(*ptr)[]Pointer to an integer array

8) int *ptr[] Pointer array

9) int(*a[10])(int)An array of 10 pointers, each pointing to a function that takes an integer parameter and returns an integer value

3、Pointer Arrays and Array Pointers

1)int a[]={1,2,3,4,5}

int*ptr=(int *)(&a+1) //&a+1 points to the end of the array

cout<<*(ptr-1); //Outputs5

2) C++ has malloc/free, why still neednew/delete

For non-internal data type objects, using only malloc/free cannot meet the requirements of dynamic objects. Objects need to automatically execute constructors when created and destructors before they are destroyed. Since malloc/free are library functions and not operators, they cannot impose the execution of constructors and destructors. Therefore, the C++ language needs an operator new that can perform dynamic memory allocation and initialization, and an operator delete that can perform cleanup and memory release.

4、Pointers and Handles

1)What is the difference and connection between handles and pointers??

A handle is a pointer to a pointer

Handles and pointers are actually two completely different concepts. The Windows system uses handles to mark system resources, hiding system information. You just need to know that this thing exists and call it; it is a 32bit unit. A pointer marks a specific physical memory address; the two are different concepts.

Summary of C++ Interview Knowledge Points
STL Templates and Containers

1、Generic Programming

1)What is generic programming?

STL represents that it is possible to program in a consistent manner. It is not trying to program with C++, but rather attempting to find a correct way to handle software, seeking a language that can express our ideas. Generic programming is a programming method based on discovering the most abstract representation of efficient algorithms, meaning starting with algorithms and finding the most general necessary conditions that make them work efficiently, which can have multiple different implementations.

Summary of C++ Interview Knowledge Points
Inheritance and Interfaces

1、What is the output of the following code?

class A{ protected: int m_data; public:A(int data=0){m_data=data;

int GetData(){return doGetData();} virtual int doGetData(){return m_data;}};

class B: public A{ protected: int m_data; public:B(int data=1){m_data=data;

int doGetData(){return m_data;}};

class C: public B{ protected: int m_data; public:C(int data=2){m_data=data;}}

c.GetData() C is undefined, callsB,B is undefined, callsA. A is virtual, callsB.

c.A::GetData()A is virtual,C is undefined, callsB.

c.A::doGetData()—directly callsA.

2、Three Inheritance Types

Public Inheritance: Public members and protected members of the base class are visible, and they maintain their original state when inherited as members of the derived class; private members of the base class are not visible and remain private. In public inheritance, objects of the derived class can access public members of the base class, and member functions of the derived class can access public and protected members of the base class.

Private Inheritance: Public and protected members of the base class are inherited as private members of the derived class and cannot be accessed by subclasses of this derived class.

Protected Inheritance: Public and protected members of the base class are inherited as protected members of the derived class.

Example: class Parent{public: Parent(int var=-1){m_nPub=var;m_nptd=var;m_nptr=var;}

public:int m_nPub; protected: int m_nptd; private: int m_nPtr;};

class Child1: public Parent{public: int GetPub(){return m_nPub;};

int GetPtd(){return m_nPtd;}; int GetPtr(){return m_nPtr;};};

class Child2: protected Parent{public: int GetPub(){return m_nPub;};

int GetPtd(){return m_nPtd;}; int GetPtr(){return m_nPtr;};};

class Child3: private Parent{public: int GetPub(){return m_nPub;};

int GetPtd(){return m_nPtd;}; int GetPtr(){return m_nPtr;};};

Child1 cd1; Child2 cd2; Child3 cd3; int nVar=0;

cd1.m_nPub=nVar;

cd1.m_nPub=nVar; //Base class protected variable can be accessed by public inherited derived class object but cannot be modified

nVar=cd1.GetPtd();

cd2.m_nPub=nVar;//Protected inherited derived class object cannot directly modify base class public variable

nVar=cd2.GetPtd();

cd3.m_nPub=nVar;

nVar=cd3.GetPtd();//Private inherited derived class object can access base class protected variable through function

Summary of C++ Interview Knowledge Points
Bitwise Operations and Embedded Programming

1、What is the output of the following union program?

union{ char a; int i;} u; u.i=0xf0f1f2f3;

cout<<hex<<u.i; //f0f1f2f3

cout<<hex<<int(u.a); //f3Memory stores low-order bytes at low addresses, data addresses are represented by low addresses

2、Write the output of the program

int *pa=NULL; int *pb=pa+15;

printf(“%x”,pb);//15*4=60,60 in hexadecimal is 3C

3、Embedded Programming

Interrupt Service Routine(ISR): ISR cannot return a value; ISR cannot pass parameters; ISR should be short and efficient; doing floating point operations in ISR is unwise; generally, printf() is not used in ISR.

4、What does the keywordvolatile mean? Can a parameter be bothconst andvolatile? Can a pointer bevolatile?

Defined as volatile, it means this variable may be changed unexpectedly, so the compiler won’t assume this variable’s value, and the optimizer must carefully re-read this variable’s value each time it is used instead of using a backup stored in a register.

It can be both const and volatile, an example being a read-only register

It can be a pointer, an example being when an interrupt service routine modifies a pointer to a buffer.

5、Set a value at an absolute address

Int *ptr; ptr=(int*)0x67a9; *ptr=0xaa55;

6、unsigned int compzero=0xFFFF;//This is incorrect, should beunsigned int compzero=~0;

7、char *ptr; if((ptr=(char*)malloc(0))==NULL) cout<<“null”; else cout<<“valid”;//Valid

8、What is the result of this C program in a little-endian system?

struct bitstruct{ int b1:5; int: 2; int b2:2;} bitstruct;

bitstruct b; memcpy(&b,”EMC EXAMINATION”,sizeof(b)); cout<<b.b1<<b.b2;

E and M have ASCII values of 0x45 and 0x4D, 1010 0010 1011 0010, bitstruct is 9 bits, b in memory looks like: 1010 0010 1, b1 occupies 5 bits: 1010 0, skipping two bits 01 the result is 5 and -2

Summary of C++ Interview Knowledge Points
Design Patterns and Software Testing

1、Describe the chicken or egg problem from the perspective of design patterns.

The chicken or egg problem is merely a misunderstanding of the essence of object-oriented programming; it is inappropriate to apply human natural language to this understanding. There is fundamentally no chicken or egg problem, but rather a matter of type and value, and pointer reference, as the two objects (chicken and egg) are in a reference relationship rather than a containment relationship.

2、Describe the differences between crayons and brushes using design patterns.

This is a Bridge pattern. To create a painting, 36 different crayons are needed, whereas only 3 brushes suffice. This pattern allows multiplication operations to be transformed into addition operations. The purpose of this pattern is to decouple abstraction from implementation so that both can vary independently. The Bridge pattern transforms inheritance relationships into composition relationships, thereby reducing coupling between systems.

3、Observer Pattern

The observer pattern defines a one-to-many dependency between objects, allowing multiple observer objects to listen to a subject. When the state of the subject changes, it notifies all observer objects, allowing them to automatically update themselves.

4、Software Testing

  • Describe a criterion for the end of testing:A standard for ending testing can be that all submitted bugs have been resolved and verified closed, generally with a bug verification rate above 95%, and no critical bugs affecting functionality are in unresolved status, then testing can be considered passed.

  • What can be included in a test plan

It can include the characteristics and main functional modules of the product to be tested, a list of functions to be tested, and indicate the focus; testing strategies and records; testing resource allocation.

  • Describe the difference between functional testing and usability testing

Functional testing is primarily black-box testing conducted by testers, mainly verifying whether the product meets the requirements and design specifications; usability testing is primarily conducted by users, mainly testing the product’s ease of use, including usefulness, efficiency, and user satisfaction. Effectiveness refers to the correctness and completeness of users completing specific tasks and achieving specific goals; efficiency refers to the ratio of resources used to the correctness and completeness of task completion; satisfaction refers to the subjective satisfaction and acceptance level felt by users during product use.

  • White-box testing testing methods include code inspection, static structure analysis, static quality measurement, logical coverage, basic path testing, etc.

  • Basic path testing steps: Draw the control flow graph of the program; calculate the cyclomatic complexity; derive test cases; prepare test cases to ensure the execution of each path in the basic path set.

Summary of C++ Interview Knowledge Points

Editor: Zhao Yingfei

Reviewer: Xia Huanghuang

Leave a Comment