C/C ++ written examination and interview questions 16-42

Source: Internet
Author: User
Tags ole

 16. What are the differences between Association, aggregation, and composition?

Some concepts in UML are involved:Association
Indicates the general relationship between two classes. For example, "student" and "teacher" are associated;Aggregation
Indicates the relationship between has-a, which is relatively loose and does not need to be used by aggregation classes.

Responsible for the aggregated class, as shown in. Use an empty diamond to represent the aggregated relationship:

From the implementation perspective, aggregation can be expressed:
Class A {...} Class B {A * ;.....}
WhileCombination
Indicates the contains-a relationship, with stronger relevance than aggregation: the composite class has the same lifecycle as the composite class, the composite class is responsible for the composite class, and the solid diamond is used to indicate the composite relationship:

Implementation

The format is:
Class A {...} Class B {;...}
Reference: http://blog.csdn.net/wfwd/archive/2006/05/30/763753.aspx


Http://blog.csdn.net/wfwd/archive/2006/05/30/763760.aspx


17. What are the three basic features of object-oriented architecture?

1. encapsulation: Abstract Objective objects into classes. Each class implements protection (private, protected, public) on its own data and methods)
2. Inheritance: generalized inheritance has three implementation forms: Implementation inheritance (the ability to use basic class attributes and methods without additional encoding)

), Visual inheritance (child forms use the appearance of the parent form and implementation code), interface inheritance (only use properties and methods, implementation lags behind the implementation of sub-classes ). The first two methods (class inheritance) and the last one (Object combination => interface inheritance and pure virtual functions) constitute two methods of function reuse.
3. Polymorphism: it is a technology that sets a parent object to be equal to one or more other sub-objects.

After the value is assigned, the parent object can operate in different ways according to the features of the sub-objects assigned to it. Simply put, it is a sentence:Assign a pointer of the subclass type to a pointer of the parent class.

18. What is the difference between overload and overried?

Common Questions. In terms of definition:
Heavy Load
: Multiple allowedFunctions with the same name
AndDifferent parameter tables
(Maybe the number of parameters is different, maybe the parameter type is different, or both are different ).
Rewrite
: It refers to the method used by the subclass to redefine the complex class virtual function.
In terms of implementation principle:
Heavy Load
: The Compiler modifies the names of functions with the same name based on different parameter tables of the function, and then these functions with the same name become different functions (at least for the compiler ). For example, there are two functions with the same name: function func (P: integer): integer; and function func (P: string): integer ;. The modified function names of the compiler may be: int_func and str_func. The call to the two functions is determined between the compilers.Static
. That is to say,Their addresses are bound during compilation (early binding ),
Therefore,Overload is not related to Polymorphism
!
Rewriting: It is really related to polymorphism. After the subclass re-defines the virtual function of the parent class, the parent class pointer is assigned to it according to different subclass pointers,Dynamic call
This function belongs to the subclass.The number of calls cannot be determined during compilation.
(The address of the virtual function of the subclass called cannot be provided ). Therefore,Such function addresses are bound at runtime (late binding ).

19. What is the role of polymorphism?


There are two main reasons: 1. Hiding implementation details to modularize the Code; extending the code module to achieve code reuse; 2. Reusing interfaces: for classes to inherit and deriveTo ensure that a property of any type of instance in the family is called correctly.
.
20. What are the differences between ADO and ado.net?

Apart from the basic similarities that allow applications to process data stored in DBMS, there is not much in common between the two. 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.
21. What are the relationships and differences between New Delete and malloc free?

Answer: all are dynamic memory operations on heap. To use the malloc function, you must specify the number of bytes allocated by the memory and Cannot initialize the object. New automatically calls the object constructor. Delete calls the destructor of the object, but free does not call the destructor of the object.
22. # define double (x) x + X,
I = 5 * double (5); what is I?

Answer: I is 30.
23. In which situations can I use the intialization list instead of the assignment?

Answer: when the class contains const and reference member variables, the base class constructors need to initialize the table.
24. Is C ++ type safe?

Answer: No. Two different types of pointers can be forcibly converted (reinterpret cast ). C # is type-safe.
25. What code will be executed before the main function is executed?

Answer: The constructor of the global object is executed before the main function.
26. describes the memory allocation methods and their differences?

1)Distribute from static storage area
. The program has been allocated when it is compiled, and the program exists throughout the entire runtime. For exampleGlobal variable, static variable
.
2)Create on Stack
. When executing a function,The storage units of local variables in the function can be created on the stack.
When function execution ends, 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 the programmer. It is very flexible to use, but the problem is

The maximum.

27. Differences between struct and class


Answer: struct members are public by default, while Class Members are private by default. Struct and class are quite functional in other aspects.

Emotionally, most developers feel that classes and structures are very different. It seems that the structure is just like a bunch of open memory bits without encapsulation and functionality, and the class is like a living and reliable social member, it has intelligence

Service, with a strong encapsulation barrier and a well-defined interface. Since most people think so, there are only a few methods and public data in your class (this kind of thing is well designed ).

Is in the system !) You should use the struct keyword. Otherwise, you should use the class keyword.

28. When 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]… .

29. In the 8086 assembly, how is the logical address and physical address converted? (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.

30. How do I compare the four types of conversion methods in C ++?


See http://blog.csdn.net/wfwd/archive/2006/05/30/763785.aspx


Focuses on the differences and applications between static_cast, dynamic_cast and reinterpret_cast.

31. Write the comparison statements of Boolean, Int, float, and pointer type variables A and zero respectively.

Answer:
Bool: If (! A) or if ()
INT: if (a = 0)
Float: const expressions exp = 0.000001
If (A <exp & A>-exp)
Pointer: if (! = NULL) or if (a = NULL)

32. What are the advantages of const compared with # define?

Answer:
1)Const constants have data types, while macro constants do not.
. The compiler can type the former.Security

Check

. 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 macro constants cannot be debugged.

33. What are the 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'; // the compiler cannot find

This error indicates a running error.
(2) the sizeof operator can be used to calculate the array capacity (number of bytes ).Sizeof (P), P is the pointer to get the number of bytes of a pointer variable, rather than the memory capacity referred to by P
. C ++/C Language

There is no way to 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
}

34. What are the differences between Overloading, overwriting, and hiding of class member functions?

Answer:
A. Features of member functions being overloaded:
(1) Same range

(In the same class );
(2) The function name is the same;
(3) parameters are different;
(4) virtual keywords are optional.
B. Override refers to the function of the 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.
C."Hide
"Indicates 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)

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

36. How do I 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.

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

38. How can I determine whether a program is compiled by C or C ++?

Answer:
# Ifdef _ cplusplus
Cout <"C ++ ";
# Else
Cout <"C ";
# Endif

39. The file contains a group of integers, which must be sorted and output to another file.

Answer:

# I nclude <iostream>

# I nclude <fstream>

Using namespace STD;

Void Order (vector <int> & Data) // bubble sort
{
Int COUNT = data. Size ();
Int tag = false; // set whether or not

Flags that continue to bubble
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 (); // close the input file stream
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 (); // close the output file stream
}

40. linked list: the 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;

R/> 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. (Keep all nodes, even if the size is the same)

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;
}
(3) 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;
}

41. Analyze the output of this program (Autodesk)

Class B
{
Public:
B ()
{
Cout <"default constructor" <Endl;
}
~ B ()
{
Cout <"destructed" <Endl;
}
B (int I): Data (I) // B (INT) works as a converter (INT-> instance of B)
{
Cout <"Constructed by parameter" <data <Endl;
}
PRIVATE:
Int data;
};

B play (B)
{
Return B;
}

(1) results:
Int main (INT argc, char * argv []) constructed by parameter 5
{Destructed B (5)Parameters
Structure Analysis
B T1 = play (5); B t2 = play (T1); destructed T1 Structure
Return 0; destructed T2Attention order!


} Destructed T1

(2) results:
Int main (INT argc, char * argv []) constructed by parameter 5
{Destructed B (5)Parameters
Structure Analysis
B T1 = play (5); B t2 = play (10); constructed by parameter 10
Return 0; destructed B (10)Parameters
Structure Analysis
} Destructed T2Attention order!



42. 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)
{
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;

Related Article

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.