CC ++ test questions

zhaozj2021-02-16  60

C / C test questions

This test is only used to examine the basic programming skills of C / C programmers. The content is limited to C / C common grammar, does not involve data structures, algorithms, and deep syntax. The test scores reflect the quality of the programming and the degree of understanding of C / C, but can not reflect the intelligence and software development capabilities of candidates. The written test time is 90 minutes. Please ask the candidates to carefully answer questions. First, please fill in the IF statement of BOOL, FLOAT, pointer variables and "zero" comparison. (10 points) Tip: Here "zero" can be 0, 0.0, false or "empty pointer". For example INT variable N and "zero value" comparison IF statement is: if (n == 0) IF (n! = 0) is pushed.

Please write the IF statement of BOOL FLAG and "Zero" comparison: Please write the IF statement of Float X and "Zero" comparison: Please write a CHAR * P and "Zero" comparison if statement: II, below 32-bit C programs under Windows NT, calculate the value of SizeOf (10 points) char str [] = "Hello"; char * p = str; int N = 10; please calculate SIZEOF (STR) = SizeOf (p) = SIZEOF (N) = Void Func (CHAR STR [100]) {please calculate SIZEOF (STR) =} void * p = malloc (100); please calculate sizeof (p) = three, brief answer (25 points) 1, head What is IFNDEF / Define / Endif in the file? 2, # include and #include "filename.h" What is the difference? 3, what is the use? (Please explain at least two) 4, call the function compiled by the C program, why do you add an Extern "C" declaration? 5. Please briefly describe the advantages and disadvantages of the following two for cycles // First for (i = 0; i {if (condition) DOSMETHING (); elsedootHing ();} // Second IF (condition) {for (i = 0; i dosomething ();} else {for (i = 0; i dootherhes ();} Advantages: Disadvantages: Advantages: Disadvantages: Self-considering questions (20 points) void getMemory (char * P ) {p = (char *) malloc (100);} void test (void) {char * str = null; getMemory (STR); strcpy (str, "hello world"); printf (str);}, please run Test What kind of result will be? A: char * getMemory (void) {char p [] = "hello world"; returnof;} void test (void) {char * str = null; str = getMemory (); printf (STR);} What kind of results will there be a 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 kind of results will there be a Test function? A: void test (void) {char * str = (char *) Malloc (100); STRCPY (STR, "Hello"); Free (STR); if (str! = null) {struct (str, "world"); Printf (str);}}, please run Test What kind of result will be? A: 5. Writing a strcpy function (10 points) Know the prototype of the strcpy function is char * structest, const char * strsrc); where strDest is the destination string, strsrc is the source String.

(1) Do not call the C / C string library function, write the function strcpy (2) Strcpy can copy the contents of the strs to strDest, why is the return value of the CHAR * type? Sixth, write String constructor, destructor and assignment function (25 points) Known String prototype: class string {public: string (const char * str = null); // Ordinary constructor string (const) String & other); // copy constructor ~ String (void); // Destructor String & Operate = (const string & other); // assignment function private: char * m_data; // Used to save string}; please Write the above four functions of String. *********************************************************** ****************************************** C / C Questions Answers and Rating Standards, please fill in BOOL, FLOAT, pointer variables and "zero" comparison IF statements. (10 points) Please write the IF statement of BOOL FLAG and "Zero" comparison. (3 points) Standard answer: if (flag) if (! Flag) is a bad style, not score. IF (Flag == True) IF (Flag == 1) if (FLAG == FALSE) IF (FLAG == 0) Please write the IF statement of Float X and "Zero" comparison. (4 points) Standard answer example: const float epsinon = 0.00001; if ((x> = - EPSINON) && (x <= epsinon) Do not use "==" or "! =" With digital comparison, should Try to translate into "> =" or "<=" such a form. The following is the wrong way, no score .IF (x == 0.0) if (x! = 0.0) Please write char * p and "zero" Compare IF statement. (3 points) Standard answer: if (p == null) if (p! = Null) is a bad style, no score.

IF (p = 0) IF (p! = 0) IF (p) IF (!) 2, the following is the 32-bit C program under Windows NT, calculate the value of SizeOf (10 points) char STR [] = " Hello "; char * p = str; int N = 10; calculates sizeof (STR) = 6 (2 points) SIZEOF (P) = 4 (2 points) SizeOf (n) = 4 (2 points) Void func (charr) STR [100]) {please calculate SIZEOF (STR) = 4 (2 points)} void * p = malloc (100); please calculate SizeOf (P) = 4 (2 points) three, brief answer (25 points) 1, What is the use of IFNDEF / Define / Endif in the header file? (5 points) Answer: Prevent the header file from being repeatedly referenced. 2, # include and #include "filename.h" What is the difference? (5 points) Answer: For #include, the compiler starts searching for FileName.h from the standard library path For #include "filename.h", the compiler starts searching for filename.h3 from the user's work path, what is used? (Please explain at least two) (5 points) Answer: (1) You can define a Const Constant (2) Const can modify the parameters of the function, return the value, and even the definition body. Things that are modified by const are mandatory, prevent accidents, can improve the robustness of the program. 4. Call the function compiled by the C program, why do you want to add externaln "c"? (5 points) Answer: C language support function overload, C language does not support function overload. The function is compiled by C and the name in the library is different from the C language. Assuming a function of a function is: Void foo (int X, int y); the function is compiled by the C compiler to _foo in the library, while the C compiler produces a name like _foo_int_int. C provides a C-connection exchange specifying symbol EXTERN "C" to solve the name match problem. 5. Please briefly describe the advantages and disadvantages of the following two for cycles (5 points) for (i = 0; i {if (condition) DOSMETHING (); elsedootHing ();} if (condition) {for (i = 0; i DOSMETHING ();} else {for (i = 0; i doothershing ();} Advantages: Program Simple Disadvantages: Multi-1 logical judgment, and interrupt the loop "pipeline" job, make the compiler cannot be The loop is optimized and reduces efficiency.

Advantages: The efficiency of the cycle is high: the program is not simple, the story is related to the memory (5 points for each small question, a total of 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 kind of results will there be a Test function? A: The program crashes. Because getMemory does not pass dynamic memory, the STR in the TEST function has always been NULL. STRCPY (STR, "Hello World"); will make the program crash. Char * getMemory (void) {char p [] = "Hello World"; returnom p;} void test (void) {char * str = null; str = getMemory (); printf (str);}, please ask the Test function What kind of results? A: It may be garbled. Because getMemory returns a pointer to "stack memory", the address of the pointer is not null, but its original content has been cleared, and the new content is unknown. 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 kind of results will there be a Test function? Answer: (1) Output Hello (2) memory leak void test (void) {char * str = (char *) malloc (100); strcpy (Str, "Hello"); Free (STR); if (STR! = NULL) {STRCPY (STR, "World"); Printf (STR);}} What kind of results will there be a Test function? A: Tamper with the content of the dynamic memory area, the consequences are difficult to expect, very dangerous. Because Free (STR); then, the STR becomes a wild pointer, if (str! = Null) statement does not work. 5. Writing a strcpy function (10 points) The prototype of the strcpy function is char * strdest, const char * strsrc); STRDEST is the destination string, and strsrc is a source string. (1) Do not call C / C string function, write function strcpychar * struct (char * strdest, const char * strsrc); {assert ((strDest! = Null) && (strdst! = Null); / / 2 points char * address = strdest; // 2 points while (* strDest = * strsrc )! = '/ 0') // 2 points null; return address; // 2} (2) STRCPY can put strsrc Content Copy to strDest, why is the return value of the type? A: In order to achieve a chain expression.

转载请注明原文地址:https://www.9cbs.com/read-16341.html

New Post(0)