Typical programming questions for famous enterprise interviewers: C ++

Source: Internet
Author: User

Typical programming questions for famous enterprise interviewers: C ++

C ++

Most colleges and universities in China offer C ++ courses, so most programmers have learned C ++. Therefore, C ++ has become the preferred programming language for various companies to interview. Many companies, including Autodesk, have a large number of C ++ syntax questions during interviews. Although other companies do not directly interview C ++ syntax, however, you must use C ++ to implement the algorithm. Therefore, applicants should master C ++ to a certain extent no matter what company they are looking.

Generally, there are three types of language interviews. The first type is the interviewer's direct understanding of the C ++ concept. For this type of questions, the interviewer is particularly fond of understanding the degree of understanding of the C ++ keyword. For example, in C ++, which four keywords are related to type conversion? What are the characteristics of these keywords and where should they be used?

In this type of questions, sizeof is a frequently asked concept. For example, the following interview clips appear repeatedly in the technical interviews of companies.

Interviewer: defines an empty type without any member variables or member functions. Evaluate sizeof for this type. What is the result?

Applicant: the answer is 1.

Interviewer: Why not 0?

Applicant: An empty instance does not contain any information. sizeof should be 0, but when we declare an instance of this type, it must occupy a certain amount of space in the memory, otherwise, these instances cannot be used. The compiler determines how much memory is used. Each empty instance in Visual Studio occupies 1 byte of space.

Interviewer: If you add a constructor and destructor to this type and evaluate sizeof for this type, what is the result?

Applicant: Same as above, or 1. To call constructor and destructor, you only need to know the address of the function. The address of these functions is only related to the type, but not to the type instance, the compiler will not add any additional information to the instance because of these two functions.

Interviewer: What if I mark the Destructor as a virtual function?

Candidate: The C ++ compiler generates a virtual function table for this type once it finds that there is a virtual function in the type, add a pointer to the virtual function table to each instance of this type. On a 32-bit machine, a pointer occupies 4 bytes of space, so sizeof gets 4. For a 64-bit machine, a pointer occupies 8 bytes of space, therefore, obtain 8 for sizeof.

The second type of questions for the C/C ++ interview is that the interviewer uses the prepared code to let the applicant analyze the code running results. This type of question selection Code usually contains complex and subtle language features, which requires the applicant to have a thorough understanding of the C ++ test site. Even if the candidate has a vague score on the test site, the final result may be far different from the actual running result.

For example, the interviewer handed the applicant an A4 print paper with the following code asking him to analyze the compilation results and provide three options:. compilation error; B. compiled successfully. The program crashes during running. C. compile and run properly, and output 10.

Class

{

PRIVATE:

Int value;

 

Public:

A (int n) {value = N ;}

A (a other) {value = Other. value ;}

 

Void print () {STD: cout <value <STD: Endl ;}

};

 

Int _ tmain (INT argc, _ tchar * argv [])

{

AA = 10;

AB =;

B. Print ();

 

Return 0;

}

In the above Code, the input parameter of the copy constructor A (a other) is an instance of. Because it is a value parameter, copying the form parameter to the real parameter will call the copy constructor. Therefore, if you allow the copy constructor to pass values, the copy constructor will be called within the copy constructor, and an endless recursive call will be formed, resulting in stack overflow. Therefore, the C ++ standard does not allow copying constructors to pass value parameters. in Visual Studio and GCC, compilation errors occur. To solve this problem, we can change the constructor to A (const A & Other), that is, to change the value passing parameter to a constant reference.

The third type of question requires the applicant to write code to define a type or implement a member function in the type. It is more difficult for a candidate to write code than to analyze the code, because what he wants to understand may not be clearly written. Many questions about the C ++ syntax are centered on constructor, destructor, and operator overloading. For example, in question 1, "value assignment operator function" is an example.

In order to allow everyone to pass the C ++ interview smoothly, more importantly, they can better learn and master the C ++ programming language. Here we recommend several C ++ books, you can select the reading sequence based on your actual situation:

License tivec ++. This book is suitable for attacking C ++ before the interview. This book lists the frequently encountered problems in C ++ and the skills used to solve these problems. The questions mentioned in this book are also the questions that the interviewer prefers to ask.

C ++ primer. After reading this book, you will have a comprehensive understanding of the C ++ syntax.

Inside C ++ object model. This book helps us gain an in-depth understanding of the interior of C ++ objects. After reading this book, many c ++ problems, such as the sizeof problem and the calling mechanism of virtual functions, will become very easy.

The C ++ programming language. If you want to thoroughly master C ++, no book is more suitable than this book.

Interview Question 1: assignment operator Function

Question: The following is a declaration of the type cmystring. Please add a value assignment operator function for this type.

Class cmystring

{

Public:

Cmystring (char * pdata = NULL );

Cmystring (const cmystring & Str );

~ Cmystring (void );

PRIVATE:

Char * m_pdata;

};

When the interviewer asks the applicant to define a value assignment operator function, he will pay attention to the following points when checking the code written by the applicant:

Whether to declare the type of the returned value as a reference of this type, and return the instance reference (* This) before the function ends ). Continuous value assignment is allowed only when a reference is returned. Otherwise, if the return value of the function is void, consecutive values cannot be assigned when this assignment operator is applied. Suppose there are three cmystring objects: str1, str2, and str3. in the program, the statement str1 = str2 = str3 cannot be compiled.

Whether to declare the input parameter type as a constant reference. If the input parameter is not a reference but an instance, a copy constructor is called from the form parameter to the real parameter. Declaring a parameter as a reference can avoid unnecessary consumption and improve code efficiency. At the same time, we will not change the status of the input instance in the value assignment operator function, so we should add the const keyword to the input reference parameter.

Whether to release the existing memory of the instance. If we forget to release our existing space before allocating new memory, the program will experience memory leakage.

Determines whether the input parameter and the current instance (* This) are the same instance. If it is the same, the value assignment is not performed, and the result is returned directly. If a value is assigned without prior judgment, releasing the instance's memory will cause a serious problem: when * This is the same as the input parameter, once the memory is released, the memory of the input parameter is also released, so the content to be assigned is no longer found.

Classic Solution for beginners

After fully considering the above four aspects, we can write the following code:

Cmystring & cmystring: Operator = (constcmystring & Str)

{

If (this = & Str)

Return * this;

 

Delete [] m_pdata;

M_pdata = NULL;

 

M_pdata = new char [strlen (Str. m_pdata) + 1];

Strcpy (m_pdata, str. m_pdata );

 

Return * this;

}

This is a reference code provided in C ++ textbooks. If the interview is for a fresh graduate or a C ++ junior programmer, the interviewer may ask him to pass the interview if he can fully consider the previous four points and write the code. However, if you are interviewing a C ++ Senior Programmer, the interviewer may ask for higher requirements.

Advanced programmers must consider exception Security Solutions

In the previous function, we used Delete to release the m_pdata memory of the instance before allocating memory. If the new char throws an exception due to insufficient memory at this time, m_pdata will be a null pointer, which can easily cause program crash. That is to say, once an exception is thrown inside the value assignment operator function, the cmystring instance is no longer valid, which violates the exception safety principle.

There are two methods to implement exception security in the value assignment operator function. A simple method is to use new to allocate new content and then delete to release existing content. In this way, only the original content is released after the allocation is successful, that is, when the allocation of memory fails, we can ensure that the instance of cmystring will not be modified. Another better way is to create a temporary instance first, and then switch the temporary instance and the original Instance. The following is a reference code for this idea:

Cmystring & cmystring: Operator = (constcmystring & Str)

{

If (this! = & Str)

{

Cmystring strtemp (STR );

 

Char * ptemp = strtemp. m_pdata;

Strtemp. m_pdata = m_pdata;

M_pdata = ptemp;

}

 

Return * this;

}

In this function, we first create a temporary instance strtemp, and then exchange strtemp. m_pdata with the instance's m_pdata. Strtemp is a local variable, but when the program runs outside the if program, the scope of the variable is exceeded. Then, the strtemp destructor is automatically called to change strtemp. m_pdata points to the memory to be released. Strtemp. m_pdata points to the memory of m_pdata before the instance, which is equivalent to automatically calling the destructor to release the instance memory.

In the new code, we use new to allocate memory in the cmystring constructor. If an exception such as bad_alloc is thrown due to insufficient memory, we have not modified the status of the original Instance, so the instance status is still valid, which guarantees the exception security.

If the applicant can take this level into consideration during the interview, the interviewer will feel that he has a deep understanding of the exceptional security of the code, then he will naturally be able to pass this round of interview.

Source code:

For the complete source code of this question, see the 01_assignmentoperator project.

Test cases:

Assign a cmystring instance to another instance.

Assign a cmystring instance to itself.

Continuous assignment.

Exam point:

Measure the test taker's knowledge about the basic syntax of C ++, such as operator functions and constant references.

Measure the test taker's knowledge about memory leakage.

For senior C ++ programmers, the interviewer will also examine the applicant's understanding of code exception security.

 

 

 

This article is from the book "offoffoffer-famous enterprise interviewer excellent typical programming questions"

Book details: http://blog.csdn.net/broadview2006/article/details/7043805

 

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.