考查C++/C程序员的基本编程技能面试题

来源:互联网 发布:techmark 如何知乎 编辑:程序博客网 时间:2024/05/22 00:46

一、请填写BOOL , float, 指针变量 与“零值”比较的 if 语句。

提示:这里“零值”可以是0, 0.0 , FALSE或者“空指针”。例如 int 变量 n 与“零值”比较的 if 语句为:

    if ( n == 0 )                if ( n != 0 )

以此类推。

请写出 BOOL  flag 与“零值”比较的 if 语句:

答:if ( flag )

If ( !flag )

 

请写出 float  x 与“零值”比较的 if 语句:

答:const float EPSINON = 0.01;

if ((x >= -EPSINON)&&(x <= EPSINON ) )

 

请写出 char  *p 与“零值”比较的 if 语句:

答:if ( p == NULL )

    If( p != NULL )

 

二、以下为Windows NT下的32C++程序,请计算sizeof的值

       char  str[] = “Hello” ;

       char   *p = str ;

int     n = 10;

请计算

sizeof (str ) =  6      

         

sizeof ( p ) =  4    

          

sizeof ( n ) =  4

void Func ( char str[100])

{

请计算

 sizeof( str ) =   4

}

 

void *p = malloc( 100 );

请计算

sizeof ( p ) =  4

 

三、简答题

1const 有什么用途?(请至少说明两种)

 

 

2new/deletemalloc/free有何异同?

 

 

          3、简述进程和线程的区别?

 

 

 

4、多线程编程时,线程间同步的方法有哪些?

 

                          5、多线程编程,在一个线程中用Sleep()延时和用循环来延时有何区别?

 

 

四、有关内存的思考题

 

 

void GetMemory(char *p)

{

p = (char *)malloc(100);

}

void Test(void)

{

char *str = NULL;

GetMemory(str);  

strcpy(str, "hello world");

printf(str);

}

 

请问运行Test函数会有什么样的结果?

答:可能是乱码,程序崩溃

因为GetMemory不能传递动态内存

 

 

 

char *GetMemory(void)

{  

char p[] = "hello world";

return p;

}

void Test(void)

{

char *str = NULL;

str = GetMemory();   

printf(str);

}

 

请问运行Test函数会有什么样的结果?

答:可能结果是乱码

因为GetMemory返回的是指向“栈内存”的指针,该指针的地址不是 NULL,但其原现的内容已经被清除,新的内容不可知

Void GetMemory(char **p, int num)

{

*p = (char *)malloc(num);

}

void Test(void)

{

char *str = NULL;

GetMemory(&str, 100);

strcpy(str, "hello");  

printf(str);   

}

请问运行Test函数会有什么样的结果?

答:输出hello但有内存泄漏

 

 

 

 

 

 

 

void Test(void)

{

char *str = (char *) malloc(100);

    strcpy(str, hello);

    free(str);     

    if(str != NULL)

    {

      strcpy(str, world);

printf(str);

}

}

请问运行Test函数会有什么样的结果?

答:后果难以预料,

因为free(str)之后,if(str != NULL)

  不起任何作用.

 

 

 

 

五、编写strcpy函数

已知strcpy函数的原型是

       char *strcpy(char *strDest, const char *strSrc);

       其中strDest是目的字符串,strSrc是源字符串。

1)不调用C++/C的字符串库函数,请编写函数 strcpy

 

2strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?

 

编写一个函数,求从1加到n的和,请注意程序的执行效率

 

 编写二分法查找函数

编写一个函数,求从1加到n的和,请注意程序的执行效率

int total(int n)

{

int i,k=0;

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

   k=k+n;

printf(“k=%d/n”,k);

}

概念分类

下列名词符合哪些类别:在所属类别的空格中画勾(可以多选)

名词/类别

 

工具

语言

系统

方法

协议

 

Vc

 

 

 

 

 

 

C/c++

 

 

 

 

 

 

PB

 

 

 

 

 

 

VB

 

 

 

 

 

 

Pascal

 

 

 

 

 

 

Word

 

 

 

 

 

 

Excel

 

 

 

 

 

 

面向对象

 

 

 

 

 

 

Basic

 

 

 

 

 

 

SQL

 

 

 

 

 

Oracle

 

 

 

 

 

 

http

 

 

 

 

 

 

Tcp

 

 

 

 

 

 

udp

 

 

 

 

 

 

java

 

 

 

 

 

 

winCE

 

 

 

 

 

 

 

附加题、对于一个判断三角形的三条边是否构成等腰三角形的函数,设计其测试用例

原创粉丝点击