C/C ++ test questions (Lin Rui)

Source: Internet
Author: User
This question is only used to test C ++/CProgramBasic programming skills. The content is limited to common C ++/C syntaxes and does not involve data structures,AlgorithmAnd profound syntax. 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. 1. Enter the if statement for bool, float, and comparison between pointer variables and "zero value. (10 points) Tip: here, the "zero value" can be 0, 0.0, false, or "Null Pointer ". For example, the IF statement for the comparison between 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: II. The following is a 32-bit C ++ program in Windows NT. Calculate the value of sizeof (10 points)
Char STR [] = "hello ";
Char * P = STR;
Int n = 10; Calculate
Sizeof (STR) =
Sizeof (p) =
Sizeof (n) = Void func (char STR [1, 100])
{
Calculate
Sizeof (STR) =
} Void * P = malloc (100 );
Calculate
Sizeof (p) = 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? (Please specify 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 ();
} // 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 are the results 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 are the results 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 are the results of running the test function?
A: 5. Compile the strcpy function (10 points)
It is known that the prototype of the strcpy function is char * strcpy (char * strdest, const char * strsrc );
Here, strdest is the destination string and strsrc is the source string.
(1) do not call the string library functions of C ++/C. Compile the strcpy function. (2) Why should strcpy copy the content of strsrc to strdest? 6. 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); // 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
}; Compile the preceding four functions of string. Stubborn comment on
# Re: C ++ interview question reply
Appendix C: answers and scoring criteria for C ++/C questions 1. Enter the if statement for bool, float, and comparison between pointer variables and "zero value. (10 points) Write the if statement for comparing bool flag with zero value. (3 points)
Standard answer: If (FLAG) if (! Flag.
If (flag = true) If (flag = 1) If (flag = false) If (flag = 0) Write the if statement for comparing float X with "zero value. (4 points)
Standard answer example:
Const float epsinon = 0.00001; If (x> =-epsinon) & (x <= epsinon) Do not use "=" or "! = "To the number, you should try to convert it into"> = "or" <=.
The following is an incorrect statement without scoring.
If (x = 0.0) If (X! = 0.0) Write the if statement for comparing char * P with "zero value. (3 points)
Standard answer:
If (P = NULL) If (P! = NULL)
The following statement is a poor style, with no score.
If (P = 0) If (P! = 0) If (P) If (!) II. The following is a 32-bit C ++ program in Windows NT. Calculate the value of sizeof (10 points) Char STR [] = "hello ";
Char * P = STR; Int n = 10; Calculate Sizeof (STR) = 6 (2 points) Sizeof (p) = 4 (2 points) Sizeof (n) = 4 (2 points)
Void func (char STR [1, 100])
{ Calculate sizeof (STR) = 4 (2 points) } Void * P = malloc (100 );
Calculate sizeof (p) = 4 (2 points) Iii. Short answer (25 points) 1. What is the use of ifndef/define/endif in the header file? (5 points) A: prevent this header file from being repeatedly referenced. 2. What are the differences between # include <FILENAME. h> and # include "filename. H? (5 points) A: For # include <FILENAME. h>, the compiler searches for filename. h from the standard library path. For # include "filename. H", the compiler searches for filename. h from the user's working path. 3. What is the purpose of const? (Please specify at least two types) (5 points) A: (1) const constants can be defined. (2) const can modify function parameters, return values, and even function definitions. All things modified by const are strongly affected. Protection can prevent unexpected changes and improve program robustness. 4. Why should I add the extern "C" to the function called by the C compiler after being compiled in the C ++ program "? (5 points) A: function Overloading is supported in C ++ and function Overloading is not supported in C. The name of the function in the library after being compiled by C ++ is different from that in the C Language . Assume that the prototype of a function is void Foo (int x, int y). The name of the function in the library after being compiled by the C compiler. Is _ Foo, while the C ++ compiler generates names such as _ foo_int_int. C ++ provides a C connection to exchange specified symbols. Extern "C" to solve the name matching problem. 5. Briefly describe the advantages and disadvantages of the following two for loops (5 points) For (I = 0; I <n; I ++)
{ If (condition) Dosomething (); Else Dootherthing (); }
If (condition)
{ For (I = 0; I <n; I ++) Dosomething (); } Else { For (I = 0; I <n; I ++) Dootherthing (); } Advantage: simple program
Disadvantages: execute more N-1 logic judgment, and interrupt the loop "Pipeline" job, so that the compiler can not optimize the loop, reduce the efficiency.
Advantage: High Cycle Efficiency
Disadvantage: The program is not concise 4. Questions about memory (5 points for every question, 20 points in total) 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: The program crashes. Because getmemory cannot Transmit dynamic memory, STR 1 in the test function It is always null. Strcpy (STR, "Hello World "); Will crash the program.
Char * getmemory (void)
{ Char P [] = "Hello World "; Return P; } Void test (void) { Char * STR = NULL; STR = getmemory (); Printf (STR ); } What are the results of running the test function? A: It may be garbled. Because getmemory returns Is a pointer to the "stack memory", the address of the pointer It is not null, but its original content has been cleared Except, 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 are the results of running the test function? Answer: (1) 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 are the results of running the test function? A: It is difficult to tamper with the content in the dynamic memory zone. Expected, very dangerous. Because free (STR); after STR becomes a wild pointer, if (STR! = NULL) Statement Does not work. 5. Compile the strcpy function (10 points) It is known that the prototype of the strcpy function is char * strcpy (char * strdest, const char * strsrc ); Here, strdest is the destination string and strsrc is the source string. (1) do not call the string library functions of C ++/C. Write the functions. Strcpy char * strcpy (char * strdest, const char * strsrc ); { Assert (strdest! = NULL) & (strsrc! = NULL); // 2 points Char * address = strdest; // 2 points While (* strdest ++ = * strsrc ++ )! = '') // 2: NULL; Return address; // 2 points } (2) Why should strcpy copy the content of strsrc to strdest? A: To implement a chained expression. // 2 points For example, int length = strlen (strcpy (strdest, "Hello World ")); 6. 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); // 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 }; Compile the preceding four functions of string. Standard answer: // String destructor String ::~ String (void) // 3 points { Delete [] m_data; // because m_data is an internal data type, you can also write it as delete m_data; } // String Constructor String: string (const char * Str) // 6 points { If (STR = NULL) { M_data = new char [1]; // It is better if null can be added * m_data = ''; } Else { Int length = strlen (STR ); M_data = new char [Length + 1]; // It is better if null can be added. Strcpy (m_data, STR ); } } // Copy the constructor String: string (const string & Other) // 3 points { Int length = strlen (other. m_data ); M_data = new char [Length + 1]; // It is better if null can be added. Strcpy (m_data, other. m_data ); } // Value assignment function String & string: operate = (const string & Other) // 13 points { // (1) Check the auto-assigned value // 4 points If (this = & Other) return * this; // (2) release the original memory resource // 3 points Delete [] m_data; // (3) allocate new memory resources and copy the content. // 3 points Int length = strlen (other. m_data ); M_data = new char [Length + 1]; // It is better if null can be added Strcpy (m_data, other. m_data ); // (4) return the reference of this object // 3 points Return * this; }

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.