Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2094907
  • 博文数量: 249
  • 博客积分: 1305
  • 博客等级: 军士长
  • 技术积分: 4733
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-17 10:37
个人简介

不懂的东西还有很多,随着不断的学习,不懂的东西更多,无法消灭更多不懂的东西,那就不断的充实自己吧。 欢迎关注微信公众号:菜鸟的机器学习

文章分类

全部博文(249)

文章存档

2015年(1)

2014年(4)

2013年(208)

2012年(35)

2011年(1)

分类: C/C++

2013-11-18 12:36:36


一.  单选题(每题4分,15题,共60分)
1.考虑函数原型void hello(int a,int b=7,char* pszC="*"),下面的函数调用钟,属于不合法调用的是:C
    A hello(5)         B.hello(5,8)         C.hello(6,"#")         D.hello(0,0,"#")
解析:
  (1)不填写参数,参数按默认值;
  (2)
    

2.下面有关重载函数的说法中正确的是:C
  A.重载函数必须具有不同的返回值类型   B.重载函数形参个数必须不同
  C.重载函数必须有不同的形参列表         D.重载函数名可以不同
解析:
    重载:必须同名函数;必须参数表不同(包含参数个数不同;参数类型不同;或参数个数与类型都不同)

3. 分析一下程序的运行结果:C
  1. #include<iostream.h>
  2.   class CBase
  3.   {
  4.   public:
  5.   CBase(){cout<<”constructing CBase class”<<endl;}
  6.   ~CBase(){cout<<”destructing CBase class”<<endl;}
  7.   };
  8.   class CSub : public CBase
  9.   {
  10.   public:
  11.   CSub(){cout<<”constructing CSub class”<<endl;}
  12.   ~CSub(){cout<<”destructing CSub class”<<endl;}
  13.   };
  14.   void main()
  15.   {
  16.    CSub obj;
  17.   }
        A. constructing CSub class           B. constructing CBase class
       constructing CBase class             constructing CSub class
       destructing CSub class               destructing CBase class
       destructing CBase class              destructing CSub class
  C. constructing CBase class        
    constructing CSub class         
    destructing CSub class          
    destructing CBase class         
   D. constructing CSub class
      constructing CBase class
      destructing CBase class
       destructing CSub class
解析:
    子类对象生成时:先调用父类的构造函数,然后在调用子类的构造函数;析构时相反

4.在一个cpp文件里面,定义了一个static类型的全局变量,下面一个正确的描述是:A
    A.只能在该cpp所在的编译模块中使用该变量     
    
B.该变量的值是不可改变的
    C.该变量不能在类的成员函数中引用        
    D.这种变量只能是基本类型(如int,char)不能是C++类型
解析:
        Static全局变量和普通全局变量:
  针对:一个工程里有多个cpp文件时
  相同点:存储方式相同,都是静态存储;
  不同点:作用域不同。
  普通全局变量---作用域是整个源程序(含有多个源文件),在各个源文件中都有效
  Static全局变量----作用域是当前源文件

5.观察下面一段代码:
  1. class ClassA
  2. {
  3. public:
  4.   virtual ~ ClassA(){};
  5.   virtual void FunctionA(){};
  6. };
  7. class ClassB
  8. {
  9. public:
  10.    virtual void FunctionB(){};
  11. };
  12. class ClassC : public ClassA,public ClassB
  13. {
  14. public:
  15. };
  16.  
  17. ClassC aObject;
  18. ClassA* pA=&aObject;
  19. ClassB* pB=&aObject;
  20. ClassC* pC=&aObject;
关于pA,pB,pC的取值,下面的描述中正确的是:C
    A. pA,pB,pC的取值相同        B. pC=pA+pB        C. pA和pB不相同             D.pC不等于pA也不等于pB
6. 参照1.5的代码,假设定义了ClassA* pA2,下面正确的代码是:D
    A. pA2=static_cast(pB);
    
B. void* pVoid=static_cast(pB);
   pA2=static_cast(pVoid);
    
C. pA2=pB;
    
D. pA2=static_cast(static_cast(pB));//将子对象赋值给父对象
7.参照1.5的代码,下面那一个语句是不安全的:ABC
    A. delete pA        B. delete pB           C. delete pC
    删除哪个都有错误,编译是无错误,运行是有错误
8.下列程序的运行结果为:B
#include
void main()
{
  int a=2;
  int b=++a;
  cout<
}
   A.0.5   B.0   C0.7   D.0.6666666

9.有如下一段代码:A
#define ADD(x,y) x+y
int m=3;
m+=m*ADD(m,m); //展开后为m=m+m*m+m=3+3*3+3
则m的值为:
    A.15           B.12           C.18          D.58
10.如下是一个带权的图,图中结点A到结点D的关键路径的长度是:
    A.13       B.15       C.28       D.58
  
11.下面的模板声明中,正确的是:C
  A. template //改为template
  B. template
  C. template
  D. template//分号改为逗号
12.在Windows编程中下面的说法正确的是:C
  A. 两个窗口,他们的窗口句柄可以是相同的     
  B. 两个窗口,他们的处理函数可以是相同的——正确
  C. 两个窗口,他们的窗口句柄和窗口处理函数都不可以相同.
13.下面哪种情况下,B不能隐式转换为A?B
  A. class B:public A{}                    B. class A:public B{}
  C. class B{operator A();}             D. class A{A(const B&);}

14.某公司使用包过滤防火墙控制进出公司局域网的数据,在不考虑使用代理服务器的情况下,下面描述错误的是”该防火墙能够(  B )”.
  A. 使公司员工只能访问Internet上与其业务联系的公司的IP地址.
  B. 仅允许HTTP协议通过,不允许其他协议通过,例如TCP/UDP.
  C. 使员工不能直接访问FTP服务器端口号为21的FTP地址.
  D. 仅允许公司中具有某些特定IP地址的计算机可以访问外部网络
15.数字字符0的ASCII值为48,若有以下程序:
  main()
  {
     char a=’1’,b=’2’;
    printf(“%c,”,b++);
    printf(“%d\n”,b-a);
  }
    程序运行之后的输出结果是:C
  A. 3,2      B. 50,2       C. 2,2     D. 2,50
二.  填空题(共40分)
    本程序从正文文件text.in读入一篇英文短文,统计该短文中不同单词和它的出现次数,并按词典编辑顺序将单词及它的出现次数输出到正文文件word.out中.
 程序用一棵有序二叉树存储这些单词及其出现的次数,一边读入一边建立.然后中序遍历该二叉树,将遍历经过的二叉树上的节点的内容输出.
 程序中的外部函数
    int getword(FILE* pFile,char* pszWordBuffer,int nBufferLen);
    从与pFile所对应的文件中读取单词置入pszWordBuffer,并返回1;若单词遇文件尾,已无单词可读时,则返回0.
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5.  
  6. #define SOURCE_FILE "text.in"
  7. #define OUTPUT_FILE "word.out"
  8. #define MAX_WORD_LEN 128
  9.  
  10. typedef struct treenode
  11. {
  12.       char szWord[MAX_WORD_LEN];
  13.       int nCount;
  14.       struct treenode* pLeft;
  15.       struct treenode* pRight;
  16. }BNODE;
  17.  
  18. int getword(FILE* pFile,char* pasWordBuffer,int nBufferLen);
  19.  
  20. void binary_tree(BNODE** ppNode,char* pszWord)
  21. {
  22.       if(ppNode != NULL && pszWord != NULL)
  23.       {
  24.              BNODE* pCurrentNode = NULL;
  25.              BNODE* pMemoNode = NULL;
  26.              int nStrCmpRes=0;
  27.  
  28.              ____(1)_____;pCurrentNode=*ppNode
  29.  
  30.              while(pCurrentNode)
  31.              {
  32.                     /*寻找插入位置*/
  33.                     nStrCmpRes = strcmp(pszWord, ___(2)___ );pCurrentNode->nCount
  34.  
  35.                     if(!nStrCmpRes)
  36.                     {
  37.                            ___(3)___; pCurrentNode->nCount++
  38.  
  39.                            return;
  40.                     }
  41.                     else
  42.                     {
  43.                            ___(4)___; pMemoNode=pCurrentNode
  44.                            pCurrentNode = nStrCmpRes>0? pCurrentNode->pRight : pCurrentNode->pLeft;
  45.                     }
  46.              }
  47.       }
  48.  
  49.       pCurrent=new BNODE;
  50.  
  51.       if(pCurrentNode != NULL)
  52.       {
  53.              memset(pCurrentNode,0,sizeof(BNODE));
  54.              strncpy(pCurrentNode->szWord,pszWord,MAX_WORD_LEN-1);
  55.              pCurrentNode->nCount=1;
  56.       }
  57.  
  58.       if(pMemoNode==NULL)
  59.       {
  60.              ___(5)___; *ppNode= pCurrentNode
  61.       }
  62.       else if(nStrCmpRes>0)
  63.       {
  64.              pMemoNode->pRight=pCurrentNode;
  65.       }
  66.       else
  67.       {
  68.              pMemoNode->pLeft=pCurrentNode;
  69.       }
  70. }
  71.  
  72. void midorder(FILE* pFile,BNODE* pNode)
  73. {
  74.       if(___(6)___) return;!pNode||!pFile
  75.  
  76.       midorder(pFile,pNode->pLeft);
  77.       fprintf(pFile,"%s %d\n",pNode->szWord,pNode->nCount);
  78.       midorder(pFile,pNode->pRight);
  79. }
  80.  
  81. void main()
  82. {
  83.       FILE* pFile=NULL;
  84.       BNODE* pRootNode=NULL;
  85.       char szWord[MAX_WORD_LEN]={0};
  86.  
  87.       pFile=fopen(SOURCE_FILE,"r");
  88.  
  89.       if(pFile==NULL)
  90.       {
  91.              printf("Can't open file %s\n",SOURCE_FILE);
  92.              return;
  93.       }
  94.  
  95.       while(getword(pFile,szWord,MAX_WORD_LEN)==1)
  96.       {
  97.              binary_tree(___(7)___);// pRootNode,szWord
  98.       }
  99.  
  100.       fclose(pFile);
  101.  
  102.       pFile=fopen(OUTPUT_FILE,"w");
  103.       midorder(pFile,pRootNode);
  104.       fclose(pFile);
  105. }

三.  附加题(每题30分,2题,共60分)
1.  从程序健壮性进行分析,下面的FillUserInfo函数和Main函数分别存在什么问题?
  1. #include <iostream>
  2. #include <string>
  3.  
  4. #define MAX_NAME_LEN 20
  5.  
  6. struct USERINFO
  7. {
  8.       int nAge;
  9.       char szName[MAX_NAME_LEN];
  10. };
  11.  
  12. void FillUserInfo(USERINFO* parUserInfo)
  13. {
  14.       stu::cout<<"请输入用户的个数:";
  15.  
  16.       int nCount=0;
  17.       std::cin>>nCount;//未判断输入是否合法
  18.  
  19.       for(int i=0;i<nCount;i++)
  20.       {
  21.              std::cout<<"请输入年龄:";
  22.              std::cin>>parUserInfo[i]->nAge;//未判断输入是否合法
  23.  
  24.              std::string strName;
  25.              std::cout<<"请输入姓名:";
  26.              std::cin>>strName; //未判断输入是否合法
  27.              strcpy(parUserInfo[i].szName,strName.c_str());
  28.       }
  29. }
  30.  
  31. int main(int argc,char* argv[])
  32. {
  33.       USERINFO arUserInfos[100]={0};//
  34.       FillUserInfo(arUserInfos);
  35.       
  36.       printf("The first name is:");
  37.       printf(arUserInfos[0].szName);
  38.       printf("\n");
  39.  
  40.       return 0;
  41. }

2. 假设你在编写一个使用多线程技术的程序,当程序中止运行时,需要怎样一个机制来安全有效的中止所有的线程?请描述其具体流程.
 

阅读(3418) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~