C Language test questions (feel better topic)

Source: Internet
Author: User

1. What is the difference between a static global variable and a normal global variable? What is the difference between a static local variable and a normal local variable? What is the difference between a static function and a normal function?

A: The static global variable--valid only within the source file in which the variable is defined, is initialized once and is prevented from being referenced in other file units.

The normal global variable----is valid in each source file.

The same is the static storage method.

Static local variable--the function is initialized one time throughout the life cycle, and the next one is based on the previous result.

The normal local variables----function within the current function, and the life cycle is only within the function and ends with the end of the run.

The static function, which acts in this file, is an intrinsic function.

The normal function----functions that can be used outside of the current source file, which should be described in a header file, which is to be included in the source file of these functions.

The static function has only one copy in memory, and the normal function maintains one copy of each call.

2, write out the following code output content

#include <stdio.h>

INT Inc (int a)

{return (++a);}

int multi (INT*A,INT*B,INT*C)

{return (*c=*a**b);}

typedef int (FUNC1) (int in);

typedef int (FUNC2) (int*,int*,int*);

void Show (FUNC2 fun,int arg1, INT*ARG2)

{

FUNC1 p=&inc;

int temp =p (arg1);

Fun (&TEMP,&ARG1, arg2);

printf ("%dn", *arg2);

}

Main ()

{

int A; The local variable a is 0;

Show (Multi,10,&a);

return 0;

}

Answer:

3. Please find out all the errors in the code below (good title, worth a look)

Description: The following code is the reverse of a string, such as "ABCD" after the reverse into "DCBA"

#include "string.h"

Main ()

{

Char*src= "Hello,world";

char* Dest=null;

int Len=strlen (SRC);

Dest= (char*) malloc (len);

char* d=dest;

char* s=&src[len-1];

while (Len--! =0)

*d++=*s--;

printf ("%s", dest);

Free (dest);

Dest=null;

return 0;

}

For:

Method 1: A total of 4 errors;

int main ()

{

char* src = "Hello,world";

int len = strlen (src);

char* dest = (char*) malloc (len+1);//To allocate a space

char* d = dest;

char* s = &src[len-1]; Point to last character

while (len--! = 0)

*d++=*s--;

*d = 0; The tail is going to be ' plus '.

printf ("%sn", dest);

Free (dest); When you are finished, you should free up space to avoid a memory sink leak

Dest = NULL; Prevents the creation of wild pointers

return 0;

}

Method 2: (method one requires additional storage space, not high efficiency.) Good idea.

#include <stdio.h>

#include <string.h>

Main ()

{

Char str[]= "Hello,world";

int Len=strlen (str);

Char T;

for (int i=0; i<len/2; i++)

{

T=STR;

STR=STR[LEN-I-1]; Be careful.

str[len-i-1]=t;

}

printf ("%s", str);

return 0;

}

4. What kinds of software tests are there?

Black box: Testing for system functions

White box: Test function function, each function interface.

5. Test method

A: Manual testing: Individual review, spot checks and triage

Machine test: Black box test and white box test

6, unsigned char *p1;

unsigned long *p2;

p1= (unsigned char *) 0x801000;

p2= (unsigned long *) 0x810000;

May I ask p1+5=;

p2+5=;

Answer: 0x801005 (equivalent to add 5 bits) 0x810014 (equivalent to 20 bits);

7, Windows message scheduling mechanism is C

A. Command queue; B. instruction Stack; C. Message Queuing; D. message stack;

8, TCP/IP communication established process, what is the port role?

Three-time handshake to determine which application uses the protocol

9, #define MAX_CB 500

void Lmiquerycsmd (Struct MSGCB * pmsg)

{

unsigned char uccmdnum;

......

for (uccmdnum=0;uccmdnum<max_cb;uccmdnum++)

{

......;

}

Answer: Dead loop, unsigned char range is 0~255

10.

The following is a procedure for calculating the square of a number, please find the error:

#define SQUARE (a) ((a) * (a))

int a=5;

int b;

B=square (a++);

A: The result is related to the compiler, and the resulting value may not be squared. (I'm showing 25 on my computer, 36-bit editor)

11. The difference between process and thread.

A: A thread is an execution unit within a process and a scheduled entity within a process.

Differences from the process:

(1) Dispatch: The thread acts as the basic unit of dispatch and allocation, and the process as the basic unit of resources

(2) Concurrency: Not only can concurrent execution between processes, but also concurrent execution between multiple threads of the same process

(3) Owning a resource: a process is an independent unit that owns resources, and threads do not own system resources, but they can access resources that belong to the process.

(4) Overhead: When you create or revoke a process, the overhead of the system is significantly greater than the cost of creating or revoking a thread, because the system allocates and reclaims resources for it.

12. The difference between heap and stack.

A: Heap is heap, stack is stack.

Stack space is automatically allocated/freed by the operating system, and space on the heap is allocated/released manually.

Stack space is limited, heap is a large free storage area

The memory space allocated by the malloc function in C is on the heap, which corresponds to the new operator in C + +.

The program allocates memory for variables and functions on the stack at compile time, and the arguments are passed on the stack when the function is called during the program's operation.

13. What is the difference between the following three?

char * const p;

Char Const * p

const CHAR *p

Answer:

char * const p; Constant pointer, the value of P cannot be modified

Char const * p;//pointer to constant, the constant value pointed to cannot be changed

const char *p;//and Char const *P

14, explain the following output results

Char str1[] = "ABC";

Char str2[] = "ABC";

const char str3[] = "ABC";

const char str4[] = "ABC";

const char *STR5 = "abc";

const char *STR6 = "abc";

char *STR7 = "abc";

char *str8 = "abc";

cout << (str1 = = str2) << Endl;

cout << (Str3 = = STR4) << Endl;

cout << (STR5 = = STR6) << Endl;

cout << (STR7 = = str8) << Endl;

The result is: 0 0 1 1

Answer: STR1,STR2,STR3,STR4 are array variables, they have their own memory space;

Whereas str5,str6,str7,str8 are pointers, they point to the same constant area.

15, point out the output of the following code, and explain why. (Good, the address of the deep potential to grasp)

Main ()

{

int a[5]={1,2,3,4,5};

int *ptr= (int *) (&a+1);

printf ("%d,%d", * (a+1), * (ptr-1));

}

Output: 2,5

* (a+1) is a[1],* (ptr-1) is a[4], the result of the implementation is 2,5

&a+1 is not the first address +1, the system will assume that the offset of an array of a, is offset by the size of an array (this example is 5 int)

int *ptr= (int *) (&a+1);

Then PTR is actually & (a[5]), i.e. a+5

The reasons are as follows:

&a is an array pointer whose type is int (*) [5];

and the pointer plus 1 to be based on the pointer type plus a certain value,

Different types of pointers +1 increase in size after different

A is an int array pointer of length 5, so add 5*sizeof (int)

So ptr is actually a[5]

However, PRT is not the same as (&a+1) type (this is important)

So prt-1 will only subtract sizeof (int*)

A,&a address is the same, but the meaning is not the same, a is the address of the first address of the array, that is, a[0], &a is the first address of the object (array), a+1 is the address of the next element of the array, that is, a[1],&a+1 is the address of the next object, that is, a[5].

16.

char* s= "AAA";

printf ("%s", s);

s[0]= ' B ';

printf ("%s", s);

What's wrong with that?

Answer: "AAA" is a string constant. S is a pointer to this string constant, so there is a problem when declaring S.

cosnt char* s= "AAA";

And then because it is a constant, the assignment to be s[0] is illegal.

17.

int main ()

{

Char A;

Char *str=&a;

strcpy (str, "Hello");

printf (str);

return 0;

}

A. No memory space allocated for STR, exception will occur

The problem is in copying a string into the address indicated by a character variable pointer. Although the results can be output correctly, the program crashes because of an internal read and write across the bounds.

strcpy is in the library function string.h. The main error of the program is that the memory reads and writes out of bounds causing the program to crash//

18.

What is the role of the keyword static?

A: 1) defines a static local variable, scoped from the beginning of the function to the end.

2) The static function within the module can only be called by other functions within the module, and the use of the function is limited to the module in which it is declared;

3) static member variables in a class are owned by the entire class and have only one copy of all objects of the class

19.

What is the meaning of the keyword const?

A: 1) represents a variable that cannot be modified by a constant.

2) parameters can be modified as input parameters.

3) modifier function to prevent other changes.

4) Modifies a member function of a class without changing the data members in the class.

20.

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

For:

The main difference between structs in C and C + + is that structs in C can not contain member functions, whereas structs in C + + can. The main difference between struct and class in C + + is that the default access permissions are different, struct defaults to public, and class defaults to private.

21. is Atool () a function that converts a character to an integer in a C-Library function, what is the prototype of this function?

Function Name: ATOL

Function: Convert string to grow integer number

Usage: Long atol (const char *nptr);

#include <stdio.h>

int main ()

{

Long L;

Char *str= "123456789";

L = atol (str);

printf ("str=%s,l=%d\n", str,l);

return 0;

}

22. What are the problems in the following 4 examples, please one by one indicate:

(1),

void GetMemory (char *p)
{
  p = (char *) malloc (100);
}

void Test (void)
{
  char *str = NULL;
  GetMemory (str);
  strcpy (str, "Hello World");
  printf (str);
}
(2),
Char *getmemory (void)
{
  Char p[] = "Hello World";
  return p;
}

void Test (void)
{
  char *str = NULL;
  str = GetMemory ();
  printf (str);
}
(3),
void GetMemory (char **p, int num)
{
  *p = (char *) malloc (num);
}

void Test (void)
{
  char *str = NULL;
  GetMemory (&AMP;STR, 100);
  strcpy (str, "Hello");
  printf (str);
}
(4),
void Test (void)
{
  Char *str = (char *) malloc (100);
  strcpy (str, "Hello");
  Free (str);
  ...//omitted other statements
}

(1), getmemory (char *p) functions are string pointers, modifying the parameters inside the function does not really change the values of the incoming parameters, and executes char *str = NULL; GetMemory (str); str is still null;
(2), char p[] = "Hello World"; return p; p[" array is a local automatic variable within the function, and after the function returns, the memory is freed. This is a common mistake many programmers make, the root of which is the failure to understand the lifetime of a variable.
(3 ), getmemory avoids the question 4 problem, incoming getmemory is a pointer to a string pointer, but executes the request memory and assignment statement in getmemory Lang= "en-us" xml:lang= "en-us" >*p = (char *) malloc (num), after which the memory is not determined for success, should be added:

if (*p = = NULL)
{
 ...//To request memory failure handling
}

(4), test questions4 Existence and questions3 The same problem in the executionChar *str = (char *) malloc (100); no memory is applied for successful judgment; In addition, theFree (str) after not placedSTR is empty, resulting in the possibility of becoming a "wild" pointer, plus:
str = NULL; questions3 ofThe test function is also not in theThe memory of Malloc is released.

23.Let's see what's wrong with the following program:
Swap (int* p1,int* p2)
{
  int *p;
  *p = *P1;
  *P1 = *P2;
  *P2 = *p;
}
InSwap function,P is a "wild" pointer, which may point to the system area, causing the program to run in a crash. InVC + + inDebug Runtime Prompt Error "Access violation ". The procedure should read:
Swap (int* p1,int* p2)
{
  int p;
  p = *p1;
  *P1 = *P2;
  *P2 = p;
}
24. Windows nt 32-bit c++ program, please calculate sizeof
void Func (char str[100])
{
sizeof (str) =?
}
void *p = malloc;
sizeof (p) =?
Answer:
sizeof (str) = 4
sizeof (P) = 4
Anatomy:
func (char str[100]) functions in the function body, the array name loses its own connotation, just a pointer In the loss of its connotation, it also loses its constant characteristics, can be self-increment, self-reduction and other operations, can be modified. array name is essentially the following:
(1) array name refers to a data structure, this data structure is an array; For example:

Char Str[10];cout <<sizeof (str) << Endl; output is ten,str refers to data structure CHAR[10].
(2) The array name can be converted to a pointer to its reference entity, and is a pointer constant, can not be self-increment, self-reduction and other operations, cannot be modified;char str[10]; str++;//Compile error, indicating str is not an lvalue
(3) when the array name is a function parameter, it is reduced to a normal pointer. under the Windows NT 32-bit platform, the length of the pointer (memory size) is 4 bytes, so sizeof (str) andsizeof (p) are 4.

C language question (feeling better)

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.