C,c++ Classic Questions

Source: Internet
Author: User
Tags win32

C,c++ Classic Questions

?

1 Programming Basics

1.1 Basic concepts

1.1.1 Pointer Understanding: Const char*, char const*, char*const the difference problem is almost every time in C + + interview each will have a topic. In fact, the concept of who has just three ways of declaring very similar is easy to mix. Bjarne a mnemonic method in his C + + programming language: Read a statement from right to left.

Char *const CP; (* read into pointer to)

CP is a const pointer to char//int* const pointer to constant

const char * p;

P is a pointer to const char; Const INT* Constant pointer

Char const * p;

Ibid. because there are no const* operators in C + +, const can only belong to the preceding type.

?

2. Pointer c

int *p[n];-----pointer array, each element is a pointer to the integer data.

Int (*) p[n];------p is a pointer to a one-dimensional array that has n integral data in one-dimensional array.

int *p ();----------function returns a pointer to a value returned by the pointer.

Int (*) p (),------p is a pointer to a function.

?

3. Array out-of-bounds issues

The following program will be executed after the error or effect:

#define MAX 255

int main ()

{

unsigned char a[max],i;

for (i=0;i<=max;i++)

A[i]=i;

}

Answer: max=255, the subscript range for array A is: 0. MAX-1, this is the first and second when I loop to 255, the loop executes:

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 2/24

a[255]=255, this sentence itself is not a problem, but when the for (i=0;i<=max;i++) statement is returned, because unsigned char has a range of values (0..255), i++ i is 0. The infinite loop goes on.

Note: The char type is one byte and the value range is [ -128,127],unsigned char [0, 255]

?

4. What are the fundamental differences between C++:memset, memcpy and strcpy?

#include "Memory.h"

Memset is used to set a memory space to a character, and is typically used to initialize a defined string to ' or '

; Example: Char A[100];memset (A, ' n ', sizeof (a));

memcpy is used to make a memory copy, you can copy any data type object, you can specify the data length of the copy;

Char a[100],b[50];

memcpy (b, A, sizeof (b)); Note that using sizeof (a) will cause the memory address of B to overflow.

strcpy can only copy the string, it encounters ' \ ' on the end of the copy; for example: Char a[100],b[50];strcpy (A, B), if using strcpy (b,a), note that the length of the string in a (before the first ' + ') is more than 50 bits, such as over, Will cause the memory address of B to overflow.

strcpy

Prototype: extern char *strcpy (char *dest,char *src);

Usage: #include <string.h>

function: Copy the null-terminated string from SRC to the array referred to by Dest.

Description: The memory areas referred to by SRC and dest cannot overlap and dest must have sufficient space to accommodate the SRC string.

Returns a pointer to the dest.

memcpy

Prototype: extern void *memcpy (void *dest, void *src, unsigned int count);

Usage: #include

Function: the memory area referred to by SRC is copied from count bytes to the memory area of Dest.

Description: The memory area referred to by SRC and dest cannot overlap, and the function returns a pointer to Dest.

Memset

Prototype: extern void *memset (void *buffer, char c, int count);

Usage: #include

Function: Sets the first count bytes of the memory area indicated by buffer to character C.

Description: Returns a pointer to buffer.

?

5. What is an ASSERT ()?

ASSERT () is a macro that is used frequently when a debugger is run, and it calculates the expression in parentheses when it runs, and if the expression is False (0), the program reports an error and terminates execution. If the expression is not 0, the following statement continues. This macro usually turns out that there is clearly illegal data in the program, and if a termination procedure is used to avoid serious consequences, it is also easy to find errors. For example, the variable n should not be 0 in the program, and if 0 could cause an error, you could write the program like this:

......

ASSERT (n! = 0);

K = 10/n;

......

Assert is only valid in debug versions and is ignored if compiled to release version.

The function of assert () is similar, it is a function specified in the ANSI C standard, and an important difference from assert is that it can be used in release version.

?

6. ("pause"); pause the program, press any key to continue, the screen will print, "Press any key to continue ..... "Eliminates the use of getchar (); system

?

7. What is the difference between a C + + class and a struct in C?

Classes in C + + have member-protection capabilities, and have inheritance, polymorphism, and so on, while the struct in C does not

8. Tell me about the use and function of destructors and virtual functions?

Destructors are also special class member functions that have no return type, no parameters, no arbitrary invocation, and no overloads. The resources allocated in the constructor are freed by the system's automatic invocation only at the end of the lifetime of the class object. At runtime, the ability to invoke that function according to its type is called polymorphism, or late-linking. Another: The destructor is generally done before the object undo work, such as the recovery of memory, such as the function of the virtual function is to enable subclasses with the same name of the function of the parent class function overload, and call the child class overload function automatically, if it is pure virtual function, it is purely for the subclass overload when there is a unified name.

?

?

9. What is the difference between a global variable and a local variable? How did it come true? How does the operating system and the compiler know?

The life cycle of a global variable is the time that the whole program runs, while the life cycle of a local variable is the time period of a local function or procedure call. The implementation is implemented by the compiler using a different memory allocation method at compile time. The global variable is assigned after the main function is called, and if it is a static variable it is initialized before the main function. While local variables are dynamically allocated in the user stack (or are suggested to look at the activity record in the compilation principle)

?

?

10. How many bits of the system are 8086? How is it implemented on the data bus?

The 8086 system is a 16-bit system with a data bus of 20 bits.

?

?

1.2 Programming

?

1. Write a recursive algorithm for solving n-order factorial problems by C language:

Long int fact (int n)

{

int x;

long int y;

if (n<0)

{

printf ("error!");

}

if (n==0)

return 1;

x=n-1;

Y=fact (x);

return (n*y);

}

?

2. Two-point lookup algorithm:

1) Recursive method implementation:

int bsearch (elemtype a[],elemtype x,int low,int High)

/* The next term is low, and the upper bound is high in array a binary find the data element x*/

{

int mid;

if (Low>high)

return-1;

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 5/24

Mid= (Low+high)/2;

if (X==a[mid])

return mid;

if (X<a[mid])

Return (bsearch (a,x,low,mid-1));

Else

Return (bsearch (A,x,mid+1,high));

}

?

2) Non-recursive method implementation:

int bsearch (elemtype a[],keytype key,int N)

{

int low,high,mid;

low=0;high=n-1;

while (Low<=high)

{

Mid= (Low+high)/2;

if (A[mid].key==key)

return mid;

else if (A[mid].key<key)

low=mid+1;

Else

High=mid-1;

}

return-1;

}

?

3. Recursively calculates the value of the recursive function as follows (Fibonacci):

F (1) =1

F (2) =1

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 6/24

F (n) =f (n-1) +f (n-2) n>2

Solution:

int f (int n)

{

int i,s,s1,s2;

S1=1;/*S1 to save the value of f (n-1) */

S2=1;/*s2 to save the value of f (n-2) */

S=1;

for (i=3;i<=n;i++)

{

S=S1+S2;

S2=S1;

S1=s;

}

return (s);

}

?

4. Exchange two numbers without a third block of memory:

int a = "";

int b = "";

A = a + B;

b = a A;

A = a-B;

Or:

A = a ^ b;

b = a ^ b;

A = a ^ b;

?

5. Bubble Sort:

void Bubblesort (Elemtype x[],int N)

{

int i,j;

Elemtype temp;

for (i=1;i<n;i++)

for (j=0;j<n-i;j++)

{

if (X[j].key>x[j+1].key)

{

TEMP=X[J];

X[J]=X[J+1];

X[j+1]=temp;

}

}

}

?

6. C Language file reading and writing

#include "stdio.h"

Main ()

{

FILE *FP;

Char ch,filename[10];

scanf ("%s", filename);

if ((Fp=fopen (filename, "w") ==null)

{

printf ("Cann ' t Open file\n");

Exit (0);

}

Ch=getchar ();

while (ch!= ' # ')

{

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 8/24

FPUTC (CH,FP);

Putchar (CH);

Ch=getchar ();

}

Fclose (FP);

}

?

7. Programming WinSocket

#include <Winsock2.h>

#include <stdio.h>

void Main ()

{

WORD wversionrequested;

Wsadata Wsadata;

int err;

wversionrequested = Makeword (a);

Err = WSAStartup (wversionrequested,&wsadata);

if (err! = 0)

{

Return

}

if (Lobyte (wsadata.wversion)! = 1| | Hibyte (wsadata.wversion)! = 1)

{

WSACleanup ();

Return

}

SOCKET Socksrv=socket (af_inet,sock_stream,0);

Sockaddr_in addrsrv;

Addrsrv.sin_addr. S_un. S_addr=htonl (Inaddr_any);

Addrsrv.sin_family=af_inet;

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 9/24

Addrsrv.sin_port=htons (6000);

Bind (Socksrv, (sockaddr*) &addrsrv,sizeof (sockaddr));

Listen (socksrv,5);

Sockaddr_in addrclient;

int len=sizeof (SOCKADDR);

while (1)

{

SOCKET sockconn=accept (Socksrv, (sockaddr*) &addrclient,&len);

Char sendbuf[100];

Sprint (SendBuf, "Welcome%s to http://www.sunxin.org",

Inet_ntoa (ADDRCLIENT.SIN_ADDR));

Send (Sockconn,sendbuf,strlen (SENDBUF) +1,0);

Char recvbuf[100];

Recv (SOCKCONN,RECVBUF);

printf ("%s\n", recvbuf);

Closesocket (Sockconn);

WSACleanup ();

}

}

Note: This is the server side; file->new->win32 Console application, project name: Tcpsrv; then, file-new->c++ Source file, file name: tcpsrv In the project of the setting link of object/library modules to join the Ws2_32.lib

#include <Winsock2.h>

#include <stdio.h>

void Main ()

{

wordwversionrequested;

Wsadata Wsadata;

int err;

wversionrequested = Makeword (a);

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 10/24

Err = WSAStartup (wversionrequested,&wsadata);

if (err! = 0)

{

Return

}

if (Lobyte (wsadata.wversion)! = 1| | Hibyte (wsadata.wversion)! = 1)

{

WSACleanup ();

Return

}

SOCKET Sockclient=socket (af_inet,sock_stream,0);

Sockaddr_in addrsrv;

Addrsrv.sin_addr. S_un. S_ADDR=INET_ADDR ("127.0.0.1");

Addrsrv.sin_family=af_inet;

Addrsrv.sin_porthtons (6000);

Connect (sockclient, (sockaddr*) &addrsrv,sizeof (sockaddr));

Char recvbuf[100];

Recv (sockclient,recvbuf,100,0);

printf ("%s\n", recvbuf);

Send (Sockclient, "This was Zhangsan", strlen ("This is Zhangsan") +1,0);

Closesocket (sockclient);

WSACleanup ();

}

Note: This is the client side; file->new->win32 Console application, project name: TcpClient; then, file->new->c++ Source File, File name: TcpClient; In the same vein, the project's setting link object/library modules to join Ws2_32.lib

?

?

8. Knowledge of C + + classes

#include <iostream.h>

Class human

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 11/24

{

Public

Human () {human_num++;};

static int human_num;

~human ()

{

human_num--;

Print ();

}

void print ()

{

cout<< "Human num is:" <

}

Protected

Private

};

int human::human_num = 0;

Human F1 (Human x)

{

X.print ();

return x;

}

int main (int argc, char* argv[])

{

Human H1;

H1.print ();

Human H2 = F1 (H1);

H2.print ();

www.dajie.com-China's most advanced interactive job search platform for college students

Www.dajie.com 12/24

return 0;

}

Output:

1

1

0

0

-1

-2

----------------------------

Analysis:

Human H1; Call the constructor,---Hum_num = 1;

H1.print (); Output: "Human is 1"

Human H2 = F1 (H1); In the process of calling F1 (H1), because the function parameter is to pass the object by value, the default copy constructor is called, and it is not on hum_num++, so hum_num still = 1, so the X.print () output: "Human is 1"; To destroy X when the F1 function is rolled out, call the destructor (human_num--), Output: "Human is 0" (since the function returns a human object, call the default constructor, create a temporary object (human_num = 0;), assign the temporary object to H2, The default constructor is also called (human_num = 0); H2.print (); Output: human is 0;

When exiting the main () function, destroy H2, call the destructor (human_num--), output "Human_num is-1" and then destroy H1, call the destructor (-), output "Human_num is-2"

C,c++ Classic Questions

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.