How many of these 19 c/c+ interview questions can you answer? -Personal article think No

Source: Internet
Author: User
Tags modifier object model
What is the difference between C and C + +? A: C + + adds classes based on C, and C is a structured language that focuses on algorithms and data structures. Software engineer interviews, routines are similar, this time about the problem, next time you may also encounter.

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

A: C + + is an object-oriented programming language developed on the basis of C language, which is widely used. C + + supports a variety of programming paradigms-object-oriented programming, generic programming, and procedural programming. Its wide range of programming fields, often used in system development, engine development and other applications, is the most popular programmers to benefit from one of the most powerful programming language, support class: Class, encapsulation, overloading and other features!

Q. What is the difference between 2:C and C + +?

A: C + + adds classes based on C, and C is a structured language that focuses on algorithms and data structures. The design of C program is the primary consideration is how to get the output (or implementation process (transaction) control) through a process, the input (or the environment condition), and for C + +, the first consideration is how to construct an object model, so that the model can fit with the corresponding problem domain, This allows the output or implementation process (transaction) control to be obtained by acquiring the state information of the object. Small series recommended a learning C language/c++ study Skirt "627, 012, 464", whether you are Daniel or small white, is to change careers or want to join the study together to learn about progress together! The skirt has the development tool, many dry goods and the technical information to share!

Q 3: What is Object-oriented (OOP)?

A: Object-oriented is a way of understanding and abstraction of the real world, a thought of the problem processing by transforming the requirement elements into objects.

Q 4: What is polymorphism?

A: polymorphism refers to the same operation or function, the process 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.

Q 5: Design patterns You know, just a simple example?

A: Design pattern is a set of reusable, most known, categorized purposes, code design experience Summary.

A singleton pattern, for example, guarantees that a class has only one instance and provides a global access point to access it.

Applies to: When a class can have only one instance and the customer can access it from a well-known access point, when this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.

For example, a factory pattern, which defines an interface for creating objects, so that subclasses decide which class to instantiate. The Factory Method defers the instantiation of a class 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 delegates the responsibility of creating an object to one of several helper subclasses, and you want to put which helper subclass is the agent this information is localized.

Have you ever asked the 6:stl library? What are the common STL containers? How many algorithms have been used?

A: STL consists of two parts: containers and algorithms. (and importantly, the iterators that fuse both)

container, where the data is stored. such as array.

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

A sequence container in which elements are not necessarily ordered, but can be sorted. such as: vector, List, deque, stack, queue, heap, priority_queue, slist;

Associative container, the internal structure is basically a balanced two-fork tree. The so-called Association, refers to each element has a key value and a real value, 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.

Select one of the following as a description.

Vector: It is a container for dynamically allocating storage space. The space allocated by Array,array in C + + is static, cannot be changed after allocation, and vectors automatically redistribute (expand) space.

Set: Its inner elements are automatically sorted according to the key value of the element. Unlike map, its key value is the real value, and map can have different key and real values at the same time.

Algorithms, such as sorting, copying ... and a container-specific algorithm. This does not need to introduce too much, mainly look at the contents of the following iterators.

Iterators are the essence of STL, and we describe it this way: iterators provide a way to access the individual elements contained in a container in order, without exposing the internal structure of the container. It separates the containers and algorithms so that they are designed independently.

Q 7: Will the data structure be? What are the main uses of the project development process?

A: The data structure is mainly used in the array, linked list, tree (less), will also use the idea of stacks and queues.

Ask 8:const, you know? Explain its role.

For:

A member variable of a 1.const decorated class that represents a member constant and cannot be modified.

The 2.const modifier function commits to not modify the data members within the class within this function, and does not invoke other non-const member functions.

3. If const constitutes a function overload, the const object can only call the const function, and non-const objects call the non-const function first.

The 4.const function can only call the const function. A non-const function can call a const function.

5. The const member function defined in the class body requires the Const modifier at both the definition and the declaration.

Q 9: When is the static variable of a class initialized? When is the static variable of a function initialized?

A: A static member variable of a class already exists before the class is instantiated, and memory is allocated. The static variable of the function is initialized when the function is executed.

Q 10: What is the difference between heaps and stacks? The life cycle of heaps and stacks?

For:

One, stack space allocation difference:

1, stack (operating system): automatically allocated by the operating system release, storage function parameter value, local variable value and so on. It works like a stack in a data structure;

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

Second, the stack cache mode difference:

1, the stack uses a first-level cache, they are usually transferred in the storage space, the call is finished immediately released;

2, the heap is stored in the level two cache, the life cycle is determined by the garbage collection algorithm of the virtual machine (not once orphaned objects can be recycled). So the speed of calling these objects is relatively low.

Third, the stack data structure difference:

Heap (data structure): Heaps can be thought of as a tree, such as: heap sort;

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

Q 11: Explain the encapsulation, inheritance, and polymorphism?

For:

First, Package:

Encapsulation is the first step in implementing object-oriented programming, in which a collection of data or functions is set in a unit (what we call a Class).

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

Second, Inherit:

Inheritance mainly implements reusing code, saving development time.

Subclasses can inherit something from the parent class.

Three, polymorphic

Polymorphism: The same operation acts on different objects, can have different interpretations, and produces different execution results. At run time, you can invoke methods in the derived class by pointing to a pointer to the base class.

Q 12: What is the difference between a pointer and a reference?

For:

    1. The pointer is a variable, except that the variable stores an address that points to a memory cell, and the reference is only an individual name;

    2. The reference does not need to be dereferenced (*), and the pointer needs to be dereferenced;

    3. A reference can be initialized only once at the time of definition, and then immutable;

    4. The reference has no const and the pointer has a const;

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

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

    7. Pointer and reference self-increment (+ +) operation has different meanings;

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

9. From memory allocation: The program allocates memory regions for pointer variables, whereas references do not need to allocate memory areas.

Q 13: What is a memory leak? What methods do you have to face memory leaks and pointers out of bounds? What methods do you usually use to avoid and reduce such errors?

A: The dynamic storage allocation function dynamically open up the space, after the use is not released, the result is that the memory unit has been occupied is a memory leak.

Remember the length of the pointer when you use it.

You have to be sure there's free at malloc.

When assigning values to pointers, be aware that the assigned pointers need not be freed.

A pointer that allocates memory dynamically is best not to assign a value again.

Q 14: What are the commonly used sorting algorithms? Describe briefly the pros and cons of several sorting algorithms?

A: Select, bubble, fast, * *, Hill, merge, Heap and so on.

1. Quick-line: is an improvement of bubble sort.

Pros: Fast, less data movement

Cons: Low stability

2. Merge: Divide and conquer the ranking, stable ranking algorithm, generally used for the general disorder, but the local ordered sequence.

Advantages: High Efficiency O (n), stable

Cons: Comparing memory consumption

Q. What is the difference between 15:new and malloc?

For:

1. malloc and free are standard library functions for c++/c languages, and new/delete are operators of C + +. They can all be used to request dynamic memory and free memory.

2, for non-internal data types of objects, optical maloc/free can not meet the requirements of dynamic objects. Objects are automatically executed when they are created, and the object executes the destructor automatically before it dies.

3. Because Malloc/free is a library function and not an operator, the task of executing constructors and destructors cannot be imposed on malloc/free, not within the control of the compiler. Therefore, the C + + language requires an operator new that can perform dynamic memory allocation and initialization, with an operator delete that can perform cleanup and release of memory work. Note New/delete is not a library function.

4, C + + programs often call C functions, and C programs can only use Malloc/free to manage dynamic memory.

5. New can be considered as the execution of malloc plus constructors. The new pointer is directly with the type information. and malloc returns a void pointer.

Q. What is the difference between 16:tcp and UDP communication? What is IOCP?

For:

1.TCP connection-oriented, UDP for non-connected

2.TCP guaranteed, UDP transmission unsecured

3.TCP is low efficiency, high UDP efficiency

4.TCP is a stream-based, UDP-based data packet

5.TCP transmits important data, UDP transmits unimportant data

IOCP Full name I/O completion port, Chinese to I/O completion port.

IOCP is an asynchronous I/O API that effectively notifies an application of I/O events.

Unlike using Select () or other asynchronous methods, a socket [socket] is associated with a completion port, and then the normal Winsock operation can proceed. However, when an event occurs, the completion port is added to the queue by the operating system. The application can then query the core layer to get the completion port.

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

For:

A. Synchronization

The so-called synchronization is that when a function call is made, the call does not return until the result is obtained.

By this definition, the vast majority of functions are synchronous calls (such as sin isdigit, etc.).

But generally speaking, we are talking about synchronous and asynchronous tasks, especially those that require other components to collaborate or need to be done in a certain amount of time.

The most common example is SendMessage.

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

When the other party finishes processing, the function returns the value returned by the message handler function to the caller.

B. Asynchronous

Asynchronous concepts and synchronization are relative.

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

The part that actually handles the call is to notify the caller by state, notification, or through the callback function, after the call is made.

Q 18: Explain static and static variables in C + +?

For:

(1) A class static data member is created and initialized at compile time: exists before any object of the class is established, does not belong to any object, and non-static class member variables are owned by the object. A class static data member has only one copy, which is shared for all objects of this class.

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

1. Static member variable realizes information sharing among similar objects.

2, static member class outside storage, to find the size of the class, not included.

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

4. Static members can only be initialized out of class.

5, can be accessed through the class name (no object generation is also possible), or through object access.

1, the meaning of static member functions, not in information sharing, data communication, but in the management of static data members, complete the encapsulation of static data members.

2. Static member functions can only access static data members. Cause: A non-static member function that is passed in as a parameter when the this pointer is called. A static member function belongs to a class, not to an object, and has no this pointer.

Q 19: What do you know about memory?

For:

1. Stack-the release is automatically assigned by the compiler

2. Heap-usually released by the programmer, if the programmer does not release, the program may end up being recycled by the OS

3. Global zone (Static zone), global variables and static variables are stored in a block, initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another area adjacent. -Program End Release

4. There is also a special place where constants are placed. -Program End Release

5 Code area of the program, storing 2 binary code.

The variables defined in the function body are usually on the stack, and the allocated memory functions such as malloc, Calloc, realloc, etc. are allocated on the heap. The global amount is defined in the body of all functions, and after the static modifier is placed in the global zone (static zone), the static variable defined in the body of all functions is valid in the file and cannot be extern to another file. The static representation defined in the function body is valid only within the function body. In addition, a string such as "ADGFDF" in the function is stored in a constant area.

Related articles:

Share 125 Basic C # interview questions and Answers

Related videos:

A video tutorial that sweeps through the PHP workplace in the face of questions

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.