How many of these 19 C/C+ interview questions can you answer correctly? - Personal article Sifu

php是最好的语言
Release: 2018-07-26 18:07:03
Original
1767 people have browsed it

What is the difference between C and C? Answer: c adds classes on the basis of c. C is a structured language whose focus is on algorithms and data structures. The interview routines for software engineers are all the same. You may encounter the same questions you asked this time, next time.

Question 1: Please tell me what C is in simple language?

Answer: C is an object-oriented programming language developed on the basis of C language and is widely used. C supports multiple programming paradigms - object-oriented programming, generic programming and procedural programming. It has a wide range of programming fields and is often used in system development, engine development and other application fields. It is one of the most powerful programming languages ​​​​used by programmers. It supports classes: classes, encapsulation, overloading and other features!

Question 2: What is the difference between C and C?

Answer: c adds classes on the basis of c. C is a structured language, and its focus is on algorithms and data structures. The primary consideration in the design of a C program is how to process the input (or environmental conditions) to obtain the output (or implement process (transaction) control) through a process. For C, the primary consideration is how to construct an object model so that This model can fit the corresponding problem domain, so that output or process (transaction) control can be achieved by obtaining the state information of the object. The editor recommends a learning skirt for learning C language/C [627, 012, 464]. Whether you are an expert or a novice, want to change careers or want to join the industry, you can learn about it and learn together! There are development tools inside the skirt, and a lot of useful information and technical information are shared!

Question 3: What is object-oriented (OOP)?

Answer: Object-oriented is a method and idea for understanding and abstracting the real world. It is an idea that solves problems by converting demand elements into objects.

Question 4: What is polymorphism?

Answer: Polymorphism means that the same operation, function, or process can act on multiple types of objects and obtain different results. Different objects can produce different results when receiving the same message. This phenomenon is called polymorphism.

Question 5: Do you understand design patterns? Can you give me a simple example?

Answer: Design pattern (Design pattern) is a set of classified and cataloged summary of code design experience that is used repeatedly, known to most people.

For example, the singleton mode ensures that a class has only one instance and provides a global access point to access it.

Applies to: when the class can have only one instance and customers can access it from a well-known access point; when this single instance should be extensible through subclassing, and customers should not need to change the code When an extended instance can be used.

For example, the factory pattern defines an interface for creating objects and lets subclasses decide which class to instantiate. Factory Method causes the instantiation of a class to be deferred to its subclasses.

Applies to: when a class does not know the class of the object it must create; when a class wants its subclass to specify the object it creates; when the class will create the object When responsibility is delegated to one of multiple helper subclasses, and you want to localize the information about which helper subclass is the delegate.

Question 6: Have you ever used the STL library? What are the common STL containers? Which algorithms have been used?

Answer: STL includes two parts: container and algorithm. (It is also important to combine the two iterators)

Container, which is where data is stored. Such as array, etc.

In STL, containers are divided into two categories: sequential containers and associative containers.

Serial container, the elements in it are not necessarily in order, but they can all be sorted. Such as: vector, list, deque, stack, queue, heap, priority_queue, slist;

Associative container, the internal structure is basically a balanced binary tree. The so-called association means that each element has a key value and a real value, and the elements are stored according to certain rules. Such as: RB-tree, set, map, multiset, multimap, hashtable, hash_set, hash_map, hash_multiset, hash_multimap.

Choose one of each below as an explanation.

Vector: It is a container that dynamically allocates storage space. Different from array in C, the space allocated by array is static and cannot be changed after allocation, while vector will automatically reallocate (expand) the space.

set: Its internal elements will be automatically sorted according to the key value of the element. Different from map, its key values ​​are real values, and map can have different key values ​​and real values ​​at the same time.

Algorithms, such as sorting, copying... and container-specific algorithms. This point does not need to be introduced too much, mainly depends on the content of the iterator below.

Iterator is the essence of STL. We describe it like this: Iterator provides a way to access the various elements contained in a container in sequence without exposing the internal structure of the container. It separates the container and the algorithm so that they can be designed independently.

Question 7: Will the data structure be known? What are the main ones used in the project development process?

Answer: Arrays, linked lists, and trees (less often) are mainly used in data structures, and the ideas of stacks and queues are also used.

Question 8: Do you know const? Explain its function.

Answer:

1.const modifies the member variables of the class, indicating member constants and cannot be modified.

2. The const modified function promises not to modify the data members in the class within this function, and not to call other non-const member functions.

3. If const constitutes function overloading, const objects can only call const functions, and non-const objects call non-const functions first.

4.const functions can only call const functions. Non-const functions can call const functions.

5. Const member functions defined outside the class require const modifiers at both the definition and declaration.

Question 9: When are the static variables of the class initialized? When are the static variables of a function initialized?

Answer: The static member variables of the class already exist before the class is instantiated, and memory is allocated. The static variables of a function are initialized when this function is executed.

Question 10: What is the difference between heap and stack? Heap and stack life cycle?

Answer:

1. Differences in stack space allocation:

1. Stack (operating system): automatically allocated and released by the operating system to store function parameter values ​​and local variables value etc. Its operation method is similar to the stack in the data structure;

2. Heap (operating system): It is generally allocated and released by the programmer. If the programmer does not release it, it may be recycled by the OS when the program ends. The allocation method is similar. in the linked list.

2. Differences in stack caching methods:

1. The stack uses the first-level cache. They are usually in the storage space when they are called and are released immediately after the call is completed;

2. The heap is stored in the second-level cache, and its life cycle is determined by the garbage collection algorithm of the virtual machine (it does not mean that it can be recycled once it becomes an orphan object). Therefore, the speed of calling these objects is relatively low.

3. Differences in stack data structures:

Heap (data structure): The heap can be regarded as a tree, such as: heap sort;

Stack (data structure) ): A first-in-last-out data structure.

Question 11: Explain encapsulation, inheritance and polymorphism?

Answer:

1. Encapsulation:

Encapsulation is the first step in realizing object-oriented programming. Encapsulation is to collect data or functions in units. (We call them classes).

The meaning of encapsulation is to protect or prevent code (data) from being inadvertently destroyed by us.

2. Inheritance:

Inheritance mainly realizes code reuse and saves development time.

Subclasses can inherit some things from the parent class.

3. Polymorphism

Polymorphism: The same operation acts on different objects and can have different interpretations and produce different execution results. At runtime, you can call methods in a derived class through a pointer to the base class.

Question 12: What is the difference between pointers and references?

Answer:

  1. The pointer is a variable, but this variable stores an address, pointing to a storage unit in the memory; and the reference is just an alias;

  2. There is no need to dereference (*) when using references, pointers need to be dereferenced;

  3. References can only be initialized once when defined, and are immutable thereafter. ;The pointer is variable;

  4. The reference does not have const, the pointer has const;

  5. The reference cannot be null, the pointer can be null;

  6. "sizeof reference" gets the size of the variable (object) pointed to, while "sizeof pointer" gets the size of the pointer itself;

  7. The meaning of the increment () operation of pointers and references is different;

  8. Pointers can have multiple levels, but references can only be one level (int **p; is legal and int &&a is Illegal)

9. From the perspective of memory allocation: the program allocates a memory area for pointer variables, while references do not need to allocate a memory area.

Question 13: What is a memory leak? What methods do you have in the face of memory leaks and pointer out-of-bounds? What methods do you usually use to avoid and reduce these types of errors?

Answer: The space dynamically opened using the dynamic storage allocation function is not released after use. As a result, the memory unit is always occupied, which is a memory leak.

Remember the length of the pointer when using it.

You must make sure it is free when mallocing.

When assigning a value to a pointer, you should pay attention to whether the assigned pointer needs to be released.

It is best not to use dynamically allocated memory pointers. Assign again.

Question 14: What are the commonly used sorting algorithms? Briefly describe the advantages and disadvantages of several sorting algorithms?

Answer: Select, bubble, fast, **, Hill, merge, stack sort, etc.

1. Quick sort: It is an improvement of bubble sort.

Advantages: fast, less data movement

Disadvantages: insufficient stability

2. Merge: divide-and-conquer sorting, a stable sorting algorithm, generally used for overall order, but a locally ordered sequence.

Advantages: High efficiency O(n), stable

Disadvantages: Comparative memory usage

Question 15: What is the difference between new and malloc?

Answer:

1. malloc and free are standard library functions of C/C language, and new/delete are C operators. They can both be used to apply for dynamic memory and release memory.

2. For objects of non-internal data types, maloc/free alone cannot meet the requirements of dynamic objects. The object must automatically execute the constructor when it is created, and the object must automatically execute the destructor before it dies.

3. Since malloc/free is a library function rather than an operator, it is not within the control authority of the compiler. The task of executing constructors and destructors cannot be imposed on malloc/free. Therefore, C language needs an operator new that can complete dynamic memory allocation and initialization work, and an operator delete that can complete the work of cleaning up and releasing memory. Note that new/delete are not library functions.

4. C programs often call C functions, but C programs can only use malloc/free to manage dynamic memory.

5. New can be considered as the execution of malloc plus constructor. The pointer from new directly carries type information. Malloc returns void pointers.

Question 16: What is the difference between TCP and UDP communication? What is IOCP?

Answer:

1.TCP is connection-oriented, UDP is connection-less

2.TCP is guaranteed, UDP transmission is not guaranteed

3 .TCP is inefficient, but UDP is highly efficient

4.TCP is based on streams, and UDP is based on data packets

5.TCP transmits important data, and UDP transmits unimportant data

IOCP's full name is I/O Completion Port, which is translated into Chinese as I/O completion port.

IOCP is an asynchronous I/O API that can efficiently notify applications of I/O events.

Unlike using select() or other asynchronous methods, a socket [socket] is associated with a completion port, and then normal Winsock operations can continue. However, when an event occurs, the completion port will be added to a queue by the operating system. The application can then query the core layer for this completion port.

Question 17: What is the difference between synchronous IO and asynchronous IO?

Answer:

A. Synchronization

The so-called synchronization means that when a function call is issued, the call will not return until the result is obtained.

According to this definition, in fact, most functions are called synchronously (such as sin isdigit, etc.).

But generally speaking, when we talk about synchronous and asynchronous, we specifically refer to tasks that require the cooperation of other components or require a certain amount of time to complete.

The most common example is SendMessage.

This function sends a message to a certain window. This function does not return until the other party has processed the message.

After the other party has finished processing, this function returns the value returned by the message processing function to the caller.

B. Asynchronous

The concept of asynchronous is opposite to synchronization.

When an asynchronous procedure call is issued, the caller will not get the result immediately.

The component that actually handles this call is to notify the caller through status and notification after the call is issued, or process the call through a callback function.

Question 18: Explain static functions and static variables in C?

Answer:

(1) Class static data members are created and initialized at compile time: they exist before any object of this class is created, they do not belong to any object, and they are not static class member variables. It belongs to the object. There is only one copy of static data members of a class, which is shared by all objects of this class.

(2) Class static member functions belong to the entire class, not to a certain object, and are shared by all objects of the class.

1. Static member variables realize information sharing between similar objects.

2. Static members are stored outside the class, and the class size is not included.

3. The static member is a global variable belonging to the class in the namespace and is stored in the rw segment of the data area.

4. Static members can only be initialized outside the class.

5. It can be accessed through the class name (also when no object is generated), or through the object.

1. The significance of static member functions does not lie in information sharing and data communication, but in managing static data members and completing the encapsulation of static data members.

2. Static member functions can only access static data members. Reason: For non-static member functions, the this pointer is passed in as a parameter when called. Static member functions belong to classes, not objects, and do not have this pointers.

Question 19: Tell me about your understanding of memory?

Answer:

1. Stack - automatically allocated and released by the compiler

2. Heap - generally allocated and released by the programmer. If the programmer does not release it, when the program ends May be recycled by the OS

3. Global area (static area), global variables and static variables are stored together. Initialized global variables and static variables are in the same area, and uninitialized global variables and uninitialized variables are stored in the same area. The initialized static variables are in another adjacent area. - Release when the program ends

4. There is also a special place for constants. - The

5 program code area is released when the program ends, storing binary codes.

Variables defined in the function body are usually on the stack, and those allocated by functions such as malloc, calloc, realloc, etc. that allocate memory are on the heap. Global variables are defined outside the body of all functions. After adding the static modifier, they are stored in the global area (static area) no matter where they are. The static variables defined outside the body of all functions are valid in the file and cannot be externed to other files. Use, static defined in a function body means that it is only valid within the function body. In addition, strings such as "adgfdf" in the function are stored in the constant area.

Related articles:

Share 125 basic C# interview questions and answers

Related videos:

Detailed video tutorial on interview questions that sweeps the PHP workplace

The above is the detailed content of How many of these 19 C/C+ interview questions can you answer correctly? - Personal article Sifu. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
c c# c++
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!