第二部分 Visual C++程序設(shè)計 21.設(shè)有變量說明:”short int a=0,b=0;”則表達式sizeof(‘a(chǎn)’+’b’)的值是( ) A. 1 B. 2 C.4 D.8 22.下列關(guān)于while和do … while循環(huán)語句的敘述中正確的是( ) A. do …while 的循環(huán)體至少執(zhí)行一次 B. while的循環(huán)體至少執(zhí)行一次 C.do… while的循環(huán)體不能是復(fù)合語句 D.do … while 允許從循環(huán)體外跳轉(zhuǎn)到循環(huán)體內(nèi) 23.設(shè)有函數(shù)原型說明, void test(int a, int b=7, char *c=”#”) 下面的函數(shù)調(diào)用中存在語法錯誤的是( ) A.test(3) B. test(3,8.2) C. test(6,”*”) D. test(0,0,”*”) 24.執(zhí)行以下程序時,輸入一行字符串為: Thank you very much! 程序的輸出結(jié)果為( ) #include void main(void) { char line[100]; cin>>line;cout< } A.T B.Thank C. Thank you very much! D.Thank you 25.下列關(guān)于運算符重載的敘述中正確的是( ) A. 運算符重載可以改變操作數(shù)的個數(shù) B.運算符重載可以改變運算符的優(yōu)先級 C.運算符重載可以改變運算符的結(jié)合性 D.運算符的重載不能改變操作數(shù)的個數(shù),也不能改變運算符的優(yōu)先級和結(jié)合性 26.定義以下的類: class X{ int a; public: X(int x=0){ a=x;} } ; class Y : public X{ int b; public:Y(int x=0,int y=0):X(y){b=x;} }; 在下列選項的說明語句中,存在語法錯誤的是( ) A. X *pa=new Y(1,2) B. X a1= Y(1,3); C. X b2(2,3); Y &a2=b2; C. Y b3(10); X a3(b3); 27.設(shè)有以下語句: int a=5; int arr[a]; const int b=5; int x[b]; int c=5; int *p=new int[c]; const int d=5; int *p1= new int[d]; 其中存在語法錯誤的是( ) (A).第一行 (B)第二行 (C)第三行 (D) 第四行 28.設(shè)有說明語句: float a[3][3]={1,2,3,4,5}, *b[3]={0}, (*c)[3]=a, **d=0; 以下選項中語法正確的語句是( ) A. a[0]=b[0]; B.b[0]=c[0]; C. c[0]=d[0]; D.d[0]=a[0][0]; 29.下列關(guān)于虛函數(shù)的描述中正確的是( ) (A).虛函數(shù)可以是一個static類型的成員函數(shù) (B).虛函數(shù)可以是一個非成員函數(shù) (C).虛函數(shù)實現(xiàn)靜態(tài)多態(tài)性 (D).基類中采用virtual說明一個虛函數(shù)后,派生類中定義相同原型的虛函數(shù)時,可不必加virtual說明 30.設(shè)變量a、b是整型變量,下列switch語句中正確的是( ) A.switch(a) B. switch(a+b) { { case a: a++; break case 1: b=a++;break; case b: b++;break case 1: a=++b; } } C. switch(a*a) D. switch(a/10+b) { { case 10,12 :++a; case 3: b=a/10;break; case 14,16: ++b; default: a+=b; } }
二:填空題: 1.對于switch(e),表達式e只能是整型、( )或枚舉型表達式 2.面向?qū)ο蟪绦蛟O(shè)計的三大特性是封狀性,繼承性和( ) 3.執(zhí)行以下語句后,a的值為 ( ) int a=0, m=3, n=10; a=(m++,m+n); 4.在c++中,標識符是以字母或( )開頭的,由字母、數(shù)字和( )組成的字符序列 5.在C++中,類的每一個非靜態(tài)成員函數(shù)都有一個( )指針,該指針指向正在調(diào)用成員函數(shù)的對象 閱讀程序題 6.[程序](2分) # include int fun(int x,int y) { x=x+y; y=x+y; cout<<”x=”< return x+y; }
void main(void) { int x=5, y=8, z=fun(x,y); cout<<”x=”< cout<<”z=”< } 程序輸出的第二行是( ),第三行是( ) 7.[程序] (3分) # include int f(int m, int &n) { static int a=1; int c=2; m+=++a; n+=++c; return m+n; }
void main(void) { int a=1, b=2; cout< cout< } 程序輸出的第一行是( ),第二行是( ),第三行是( ) 8.[程序] # include
void findmax(int *a, int n, int i, int *pk) { if(i { if(a[i]>a[*pk]) *pk=i; findmax(a,n,i+1,pk); } }
void main(void) { int a[10]={34,32,23,12,67,54,44,60,33,24}, index =0; findmax(a,10,0,&index); cout< cout<<”Its’ index is:”<} 程序輸出的第一行是( ),第二行是( ) 9.[程序] #include int a=100; int fun(int *a, int &b,int c) { static int e; *a+=e++; b++; c+=::a++; e=*a+b+c; return e; }
void main(void) { int a=10, b=1; c=1; cout< cout<<::a+a+b+c<<’\n’; }
程序輸出的第一行是( ),第二行是( ) 第三行是( ) 10.[程序] #include #include
class Base{ char str[20]; public: Base(char *s=”Base default”) { strcpy(str,s); cout< } };
class Inh1:public virtual Base{ char str1[20]; public: Inh1(char *s1, char *s2): Base(s1) { strcpy(str1,s2); cout< } };
class Inh2: public virtual Base{ char str2[20]; public: Inh2(char *s1, char *s2): Base(s1) { strcpy(str2,s2); cout< } };
class Inh3: public Inh1, public Inh2 { char str3[20]; public: Inh3(char *s1, char *s2, char *s3, char *s4): Inh1(s1,s2),Inh2(s1,s3) { strcpy(str3,s4); cout< } };
void main(void) { Inh3 a(“class Base,”class Inh1”,”class Inh2”,”class Inh3”); } 程序輸出的第二行是( ),第三行是( ),第四行是( ) 完善程序題 11.下面程序的功能是:將二維數(shù)組a中的每個元素向右移一列,最后一列移到最左邊,并按矩陣形式輸出數(shù)組a.例如: 數(shù)組移動前為: 1 2 3 4 5 6 7 8 9 移動后為: 3 1 2 6 4 5 9 7 8 [程序] #include #define ROW 3 #define COL 3 void fun( ) { int i,j, t; for(i=0; i { t=*(*(p+i)+ROW-1); for(j=COL-1;j>0;j--) *(*(p+i)+j)=( ) ( )=t; } }
void main(void) { int a[ROW][COL]={1,2,3,4,5,6,7,8,9}; int i,j; fun(a); for( ) { for(j=0;j cout<<’\n’; } } 12.以下程序的功能是:從一個字符串str中刪除或添加一個指定的字符,若指定的字符c出現(xiàn)在字符串str中,則從str中刪除第1個值為c的字符,否則把字符c添加到str的尾部,在程序中,函數(shù)dele()從字符串中刪除第一個字符,函數(shù)add()添加一個字符到字符串尾部,函數(shù)search()用于查找指定的字符是否在字符串中,若在,則返回所在位置,否則返回0.
#include
char *search(char *s, char ch) { while(*s) if(*s++==ch) ruturn ( ) return 0; }
void dele(char *s , char ch) { char *p1=search(s,ch), *p2=p1+1; while(*p2) *p1++=*p2++; *p1=’\0’; }
void add(char *s, char ch) { while(*s) s++; ( )=ch; *s=’\0’; }
void main(void) { char str[80]=”abc12123”, c; cout< cin>>c; void( ); if(search(str,c)) fp=dele; else fp=add; fp( ); cout<} 13.下面程序中,主函數(shù)建立一條單向鏈表,鏈表上的一個結(jié)點為一個學(xué)生的記錄(由學(xué)號和成績組成),在主函數(shù)中產(chǎn)生若干名學(xué)生的記錄,并放在鏈表中,函數(shù)fun()的功能是:先求出鏈表上所有學(xué)生的平均成績,并通過形參aver帶回,然后將高于或等于平均成績的學(xué)生記錄放在h所指向的新鏈表中,最后返回新鏈表的頭接點指針h # include #include struct student { char no[10]; float grade; student *next; }
student *fun(student *head, float &aver) { student *h, *p, *p1; float sum=0; int n=0; aver=0; h=null; p=head; while(p!=null) { ( ); n++; p=p->next; } aver=sum/n; p=head; while(p!=null) { if(p->grade>=aver) { p1=new student;( ); p1->grade=p->grade; p1->next=h; h=p1; } ( ); } return h; }
void main(void) { student *head, *p, *h; char no[10]; float aver; head=null; cout<<”輸入學(xué)號”; cin>>no; while(*no !=’#’) { p=new student; strcpy(p->no,no); cout<<”輸入成績”;cin>>p->grade; ( ); head=p; cout<<”輸入學(xué)號(首字符#表示結(jié)束輸入):”; cin>>no; } p=head; while(p!=null){ cout<no<<’\t’<grade<<’\t’<<’\n’; p=p->next; } h=fun(head,aver); cout< p=h; while(p!=null) { cout<no<<’\t’<grade<<’\t’<<’\n’; p=p->next; } p=head; while(p!=null) { head=head->next; delete p; p=head; } p=h; while(p!=null) { h=h->next; delete p; p=h; } }
|