In-depth analysis of common interview questions for C ++ programmers 3

Source: Internet
Author: User
C ++ programmers apply for common interview questions in-depth analysis 3-Linux general technology-Linux technology and application information, the following is to read details. Question 5: Write a function to shift n loops of a char string to the right. For example, if "abcdefghi" is n = 2, it should be "hiabcdefgh" after the shift"

The function header is as follows:

// PStr is a pointer to a string ending with '\ 0'
// Steps is the n that requires moving

Void LoopMove (char * pStr, int steps)
{
// Fill in...
}
Answer:

Answer 1:

Void LoopMove (char * pStr, int steps)
{
Int n = strlen (pStr)-steps;
Char tmp [MAX_LEN];
Strcpy (tmp, pStr + n );
Strcpy (tmp + steps, pStr );
* (Tmp + strlen (pStr) = '\ 0 ';
Strcpy (pStr, tmp );
}
Correct answer 2:

Void LoopMove (char * pStr, int steps)
{
Int n = strlen (pStr)-steps;
Char tmp [MAX_LEN];
Memcpy (tmp, pStr + n, steps );
Memcpy (pStr + steps, pStr, n );
Memcpy (pStr, tmp, steps );
}
Analysis:

This question mainly examines the interviewer's proficiency in the standard library functions. When necessary, referencing the library functions can greatly simplify the workload of programming.

The most frequently used library functions include:

(1) strcpy

(2) memcpy

(3) memset

Question 6: The following table lists the known WAV file formats. Open a WAV file, organize the WAV file header in an appropriate data structure, and parse the information in WAV format.

WAVE file format description


Offset address byte data type capacity
File Header
00 H 4 Char "RIFF" flag
04 H 4 int32 file length
08 H 4 Char "WAVE" flag
0CH 4 Char "fmt" flag
10 H 4 transition byte (optional)
14 H 2 int16 format category
16 H 2 int16 Channels
18 H 2 int16 sampling rate (number of samples per second), indicating the playback speed of each channel
1CH 4 int32 waveform audio data transmission rate
20 H 2 int16 data block adjustment (in bytes)
22 H 2 Data digits per sample
24 H 4 Char data tag "data"
28 H 4 int32 Voice Data Length

Answer:

The WAV file format is defined as the structure WAVEFORMAT:

Typedef struct tagWaveFormat
{
Char cRiffFlag [4];
UIN32 nFileLen;
Char cWaveFlag [4];
Char cFmtFlag [4];
Char cTransition [4];
UIN16 nFormatTag;
UIN16 nChannels;
UIN16 nSamplesPerSec;
UIN32 nAvgBytesperSec;
UIN16 nBlockAlign;
UIN16 nBitNumPerSample;
Char cDataFlag [4];
UIN16 nAudioLength;

} WAVEFORMAT;
Assume that the WAV file content is read and stored in the memory unit starting with the pointer buffer, the code for analyzing the file format is very simple:

WAVEFORMAT waveFormat;
Memcpy (& waveFormat, buffer, sizeof (WAVEFORMAT ));
You can directly access the members of The waveFormat to obtain the format information of a specific WAV file.

Analysis:

Exam 6 examines the ability of the interviewer to organize data structures. experienced programmers organize a whole data member into a struct using pointer type conversion, you can use functions such as memcpy and memset to directly use the struct address for the overall operation of the struct. Through this question, we can see whether the interviewer has rich experience in programming.

Question 7: Compile the constructor, destructor, and value assignment functions of the String class. It is known that the prototype of the String class is:

Class String
{
Public:
String (const char * str = NULL); // common Constructor
String (const String & other); // copy the constructor
~ String (void); // destructor
String & operate = (const String & other); // value assignment function
Private:
Char * m_data; // used to save strings
};
Answer:

// Common Constructor

String: String (const char * str)
{
If (str = NULL)
{
M_data = new char [1]; // score point: An empty string is automatically applied for storing the end sign '\ 0'.
// Extra points: determines if m_data is added with NULL
* M_data = '\ 0 ';
}
Else
{
Int length = strlen (str );
M_data = new char [length + 1]; // It is better if NULL can be added.
Strcpy (m_data, str );
}
}

// String destructor

String ::~ String (void)
{
Delete [] m_data; // or delete m_data;
}

// Copy the constructor

String: String (const String & other) // score point: the input parameter is of the const type.
{
Int length = strlen (other. m_data );
M_data = new char [length + 1]; // points for adding NULL to m_data
Strcpy (m_data, other. m_data );
}

// Value assignment function

String & String: operate = (const String & other) // score point: the input parameter is of the const type.
{
If (this = & other) // score point: Check auto-assigned values
Return * this;
Delete [] m_data; // score point: Release the original memory resource
Int length = strlen (other. m_data );
M_data = new char [length + 1]; // points for adding NULL to m_data
Strcpy (m_data, other. m_data );
Return * this; // score point: return the reference of this object
}
Analysis:

The interviewer who can accurately compile String class constructor, copy constructor, assign value function and destructor has at least 60% of the basic C ++ skills!

This class includes the pointer class member variable m_data. When the class includes pointer class member variables, you must reload the copy constructor, value assignment function, and destructor, this is not only a basic requirement for C ++ programmers, but also a special clause in Objective C ++.

Take a closer look at this class and pay special attention to the significance of adding comments to the score points and points. In this way, more than 60% of the basic C ++ skills are available!

Question 8: Tell me as many static and const keywords as possible

Answer:

The static keyword has at least n functions:

(1) The static variable in the function body applies to this function body. Unlike the auto variable, the memory of this variable is allocated only once, therefore, the value remains the value of the previous time during the next call;

(2) The static global variables in the module can be accessed by the functions used in the module, but cannot be accessed by other functions outside the module;

(3) The static function in the module can only be called by other functions in the module. The scope of use of this function is limited to the module that declares it;

(4) static member variables in the class belong to the entire class and only one copy of all objects in the class;

(5) The static member function in the class belongs to the whole class. this function does not receive the this pointer, so it can only be a static member variable of the category.

The const keyword has at least n functions:

(1) to prevent a variable from being changed, you can use the const keyword. When defining the const variable, you usually need to initialize it, because there will be no chance to change it in the future;

(2) For pointers, you can specify the pointer itself as const, or you can specify the data referred to by the pointer As const, or both of them as const;

(3) In a function declaration, const can modify the form parameter, indicating that it is an input parameter and cannot change its value within the function;

(4) If the member function of the class is specified as the const type, it indicates that it is a constant function and the member variables of the class cannot be modified;

(5) For a member function of the class, you must specify its return value as the const type so that its return value is not the "Left value ". For example:

Const classA operator * (const classA & a1, const classA & a2 );
The return result of operator * must be a const object. If not, such abnormal code will not cause compilation errors:

ClassA a, B, c;
(A * B) = c; // assign a value to the result of a * B
Operation (a * B) = c obviously does not conform to the programmer's original intention, and does not make any sense.

Analysis:

Surprised? What are the functions of small static and const? If you can only answer 1 ~ 2. You have to close the door and practice well.

This question can be used to check whether the subject's knowledge about programming is elementary, intermediate, or in-depth. Without a certain knowledge breadth and depth, it is impossible to give a comprehensive answer to this question. Most people can only answer some functions of the static and const keywords.

4. Tips

Question 1: Write a c function. If the processor is Big_endian, 0 is returned. If the processor is Little_endian, 1 is returned.

Answer:

Int checkCPU ()
{
{
Union w
{
Int;
Char B;
} C;
C. a = 1;
Return (c. B = 1 );
}
}
Analysis:

Embedded system developers should be familiar with the Little-endian and Big-endian modes. In Little-endian mode, the number of CPUs is stored in bytes from low to high, while in Big-endian mode, the number is stored in bytes from high to low. For example, the storage method of 16-bit 0x1234 in Little-endian mode CPU memory (assuming that it starts from address 0x4000) is:

Memory Address storage content
0x4000 0x34
0x4001 0x12

In Big-endian mode, the CPU memory is stored as follows:

Memory Address storage content
0x4000 0x12
0x4001 0x34

32-bit-width 0x12345678 storage method in the Little-endian mode CPU memory (assuming it starts from address 0x4000:

Memory Address storage content
0x4000 0x78
0x4001 0x56
0x4002 0x34
0x4003 0x12

In Big-endian mode, the CPU memory is stored as follows:

Memory Address storage content
0x4000 0x12
0x4001 0x34
0x4002 0x56
0x4003 0x78

The storage order of union is that all members are stored from the low address. The interviewer's answer uses this feature to easily obtain the CPU reads and writes to the memory in the Little-endian or Big-endian mode. If anyone could give this answer on the spot, it would be a talented programmer.

Question 2: Write a function and return 1 + 2 + 3 +... + N value (assuming that the result does not exceed the range of long integer variables)

Answer:

Int Sum (int n)
{
Return (long) 1 + n) * n/2; // or return (1l + n) * n/2;
}
Analysis:
 
The simplest answer is the best one. The following answers, or optimization based on the following solutions, cannot be compared with direct return (1 l + n) * n/2 no matter how hard it is!

Int Sum (int n)
{
Long sum = 0;
For (int I = 1; I <= n; I ++)
{
Sum + = I;
}
Return sum;
}
So programmers need to use mathematics and other knowledge in programming with sensitivity.
Related Article

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.