C + + Interview Highlights (questions asked for interview)

Source: Internet
Author: User
Tags function prototype variable scope

1. C and C + + differences


2. What is the use of const

There are three main points:

1: Define read-only variables, constants

2: parameter of modifier function and return value of function

3: The definition body of the modifier function, where the function is a member function of the class, the const-decorated member function represents the value of the member variable is not modified

3. The difference between a pointer and a reference

1: A reference is an alias of a variable, and an internal implementation is a read-only pointer

2: References can only be assigned at initialization time, other times the value cannot be changed, and the value of the pointer can be changed at any time

3: Reference cannot be NULL, pointer can be null

4: Reference variable memory cell holds the address of the referenced variable

5: "sizeof Reference" = the size of the variable, "sizeof pointer" = size of the pointer itself

6: Reference can take address action, return the address of the memory unit where the referenced variable itself resides

7: References are used at source level equivalent to ordinary variables, when doing function arguments, internally passed is actually the variable address

4. If you have malloc/free in C + +, why do you need new/delete

  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 objects with non-intrinsic data types, the light Maloc/free cannot satisfy the requirements of dynamic objects.     objects are automatically executed when they are created, and the object executes the destructor automatically before it dies.     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.  3, so 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.

5. Write a constructor for a class string, a destructor, a copy constructor, and an assignment function

6. Multi-State implementation

7. Reverse of a single-linked list

8. Heap and Stack differences

  The memory used by a program compiled by C + + is divided into the following sections   1, stack (stack)-   automatically allocated by the compiler to release, store the function's parameter value, local variable value, etc. It operates in a manner similar to a stack in a data structure.   2, Heap area (heap)-   generally by the programmer to allocate the release, if the programmer does not release, the end of the program may be recycled by the OS. Note that it is not the same as the     heap in the data structure, the distribution is similar to the list, hehe.   3, Global (static)-the storage of global variables and static variables is placed in a block,     initialized global variables and static variables in an area, uninitialized global variables and uninitialized static variables in another adjacent area. -System release after   the end of the program 4, literal constant area  -the constant string is placed here. After the end of the program, the system releases   5, program code area-the binary code that holds the function body.

10. Do not call the C + + string library function, write strcpy

   char * strcpy (char * strdest,const char * strsrc)        {                if (strdest==null) | | strsrc==null))                                        return NULL;                    char * strdestcopy=strdest;                 while ((*strdest++=*strsrc++)! = ' + ');                 *strdest = ' + ';                return strdestcopy;        }

11. The role of the keyword static

1. The function body static variable scope is the function body, differs from the auto variable, the memory of the variable is allocated only once, so its value will maintain the last value at the next call

2. Static global variables within a module can be accessed by all functions within the module, but not by other functions outside the module

3. The static function within the module can only be called by other functions within this module, and the function's scope of use is limited to the module that declares it.

4. Static member variables in a class are owned by the entire class, so objects with only one copy of the class

5. The static member function in the class is owned by the entire class, which does not receive the this pointer, so it can only access the static member variables of the class

Describe one of its most important: hide. (static function, static variable)--corresponds to the above 2, 3 items
When multiple files are compiled at the same time, all global variables and functions that do not have a static prefix have global visibility.
For example, it is clear. Compile two source files at the same time, one is A.C and the other is main.c.

   A.C    char a = ' a ';               global variable    void msg ()    {      printf ("hello\n");    }
  MAIN.C   int main ()   {     extern char A;       extern variable must is declared before use     printf ("%c", a);     (void) msg ();     return 0;   }

The running result of the program is:

A Hello

Why are global variables A and function msg defined in A.C used in MAIN.C?

As mentioned earlier, all global variables and functions that do not have a static prefix have global visibility and other source files can be accessed. In this example, a is a global variable, and MSG is a function, and none of the static prefixes are added.

Therefore, for additional source files, Main.c is visible.

If static is added, it is hidden from other source files. For example, before the definition of a and MSG, add STATIC,MAIN.C to see them.

With this feature, you can define a function with the same name and a variable of the same name in different files without worrying about naming conflicts. Static can be used as a prefix for functions and variables, and for functions, the function of static is limited to hiding

12. Call the C compiler-compiled function in a C + + program, why add extern "C"

the C + + language supports function overloading, which does not support function overloading, the function is compiled by the C + + compiler in the library after the name is different from the C language,

Suppose a function prototype is:

    1. void foo (int x, inty);
The function is compiled by the C compiler in the library with the name: _foo and the C + + compiler produces names like: _foo_int_int.
To address this type of name matching problem, C + + provides a C-link interchange with the specified symbol extern "C".

13. What is the ifndef/define/endif of the head file?

prevents the header file from being repeatedly included

14. Connections and differences between threads and processes

http://blog.csdn.NET/wolenski/article/details/7969908

15. What are the various states of threads

http://blog.csdn.Net/wolenski/article/details/7969908

16. Inter-process communication modalities

Pipelines, known pipelines, signals, shared memory, message queues, semaphores, sockets, files.

17. The difference between thread synchronization and thread mutex

http://blog.csdn.net/wolenski/article/details/7969908

18. How threads are synchronized

Linux: mutexes, condition variables, and semaphores

http://blog.csdn.net/zsf8701/article/details/7844316

19. Network Seven Layer


What is the difference between TCP and UDP?

TCP---Transmission Control Protocol, providing a connection-oriented, reliable byte-stream service.

Before the customer and the server Exchange data with each other, a TCP connection must be established between the two parties before the data can be transferred.

TCP provides time-out re-send, discard duplicate data, test data, flow control and other functions to ensure that data can be transmitted from one end to the other.

UDP---User Datagram Protocol, is a simple transport layer protocol for datagram.

UDP does not provide reliability, it simply sends the application to the IP layer's datagram, but does not guarantee that it will reach its destination.

Because UDP does not have to establish a connection between the client and the server before transmitting the datagram, and there is no mechanism such as time-out retransmission, the transmission speed is fast

21. Steps for writing socket sockets

TCP three handshake and four waves, and the role of each state

http://hi.baidu.com/suxinpingtao51/item/be5f71b3a907dbef4ec7fd0e?qq-pf-to=pcqq.c2c

. HTTP protocol

HTTP (Hypertext Transfer Protocol) is a non-stateful, application-level protocol based on request and response patterns, often based on TCP connection,

A continuous connection mechanism is given in the HTTP1.1 version, and the vast majority of web development is a Web application built on top of the HTTP protocol.

TCP and HTTP differences: http://blog.csdn.net/lemonxuexue/article/details/4485877

24. Used Shell commands

CP, MV, RM, mkdir, Touch, pwd, CD, LS, top, cat, tail, less, DF, Du, man, find, kill, Sudo, cat

25. Used VIM command

wq!, DD, DW, YY, p, I,%s/old/new/g,/abc backwards search string abc,? ABC forward search string ABC

26. Used GDB Commands

http://blog.csdn.net/dadalan/article/details/3758025

27. Common algorithms

Quick Sort, heap sort, and merge sort

Heap Sort: http://blog.csdn.net/xiaoxiaoxuewen/article/details/7570621

Quick Sort, merge sort: http://blog.csdn.net/morewindows/article/details/6684558

stability Analysis Http://baike.baidu.com/link?url=ueoZ3sNIOvMNPrdCKbd8mhfebC85B4nRc-7hPEJWi-hFo5ROyWH2Pxs9RtvLFRJL

C. library function implementation

29. The difference between a static linked list and a dynamic list

http://blog.csdn.net/toonny1985/article/details/4868786

31. Large Concurrency (Epoll)

Advantages:

http://blog.csdn.net/sunyurun/article/details/8194979

Instance:

Http://www.cnblogs.com/ggjucheng/archive/2012/01/17/2324974.html


32. Mass data processing of knowledge points, (hash table, hash statistics)

Hash Table: http://hi.baidu.com/05104106/item/62736054402852c09e26679b

Massive data processing method: http://blog.csdn.net/v_july_v/article/details/7382693

33. When to use virtual destructors

When you delete an object of a derived class from a pointer to a base class, the destructor for the base class should be virtual. Otherwise its delete effect will not be implemented.

In general, such deletions are only able to delete the base class object, and cannot delete the subclass object, resulting in the deletion of half the image, thus tens of thousands of memory leaks.

Reason:

In public inheritance, the operation of a base class on a derived class and its objects affects only those members inherited from the base class.

If you want to manipulate a non-inherited member with a base class, define the operation (function) of the base class as a virtual function.
The destructor, then, should also be true: if it wants to refactor the redefinition of a subclass or new members and objects, it should also be declared virtual.

Attention:

If you do not need a base class to manipulate derived classes and objects, you cannot define virtual functions, including virtual destructors, because this increases memory overhead.

How C + + makes the function of the returned object not call the copy constructor

"Explicit" keyword before copy constructor

35. Orphan process and zombie process

Http://www.cnblogs.com/Anker/p/3271773.html

Written and interview questions for major computer companies

http://blog.csdn.net/huyfaeng/article/category/880022

Interview questions are difficult and easy, not because of easy, we despise, but not because it is difficult, we give up. We face the high-paying employment attitude will never change, that is, adhere to, adhere to, and then persist. Come up with a problem, find a reason; We have always believed that only when we hold on can we see hope, not to see hope to persist.

Life without if, only results and consequences. Now that you have chosen, you will not regret it. Young is capital, young will endure hardship, must experience. We must learn to grow in persistence. So feeling, to the depth of experience, absolute experiences.

OK, to get to the point, the following is the "must master the 20 technical face questions."

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.

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. What is the difference between 11:C and C + +?

A: C + + On the basis of the addition of the class

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.

Q 12: 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 13: 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 dereference (*), the pointer needs to be dereferenced;

3. References can only be initialized once at the time of definition, and then immutable; pointers are variable;

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

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

6. "sizeof Reference" gets the size of the variable (object) that is pointed to, and the "sizeof pointer" gets the size of the pointer itself;

7. The pointer and the reference self-increment (+ +) operation has different meanings;

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

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

Q 14: 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 15: 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 16: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 17: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 18: 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 19: 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 20: 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.

C + + Interview Highlights (questions asked for interview)

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.