Common Test/interview questions (zz)

Source: Internet
Author: User
The answers to common written or interview questions compiled by one student are for reference only and do not represent my opinion.

1. 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. Do not call the string library function of C ++/C. Compile the strcpy function.
Answer:
Char * strcpy (char * strdest, const char * strsrc)
{
If (strdest = NULL | strsrc = NULL)
Return NULL;
If (strdest = strsrc)
Return strdest;
Char * tempptr = strdest;
While (* strdest ++ = * strsrc ++ )! = '\ 0 ')
;
Return tempptr;
}

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] = '\ 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: prevent this header file from being repeatedly referenced.
3.2 # What is the difference between include <FILENAME. h> and # include "filename. H?
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.3 In C ++ Program To call the function compiled by the C compiler. Why should we add the extern "C "?
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. (Autodesk)
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 (Autodesk)
3.6 what is the difference between aggregation and composition in UML? Autodesk)
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? (Autodesk, Microsoft)
Answer: (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 (Autodesk)
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 (Autodesk)
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? (Autodesk)
Answer: when the class contains const and reference member variables, constructors of the base class all need parameters. The class contains member variables of other classes, and constructors of the class all need parameters.
3.11 C ++ is it type-safe? (Autodesk)
Answer: No. Two different types of pointers can be forcibly converted. C # is type-safe.
3.12 what else will be executed before the main function is executed? Code ? (Autodesk)
Answer: The constructor of the global object is executed before the main function.
3.13 describes the memory allocation methods and their differences. (Autodesk, Microsoft)
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? Page Replacement Algorithm What are there? (Microsoft)
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 (Microsoft)
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 ++. (Autodesk)
3.17 differences between struct and class (Autodesk)
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. (Autodesk)
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? (Intel)
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 (Microsoft)
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 created either 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, 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 = A;
cout cout calculate the memory capacity of the array and pointer
void func (char a [100])
{< br> cout }

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 execute int fn1 (void), FN2 (void), fn3 (void), and FN4 (void) after main );
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. there is a group of integers in the file, which must be sorted and output to another file
answer:
void Order (vector & Data) // Bubble Sorting
{< br> int COUNT = data. size ();
int tag = false;
for (INT I = 0; I {< br> for (Int J = 0; j {< br> If (data [J]> data [J + 1])
{< br> tag = true;
int temp = data [J];
data [J] = data [J + 1];
data [J + 1] = temp;
}< BR >}< br> If (! Tag)
break;
}< BR >}< br> void main (void)
{< br> vector data;
ifstream in ("C: \ data.txt");
If (! In)
{< br> cout <"file error! ";
exit (1);
}< br> int temp;
while (! In. EOF ()
{< br> in> temp;
data. push_back (temp);
}< br> in. close ();
Order (data);
ofstream out ("C: \ result.txt");
If (! Out)
{< br> cout <"file error! ";
exit (1);
}< br> for (I = 0; I out 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 (Autodesk)
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 [I]> maxnumber)
{
Sec_max = maxnumber;
Maxnumber = data [I];
}
Else
{
If (data [I]> sec_max)
Sec_max = data [I];
}
}
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.