Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

20 must-have C-interview questions


May 11, 2021 C++



In the interview for the work of C , often encounter a variety of interview questions, which corresponds to the staff's knowledge and ability requirements are higher. This article will bring you is 20 must master the interview questions, don't miss Oh!


To quickly and easily master the knowledge of C, please click on the C- micro-class while learning to practice


Q1: Could you tell me what C is in simple language?

A: C is an object-oriented programming language developed on the basis of C language, which is widely used. I t supports a variety of programming paradigms -- object-oriented programming, generic programming, and process programming. Its wide range of programming fields, often used in system development, engine development and other applications, is the most popular programmer used in one of the most powerful programming languages, supporting classes: classes, packaging, overloading and other characteristics!


Q2: What is the difference between C and C?

A: C is a structured language that focuses on algorithms and data structures by adding classes to c. The primary consideration in the design of the C program is how to obtain output (or implementation process (transaction) control through a process of computational processing of inputs (or environmental conditions), while for C , the primary consideration is how to construct an object model so that the model can fit the corresponding problem domain, so that the output or implementation process (transaction) control can be obtained by obtaining the state information of the object.


Q3: What is Object Oriented (OOP)?

A: Object-oriented is a kind of understanding and abstract method and thought of the real world, which deals with problems by transforming demand elements into objects.


Q4: What is polymorphism?

A: Polymorphism refers to the same actions or functions, processes that can be used on multiple types of objects and get different results. Different objects, receiving the same message can produce different results, a phenomenon known as polymorphism.


Q5: Do you understand the design pattern, a simple example?

For:

Design pattern is a set of repeated use, most people know, classified, code design experience summary.

For example, a single-case pattern ensures that a class has only one instance and provides a global access point to it.

For: When a class can have only one instance and the customer can access it from a well-known access point, when the unique instance should be scalable through sub-classization, and the customer should be able to use an extended instance without changing the code.

For example, factory mode, define an interface for creating objects, and let sub-classes decide which class to instantiate. Factory Method delays the instantiation of a class to its sub-classes.

Applies when a class does not know the class of an object it must create, when a class wants its sub-class to specify the object it creates, when a class delegates responsibility for creating an object to one of several help sub-classes, and when you want to localize the information about which help sub-class is the agent.


Q6: Has the STL library been used? W hat are the common STL containers? How many algorithms have been used?

For:

STL consists of two parts: containers and algorithms. (And importantly, the iterator that fuses both)

Containers, where data is stored. Like array, etc.

In STL, containers fall into two categories: sequence containers and association containers.

A sequencing container in which elements are not necessarily ordered, but can be sorted. e.g. vector, list, deque, stack, queue, heap priority_queue, slist;

Associated container, the internal structure is basically a balanced binary tree. T he so-called association, refers to each element has a key value and a real value, the element is stored according to certain rules. e.g. RB-tree, set, map, multiset, multimap, hashtable, hash_set, hash_map, hash_multiset, hash_multimap.

Each of the following is selected as a description.

Vector: It is a container that dynamically allocates storage space. In the case of array in c , the space allocated by array is static, cannot be changed after allocation, and vector automatically reallocations (extends) space.

set: Its internal elements are automatically sorted based on the key value of the element. Different from map, its key value is real value, and map can have different key values and real values at the same time.

Algorithms, such as sorting, copy... a nd a container-specific algorithm. This doesn't need to be over-introduced, mainly looking at the contents of the iterator below.

The iterator is the essence of the STL, and we describe it this way: the iterator provides a way to access the elements contained in a container sequentially without exposing the internal structure of the container. It separates the container from the algorithm so that the two can be designed independently.


Q7: Will the data structure be? What are the main uses in the development of the project?

A: Arrays, lists, trees (less) are used primarily in data structures, as well as stack and queue ideas.


Q8: Do const know? Explain its role.

For:

1.const decorates the member variables of the class, which represents the member constant and cannot be modified.

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

3. If const constitutes a function overload, the const object can only call the const function, and the non-const object takes precedence over the non-const function.

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

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


Q9: When is the static variable of the class initialized? When is the static variable of the function initialized?

A: The static member variables of the class existed before the class was instantiated and memory was allocated. The static variable of the function is initialized when it is executed.


Q10: What is the difference between a stack and a stack? The life cycle of the heap and stack?

For:

First, the difference between stack space allocation:

1, stack (operating system): by the operating system automatically allocated release, store the parameter value of the function, the value of local variables and so on. It operates in a similar way to a stack in a data structure;

2, heap (operating system): generally by the programmer assigned release, if the programmer does not release, the end of the program may be recycled by OS, the distribution method is similar to the list.

Second, the stack caching method is different:

1, the stack is using a first-level cache, they are usually called when in storage space, the call is immediately released;

2, heap is stored in the secondary cache, the life cycle is determined by the virtual machine garbage collection algorithm (not once orphaned objects can be recycled). So these objects are called at a relatively low speed.

Third, the stack data structure is different:

Heap (data structure): Heap can be thought of as a tree, e.g. heap sort;

Stack (data structure): An advanced, post-out data structure.


Question 11: C and C ++ differences?

For:

Add classes to the C-plus basis

C is a structured language that focuses on algorithms and data structures.

The primary consideration in the design of the C program is how to obtain output (or implementation process (transaction) control through a process of computational processing of inputs (or environmental conditions), while for C , the primary consideration is how to construct an object model so that the model can fit the corresponding problem domain, so that the output or implementation process (transaction) control can be obtained by obtaining the state information of the object.


Q12: Explain encapsulation, inheritance, and polymorphism?

For:

First, encapsulation:

Encapsulation is the first step in implementing object-oriented program design, and encapsulation is the collection of data or functions in one unit after another (we call it a class).

Encapsulation is about protecting or preventing code (data) from being inadvertently compromised by us.

Second, inheritance:

Inheritance primarily implements reuse of code, saving development time.

A child class can inherit something from the parent class.

Three, polymorphism

Polymorphism: The same operation is performed on different objects and can be interpreted differently, resulting in different execution results. At runtime, you can call methods in the implementation derived class by pointing to the base class.


Q13: What is the difference between a pointer and a reference?

For:

1. The pointer is a variable, except that it stores an address that points to a storage unit of memory;

2. References do not need to be de-referenced when used, pointers need to be de-referenced;

3. References can only be initialized once at the time of definition and become immedic;

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

5. The reference cannot be empty and the pointer can be empty;

6. The "sizeof reference" gets the size of the variable (object) to which it points, while the "sizeof pointer" gets the size of the pointer itself;

7. The meaning of the self-addition operation of pointers and references is different;

8. The pointer can be multiple levels, but the reference can only be one level (int s p; legal and int samp;a is illegal)

9. From the memory allocation point of view: the program allocates the memory area for the pointer variable, while the reference does not need to allocate the memory area.


Q14: What is a memory leak? W hat are your methods for facing memory leaks and pointer crossings? What methods do you usually use to avoid and reduce such errors?

A: The space that is dynamically opened up by the dynamic storage allocation function is not released after use, resulting in a memory leak that has been occupying the memory unit.

Remember the length of the pointer when using it.

Malloc had to make sure it was there.

When assigning a pointer, you should be aware that the assigned pointer does not need to be released.

It is best not to assign a pointer to the dynamically allocated memory again.


Q15: What are the common sorting algorithms? A brief description of the advantages and disadvantages of several sorting algorithms?

A: Select, bubble, fast, Hill, go-to, stack, etc.

1. Quick row: is an improvement on bubbling sorting.

Pros: Fast, less data movement

Cons: Insufficient stability

2. Summation: sorting by division, stable sorting algorithm, generally used for the overall disorder, but local ordered number of columns.

Pros: High efficiency O(n), stable

Cons: Comparisons consume memory


Q16: What's the difference between new and malloc?

For:

1, malloc and free are the standard library functions in the C- / C language, and new/delete is the operator of C. They can be used to request dynamic memory and free up memory.

2, for non-internal data types of objects, light maloc/free can not meet the requirements of dynamic objects. Objects are created with constructors that are automatically executed, and objects are destructors that are executed before they die out.

3. Since malloc/free is a library function and not an operator, it is not within compiler control permissions and the task of executing constructors and destructors can not be imposed on malloc/free. T herefore, the C?language requires an operator that can complete dynamic memory allocation and initialization work, and an operator that can clean up and free memory. Note that new/delete is not a library function.

4, the C program often to call C function, and C program can only use malloc / free to manage dynamic memory.

5, new can be thought of as malloc plus the execution of the constructor. T he new pointer comes out with type information directly. Malloc returns the void pointer.


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

For:

1.TCP for connections, UDP for connectionless

2. TCP is guaranteed, UDP transmission is not guaranteed

3.TCP is inefficient and UDP is efficient

4.TCP is stream-based and UDP is data-based

5. TCP transmits important data, and UDP transmits data that is not important

IOCP is full name I/O Complete Port and Chinese as I/O completion port.

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

Unlike using select() or other asynchronous methods, a socket is associated with a completion port, and then the normal Winsock operation can continue. H owever, when an event occurs, this completion port is joined into a queue by the operating system. The application can then query the core layer to get this completion port.


Q18: What is the difference between synchronous IO and asynchronous IO?

For:

A. Sync

Synchronization means that when a function call is made, it is not returned until the result is available.

By this definition, the vast majority of functions are called synchronously (e.g. sin isdigit, etc.).

But in general, when we're talking about synchronization, asynchronous, we're talking about tasks that require collaboration by other parts or that take time to complete.

The most common example is SendMessage.

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

The function returns the value returned by the message handler to the caller only after the other party has processed it.

B. Asynchronous

Asynchronous concepts and synchronization relative.

When an asynchronous procedure call is made, the caller does not get the result immediately.

The part that actually handles the call is notified of the caller by state, notification, or by callback function after the call is made.


Q19: Explain static functions and static variables in C?

For:

(1) Class static data members are created and initialized at compile time: any object in the class exists before it is established, does not belong to any object, and non-static class member variables belong to the object. Class static data members have only one copy, shared by all such objects.

(2) Class static member functions belong to the entire class, do not belong to an object, by all objects of the class shared.

1, static member variables to achieve the same kind of information sharing between objects.

2, static member class storage, class size, is not included.

3, static member is a global variable of the namespace belonging to the class, stored in the rw segment of the data region.

4, static members can only be out-of-class initialization.

5, can be accessed by class name (no object generation can also be), can also be accessed through objects.


Q20: What do you know about memory?

For:

1. Stack - Released by the compiler's automatic assignment

2. Heap - Generally assigned release by the programmer, if the programmer does not release, the end of the program may be recycled by OS

3. Global zones (static zones), the storage of global and static variables is placed together, the initialized global and static variables in one area, the uninitialized global variables and the uninitialized static variables in another adjacent area. - Program ends release

4. There is also a place dedicated to constants. - Program ends release

5 Program code area, store 2-in code.

Variables defined in the function body are usually on the stack, and the allocation of memory functions such as malloc, calloc, realloc is done on the heap. T he global amount is defined outside the body of all functions, the static modifier is stored in the global area (static area) wherever it is, the static variable defined outside the function is valid in the file, cannot be used in any other file, and the static variable defined in the function body is valid only in the function body. In addition, strings such as "adgfdf" in the function are stored in the constant area.


The above is 20 must master the C-plus interview questions to share, I hope to be of great help to you. For more tutorial information on C + , you can continue to follow W3Cschool.