您的位置:首页 > 编程语言 > C语言/C++

自考C++程序设计试题2009年1月

2016-03-09 22:20 441 查看


全国2009年1月自学考试C++程序设计试题

课程代码:04737
一、单项选择题(本大题共20小题,每小题1分,共20分)

在每小题列出的四个备选项中只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。

1.C++源程序文件扩展名为(     )

A..cpp       B..h    C..1ib   D..obj

2.在C++中使用流进行输入输出,其中专用于从键盘进行输入的流是(     )

A.cerr      B.cin    C.cout D.cfile

3.包含自定义头文件file.h的预处理指令是(     )

A.#define<file.h>          B.#include file.h    C.#define file.h   D.#include"file.h"

4.用于标识十六进制前缀或后缀是(     )

A.无         B.后缀L或e    C.前缀零      D.前缀0x

5.设存在整型变量int x,则下列句与其它三项含义不同的是(     )

A.int* p=&x;       B.int& p=x;    C.int & p=x;  D.int &p=x;

6.在定义类成员时,为产生封装性,则需使用哪个关键字(     )

A.public           B.publish    C.protected      D.private

7.设函数void swap(int&,int&)将交换两形参的值,如两整型变量int a=10;int b=15;

则执行swap(a,b)后,a、b值分别为(     )

A.10,10        B.10,15    C.15,10 D.15,15

8.函数默认参数在函数原型中说明,默认参数必须放在参数序列的(     )

A.前部     B.中部    C.后部       D.两端

9.设存在函数int min(int,int)返回两参数中较小值,若求15,26,47三者中最小值,下列表达式中错误的是(     )

A.int m=min(min(15,26),min(15,47));

B.int m=min(15,26,47);

C.int m=min(15,min(47,26));  

D.int m =min(min(47,26),16);

10.下列函数不能和函数void print(char)构成重载的是(     )

A.int print(int);          B.void print(char,char);

C.int print(char);       D.void print(int,int);

11.在下列成对的表达式中,运算结果类型相同的一对是(     )

A.7/2和7.0/2.0      B.7/2.0和7/2

C.7.0/2和7/2            D.7.0/2.0和7.0/2 

l2.内联函数的特点是(     )

A.减少代码量,加快访问速度       B.减少代码量,减缓访问速度

C.增加代码量,减缓访问速度       D.增加代码量,加快访问速度

13.类的私有成员可在何处被访问(     )

A.本类的成员函数中         B.本类及子类的成员函数中

C.通过对象名在任何位置        D.不可访问

14.类的构造函数在以下什么情况下会被自动调用(     )

A.定义成员函数时      B.定义对象时

C.定义数据成员时      D.定义友元函数时

15.下列关于析构函数描述正确的是(     )

A.可以重载           B.函数体中必须有delete语句

C.返回类型必须是void类型          D.不能指定返回类型   

16.设有函数T Sum(T x,T y){return x+y;},其中T为模板类型,则下列语句中对该函数错误的使用是(     )

A.Sum(1,2);      B.Sum(3.0,2.2); C.Sum(‘A’,‘C’);asc码相加           D.Sum("A","C");

17.下列哪个编译指令属于条件编译指令(     )

A.#include       B.#define    C.#else      D.#pragma

18.关于类的静态成员函数描述错误的是(     ) 

A.在创建对象前不存在     B.不能说明为虚函数

C.不能直接访问非静态函数           D.不是对象的成员

l9.如果类A被声明成类B的友元,则(     )  

A.类A的成员即类B的成员

B.类B的成员即类A的成员

C.类A的成员函数不得访问类B的成员

D.类B不一定是类A的友元

20.派生类的对象可以访问以下那种情况继承的基类成员(     )  

A.私有继承的私有成员    B.公有继承的私有成员

C.私有继承的保护成员    D.公有继承的公有成员

二、填空题(本大题共20小题,每小题1分,共20分)

请在每小题的空格中填上正确答案。错填、不填均无分。

21.若使用标准输出流把整型变量a的值输出到屏幕上,实现此操作的C++语句是__cout<<a____。

22.C++将数据从一个对象流向另一个对象的流动抽象为“流”,从流中获取数据的操作称为_提取_____。

23.执行下列代码

    int b=100;

    cout<<"Hex:"<<hex<<b;

    程序的输出结果是__Hex:64____。

24.静态联编所支持的多态性称为_编译时_____的多态性。

25.C++程序必须有且只有一个主函数,其函数名为___main___。

26.写出声明一个复数对象的语句,并使该对象被初始化为2.2+1.3i,此声明语句是_

Complex<double> a(2.2,1.3)_____。

27.若有函数定义为:

    int add(int ml=0,int m2=2,int m3=4)

    {return ml+m2+m3;}

    在主函数中有int s=add(1)+add(0,1)+add(0,1,2);

    则执行此语句后s的值为__15____。

28.C++中使用_inline_____关键字说明函数为内联函数。

29.在源程序中有宏定义:#define PI  3.14,则若不想使该宏定义影响到程序的其它地方,可以使用__#undef____删除该宏定义。

30.类和其它数据类型不同的是,组成这种类型的不仅可以有数据,还可以有对数据进行操作的__函数_____。

31.有下列代码int a=0;double b=0;cin>>a>>b;当用键盘输入1.25时,b=_0.25__a=1___。

32.对于类Point而言,其析构函数的定义原型为__~point()____。

33.对象成员构造函数的调用顺序取决于这些对象在类中说明的顺序,与它们在成员初始化列表中给出的顺序_无关_____。

34.类的简单成员函数是指声明中不含const______、volatile、static关键字的函数。

35.与操作对象的数据类型相互独立的算法称为_泛型算法_____。

36.从一个或多个以前定义的类产生新类的过程称为_派生_____。

37.在vector类中向向量尾部插入一个对象的方法是__push_back____。

38.C++中用于动态创建对象,并返回该对象的指针的关键字是_new_____。

39.C++的流类库预定义的与标准输出设备相联接的流是__cout____。

40.执行下列代码

    cout<<noshowpoint<<123.0;

    程序输出结果是_123_____。

三、改错题(本大题共5小题,每小题2分,共10分)

下面的类定义中有一处错误,请用下横线标出错误所在行并给出修改意见。

41.#include<iostream.h>

    class f{

    private:float x,y;

    public:f(float a,float b){x=a;y=b;}

      float max(){return(x<y)?xy}//求最大值float max(){return(x>y)?xy}

    }  

    main( ){

    f a(1.5,3.8);

    cout<<a.max( )<<endl;

    }

42.#include<iostream.h>

    class test{

    private:int x;

    public:test(int a){x=a;}

      void set(int a){x=a;}

    void get(){cout<<x<<endl;}

};

main(){

    const test a(3);--------- test a(3);常成员只能调用常成员函数

a.set(5);

a.get();

    }

43.#include<iostream.h>

    class point{

    private:float x,y;

    public:f1(float a,float b){x=a;y=b;}

        point(){x=0;y=0;}

        void getx(){cout<<x<<endl;}

        void gety(){cout<<y<<endl;}

    };

    void print(point a){cout<<a.x<<endl}------ void print(point a){cout<<a.getx()<<endl}

    main(){

    point a;

a.f1(3.0,5.0);

    print(a);

    }

44.#include<iostream.h>  

  class f{

  private:int x,y;  

  public:fl(int a,int b){x=a;y=b;}

   void print(){cout<<x<<y<<endl;}

    }    

    main(){

    f a; 

a.f1(1.5,1.8); a.f1(1,1);

 

a.print();

}

45.#include<iostream.h>

  main(){

  int x=6;

  const int*p=x const int*p=&x;

  cout<<*p<<endl;

  }

四、完成程序题(本大题共5小题,每小题4分,共20分)

46.完成下面程序,使其输出l0,并在退出运行时正确释放分配给指针的存储空间。

#include  <iostream>

using namespace std;

void main()

{

int  *a,*p;

a=new int(10);

p=  __a____

cout<<*p<<endl;

__delete  a;________

}

47.#include  <iostream>

    using namespace std;

    class base

    {

    private:int x;

    public:base(int a){x=a;}

    int get(){return x;}

      void showbase()  {cout<<"x="<<x<<endl;}

    };

    class Derived:public base

    {private:int y;

    public:Derived(int a,int b):base(a){y=b;}

    void showderived()

       {cout<<"x="<<get()<<",y="<<y<<endl;}

    };  

    void main()

    {

    base b(3);

    Derived d(6,7);

    b.showbase();   --3

    d.showderived();--6,7

    _b=d---赋值兼容性规则_______;

       b.showbase();--6

   _ Derived  b1(6,7)_______;

b1.showbase();---------6

base* pb=&bl;

pb—>showbase();----6

       d.showderived();---6,7

       b.showbase();--6

}

输出结果如下:

x=3

x=6,y=7

x=6

x=6

x=6

x=6,y=7

x=6

48.下面程序的运行结果如下:

  B::display()

  C::display()

  在下划线处填上缺少的部分。源程序如下:

  #include<iostream>

  using namespace std;

  class B

  {

  public:

    __virtual_ void___display( )  {cout  <<  "B::display( ) "<<endl;}

    };

    class C:public B

    {

    public:

         __ void_____display(){cout<<"C::display()"<<endl;}

    };

    void fun(B*p)

    {

    p—>display();

    }

    void main()

    {

    B b,*pb;

    C c;

    pb=&b;

    fun(pb);

    pb=&c;----赋值兼容性规则

    fun(pb);

    }

49.下面程序的运行结果如下:

    This is line1

    This is line2

    This is line3

    在下划线处填上缺少的部分。源程序如下:

    #include  <iostream>

    #include__<fstream
>___

    using namespace std;

    void main()

    {

    fstream fin, fout;

    fout.open("my.txt",ios::out);

    if(!fout.is_open())

       return;

    for(int i=0;i<3;i=i+1)

       fout<<"This is line"<<i+1<<endl;

    fout.close();

    fin.open("my.txt",ios::in);---- ios::in);打开文件进行读取

    if(! fin.is_open())

       return;

    char str[100];

    while(_!fin.eof()____)

    {

      fin.getline(str,100);

      cout<<str<<endl;

    }

    fin.close();

    }

50.在下划线处填上缺少的部分。源程序如下:

    #include  <iostream>

using namespace std;

__template<class T>______

    T fun(T x)

    {

    ___T___y;

    y=x*x-T(5);

    return y;

    }

    void main()

    {

    float a=2;

    cout<<fun(a);

    }

五、程序分析题(本大题共4小题,每小题5分,共20分)

51.请写出myTextl.txt文本文件中的内容

#include<iostream>

#include  <string>

using namespace std;

#include<fstream>

void main()   

{

ofstream myFile1;

myFilel.open("myTextl.txt");

cout<<"Enter the data in Chinese format(e.g.  ,2008,May 25):"<<endl;

string Date("2008,January 1");

string Year=Date.substr(0,4);

int k=Date.find(",");

int i=Date.find(" ");

string Month=Date.substr(k+l,i-k-1);

string Day=Date.substr(i+1,2);

string NewDate=Day+" "+Month+" "+Year;

myFilel<<"original date:"<<Date<<endl;

myFile1<<"Converted date:"<<NewDate<<endl;

myFilel.close();

original date:2008,January 1

Converted date:1 January 2008

}

52.给出下面程序的输出结果

#include<iostream>  

using namespace std;

class Simple

{

int x,y;

public:

Simple(){x=y=0;}

Simple(int i,int j){x=i;y=j;}

void copy(Simple&s);

void setxy(int i,int j){x=i;y=j;}

void print(){cout<<"x="<<x<<",y="<<y<<endl;}

};

void Simple::copy(Simple&s)

{

x=s.x;y=s.y;

}

void func(Simple s1,Simple&s2)

{

s1.setxy(30,40);

s2.setxy(70,80);

}

void main()

{

Simple obj1(1,2),obj2;

obj2.copy(obj1);

func(obj1,obj2);

obj1.print();

obj2.print();

}

x=1,y=2

x=70,y=80

53.给出下面程序的输出结果

  #include"iostream.h"

  int main()

  {

  int i=17;

  while(i>=10)

  if(--i%4==3)continue;

  else

  cout<<"i="<<i--<<endl;

  }

i=16;

i=14;

i=12;

i=10

54.给出下面程序的输出结果

  #include<iostream>

  using namespace std;

  void main()

  {

  int num=300;

  int &ref=num;

  cout<<ref;

  ref=ref-100;

  cout<<" "<<num;

  num=num-50;

  cout<<" "<<ref<<endl;

  }

300 200 150

六、程序设计题(本大题共1小题,共10分)

55.定义堆栈类模板Stack(先进后出),栈的大小由使用者确定。要求该类模板对外提供

  如下二种基本操作:

  (1)push入栈(2)pop出栈,用数组来实现

  #include<iostream>

  using namespace std;

  template<class T,int size>

  class Stack{

  T x[size];

  int current;

  public:

  Stack(){current=0;}

  ....push(....);

  ....pop(....);

  };

  请写出两个函数的过程(如果需要形式参数,请给出形参类型和数量,以及返回值类型)

Void  push()//入栈

{

       For(int i=0;i<size;i++)

{

       Cin>>current;

       X[i]=current;

}

}

Void pop()

{

       For(int i=size-1;i>=0;i--)

{

       Cout<<x[i]<<endl;

}

 

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息