C Language Interview

Source: Internet
Author: User
Tags mul

P1 (multiple selection)
has the following definition int A; int *b; Which of the following statements is correct:

a:b=&a;
B:b=*a;
c:b= (int*) A;
D: *b=a;


The idea is as follows: b is an int type pointer, a is an int variable, the relationship between the two is very clear, select AD.


P2 (radio)
Has the following definition
char* const S1 = "string";
char Const *S2 = "string"; This sentence also equals the const char* s2 = "string";
Which of the following statements is correct:

A:S1 = "W";
B: *s1 = ' W ';
C:S2 = "W";
D: *s2 = ' W ';


The idea is as follows: S1 is a constant pointer, pointing to a constant string. The direction of itself cannot be changed, and the part that points to it is also constant and cannot be changed. The AB operation is problematic.
S2 is a variable pointer to a constant string that can be directed to make a change, and it is wrong to mention its contents and modify it. Select C.


P3 (multiple selection)
Has the following definition
char* S1 = "string";
Char s2[] = "string";
Which of the following statements is correct:

A: *s1 = ' W ';
B:S1 = "W";
C: *s2 = ' W ';
D:S2 = "W";


The idea is as follows: S1 is a char pointer pointing to a constant string, which can be changed by its own direction;
S2 is a fixed length of buffer, we can pick and change its contents, cannot change its point, because S2 this name and this buffer is one, such as to change the buffer, need String.h series API.
Select BC.


P4 (radio)
Has the following definition
#define SQUARE (x) x*x
The value of square (square) is:

A:6
B:10
C:18
D:32


The idea is as follows: To do this problem has a lot of confusion, I also see the results of the operation to understand, when solving the problem of the thinking of the fixed bracket, the actual completely regardless of parentheses can be written:
1+1*1+1*1+1*1+1*2 = 6, select a.


P5 (radio)
A pointer to a shaped array is defined as:

A:int (*PTR) []
B:int *ptr[]
C:int * (ptr[])
D:int ptr[]


Really powerless, this problem I think B and C are right, everybody help


P6 (radio)
B=2;c=1 which of the following statements causes the value of a to be 1:

a:a=b++;
B:a=++b;
c:a=++ (c+1);
D:a= (b+1) + +;
E: None of the above


The idea is as follows: Each cumulative result is more than 1, there is no correct, so choose E.


P7 (radio)
There are the following definitions of Char tt[256]; then the value of sizeof (TT) is:

A:1
b:256
C:4
d:0


The idea is as follows: Since the variable is a char array, the natural byte count is 256*sizeof (char), the result is 256, select B.


P8 (radio)
Please write out the output of the following program
Main ()
{
Char text[] = "Abcdefghijklmno";
int i = 0;
TEXT[12] = ' n ';/M is cleared
while (text[++i]! = ' + ')//present a trend isolated from one, 1,3,5 ...
{
printf ("%c", text[i++]);
}
}

A: "Acegik"
B: "Bdfhjln"
C: "Bdfhjl"
D: "Acegiko"


Idea: By adding the notes to see the law, when the progress to MN, although M zero, but does not prevent access to its subsequent elements, select B.


P9 and P10 are based on the following procedures:
This is a bubble-sort program, please fill in the appropriate code in the blanks
#include <stdio.h>
#include <string.h>


void sort (char s[], int nnum)
{
int i,j;
char temp;
for (i = 0; i < nnum; i++)
{
for (j = i+1; J < Nnum; J + +)
{
if (S[i] < s[j])
{
temp = S[i];
P9;
P10;
}
}
}
}
void Main ()
{
Char str[]= "OLWF";
Sort (str, 4);
printf ("%s", str);
}

P9: (Radio)
A:s[i] = S[j];
B:S[J] = temp;
C:S[J] = s[i];
D:temp = S[j];
P10: (Radio)
A:s[i] = S[j];
B:S[J] = temp;
C:S[J] = s[i];
D:temp = S[j];


Idea: This bubbling algorithm is still very clear, choose A and B.




//The following issues P11-P20 will be debugged in memory management, stack management, forest, and the C language implementation of the two fork tree
#include <stdio.h>
#include <string.h>
The following are definitions for constants, types, and function pointers
#define Memory_size102400
typedef void* PTR;
typedef unsigned charbyte;
typedef PTR (*nodefunc) (void);
typedef void (*DOFUNC) (int n,ptr p);
The following is a definition of type definition and processing function for memory management
typedef struct MEMORY*PCMEMORY;
typedef struct MEMORY
{
BYTE Amem[memory_size];
int nmempoint;
}cmemory;
void Minit (Pcmemory pmem); Memory initialization
PTR MAlloc (pcmemory pmem, int nSize); Memory allocation functions
The following is the definition of type definition and handler for stack management
typedef struct STACK*PCSTACK;
typedef struct STACK
{
PTR pcontent;
Pcstack Pnext;
}cstack;
Pcstack snew (); Stack initialization
Pcstack Spush (Pcstack pstk, PTR p);//Stack Pit stop
Pcstack SPop (Pcstack pstk,ptr* pp); Stack out Stack
The following is a definition of the type definition and processing function for forest processing represented by a binary tree
typedef struct TREE*PCTREE;
typedef struct TREE
{
PTR pcontent;
Pctree Pson;
Pctree Pbrother;
}ctree;
Pctree tnew ();//Initialize a forest
Add a son or brother to a root node or subtree node
void Taddson (Pctree ptree,nodefunc f);
void Taddbrother (Pctree ptree,nodefunc f);
Iterating using the recursive method and the Stack method logarithm
void Tbrowse (Pctree ptree, dofunc F, int n);
void Tbrowsestack (Pctree ptree, dofunc F, int n);
The following is the global variable definition for the memory entity
Static Cmemory mem;
Pcmemory pmem=&mem;
The following is the implementation of the memory management handler function
void Minit (Pcmemory pmem)
{
Pmem->nmempoint = 0;
}//Minit
PTR MAlloc (pcmemory pmem,int nSize)
{
int ncurmempoint;
ncurmempoint=pmem->ncurmempoint;
Pmem->ncurmempoint + = NSize;
ReturnP11;
}//MAlloc
The following is the implementation of the processing function for stack processing
Pcstack Spush (Pcstack pstk,ptr p)
{
Pcstack pstknew;
Pstknew=malloc (pmem,sizeof (cstack));
pstknew->pcontent=p;
pstknew->pnext=pstk;
ReturnP13;
}//Spush
/pcstack Spop (pcstack Ptsk, ptr* pp)
{
if (Ptsk! = NULL)//P14
{
*PP = ptsk->pcontent;
Ptsk = ptsk->pnext;
return Ptsk;
}
return NULL;
}//Spop
Pcstack snew ()
{return NULL;}
The following is the implementation of a binary tree representation of forest processing functions
Pctree tnew ()
{
Pctree Proot;
Proot = MAlloc (pmem,sizeof (ctree));
proot->pbrother=null;
proot->pson=null;
Proor->pcontent= "ROOT";
return proot;
}//tnew//P15
void Taddson (Pctree ptree, Nodefunc f)
{
PTR p=f ();
Pctree ptreenew;
if (p==null)
Taddbrother (PTREE,F);
Else
{
Ptreenew=malloc (pmem,sizeof (ctree));
ptreenew->pbrother=null;
ptreenew->pson=null;
ptreenew->pcontent=p;
Ptreenew->pson= ptreenew;
Taddson (PTREENEW,F);
p16-1;//P16
}
}//Taddson
void Taddbrother (Pctree ptree,nodefunc f)
{
PTR P = f ();
Pctree ptreenew;
if (p==null)
Return
Else
{
Ptreenew=malloc (pmem,sizeof (ctree));
ptreenew->pbrother=null;
ptreenew->pson=null;
ptreenew->pcontent=p;
ptreenew->pbrother=ptreenew;
Taddson (PTREENEW,F);
p16-2;//P16
}
}//Taddbrother
void Tbrowse (Pctree ptree,dofunc f,int N)
{
if (ptree! = NULL)
{
P17; P17
Tbrowse (ptree->pson,f,n+1);
Tbrowse (Ptree->pbrother,f,n);
}
}//Tbrowse
void Tbrowsestack (Pctree ptree, dofunc F, int n)
{
Pcstack pstk=snew ();
while (1)
{
P17;
if (ptree->pson==null)
{
P18;//P18
{
Pstk=spop (Pstk,&ptree);
n--;
if (pstk==null)
Return
}
ptree=ptree->pbrother;
}
Else
{
Pstk=spush (Pstk,ptree);
n++;
ptree=ptree->pson;
}
}
}//Tbrowse
Here are the test functions and data
Static char* aanode[22]=
{
"1", "2", "3", NULL, "4", NULL, "5", Null,null, "6", NULL, "7", "8", NULL, "9", "ten", NULL, "One", NULL, "Null,null",
};//P19
static int nnode=0;
PTR Node () {return aanode[nnode++];}
void out (int n, PTR p)
{
int i;
for (i=0;i<n;i++)
printf ("_");
printf ("%s\n", p);
}//out


void Main ()
{
Pctree Ptree;
Minit (PMEM);
Ptree=tnew ();
Taddson (Ptree,node);
Tbrowse (ptree->pson,out,0);
Tbrowsestack (ptree->pson,out,0);//P20
}//main


The following are the correct output results
1
_2
__3
__4
__5
_6
_7
__8
__9
___10
___11
___12
1
_2
__3
__4
__5
_6
_7
__8
__9
___10
___11
___12


P11 (radio)
The statement that should be written at P11 is

A: (void*) (& (Pmem->amem[ncurmempoint))
B: (void*) (Pmem->amem[ncurmempoint])
C: (void*) (& (Pmem->amem[pmem->nmempoint]));
D: (void*) (Pmem->amem[pmem->nmempoint])
E: None of the above


The idea is as follows: The function is to delimit a portion of the buffer, and then the cursor record buffer is added to the used amount, then the return of the available memory part of the starting address is where the last cursor, select a.




P12 (multiple selection)
For the above memory management type definition and implementation functions are described correctly:

A: The type defines space in the static storage of the program, cannot be used for dynamic memory allocation, and should be allocated using functions such as malloc
B: No memory allocation out of bounds check
C: cannot be used to allocate memory for long integers and double integers, otherwise it will cause memory errors
D: Without regard to the effect of computer word length, memory errors may be caused on machines such as the sun workstation in words instead of bytes
E: cannot be used to allocate memory for complex variables defined with a struct, otherwise it will cause memory errors


The idea is as follows: A, correct, the memory allocation here is the surface, the actual is in a static large buffer to manage the use of memory.
B, it's correct, there's no scope check for Nmempoint.
CD two, not sure, guys, help analyze and analyze.
E, the argument is correct, if there are complex types in struct structure, then the actual size may exceed the size of sizeof (struct) =4, there is a potential error.




P13 (radio)
The statements to be written at P13 are:

A:pstk+1
B:pstk->pnext
C:pstk
D:pstknew
E: None of the above


The idea is as follows: Each call to push once, there is a new pcstack element, return is the topmost one, that is, pstknew, choose D.




P14 (radio)
The function of the statement at P14 is

A: Prevent the stack pointer from looping
B: Check the legality of stack contents
C: Determine if the stack is empty
D: Prevent memory overflow from stack usage
E: None of the above


Idea: Before manipulating the target pointer, it is empty, and if the pointer is invalid, the operation is discarded and C is selected.




P15 (multiple selection)
According to this creation function it can be judged that this forest

A: a forest that is sorted by content
B: Only strings can be processed
C: There is a virtual root node proot, the true root node is Proot->pbrother
D: There is a virtual root node proot, the true root node is Proot->pson
E: If you take advantage of the root node's pbrother, you can handle multiple forest situations


Idea: From the title description, the forest is represented by a binary tree, which is like turning a two-fork tree to the left by 45 degrees; from the main function, we can see the result of printing, manipulate the array, and assign the 1--12 to each node in order.
a correct; b error; The new root node, Addson, starts from node 1th, after which there are 1--12 numbers, 1th nodes are the top nodes of their 12 nodes, the true root node is the child node of node 1th ———— root, D is correct;
e Correct, node 1th is the child node that has been used, then you can also use the root of the brother, and then create a forest.


P16 (radio)
The statements that p16-1 and p16-2 should write are

A:[1]taddbrother (ptree,f) [2]taddbrother (Ptree,f)
B:[1]taddbrother (PTREE,F) [2] None
C:[1] None [2]taddbrother (Ptree,f)
D:[1]taddbrother (ptreenew,f) [2]taddbrother (Ptreenew,f)
E:[1]taddbrother (PTREENEW,F) [2] None
F:[1] None [2]taddbrother (Ptreenew,f)


Idea: This recursion is not yet particularly clear, the personal choice is E.




P17 (radio)
The function of a P17 statement is to manipulate the contents of a tree node using a function pointer, which should be

A:f (n,ptree->pcontent)
B:f (N,ptree)
C:f (n,& (ptree->pcontent))
D:f (N,&ptree)
E:f ()


Idea: This place is called out callback function, typed element value, the first parameter is N, the second item is a pointer type, is the content of Pcontent point, select a.




P18 (radio)
The function of the statement at P18 is

A:while (Ptree = = NULL)
B:if (Ptree ==null)
C:while (Ptree->pson = = NULL)
D:if (Ptree->pson = = NULL)
E:while (Ptree->pbrother = = NULL)
F:if (ptree->pbrother== NULL)


Idea: Choose F.


P19 (multiple selection)
The meaning of NULL in array Aanode is

A: The current node no longer has a son
B: The current node no longer has a brother
C: The contents of the current node are empty
D: current node neither son nor brother
E: The current node must have a son
F: The current node must have a brother


Idea: Choose AB.


P20 (multiple selection)
For the use of recursive method and stack method for tree traversal in this program, the above description is correct

A: In terms of algorithmic complexity, the recursive method is O (n) and the stack method is O (NLOGN)
B: In terms of algorithmic complexity, the recursive method is O (Nlogn), and the Stack method is O (n)
C: As far as algorithmic complexity is concerned, the recursive method is the same as the Stack method
D: In recursive methods, the call stack needs to be allocated multiple times for local variables and parameters, which may cause the call stack to overflow
E: The stack method requires additional action, and the program is complex, so the speed is slow


Idea: Choose Bd.


the following issues P21-P25 based on the following procedure
A connected area is divided irregularly into n different small areas, and each small area is critical to several other small areas. is now used in different colors of the CN color for the region, requiring each small area of a color, adjacent to the small area of the same color.
Set small areas to be numbered sequentially as 0,1,...n-1. The adjacency between each small area and other small areas is represented by a two-dimensional array bordering, and the element Bordering[i][j] represents the adjacency between small area I and small J-Regions:
bordering[i][j]=0; J Small area not adjacent to I small area
Bordering[i][j]=1; J Small area adjacent to I small area
Program, the results are stored in a two-dimensional array colored, the color number is 0,1,...cn-1, element colored[color][j] meaning is:
colored[color][j]=0; J small area without color coloring
Colored[color][j]=1; J Small area colored color coloring
The function colorcountry (BORDERING,COLORED,N,CN) is based on the given small area adjacency relation array bordering, small area number n, color number cn, the found coloring scheme is recorded in the array colored, the function uses the heuristic method to find out, First, from the first small area, look at the first color start order for each small area to find a coloring scheme, for a small area, when he found a color that is not shaded by his adjacent small area, it is used to color the small area, and ready to process the next small area. Backtracking when a small area cannot be found with a color that is not shaded by his adjacent small area. If a shading scheme is eventually found for all small areas, the function returns 1, otherwise, 0 is assumed, the program assumes that the number of small regions is not more than 20, and the number of colors is 4.


Program
#include <stdio.h>
#define N 20
#define CN 4


int colorcountry (int bordering[][n], int colored[][n], int N, int cn)
{
int color,used,i,c;
for (color=0; color<cn; color++)
/* Set all areas not shaded */
for (i=0; i<n; i++)
colored[color][i]=0;
c=0; Start with the first small area
color=0; Start with the first color test
while (C<n)
{//loop when all small areas have not been shaded
WhileP21//order to test each color
{//Check whether the current color has been shaded by an adjacent small area
for (i=0,used=0;!used&&i<c; i++)
IfP22) used = 1;
if (!used)
Break The current color is not shaded by adjacent small areas
color++;
}
if (!used)
{//Find available colors, color them with this color, and prepare to process the next small area
P23;
color=0;
}
Else
{
No available color found, backtracking
c--;
if (c < 0)
return 0; Found no solution
for (color=0;P24;color++)
P25;
}
}
return 1;
}
void print (int colored[][n],int n,int cn)//Print Results
{
char* colortbl[] = {"Red", "Blue", "Green", "Yellow"};
int color,i;
for (color=0; color<cn; color++)
{
printf ("\n%s:\n", Colortbl[color]);
for (i=0; i<n; i++)
FF (Colored[color][i])
printf ("\t%d", I);
printf ("\ n");
}
}
int colored[cn][n],bordering[n][n];
void Main ()
{
int i,j,n;
printf ("Enter Number of areas");
scanf ("%d", &n);
printf ("Enter bordering:\n");
for (i=0; i<n; i++)
for (j=0;j<n;j++)
bordering[i][j]=0;
for (i=0;i<n;i++)
{
printf ("Enter areas to link%d area (<0 to next) \ n", i);
scanf ("%d", &j);
while (j>=0)
{
if (i!= j)
Bordering[i][j]=bordering[j][i]=1;
scanf ("%d", &j);
}
}
if (colorcountry (bordering, colored, N, cn))
Print (COLORED,N,CN);
Else
printf ("No solution.\n");
}
//Problem (P21-p25 are single-radio)
P21:

A:color<=cn
B:color<cn
C:color++<=cn
D:color++<cn


Idea: The judging condition here is the color of the traversal, select B.


P22:
a:bordering[c++][i]== 1 && colored[color][i] = = 1
b:bordering[c+1][i]== 1 && colored[color][i] = = 1
c:* (bordering[c+1]+i}== 1 && colored[color][i] = = 1
d:bordering[c][i]== 1 && colored[color][i] = = 1


Idea: Judging the shading array and adjacency array, the second half is the same, the first half, just want to single-action target, look at its adjacent area can be, bordering line number fixed, select D.


P23:
A:colored[color][c]=1
B:colored[color][c+1]=1
C:colored[color][++c]=1
D:colored[color][c++]=1


IDEA: Coloring the target area, c self-increment, start against the next line (next region), select D.


P24:
A:COLORED[COLOR][C] = = 0
B:COLORED[COLOR][C]
C:colored[color+1][c]==0
D:COLORED[COLOR+1][C]


Idea: When backtracking begins, the previous area of the scheme cannot be found to be shaded, and the condition of the traversal is to find the color of the line and select B.


P25:
A:colored[color+1][c]=0
B:colored[color-1][c]=0
C:colored[color][c]=0
D:colored[color++][c]=0


Idea: Then the previous question, find the coloring records, reset to 0, select C.


This program is a simple counter simulation program. For any of the correct arithmetic expressions, the program calculator results expand the output. The operation component in an expression is a sign-free integer. The operator is + 、-、 *,/, and parentheses are matched by regular pairing, and the expression ends with the character ' = '.
The function Gettach () is set for unifying subtraction computations by getting a valid character for the expression and storing the character in the variable curch, the function pointer array func[].
Program
#include <stdio.h>
#include <string.h>


int add (int x,int y) {return x + y;}
int sub (int x, int y) {return x-y;}
int mul (int x, int y) {return x*y;}
int div (int x, int y) {return x/y;}
Int (*func[]) (int x, int y) = {Add, sub, mul, Div};
int num, curch;
Char chtbl[] = "+-*/() =";
Char corch[] = "+-*/() =0123456789";
int Getach ()
{
unsigned int i;
while (true)
{
Curch=getchar ();
if (curch=eof)
return-1;
for (i=0; corch[i]&&curch!=corch[i]; i++);
if (I < strlen (Corch))
Break
}
return curch;
}
int GetID ()
{
int i;
if (curch>= ' 0 ' && curch<= ' 9 ')
{
for (num=0; curch>= ' 0 ' && curch<= ' 9 '; Getach ())
num=P26;
return-1;
}
Else
{
for (i=0; chtbl[i]; i++)
if (chtbl[i] = = Curch)
Break
if (I <= 5)
Getach ();
return i;
}
}
int cal ()
{
int x1,x2,x3,op1,op2,i;
I=getid ();
if (i== 4)
X1 = cal ();
Else
x1= num;
Op1=getid ();
if (op1>=5)
return x1;
I= GetID ();
if (i = = 4)
x2 = cal ();
Else
x2 = num;
OP2 = GetID ();
WhileP27)
{
I=getid ();
if (i==4)
x3 = cal ();
Else
x3 = num;
if ((op1/2==0) && (op2/2==1))
x2 = Func[op2] (x2,x3);
Else
{
X1 =P28;
x2 = x3;
P29;
}
OP2 = GetID ();
}
ReturnP30;
}


void Main ()
{
int value;
printf ("Please input an expression:\n");
Getach ();
while (curch! = ' = ')
{
Value = Cal ();
printf ("The result is:%d\n", value);
printf ("Please input an expression:\n");
Getach ();
}
}


//Problem (P26-p30 are single-radio)
P26:

A: (num+1) *10+curch-1-' 0 '
B: (num+1) *10+curch-' 0 '
c:num*10+curch-' 0 '
D:num*10+curch-1-' 0 '


Idea: here to collect input numbers, to get the bits one by one, first acquired with the number of bits increased, each multiply by 10, select C.


P27:
A: (op2>=0) && (op2<6)
B: (op2<6)
C: (op2>=0) && (op2<5)
D:op2>=0


Idea: Here to judge the OP2 operator, if it is ' = ', ', ' the results of the direct calculation of X1 and x2, do not walk the while loop directly back, if it is "+-*/" this, there is room for calculation, need while loop, select C.


P28:
A:FUNC[OP2] (X1,X2)
B: (*func[op2]) (X1,X2)
C: (*func[op1]) (X2,X2)
D:FUNC[OP1] (X1,X2)


Idea: This else area corresponds to if the meaning is: if the operator between X2 and X3 is multiplication, X1 and X2 is the addition and subtraction, then the first calculation x2 and x3, the result is considered X2;
corresponding, regardless of this operator precedence, is the left-to-right iteration of the first Count X1 and x2, the operator is op1,func at the time of declaration is a function pointer variable, directly called, select D.


P29:
A:op2=op1
B:op1=op2
C:X3=FUNC[OP1] (X1,X2)
D:x3= (*func[op1]) (X1,X2)


Idea: Then the previous question, X3 after the iteration into the X2, then the OP2 operator is OP1, select B.


P30:
A:FUNC[OP2] (X1,X2)
B: (*func[op2]) (X1,X2)
C: (*func[op1]) (X1,X2)
D:FUNC[OP1] (X1,X2)


Idea: From the above iterative method can be seen, whether it is a number of operations, need to become X1 op1 x2 form, if not go while loop, directly see the X1 and X2 operation is enough, choose D.

C Language Interview

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.