清华大学 学堂在线 《程序设计基础》

清华大学 学堂在线 《程序设计基础》

第一章 编程初步--语法自测

1.若要使用数学函数,应该包含以下哪个头文件

A.cmath   B.iostream  C. memory  D.stido

(A)

2.程序主函数的名字是

A.main     B.Main    C._main  D.MAIN

(A)

3.程序每条语句后面必须有哪个符号

A.;         B. .      C. !      D. :

(A)

4.下面那条语句是错的(多选)

A. cout>>3+4;   B.COUT<<3*4;    C.cout<<3/4;       D.cout<<3-4;

(ABD)

5.下面哪个程序段是正确的

A.#include using namespace std; int main(){ return 0;}

B.include usin namespace std; int main(){ return 0; }

C.include (iostream)using namespace std; int main(){ return0;}

D.#include usin namespace std; int main(){ return 0; }

(A)

第二章 变量有代数思维-语法自测

S2.1下面哪些项是合法的变量定义

多选题

A.pi  B._main C. int  D.CH E.5kg  F._6km  G.this_is_a_long_var H. status-idx  I.good-man

(ABDFG)

S2.2 下面哪条语句中的*表示乘法(设所有变量均已合法定义过)

A *ptr=0;

B *p=’A’;

C sum+=*p;

D sum=data *rate;

(D)

S2.3 为了从键盘输入数值给变量,下面哪条语句是正确的

A cin>>data;

B cin>>val;

C cin<<input;

D CIN>>mu;

(B)

S2.4 下面哪些给变量设定数值的方法是正确的

A.cin<<k;

B.cin>>sum;

c. cin>>sum>>data>>val;

D.cin>>”SUM=”>>sum>>”VAL=”>>val;

E. int m=0, n=1;

F. float f=0, k=1;

(BDEF)

S2.5 假定所有变量均已合法定义且有合理的存储值,下面哪条语句的写法没有错误?

A cout<<3**4+5*price+sum<<endl;

B cout<<w*h/2<<’’<<”m^2”<<endl;

C sum+=+result*4;

D CIN>>k++;

(BC)

第三章逻辑推理与枚举解题-语法自测

以下程序片段的运行结果是

char good_man=’K”;

if(good_man>=’A’)

 good_man=good_man-’A’+’a’

else

 good_man=’X’;

cout<<good_man;

A. ‘m’   B.k  c K  D X

(B)

2. 以下程序片段共输出多少个”C/C++”

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

  for (int j=1; j<=6; j++)

  {

cout<<”C/C++”<<endl;

if(j%3 ==0)

break;

  }

 A  6      B 9       C 2     D 3

3. 能用来判断变量X”在2013年2016之间(含2013和2016)”的表达式是:

A.(2013<=x<=2016)

B.(2013<=x)&&(x<=2016)

C.(x>=2013)&&(x<=2016)

D.(x<=2016&x>=2013)

E.(x>=2013)||(x<=2016)

F.(x>=2013)|(x<=2016)

(B C)

4.以下哪些写法有语法错误

A. for(i=0,i

B.’A’+3

C. for (; ;) { }

D.if(x == true){ } else (x!=true){ }

(AD )

以下哪些循环语句的循环体执行了100次

A for(int i=0; i<100; i++) cout <<”hello”;

B.for(int i=1; i<=100; i++) cout <<”hello”;

C.for(int i=0; i<=100; i++)cout <<”hello”;

D.for(int i=100; i>0; i--) cout<<”hello”;

(A B D)

以下程序片段的运行结果是

int i = 43;

cout <<(i<<2) <<endl;

A. 172

B. 432

C.32

D.45

( A)

以下能输出61到100(含61和100)中所有偶数的程序片段是

A. for (int i=61; i<=100; i++)    if(i%2==0)

B. for (int i=62; i<=100; i+=2)cout <<i<<’’;

C. for(int i=61; i<=100; i++) if (i&1)==0)cout <<i <<’’;

D.for(int i=1; i<=(100-60)/2; i++) cout <<60+i*2<<’’;

(ABCD)

第四章 筛法与查找--语法自测

下列函数声明的写法,哪些语法上是不正确的?

A void SelectionSort (int cards[], int n);

B void SelectionSort(int cards[]; int n);

C void SelectionSort(int cards[]. int n);

D void SelectionSort(int[], int);

(BC)

2 下列数组定义/初始化的写法,哪些语法上是正确的?

A int a[10]={1,2,3,4,5,6,7,8,9,10};

B int a[10]={0};

C int a[10];

D int a[]={0};

E int a[];

(ABCD)

3 下列数组定义的写法,哪些语法上是正确的?

A  int a[10];

B  bool b[10.0];

C  char c[];

D double d[4*3+2]

(AD)

4 将下面的主函数中“****”的行换成下列哪些语句,可以进行正确的数组访问?

in main()

{

 int A[10]=[0],B[20]=[0];

 int i=2, j=3, k=5;

 ****

 return 0;

}

A cin>>A[i];

B cout<<B[k];

C A[j]=4;

D B[i]=A[j]+5;

E cin>>A[i-j];

F A[i+4]=3;

G B[3*i+j]=4;

H A[i+j+k]=10;

(ABCDFG)

下列函数声明的写法,哪些语法上是不正确的?

A  bool isPrime(int);

B  double double(int);

C  int GB2UTF8(int);

D  int 256bit_count(double);

(BD)

第五章 分治思想与递归-语法自测

以下关于运算符的new的用法,正确的是:

A int *p= new int[4];

B int *q=new int{100};

C char*s=new char[34.5];

D double *pd=new double [10*15];

(D)

2关于二维数组的写法,正确的是:

A  char message[4][100];

B  double matrix[4][4*4];

C  int chess [8][8];

D  const int N=4; flags[10][N*2];

(ABC)

以下代码执行后,变量K的值是多少:

int k=-1;

while(k<=5) k++;

A 5

B 6

C 0

D 4

(B)

已知tmp是用new分配出来的动态数组变量,则以下使用delete运算符释放内存的写法,正确的是:

A delete tmp;

B DELETE[] tmp;

C delete [] tmp;

D release tmp;
(C)

与下面代码片段等价的代码片段是

int k=0;

while(k++<10) cout <<”Now ,k=” <<k<<endl;

A int k; for (k=0; k<=10; k++) cout<<”Now ,k=”<<k+1<<endl;

B int k; for (k=0; k<10; k++) cout <<”Now ,k=”<<k<<endl;k++;

C int k; for (k=0; k<10; k++)cout<<”Now,k=”<<k+1<<endl; k++;

D int k; for (k=1; k<=10; k++) cout<<”Now,k=” <<k<<endl;

(CD)

第六章 递推与动态规划--语法自测

多选题

关于引用,以下哪些代码是正确的?

A  int &a=3;

B  int a=3; int &b=a

C  int a=3; int &b; b=a;

D  int a=3; int&b=a; b=4;

(B D)

以下代码执行结果是什么?

void f(int &a, int b)

{

a = 3;

a++;

b = 4;

b++;

}

int main()

{

  int a=0, b=-1;

  f(a,b);

 cout<<a<<’ ‘<<b<<endl;

 return 0;

}

A 0 -1

B 4 -1

C 0 5

D 4 5

(B)

3.以下代码执行结果是什么?

 int i=0;

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

  {

    if(i%2==0)

      continue;

    cout<<i<<’’;

}

A 1 2 3 4 5

B 1 3 4 5

C 1 3 5

D 1

(C)

4 以下代码执行结果是什么?

for (int i=0; i<10; i++)

 {

   switch(i)

   {

   case 0;

       cout<<’a’;

       break;

  case 1:

  case 2:

       cout<<’b’;

  case 3:

       cout<<’c’;

       break;

  default:

       cout <<’d’;

       break;

}

A abbcdddddd

B abcbccdddddd

C abcbccd

D abbcd

(B)

第七章 文本数据处理-语法自测

1 以下哪些结构定义的写法是不正确的

A  struct Time_t

   {

     int year, month,day,hour,minute, second;

   };

B  struct MyTime

   {

int y, m, d ,h, m,s ;

   };

C  struct Time

   {

     int yr,mt,dy,hr,mn,sc;

   }

D  struct StructTime

   (

     int year,month, day, hour,minute ,second;

   );

 E Struct TimeStruct

  {

int year, month,day,hour,minute ,second;

  };

 F struct while

   {

int year, month,day ,hour,minute,second;

};

(BCDEF)

2 已知如下的结构定义,选项中哪些变量的初始化在语法上是正确的

  struct t

  {

      int x;

      char y;

      double z;

   };

   A int a=0;

   B bool b[10]={false};

   C t c={2014810219, ‘A’, 92.5};

   D t d[3]={{1, ‘a’,0.2},{3,’c’,1.8},{5,’e’,5.0}};

  (ABCD)

3 下列哪些字符数组初始化的写法在语法上是正确的

   A char str[10]=”Hello”;

   B char str[10]={‘H’,’e’,’l’,’l’,’o’};

   C char str[10]={“H”,”e”,”l”,”l”,”o”};

   (AB)

 4 下列哪些字符数组初始化写法,作为字符串使用将会有隐患

A  char str[10]={‘H’,’e’,’l’,’l’,’o’,’\0’};

B  char str[5]={‘H’,’e’,’l’,’l’,’o’};

C  char str[10]=”Hello”;

D  char str[5]=”Hello”;

(BD)

  5 已经包含头文件的情况下,下列哪些文件操作的片段在语法上是不正确的。

  A int x=0; ifstream myfile ;myfile .open(“a”); myfile <<x;

if (myfile.eof())  cout <<x<<”:end of file”<<endl;

  B int x=0; ifstream myfile (‘a’); myfile>>x;

   if(myfile .eof())  cout<<x<<”: end of file”<<endl;

  C int x=0; ifstream myfile; myfile.Open(“a”); myfile >>x;

if (myfile.eof())  cout<<x<<”:end of file “<<endl;

  D int x=0; ifstream myfile(“a”); myfile>>x;

if (myfile.EOF())   cout<<x<<”:end of file”<<endl;

  (ABCD)

第八章 非文本数据处理-语法自测

S8.1 已知有如下代码片段,

struct Person

{

 char name[20];

 int score;

};

Person ptr;

以下选项有错误的是:

A ptr =new Person; ptr.name=”Zhang San”;

  ptr.score=98;

B ptr.name=”Zhang San” ; ptr.score=98;

C ptr=new Person ;strcpy(prt->name ,”Zhang San”);

 ptr->score =98;

D strcpy(ptr.name,”Zhang San”); ptr.score=98;

E cin>>ptr.name>>ptr.score;

F ptr=new Person; strcpy(ptr->name, “Zhang San”);

  cin>>ptr->score;

G ptr=new Person; cin>>ptr.name>>ptr.score;

H ptr=new Person; cin>>ptr->name>>ptr->score;

(ABDEG)

S8.2 关于以二进制方法打开文件,以下选项中没有编译错误的是

A ofstream ex1(“homework1.dat”, IOS:BINARY);

B ifstream hw(“mywork”, ios: binary);

C ifstream zat(“input”,ios::binary);

D ofstream fout(“out”,IOS::binary)

E ifstream f(‘output’,ios::Binary);

F ofstream f2(“mooc.fop.txt”,ios::binary);

(C  F)

S8.3 关于以二进制方式读写文件内容,以下选项中没有错误的是

A char name[100]; ifstream f1(“input”,ios:binary);

  f1.read(name,100);

B char name[100]; ifstream f1(“input”,ios::binary);

  f1.read(name,sizeof(name));

C char*name=new char[100];

  ifstream f1(“input”.ios::binary); f1.read(name,100);

D char*name =new char[100];

  ifstream f1(“input”,ios::binary);

  f1.read(name,sizeof(name));

E char name[100]=”MOOC-CHAP08”;

  ofstream f2(“out.txt”,ios::binary); f2<<name;

F char name[100]=”MOOC-CHAP08”;

  ofstream f2(“out.txt”,ios::binary);

  f2.write(name,100);

G char name[100]=”MOOC-CHAR08”;

  ofstreamf2(“out.txt”,ios::binary);

  f2.write(name,sizeof(name));

H double score =3.45;

  ofstream f2(“out.dat”, ios::binary);

  f2.write((char*) & score,sizeof(double));

I double score =3.45;

 ofstream f2(“out.dat”, ios::binary);

 f2.write((char*)&score,sizeof(score));

J double *ptr ,score=3.45; ptr=&score;

 ofstream f2(“out.dat”, ios::binary);

 f2.write((char*)ptr, sizeof(double));

K double *ptr, score=3.45; ptr=&score;

  ofstream f2(“out.dat”,ios::binary);

  f2.write((char*)ptr,sizeof(p));

(ABCFGHIJ)

4 S8.4以下代码片段没有编译错误的是

 A int a[4]={1,2,3,4}; int b[4]; b=a;

 B struct S{int data[4];}; S a={{1,2,3,4}}; S b; b=a;

 C char* ptr; char a[10]=”MOOC”; ptr=a;

 D char ptr[10]; char a[10]=”MOOC”; ptr=a;

 E struct S{char data [10];};  S a, ptr;

  strcpy(a.data, “MOOC”);  ptr=a;

  cout <<ptr.data <<endl;

(BCE)

S8.5 以下选项中,代码片段与示意图相符的是

int  a[4];

a

int A[3][2];

int *a[3];

int ** a;

a = new int*[3];

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

a[i] = new int[2];

A int a[4];

B int A[3][2];

C int *  a[3];

D int **a ; a=new int*[3]; for (int i=0; i<3; i++)

  a[i]=new int[2];

(ABCD)

第九章 可配置的程序设计-语法自测

1 以下main函数声明写法正确的是:

A int main(int *argc, char **argv);

B int main(int argc, char **argv);

C void main(int &argc, char*argv[]);

D int main(int ARGC,char *ARGV[]);

(BD)

2 已知命令行gcc -c -o prog.o a.cpp b.cpp,

则对于gcc程序来说,它的main函数接受的argc和argv参数的取值是:

argc=6

argv[0]=gcc

argv[1]=-c

argv[2]=-o

argv[3]=prog.o

argv[4]=a.cpp

9.3 已知以下代码编译后生成可执行文件为run.exe

则依次在命令行窗口输入

run 2 3

run 4 5

run 2 9

后,程序将输出结果为()

#include <iostream> #include <fstream>#include<cstdlib>using namespace std;

int main(int argc, char*argv[])

{

 ifstream fin(“res.bk”);

 int sum=0;

 if(fin)

 {

  fin>>sum;

  fin.close();

 }

 sum+=atoi(argv[1])*atoi(argv[2]);

 ofstream fout(“res.bk”);

 fout <<sum <<endl;

 fout.close();

 if (sum>40)

   cout<<”YES!”<<sum;

 return 0;

 }

(YES!44)

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值