High Quality C ++C Programming Guide (Appendix C: C ++C Questions Answers and Score Standards)

zhaozj2021-02-17  49

Appendix C: Answer and Rating Standard for C / C Test

First, please fill in the IF statement of BOOL, FLOAT, pointer variables and "zero" comparison. (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, no 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 <= epinon)

You should try to translate into "> =" or "<=" in such forms with "==" or "! =" And digital comparisons.

The following is the wrong way of writing, not score. IF (x == 0.0)

IF (x! = 0.0)

Please write the IF statement of char * p and "zero value". (3 points) Standard answer:

IF (p == NULL)

IF (p! = null) is a bad style, not score. IF (p == 0)

IF (p! = 0)

IF (p)

IF (!)

Second, 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 (char STR [100]) {please calculate SIZEOF (STR) = 4 (2 points)

}

Void * p = malloc (100); please calculate SizeOf (P) = 4 (2 points)

Third, a brief answer (25 points)

1. What is the use of IFNDEF / Define / Endif in the header file? (5 points)

A: Prevent the header from being repeatedly referenced.

2, # include

What is the difference between #include "filename.h"? (5 points)

A: For #include

Search FileName.h from the standard library path

For #include "filename.h", the compiler starts searching from the user's work path to search for filename.h

3, what is the use? (Please explain at least two) (5 points)

A: (1) You can define constants

(2) const can modify the parameters, return values, and even the definition body of the function. 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)

A: C language support function overload, the 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. Suppose a function of a function is: Void foo (int x, int y);

This function is compiled by the C compiler to the name _foo, and 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 ();

Else

Doothershing ();

}

IF (condition)

{

For (i = 0; i

DOSMETHING ();

}

Else

{

For (i = 0; i

Doothershing ();

}

Advantages: the program is concise

Disadvantages: N-1 logic judgment, and interrupt the loop "pipeline" job so that the compiler cannot optimize the loop, reducing efficiency.

Advantages: high efficiency of cycles

Disadvantages: the program is not simple

Fourth, the story of 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";

Return P;

}

Void test (void)

{

CHAR * STR = NULL;

Str = getMemory ();

Printf (STR);

}

What kind of results will there be a Test function?

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) Ability to output Hello

(2) Memory leakage

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); after, STR becomes a wild pointer.

IF (Str! = NULL) statement does not work.

Five, write strcpy functions (10 points)

Know 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 adjust the C / C string library function, please write the function strcpy

Char * STRDEST, Const Char * strsrc);

{

Assert (strDest! = null) && (strsrc! = null); // 2 points

Char * address = strDest; // 2 points

While (* strDest = * strsrc )! = '/ 0') // 2 points

NULL;

Return Address; // 2 points

}

(2) STRCPY can copy the contents of strsrc to strDest, why should the return value of the type?

A: In order to achieve a chain expression. // 2 minutes

For example INT length = Strlen (STRDEST, "Hello World");

6. Write the constructor, destructor and assignment function (25 points)

The prototype of the known String is:

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 strings

}

Please write the above four functions of String.

standard answer:

// String destructor

String :: ~ String (void) // 3 points

{

delete [] m_data;

// Since M_DATA is the internal data type, it is also possible to write a delete m_data;

}

// String ordinary constructor

String :: string (const char * STR) / / 6 points

{

IF (str == NULL)

{

m_data = new char [1]; // If you can add NULL to determine, it is better.

* m_data = '/ 0';

}

Else

{

INT length = Strlen (STR);

m_data = new char [length 1]; // If you can add NULL to judge, better STRCPY (M_Data, STR);

}

}

// Copy constructor

String :: string (const string & other) // 3 points

{

INT length = strlen (other.m_data);

m_data = new char [length 1]; // If you can add NULL, it is better.

STRCPY (m_data, other.m_data);

}

// Assignment function

String & string :: Operate = (const string & other) // 13 points

{

/ / (1) Check self-assignment // 4 points

IF (this == & other)

RETURN * THIS;

// (2) Release the original memory resources // 3 points

delete [] m_data;

/ / (3) Assign new memory resources and copy content / 3 points

INT length = strlen (other.m_data);

m_data = new char [length 1]; // If you can add NULL, it is better.

STRCPY (m_data, other.m_data);

/ / (4) Return to the reference / 3 points of this object

RETURN * THIS;

}

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

New Post(0)