C ++ classic pen questions and answers-Trend Micro

Source: Internet
Author: User


1. What is the purpose of static? (Please specify at least two types)

Answer: 1) in the function body, a variable declared as static keeps its value unchanged when the function is called.

2) in a module (but in the function body), a variable declared as static can be accessed by the function used in the module, but not by other functions outside the module. It is a local global variable.

3) in a module, a function declared as static can only be called by other functions in the module. That is, this function is restricted to use within the local scope of the module that declares it.

2. What is the difference between reference and pointer?

Answer: 1) the reference must be initialized and the pointer is not required.

2) The reference cannot be changed after initialization. the pointer can change the object.

3) There is no reference pointing to a null value, but there is a pointer pointing to a null value.

3. Describe the basic features of the Real-Time System

Answer: complete specific tasks within a specific period of time, real-time and reliability.

4. Are global variables and local variables different in memory? If so, what is the difference?

Answer: global variables are stored in static databases, and local variables are stored in stacks.

5. What is a balanced binary tree?

Answer: The left and right Subtrees are balanced binary trees and the absolute value of the depth difference between the left and right Subtrees is not greater than 1.

6. What causes stack overflow?

Answer: No garbage collection resources have been collected.

7. What functions cannot be declared as virtual functions?

Answer: The constructor function cannot be declared as a virtual function.

8. What is the time complexity of the bubble sort algorithm?

Answer: The time complexity is O (n ^ 2 ).

9 +. Write the if statement for comparing float X with "zero value.

Answer: If (x <0.000001 & x>-0.000001)

10. What network protocol does the Internet use? What is the main hierarchy of the protocol?

Answer: TCP/IP protocol

The main layers are application layer, transmission layer, network layer, data link layer, and physical layer.

 

 

11. What protocol does the physical and IP address translation use for the Internet?

Answer: ARP (Address Resolution Protocol)

12. What are the IP address encoding components?

Answer: an IP address consists of a network number and a host number. However, it is only after bitwise AND top-up with the "subnet mask" that the network bit and host bit are distinguished.

13. The user inputs the M and N values. The number of sequential loops starts from 1 to n. This value is output on every count to m until all are output. Write the C program (Joseph's ring problem ).

Answer: Circular linked list, with the remainder operation

14. The parameter types that cannot be switched () are:

Answer: The Switch Parameter cannot be of the real type.

1. Write and judge whether the four expressions of ABCD are correct. If so, write the value of A in the expression (3 points)

Int A = 4;

(A) A ++ = (a ++); (B) a ++ = (++ A); (c) (a ++) ++ = A; (d) (++ A) ++ = (a ++ );

A =?

Answer: C error. The left side is not a valid variable and cannot be assigned a value. You can change it to (++ A) + =;

After the change, the answers are 9, 10, 10, and 11 in sequence.

2. In a 32-bit system, calculate the value of sizeof in C ++ (5 points ).

Char STR [] = "http://www.ibegroup.com /"

Char * P = STR;

Int n = 10;

Calculate

Sizeof (STR) =? (1)

Sizeof (p) =? (2)

Sizeof (n) =? (3)

Void Foo (charstr [100]) {

Calculate

Sizeof (STR) =? (4)

}

Void * P = malloc (100 );

Calculate

Sizeof (p) =? (5)

Answer: (1) 25 (2) 4 (3) 4 (4) 4 (5) 4

 

 

3. answer the following question (4 points)

(1) What is the use of ifndef/define/endif in the header file? Preprocessing

Answer: prevent header files from being repeatedly referenced.

(2). # What is the difference between include and # include "filename. h?

A: the former is used to include the library header files provided by the development environment, and the latter is used to include the header files compiled by yourself.

(3). Why should I add the extern "C" declaration to call the function compiled by the C compiler in the C ++ program?

A: The name of the function and variable in the symbol library after being compiled by C ++ is different from that in the C language. It is modified by extern "C ".

The number and function are compiled and connected in C language. C ++ programs cannot be directly called because the compiled name is different.

Use the C function. C ++ provides a C connection to exchange the specified symbol extern "C" to solve this problem.

(4) What data types are not allowed in switch?

Answer: Solid

4. answer the following question (6 points)

(1). voidgetmemory (char ** P, int num ){

* P = (char *) malloc (Num );

}

Void test (void ){

Char * STR = NULL;

Getmemory (& STR, 100 );

Strcpy (STR, "hello ");

Printf (STR );

}

What are the results of running the test function?

A: The output is "hello"

(2). voidtest (void ){

Char * STR = (char *) malloc (100 );

Strcpy (STR, "hello ");

Free (STR );

If (STR! = NULL ){

Strcpy (STR, "World ");

Printf (STR );

}

}

What are the results of running the test function?

A: output "world"

(3). char * getmemory (void ){

Char P [] = "Hello World ";

Return P;

}

Void test (void ){

Char * STR = NULL;

STR = getmemory ();

Printf (STR );

}

What are the results of running the test function?

A: The pointer is invalid and the output is uncertain.

5. Compile the strcat function (6 points)

It is known that the prototype of the strcat function is char * strcat (char * strdest, const char * strsrc );

Here, strdest is the destination string and strsrc is the source string.

(1) do not call the string library functions of C ++/C. Compile the strcat function.

A:

VC source code:

Char * _ cdeclstrcat (char * DST, const char * SRC)

{

Char * CP = DST;

While (* CP)

CP ++;/* Find end OFDST */

While (* CP ++ = * SRC ++);/* Copy SRC to end of DST */

Return (DST);/* return DST */

}

(2) Why should strcat connect the content of strsrc to strdest?

A: It is convenient to assign values to other variables.

6. Is cstring a type security class in MFC?

A: No. You can use the cstring member function Format to convert other data types to cstring.

7. Why is a template class used in C ++.

A: (1) create a Data Structure with dynamic growth and reduction

(2) It is type-independent and therefore highly reusable.

(3) It checks the data type during compilation instead of runtime, ensuring the type security.

(4) It is platform-independent and portable.

(5) can be used for basic data types

8. What does csinglelock do.

A: Synchronize multiple threads to simultaneously access a data class.

9. What is newtextmetric.

Answer: The physical font structure is used to set the font height, width, and size.

10. When the program should use the thread, and when the single thread is highly efficient.

Answer: 1. Time-consuming operations use threads to improve application response

2. threads are used for parallel operations, such as the concurrent threads on the server side in the C/S architecture to respond to user requests.

3. In multiple CPU Systems, threads are used to improve CPU utilization

4. Improve the program structure. A long and complex process can be considered to be divided into multiple threads and become several independent or semi-independent running parts. Such a program will facilitate understanding and modification.

In other cases, a single thread is used.

11. Is windows a kernel-level thread.

A: see the next question.

12 +. Does Linux have kernel-level threads.

A: A thread is usually defined as a different execution route of code in a process. There are two types of threads: user-level threads and kernel-level threads ". A user thread is a thread implemented in a user program without kernel support. It does not depend on the core of the operating system, the application process uses the thread library to provide functions for creating, synchronizing, scheduling, and managing threads to control user threads. This kind of thread can be implemented even in operating systems like DOS, but the thread scheduling needs to be completed by the user program, which is similar to Windows 3.x collaborative multitasking. In addition, the kernel needs to participate in thread scheduling. It depends on the operating system core and is created and abolished by the internal requirements of the kernel. These two models have their own advantages and disadvantages. The user thread does not require additional kernel expenses, and the Implementation Mode of the User-state thread can be customized or modified to meet the requirements of special applications.
When I/O is in the waiting state, the whole process will be switched to the waiting state by the scheduling program, and other threads will not be able to run. The kernel thread has no restrictions, it is conducive to taking advantage of multi-processor concurrency, but takes more system expenses. Windows NT and OS/2 support kernel threads. Linux supports kernel-level Multithreading

13. In C ++, what data is allocated to the stack or heap. Is New allocated to the near or remote heap data?

A: Stack: stores local variables, function call parameters, function return values, and function return addresses. Managed by the System Heap: dynamically applied when the program is running, and the memory applied for by new and malloc is on the heap.

14. How does a thread prevent large peaks.

A: It indicates how to prevent a large number of threads from being generated at the same time by using the thread pool. The thread pool can improve the scheduling efficiency and limit resource usage at the same time, when the maximum number of threads in the thread pool is reached, other threads will wait in queue.

15. What are the differences between function templates and class templates?

A: The instantiation of a function template is automatically completed by the compiler when processing the function call. The instantiation of a class template must be explicitly specified by the programmer in the program.

16. What will happen if the logs in the database are full?

A: You can only perform read operations such as query, modification, backup, and other write operations, because logs must be recorded for any write operations. That is to say, it is basically in an unusable state.

17. Does SQL server support row-level locks?

A: Yes. The blocking mechanism is mainly used to control concurrent operations and block interference to ensure data consistency and accuracy, row-level blocking ensures that the updated row is not modified by other users during this period. Therefore, row-level locks can ensure data consistency and improve data operation bursts.

18. what can happen if the database is full?

A: see section 16.

19. Memory alignment and sizeof () Output

Answer: The reason for automatic compiler alignment: To improve program performance, data structures (especially stacks) should be alignment on natural boundaries as much as possible. The reason is that the processor needs to perform two memory accesses to access non-alignment memory; however, alignment memory access only needs one access.

20. Int I = 10, j = 10, K = 3; K * = I + J; what is the final value of K?

Answer: 60. The priority of this question is actually written as K * = (I + J). The assignment operator has the lowest priority.

21. How can I operate a table in a database and another table?

A: Put the operations that operate on multiple tables into transactions for processing.

22. How does one establish a connection with TCP/IP? (3-way shake)

A: In TCP/IP, TCP provides reliable connection services and uses three handshakes to establish a connection.

First handshake: when a connection is established, the client sends the SYN Packet (SYN = J) to the server and enters the syn_send

Wait for the server to confirm;

The second handshake: when the server receives the SYN packet, it must confirm the customer's Syn (ACK = J + 1) and send

SYN Packet (SYN = K), that is, SYN + ACK packet. At this time, the server enters the syn_recv status;

Third handshake: the client receives the server's SYN + ACK packet and sends the ACK (ACK = k + 1) Confirmation packet to the server)

After the package is sent, the client and server enter the established status and complete three handshakes.

23. What protocol is ICMP and what layer is it?

A: The Internet control protocol is at the network layer (IP layer). ICMP packets are transmitted inside the IP datagram, which is usually used by the IP layer or higher layer (TCP/IP. The role is to pass error information or other information that requires attention.

24. How does a trigger work?

A: A trigger is executed when an event is triggered. When you perform operations such as update, insert, and delete on a table, the database will automatically execute the SQL statements defined by the trigger to ensure that the data processing must comply with the rules defined by these SQL statements.

25. How does Winsock establish a connection?

A: On the server side: socker () establishes a socket, binds (BIND) and listens (Listen), and waits for the client to connect with accept.

Client: socker () establishes a socket and connects to the (CONNECT) server. After the connection, use send () and Recv () to write and read data on the socket until the data exchange is complete. closesocket () close the socket.

Server: accept () finds a client connection, creates a new socket, and waits for the connection again. The new socket uses send () and Recv () to write and read data until the data exchange is complete, closesocket () closes the socket.

26. How do I dynamically connect to a database?

A: There are two methods to call a function in a DLL:

1. Load-Time Dynamic Linking. The module clearly calls an export function to make it look like a local function. This requires linking the DLL import and export of those functions, and providing the information required for loading the DLL and positioning the DLL functions to the system.

2. Run-Time Dynamic Linking. You can use the loadlibrary or LoadLibraryEx function to load the DLL during running. After the DLL is loaded, the module can call getprocaddress to obtain the exit address of the DLL function, and then call the DLL function through the returned function pointer. In this way, you can avoid importing and receiving files.

27. What are the benefits of IP multicast?

A: Many new applications on the Internet, especially high-bandwidth multimedia applications, bring about sharp bandwidth consumption and network congestion problems. Multicast is a network technology that allows one or more senders (Multicast sources) to send a single packet to multiple receivers (one-time and simultaneous. Multicast can greatly save network bandwidth, because no matter how many destination addresses there are, only a single packet is transmitted on any link of the network. Therefore, the core of multicast technology is to ensure service quality on the premise of Saving network resources.

 

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.