Prepared written examination questions

Source: Internet
Author: User

1. It is known that the prototype of the strcpy function is:

Char * strcpy (char * strdest, const char * strsrc );

  • Implement the strcpy function without calling the library function.
  • Explain why char * is returned *.

Answer:

  • Implementation Code of strcpy
    Char * strcpy (char * strdest, const char * strsrc)
    {
    If (strdest = NULL) | (strsrc = NULL) // [1]
    Throw "invalid argument (s)"; // [2]
    Char * strdestcopy = strdest; // [3]
    While (* strdest ++ = * strsrc ++ )! = '/0'); // [4]
    Return strdestcopy;
    }
    Incorrect practice:
    [1]
    (A) if the pointer is not checked, it means that the respondent does not pay attention to the robustness of the Code.
    (B) used to check the pointer validity ((! Strdest) | (! Strsrc) or (! (Strdest & strsrc). It indicates that the respondent has no deep understanding of implicit conversions of types in C language. In this example, char * is converted to bool, that is, implicit type conversion. Although this function is flexible, it is more likely to increase the error probability and increase the maintenance cost. Therefore, C ++ adds the bool, true, and false keywords to provide a safer conditional expression.
    (C) Use (strdest = 0) | (strsrc = 0) to check the pointer validity. This means that the respondent does not know the benefit of using constants. Directly Using a literal constant (such as 0 in this example) reduces the maintainability of the program. 0 is simple, but there may be a lot of pointer checks in the program. In case of PEN mistakes, the compiler cannot find that the generated program contains logical errors and it is difficult to eliminate them. If null is used to replace 0, the compiler will check if a spelling error occurs.
    [2]
    (A) return new string ("invalid argument (s)"); it indicates that the respondent does not know the purpose of the returned value, and is not cautious about memory leakage. Returning the memory allocated in the function body from the function is very dangerous. It throws the obligation to release the memory to an uninformed caller. In most cases, the caller will not release the memory, which leads to memory leakage.
    (B) Return 0;, indicating that the respondent did not master the exception mechanism. The caller may forget to check the return value, and the caller may not be able to check the return value (see the chain expression below ). If you want the return value to shoulder the dual function of returning the correct value and abnormal value, the result is often invalid in both functions. The return value should be replaced by throwing an exception, which can reduce the burden on the caller, prevent errors from being ignored, and enhance the maintainability of the program.
    [3]
    (A) if you forget to save the original strdest value, it means that the answer provider's logic is not strict.
    [4]
    (A) cyclically write it as while (* strdest ++ = * strsrc ++);, same as [1] (B ).
    (B) cyclically write while (* strsrc! = '/0') * strdest ++ = * strsrc ++;, indicating that the respondent does not check the boundary conditions effectively. After the loop body ends, '/0' is not correctly added to the end of the strdest string '.
  • Returns the original strdest value to enable the function to support chained expressions, increasing the "added value" of the function ". Functions of the same function are naturally more ideal if they can reasonably improve availability.
    The form of a chained expression is as follows:
    Int ilength = strlen (strcpy (stra, strb ));
    Another example:
    Char * stra = strcpy (New char [10], strb );
    The original strsrc value is returned incorrectly. First, the source string must be known and it does not make sense to return it. Second, the expression in the second example cannot be supported. Third, to protect the source string, the const uses const to limit the content referred to by strsrc and returns const char * as char *. If the type is different, an error is returned during compilation.

2. 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.

Answer:
String: string (const char * Str)
{
If (STR = NULL) // If the strlen parameter is null, an exception is thrown.
{
M_data = new char [1];
M_data [0] = '';
}
Else
{
M_data = new char [strlen (STR) + 1];
Strcpy (m_data, STR );
}
}
String: string (const string & other)
{
M_data = new char [strlen (other. m_data) + 1];
Strcpy (m_data, other. m_data );
}
String & string: Operator = (const string & other)
{
If (this = & other)
Return * this;
Delete [] m_data;
M_data = new char [strlen (other. m_data) + 1];
Strcpy (m_data, other. m_data );
Return * this;
}
String ::~ String (void)
{
Delete [] m_data;
}

3. Short answer
3.1 what is the use of ifndef/define/endif in the header file?
A: It is used for Conditional compilation.

3.2 # What is the difference between I nclude <FILENAME. h> and # I nclude "filename. H?
A: For # I nclude <FILENAME. h>, the compiler searches for filename. h from the standard library path.
For # I nclude "filename. H", the compiler searches for filename. h from the user's working path.

3.3 why do I need to add the extern "C" to call the function compiled by the C compiler in the C ++ program "?
A: The C ++ language supports function overloading. The C language does not support function overloading. The name of the function in the library after being compiled by C ++ is different from that in C language. Assume that the prototype of a function is void Foo (int x, int Y );
After the function is compiled by the C compiler, its name in the library is _ Foo, while the C ++ compiler generates names such as _ foo_int_int.
C ++ provides a C connection exchange with the specified symbol extern "C" to solve the name matching problem.

3.4 A class has a base class and a member object of another class. What is the execution sequence of the constructor.
Answer: first run the base class. (If the base class contains a virtual base class, run the virtual base class first. Other base classes are executed in sequence when the derived class is declared ), then execute the member object, and finally execute your own.

3.5 describe a familiar Design Model
3.6 what is the difference between aggregation and composition in UML)
Answer: The aggregation relationship is stronger, similar to the relationship between pages and books. The combination relationship is weaker, similar to the relationship between books and bookshelf.

3.7c # What are the differences between C ++ and C ++ in terms of syntax:

(C # I only understand, not very proficient)
(1) C # There is an automatic garbage collection mechanism, so programmers don't have to worry about object recycling. (2) C # It is strictly prohibited to use pointers and can only process objects. If you want to use pointers, you can only use pointers in the unsafe block. (3) C # only supports single inheritance. (4) You must use the class name to access static members. You cannot access static members through objects as in C ++. (5) When overwriting the virtual function of the parent class in the subclass, the keyword override must be used. to overwrite the method of the parent class, the keyword new must be used.

3.8ado.net and ADO?
Answer: in fact, there are not many similarities between the two except the basic similarities that allow applications to process data stored in DBMS. However, ADO uses ole db interfaces and is based on Microsoft's COM technology, while ADO. Net has its own ADO. net interface and is based on Microsoft's. NET architecture. As we all know, the. NET system is different from the com system, and the ADO. net interface is completely different from the ADO and ole db interfaces. That is to say, ADO. NET and ADO are two data access methods. Ado.net provides support for XML.

3.9 differences between New Delete and malloc free
Answer: The object cannot be initialized using the malloc function. New calls the object's constructor. Delete calls the destructor of the object, but free does not call the destructor of the object.

3.10 # define double (x) x + x

I = 5 * double (10); what is I? What is the correct statement?
Answer: I is 60. The correct statement is # define double (x) (x + x)

3.11 In which situations can I use the intialization list instead of the assignment?
Answer: when the class contains const and reference member variables, constructors of the base class all need parameters. The class contains Member objects of other classes, and constructors of the class all need parameters.

3.11 C ++ is it type-safe?
Answer: No. Two different types of pointers can be forcibly converted. C # is type-safe.

3.12 what code will be executed before the main function is executed?
Answer: The constructor of the global object is executed before the main function.

3.13 describes the memory allocation methods and their differences.
Answer: 1) distribute data from the static storage area. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For example, global variables and static variables.
(2) create a stack. When a function is executed, the storage units of local variables in the function can be created on the stack. When the function is executed, these storage units are automatically released. Stack memory allocation operations are embedded in the processor's instruction set.
(3) allocate from the stack, also known as dynamic memory allocation. When the program runs, it uses malloc or new to apply for any amount of memory. The programmer is responsible for releasing the memory with free or delete. The lifetime of the dynamic memory is determined by us. It is very flexible to use, but the problem is also the most.

3.14 what is virtual memory? How does virtual memory map to physical memory? What are the Page Replacement algorithms?
For more information, see p238. Master the page replacement algorithm NRU, FIFO, second opportunity page replacement algorithm, LRU

3.15 there are four identical containers filled with the same number of pills. The quality of normal pills is m, and the quality of bad pills is m + 1, one of them is full of bad pills, which are called once only by an electronic scale. Find out which container is loaded with bad pills.
Answer: Number the four containers in sequence as 1, 2, 3, and 4, and then take 1, 2, 3, and 4 pills, respectively. The quality of these 10 pills is called, if the quality is 10 m + 1, it indicates that the first container is installed with a bad pill. If the quality is 10 m + 2, it indicates the second spoiled pill, and so on.

3.16 compare the differences between static_cast and dynamic_cast in C ++.
Dynamic_casts is limited in helping you browse the hierarchy of inheritance. It cannot be used for types without virtual functions. It is used to safely perform type conversion along the inheritance relationship of classes. If you want to convert the data type without an inheritance relationship, you may think of static_cast.

3.17 difference between struct and class
Answer: In struct, the default access permission for member variables and member functions is public and the class is private.


3.18 if a Class A does not have any member variables and member functions, what is the value of sizeof (a)? If it is not zero, explain why the compiler didn't make it zero.

Answer: it must not be zero. For example, if it is zero, declare an array of Class A [10] objects, and each object occupies zero space. In this case, a [0] cannot be distinguished. A [1]… Now

3.19 how is the logical and physical addresses converted in the 8086 assembly?

Answer: The address provided by the general register is the intra-range offset address. The corresponding segment register address * 10 h + the intra-range address in the General Register gets the address to be accessed.

3.20 description of C ++ Polymorphism
Answer: The polymorphism of C ++ is manifested in two parts: one is the function overload under static concatenation, and the operator overload; the virtual function and pure virtual function (abstract class) under dynamic concatenation)

4. Write a comparison statement between bool, Int, float, and pointer type variable A and zero.
Answer:
Bool: If (! A)
INT: if (a = 0)
Float: const expressions exp = 0.000001
If (A <exp & A>-exp)
Pointer: if (! = NULL)

5. Advantages of const over # define
Answer:
(1) const constants have data types, while macro constants do not. The compiler can perform type security checks on the former. However, only character replacement is performed for the latter, and there is no type security check, and unexpected errors may occur during character replacement.
(2) Some integrated debugging tools can debug const constants, but cannot debug macro constants.

6. Differences between arrays and pointers
An array is either created in a static storage area (such as a global array) or on a stack. Pointers can point to any type of memory block at any time.
(1) Differences in modification content
Char A [] = "hello ";
A [0] = 'X ';
Char * P = "world"; // note that P points to a constant string
P [0] = 'X'; // This error cannot be found by the compiler. It is a runtime error.
(2) the sizeof operator can be used to calculate the array capacity (number of bytes ). Sizeof (P), P is the number of bytes of a pointer variable, rather than the memory capacity referred to by P. C ++/C language cannot know the memory capacity referred to by the pointer unless you remember it when applying for memory. Note: When an array is passed as a function parameter, the array will automatically degrade to a pointer of the same type.
Char A [] = "Hello World ";
Char * P =;
Cout <sizeof (a) <Endl; // 12 bytes
Cout <sizeof (p) <Endl; // 4 bytes
Calculate the memory size of arrays and pointers
Void func (char a [1, 100])
{
Cout <sizeof (a) <Endl; // 4 bytes instead of 100 bytes
}

7. Differences between Overloading, overwriting, and hiding of class member functions
Answer:
Features of member functions being overloaded:
(1) the same range (in the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
Override refers to the function of a derived class that overwrites the base class function. The features are as follows:
(1) different scopes (located in the derived class and the base class respectively );
(2) The function name is the same;
(3) The parameters are the same;
(4) basic functions must have virtual keywords.
"Hide" means that the function of the derived class shields the base class functions with the same name. The rules are as follows:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

8. There are two int variables: A and B, don't use "if", "? : "," Switch"
Or other judgement statements, find out the biggest one of the two
Numbers.
Answer: (a + B) + ABS (a-B)/2

9. How to print the file name of the current source file and the current row number of the source file?
Answer:
Cout <_ file __;
Cout <__line __;
_ File _ and _ line _ are pre-defined macros of the system, which are not defined in a file but defined by the compiler.

10. After the main function is executed, is it possible to execute another piece of code to explain it?
Answer: Yes. You can use _ onexit to register a function. It will be executed after main.

Int fn1 (void), FN2 (void), fn3 (void), FN4 (void );
Void main (void)
{
String STR ("Zhanglin ");
_ Onexit (fn1 );
_ Onexit (FN2 );
_ Onexit (fn3 );
_ Onexit (FN4 );
Printf ("this is executed first./N ");
}
Int fn1 ()
{
Printf ("next./N ");
Return 0;
}
Int FN2 ()
{
Printf ("executed ");
Return 0;
}
Int fn3 ()
{
Printf ("is ");
Return 0;
}
Int FN4 ()
{
Printf ("this ");
Return 0;
}
The _ onexit function is passed the address of a function (func) to be called when the program terminates normally. successive callto _ onexit create a register of functions that are executed in LIFO (last-in-first-out) Order. the functions passed to _ onexit cannot take parameters.

11. How can I determine whether a program is compiled by C or C ++?
Answer:
# Ifdef _ cplusplus
Cout <"C ++ ";
# Else
Cout <"C ";
# Endif

12. The file contains a group of integers, which must be sorted and output to another file.
Answer:
Void Order (vector <int> & Data) // Bubble Sorting
{
Int COUNT = data. Size ();
Int tag = false;
For (INT I = 0; I <count; I ++)
{
For (Int J = 0; j <count-I-1; j ++)
{
If (data [J]> data [J + 1])
{
Tag = true;
Int temp = data [J];
Data [J] = data [J + 1];
Data [J + 1] = temp;
}
}
If (! Tag)
Break;
}
}
Void main (void)
{
Vector <int> data;
Ifstream in ("C: // data.txt ");
If (! In)
{
Cout <"file error! ";
Exit (1 );
}
Int temp;
While (! In. EOF ())
{
In> temp;
Data. push_back (temp );
}
In. Close ();
Order (data );
Ofstream out ("C: // result.txt ");
If (! Out)
{
Cout <"file error! ";
Exit (1 );
}
For (I = 0; I <data. Size (); I ++)
Out <data

<"";
Out. Close ();
}

13. Comparison of sorting methods (Intel)
Sorting method Average time worst time secondary storage
Insert sort directly
Bubble Sorting
Quick sorting
Simple selection and sorting
Heap sorting
Merge Sorting
Base sort

14. node Structure of a linked list
Struct Node
{
Int data;
Node * next;
};
Typedef struct node;
(1) Head of the known head node of the linked list. Write a function to reverse the list (Intel)
Node * reverselist (node * head) // reverse sequence of the linked list
{
If (Head = NULL | head-> next = NULL)
Return head;
Node * P1 = head;
Node * P2 = p1-> next;
Node * P3 = P2-> next;
P1-> next = NULL;
While (P3! = NULL)
{
P2-> next = p1;
P1 = P2;
P2 = P3;
P3 = P3-> next;
}
P2-> next = p1;
Head = P2;
Return head;
}
(2) It is known that the two linked lists head1 and head2 are in an orderly order. Please combine them into a linked list.
Node * Merge (node * head1, node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
Node * P1 = NULL;
Node * P2 = NULL;
If (head1-> data {
Head = head1;
P1 = head1-> next;
P2 = head2;
}
Else
{
Head = head2;
P2 = head2-> next;
P1 = head1;
}
Node * pcurrent = head;
While (P1! = NULL & p2! = NULL)
{
If (P1-> data <= P2-> data)
{
Pcurrent-> next = p1;
Pcurrent = p1;
P1 = p1-> next;
}
Else
{
Pcurrent-> next = P2;
Pcurrent = P2;
P2 = P2-> next;
}
}
If (P1! = NULL)
Pcurrent-> next = p1;
If (P2! = NULL)
Pcurrent-> next = P2;
Return head;
}
(2) It is known that the two linked lists head1 and head2 are in an orderly order. Please combine them into a linked list. This time, we need to use a recursive method. (Autodesk)
Answer:
Node * mergerecursive (node * head1, node * head2)
{
If (head1 = NULL)
Return head2;
If (head2 = NULL)
Return head1;
Node * head = NULL;
If (head1-> data {
Head = head1;
Head-> next = mergerecursive (head1-> next, head2 );
}
Else
{
Head = head2;
Head-> next = mergerecursive (head1, head2-> next );
}
Return head;
}

15. Analyze the output of this program

Class B
{
Public:
B ()
{
Cout <"default constructor" <Endl;
}
~ B ()
{
Cout <"destructed" <Endl;
}
B (int I): Data (I)
{
Cout <"Constructed by parameter" <data <Endl;
}
PRIVATE:
Int data;
};
B play (B)
{
Return B;
}
Int main (INT argc, char * argv [])
{
B temp = play (5 );
Return 0;
}
Run the command on your own.

16. Write a function to find the second largest number in an integer array (Microsoft)
Answer:
Const int minnumber =-32767;
Int find_sec_max (INT data [], int count) // a sequence similar to 1 4 4 4 4 will be considered as the second largest number.
{
Int maxnumber = data [0];
Int sec_max = minnumber;
For (INT I = 1; I <count; I ++)
{
If (Data> Maxnumber)
{
Sec_max = maxnumber;
Maxnumber = Data;
}
Else
{
If (Data> Sec_max)
Sec_max = Data;
}
}
Return sec_max;
}

17. Write a function to find the first position of a substring in a string.
I will not give the general algorithm for this question if it is relatively simple. For more information, see the KMP algorithm in the data structure. However, it is difficult to write the algorithm when the test time is limited.

English question
1. Introduce yourself in English
2. What is your great advantage you think of yourself?
3. What is your drawback you think of yourself?
Maybe I will feel very tense if I make a speech in front of a lot of people.
4. How do you feel Shanghai?

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.