2022年C++程序設計期末考試卷及答案2套_第1頁
2022年C++程序設計期末考試卷及答案2套_第2頁
2022年C++程序設計期末考試卷及答案2套_第3頁
2022年C++程序設計期末考試卷及答案2套_第4頁
2022年C++程序設計期末考試卷及答案2套_第5頁
已閱讀5頁,還剩14頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、PAGE 高級語言程序設計 I 試卷 (A) 第 PAGE 19 頁 共 NUMPAGES 19 頁姓名 學號 學院 專業(yè) 座位號( 密 封 線 內 不 答 題 )密封線線_ _ 誠信應考,考試作弊將帶來嚴重后果!期末考試 高級語言程序設計 I 試卷 ( A )注意事項:1. 考前請將密封線內填寫清楚; 2. 所有答案寫在答題紙上; 3試卷和答題紙同時提交; 4考試形式:閉卷; 5. 本試卷共五大題,滿分100分,考試時間120分鐘。題 號一二三四五總分得 分評卷人一、單項選擇題。(每小題2分, 共20分) 下列選項中,合法的常量表示是( )。(A) program(B) 183AF(C) -

2、618e3(D) 1.0e-5.3執(zhí)行下列語句后,i,j,k的值為( )。int i=1,j=1,k=1; (i+,-j) & +k;(A) 2,0,1(B) 2,0,2(C) 1,1,1(D) 1,0,2以下有關C+的說法中,正確的陳述是( )。(A) const只能約束普通內存變量的的寫操作,不能約束指針變量的寫操作。(B) 靜態(tài)變量和全局變量的作用域都是文件作用域。 (C)一維數組定義中數組的長度表達式可以使用賦初值的變量。 (D) inline函數沒有普通函數調用的時空開銷。有如下代碼段,不正確的函數調用形式( )。typedef int (*pType)(int,int);int m

3、ax(int a,int b) return ab?a,b; pType pf = max;(A) pf(1,2);(B) (*pf)(1,2);(C) max(1,2);(D) (&pf)(1,2);以下程序段中,循環(huán)次數是()for(int i=10;i0&i%2;) i=i-2; (A) 0(B) 4(C) 5(D) 6已知int iArray22=0,1,2,3,下列表達式的值為2的是( )。(A) iArray21(B) iArray11(C) *(*(iArray+1) (D) *(*(iArray)+1)假設 char *a=“fortran”,“basic”,“c+”, “ja

4、va”;則語句cout(D)30*(F-C)/5-2*3表達式的值是( )。(A)6(B) 12(C)48(D)18sizeof(char)+6*(65)/(23?2:3)表達式的值是( )。(A) 3(B) 4(C) 1(D)2二、簡答題。(共20分)1寫出兩個表達變量x和y的值都不等于零的邏輯表達式。 x!=0&y!=0 x&y2有以下語句,循環(huán)體執(zhí)行次數是多少?結束后x的值是什么?10,-1int x=10; while(x-) coutxendl; 3有說明語句:int a; double x; int *p=new int100;分析以下表達式值的類型。a+xa=a+xp+1doub

5、le int int*4設有函數:void fun1(int a) a+; ;void fun2(int & a) a+; ;有以下調用:int b=5;fun1(b);/b的值是什么?fun2(b);/b的值又是什么?兩次調用函數后變量b的值有變化嗎?為什么?傳值參數,引用參數5設有函數:int function(int a) static int k=0; return a+k+; 并有調用:int t=1; t=function(t)+function(t)+function(t);有人說t的結果值等于3,對嗎?為什么?6,k是靜態(tài)變量6.設有語句:int *ap=new int10;請

6、寫出兩個動態(tài)數組最后一個元素的表示形式。7請解釋以下說明語句中標識符www的含義。double * www(double);www是函數名,有一個double值參,返回值類型為double*8.設有語句:char *s=South China University of Technology;請寫輸出子串“University of Technology”的語句。 couts+12endl;9有說明語句:int ary100; int max;賦值語句調用函數MaxAry求數組的最大元素值:max=MaxAry(ary,10);請寫出MaxAry的函數原型。int MaxAry(const i

7、nt *,int); 或 int MaxAry(const int ,int);10有語句:struct link int code; link *next; ; link *head;/push(head,256);已知head為單鏈表的頭結點,函數調用語句push在表頭插入一個數據,請寫出對應的函數原型。void push(link *&, int);三、閱讀程序寫輸出結果(共20分)1./循環(huán)#include void main() int i=0,s=0; while (i+=10) if(i%2) continue; s=s+i; coutst; 261220302./數組,指針#i

8、ncludevoid main() int num5; int *p=num, i; for(i=1;i=5;i+) numi-1=i; for(i=0; i5; i+) coutnumi+(*p+)t; coutendl;2468103. /遞歸#includevoid print(char ch) int i=0;if(ch=D)return;else print(ch+1); while(i+=ch-A) coutch; coutendl;void main() print(A);CCC BBA4. /函數指針參數#include void fun(int *x, int *y) cou

9、t*x=*xt;cout*y=*yt;*x = 3 ;*y = 4 ;void main() int x = 1, y = 2 ;fun(&y, &x);coutx=xt;couty=ytendl;*x=2 *y=1 x=4 y=3四、程序填空題(每空2分,共20分)1. 假設90分以上為A等,80分到89分為B等,70分到79分為C等,60分到69分為D等,60分以下為E等。下面是輸入一個分數,輸出相應的五級制成績的程序。#includevoid main()double score; coutscore; if( score 100 | score 0 ) cout Input Error

10、!; else switch( (1) ) / (1) (int) score/10 case 9: case 10: coutAn; break; case 8: coutBn; break; case 7: coutCn; break; case 6: coutDn; break; default: (2) ; / (2) coutEn; 2. 下面是顯示如下圖案的程序。1111111111 2222222 33333 444 5#includevoid main() int i , j, k ; for( i = 1; i = 5; i+ ) for( k = 1; (3) ; k+ )

11、 / k=i-1 coutends ; for( j =1 ; (4) ; j+ ) /( 2*(5i)+1) cout (5) ; / i coutendl; 3下面的程序的輸出結果是:3 2 1 10 9 8 7 6 5 #include void fun( (6) , int n , int m ) /int * s 或 int s int i , j , t ; i = n ; j = m ; while ( ij ) t=si; si=sj; sj=t; i+; j-; void main() int a10 = 1,2,3,4,5,6,7,8,9,10 ; fun( a , 0 ,

12、 3 ); fun( (7) ); / a,4,9 for ( int i = 0; i10; i+ ) cout ai ends; cout endl;4下面的程序運行時屏幕顯示Please input i(110) : 鍵盤輸入3后,屏幕顯示程序運行結果如圖1所示: 圖1 程序運行結果#include int i ; void prints( (8) , int i ) / char * s cout (9) endl; / (s + i 1) cout (10) endl; / *( s + i );void main() int i ; cout i; prints ( ABCDEFG

13、HIJ, i );五、編程題(20分)1、設計程序,輸入一個正整數i(256),求另一個正整數j,使i和j在用8位二進制表示時互為逆序。例如,輸入i=3(00000011),應求得j=192(11000000)。#includevoid main() int i,j=0,k; couti; for(k=0;k8;k+) j=j*2+i%2; i/=2; coutj=jendl;2、以下程序由隨機數生成一個整數序列,放在數組a中,然后按奇數在前,偶數在后的順序重新排放。程序運行效果如圖2所示。請編寫函數RandAry和函數PutAry。#include#include#includevoid R

14、andAry(int *a, int n, int m);void PutAry(int *a, int n);void main() int *a, i, n; coutn; a=new intn; RandAry(a, n, 100); /用小于100的隨機數對數組賦值 for(i=0; in; i+) coutai ;/輸出原始序列 cout endl; PutAry(a,n);/整理數組,奇數放在前,偶數在后 for(i=0; in; i+) coutai ;/輸出整理后序列 cout endl;圖2 整理數據void RandAry(int *a, int n, int m) sra

15、nd( time( 0 ) );/調用種子函數for( int i = 0; in; i+ ) ai = rand() % m; /用隨機函數初始化數組void PutAry(int *a, int n) int i,t,k=0; for( i = 0; inext; coutdatatdata=p-data) n+; t=1; else t=0; if(!t&n1) coutdatatnnext; if(n1)/考慮最后一組相鄰節(jié)點的情況 coutdatatnendl; C+程序設計試卷 單項選擇題:(每小題2分,共20分)以下敘述中錯誤的是( )。A)用戶所定義的C+標識符允許使用關鍵字B

16、)用戶所定義的C+標識符應盡量做到“見名知意”C)用戶所定義的C+標識符必須以字符或下劃線開頭D)用戶所定義的C+標識符中,大、小寫字符代表不同標識。用C+語言編制的源程序要變?yōu)槟繕顺绦虮仨毥涍^( )。 A)解釋B)匯編C)編輯D)編譯設有定義語句:int a=7;float x=2.5,y=4.7; 則表達式x+a%3*(int)(x+y)%2/4的值是( )。A) 2.5 B) 2.75C) 3.5 D) 0.0以下所列的C+語言常量中,錯誤的是()。A) 0 xFF B) 1.2e0.5C) 2L D) 72設有:int a=7,b=5,c=3, d=1; , 則條件表達式ad?c:d的

17、值為( )。A)7B) 5C) 3D) 1以下程序的運行結果是( )。 int main() short b=-1; unsigned short a; a=b; couty答案欄:1、A_ 2、_D_ 3、_A_ 4、_B_5、_C_ 6、_c_7、_D_ 8、_C_9、_B_ 10、_c_ 二、程序填空題(每空2分,共20分)答案欄:1、_ 2、_;_;_;3、_ 4、_5、_ 6、_;_; 7、_ 8、_9、_ 10、_1、以下程序的功能是:用選擇法對5個實數排序(按從小到大順序)。請?zhí)羁铡?include using namespace std; void main( )double

18、x5, t;int i, j, k; for(i=0; ixi; for(i=0; i4; i+) k=i; for( j=i+1 【1】 ; j5; j+) if(xjxk) k=j; t=xi;xi=xk ;xk=t; 【2】 /將選擇好的下標元素和下標為i的元素交換 for(i=0; i5; i+)coutxi ; 2、以下函數的功能是:用遞歸法將一個整數m轉換成字符串。例如:輸入整數1472,應輸出字符串1472。m的位數不確定,可以是任意位數的整數。請?zhí)羁?。void print( int m )if(m10) putchar(m+48) 【3】 /遞歸出口else print(m/1

19、0)【4】 /遞歸體putchar(m%10+48);3、以下程序的功能是:執(zhí)行該程序后運行結果如下: 5.1 3.2請?zhí)羁铡?includeusing namespace std;void d1( int &a, int b 【5】 ) a=a+3; b=b+3;void main()double x=2.1, y=3.2;d1(x , y);coutx yendl;4、以下程序的功能是:定義一個結構體類型,內含學生學號和一門課的成績,并開辟動態(tài)內存存放一個學生的數據:學號3001,成績95。執(zhí)行該程序后運行結果如下: 3001, 95請?zhí)羁铡?includeusing namespace

20、std;struct S1 int m; /學號 double x; /成績;void main()S1 *p;p=new S1; cinp-mp-x; 【6】 /賦值 coutm”,”xendl; 【7】 /輸出delete p;5、以下函數的功能是:判斷一個數是否為素數。請?zhí)羁?。bool prime(int m)int i;bool prime1;prime1=true;for(i=2; i=sqrt(m); i+)if( m%i=0【8】 ) prime1=false;return prime1;WIN7用戶需要將PCSX2VU.exe設置成管理員權限啟動才可以運行6、以下程序的功能是:

21、輸出二維數組各元素值。請?zhí)羁铡?includeusing namespace std;void main() void output(int (*)2);int b32=6,5,4,3,2,1;output( b 【9】 );void output(int (*p)2) int i, j; for(i=0;i3;i+)for(j=0;j2;j+) cout *(*(p+i)+j) 【10】 ; coutendl;三、閱讀程序,寫出運行結果。(每小題3分,共24分)答案欄:1、_ 2、_3、_ 4、_5、_ 6、_7、_ 8、_1、#include using namespace std;voi

22、d main()int a=-1, b; if(a2)if(a0)b=a+2; else b=a+4;else b=a+5;couta=a,b=b; a=-1,b=32、#includeusing namespace std;void main( )int n=2,m=6,a,b;a=+m+n-;9b=m+-n;7couta b m nendl; 9 7 8 03、#include using namespace std; void q(float f)coutfloatf;void q(double d)coutdoubled;void q(int i)coutinti;void main(

23、 ) q(3); q(B);q(3.1); int3int66double3.14、#include using namespace std;void main( ) int m=3, n=2; while(m5)switch(m+)m=4case 4: n-; case 5: n-=4;break; case 3: n+; coutn;-25. #include using namespace std; int x, y;void fun(int a) int y , b;b=a+3; x=x+a; y=x-b; coutx y a bendl; int main( ) int a=1, b

24、=3;x=5, y=4; fun(b); coutx y a bendl; return 0; 8 2 3 6 8 4 1 36. #include using namespace std;int f(int a) int b=1; static int c=2; b+; c+; return (a+b+c); int main( ) for (int i=0; i3; i+) coutf(i) ; return 0; 5 7 97. #include using namespace std;int main( ) char ch =PROGRAM ; coutchendlch3endlch+

25、3endl; return 0; PROGRAM G GRAM8. #include using namespace std;int main( ) int a=78, b=21, *p, *q, *r; p=&a; q=&b; if (*p!=*q) r=p; p=q; q=r; cout*p *qendl; *p=*p+*q; couta bendl; return 0;21 78 78 99四、簡答題(每小題4分,共12分)1. 試分析字符串常量 abcetfg101102 由哪幾個字符構成?該字符串占用多少個字節(jié)?若用 coutabcetfg101102 ; 會看到的輸出結果是什么?

26、a b c e 101 102 11abce fgAB2. 若有如下定義:struct stype float y; short int m; char code3;stype sdat;試畫出變量sdat的內存構造圖(示意圖),并指出該變量占用內存的字節(jié)數。3. 下面程序有錯誤: #include using namespace std; void fun(int b ) for (int i=0; i7; i+) cout*(b+) ; / (1) coutendl; int main( ) int b7=10,20,30,40,50,60,70; for (int i=0; i7; i+

27、) cout*(b+) ; / (2) coutendl; fun(b);return 0; 源程序中(1)和(2)之處寫法一樣,但編譯時(2)處出錯而(1)處正確,試指出原因,并將(2)處改正。b在(1)處是變量 而在(2)是 常變量 不能用于b+ int *a;a=b;cout*(a+)“ “;五、程序設計題(每小題8分,共24分)1. 設計程序輸出數列An的前36個數,輸出時要求每行輸出6個數。數列有下列規(guī)律: #includeusing namespace std;double a36;int main()double A(int);int i;int j;a11=A(12);for(i=0;i=35;i+)for(j=0;j=5;j+)coutai+jt; i=i+6; coutendl; return 0;double A(int i)double m;if(i=1) m=1;else if(i=2) m=2;elsem=(A(i-1)+1)/(A(i-1)*(A(i-2)+1);ai-1=m;return m;2. 逆置是指將數組中的值按逆序重新存放。例如:數組原值

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論