This question is only used to examine the basic programming skills of C ++/C programmers. The content is limited to common C ++/C syntaxes and does not involve data structures, algorithms, and esoteric syntaxes. The test score reflects the programming quality and understanding of C ++/C, but does not reflect the examinee's intelligence and software development ability. The written examination time is 90 minutes. Please answer questions carefully and never underestimate them. PS: If you want to see the data structure and common algorithms, you can refer to my Google Code project ADT-in-C Project.
1. The following is a 32-bit C ++ program in Windows NT. Calculate the value of sizeof (10 points)
Char S1 [] = ""; char S2 [] = "Hello world! "; Char * P = S2; char * q = NULL; void * r = malloc (100); calculate: sizeof (S1) = sizeof (S2) = sizeof (P) = sizeof (q) = sizeof (R) = |
Char S1 [10] = {'M', 'O', 'B', 'I', 'L'}; char S2 [20] = {'A ', 'N','s, 'I,' \ 0', 'C', '+', '+ '}; char S3 [6] = {'I','s ', 'O', 'C',' + ',' + '}; Calculate strlen (S1) = strlen (S2) = strlen (S3) = S2 [8] = |
|
Void func (char STR [100]) {calculate: sizeof (STR) =} |
2. Enter the if statement for bool, float, pointer variable comparison with "zero value. (10 points)Tip: here, the "zero value" can be 0, 0.0, false, or "Null Pointer ". For example, the IF statement for int Variable N and "zero value" is: If (n = 0) if (n! = 0) and so on.
Write the if statement for comparing bool flag with zero value: |
Write the if statement for comparing float X with "zero value: |
Write the if statement for comparing char * P with "zero value: |
Iii. Short answer (25 points)1. What is the use of ifndef/define/endif in the header file? 2. What are the differences between # include <FILENAME. h> and # include "filename. H? 3. What is the purpose of const? (There are at least two types.) 4. Why should I add the extern "C" declaration to call the function compiled by the C compiler in the C ++ program? 5. Briefly describe the advantages and disadvantages of the following two for Loops
// The first for (I = 0; I <n; I ++) {If (condition) dosomething (); else dootherthing ();} |
// The second if (condition) {for (I = 0; I <n; I ++) dosomething () ;}else {for (I = 0; I <N; I ++) dootherthing ();} |
Advantages: disadvantages: |
Advantages: disadvantages: |
4. Questions about memory (20 points)
Void getmemory (char * P) {P = (char *) malloc (100);} void test (void) {char * STR = NULL; getmemory (STR ); strcpy (STR, "Hello World"); printf (STR) ;}what are the results of running the test function? A: |
Char * getmemory (void) {char P [] = "Hello World"; return P;} void test (void) {char * STR = NULL; STR = getmemory (); printf (STR);} What is the result of running the test function? A: |
Void getmemory2 (char ** P, int num) {* P = (char *) malloc (Num);} void test (void) {char * STR = NULL; getmemory (& STR, 100); strcpy (STR, "hello"); printf (STR);} What is the result of running the test function? A: |
Void test (void) {char * STR = (char *) malloc (100); strcpy (STR, "hello"); free (STR); If (STR! = NULL) {strcpy (STR, "world"); printf (STR) ;}} what is the result of running the test function? A: |
5. type conversion. (5 points)Double D = 100.25; int x = D; int * pint = (int *) & D; are the results of the following two output statements the same? Why? Cout <x <Endl; cout <* pint <Endl; PS: For more information, see Baidu interview questions.
6. Compile the strcpy function (10 points)It is known that the prototype of the strcpy function is char * strcpy (char * strdest, const char * strsrc), where strdest is the destination string, and strsrc is the source string. (1) do not call the string library function of C ++/C. Write the strcpy function (2) strcpy to copy the content of strsrc to strdest. Why should I return a char * type? PS: for the extension of this question, refer to the commonly used string function library to implement the blog.
7. Compile string-like constructor, destructor, and assignment function (25 points)The prototype of the known class string is: Class string {public: string (const char * STR = NULL); // string (const string & Other), a common constructor ); // copy the constructor ~ String (void); // destructor string & operate = (const string & other); // value assignment function PRIVATE: char * m_data; // used to save the string }; compile the preceding four functions of string. PS: the answer is visible. O (distinct _ distinct) O ~