C/C ++ general summary of written examination and interview questions (3)

Source: Internet
Author: User

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 also the most.

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 spaces that lack encapsulation and functionality, and the class is like a living and reliable social member. It has smart services, it has a strong encapsulation barrier and a well-defined interface. Since most people think so, there are only a few methods in your class and public data (this kind of thing exists in a well-designed 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 ++?

For details, refer to the differences and Applications of role, 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'; // 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 pointer to get 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
}

34. What are the differences between Overloading, overwriting, and hiding of class member functions?
Answer:
A. 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.
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; // you can specify whether or not to continue the 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 [I] <"";
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;
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;
}

 

 

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.