#10648. 第2节 C++语言基础(小奥改编)

第2节 C++语言基础(小奥改编)

一、单项选择题(每题有且仅有一个正确选项)

1.若有如下程序段,其中s、a、b、c均已定义为整型变量,且a、c均已赋值,c > 0。

s=a;
for( b = 1; b <=c; b ++)
s += 1;

则与上述程序段功能等价的赋值语句是( )。{{ select(1) }}

  • s = a + b
  • s = a + c
  • s = s + c
  • s = b + c

2.要求以下程序的功能是计算:s = 1 + 1/2 + 1/3 + ... + 1/10。

#include<iostream>
using namespace std;
int main(){
int n;
float s;
s = 1.0;
for (n = 10; n > 1; n --)
s = s + 1/n;
cout << s << endl;
return 0;
}

程序运行后输出结果错误,导致错误结果的程序行是()。{{ select(2) }}

  • s = 1.0;
  • for (n = 10; n > 1; n--)
  • s = s + 1 / n;
  • cout << s << endl;

3.有以下程序:

#include<iostream>
using namespace std;
int main() {
int s, a, n;
s = 0;
a = 1;
cin >> n;
do {
s += 1;
a -= 2;
} while (a != n);
cout << s << endl;
return 0;
}

若要使程序的输出值为2,则应该从键盘给n输入的值是()。{{ select(3) }}

  • -1
  • -3
  • -5
  • 0

4.有以下程序:

#include<iostream>
using namespace std;
int main() {
int k = 4, n = 0;
while (n < k) {
n ++;
if (n % 3 != 0)
continue;
k--;
}
cout << k << "," << n << endl;
return 0;
}

程序运行后的输出结果是()。{{ select(4) }}

  • 2,2
  • 2,3
  • 3,2
  • 3,3

5.为了统计一个非负整数的二进制形式中1 的个数,代码如下:

int ret = 0;
while (x) {
ret ++;
________;
}
return ret;

则空格内要填入的语句是()。{{ select(5) }}

  • x >>= 1
  • x &= x - 1
  • x |= x >> 1
  • x <<= 1

6.下列程序中,正确计算1, 2, …, 100这100个自然数之和sum(初始值为0)的是()。{{ select(6) }}

  • image
  • image
  • image
  • image

7.若有变量int a,float x,y,且a=7,x=2.5,y=4.7,则表达式x+a%3*(int)(x+y)%2/4的值大约是()。{{ select(7) }}

  • 2.500000
  • 2.750000
  • 3.500000
  • 0.000000

8.设变量x为float型且已赋值,则以下语句中能将x中的数值保留到小数点后两位,并将第三位四舍五入的是()。{{ select(8) }}

  • x = (x * 100) + 0.5 / 100.0;
  • x = (x * 100 + 0.5) / 100.0;
  • x = (int) (x * 100 + 0.5) / 100.0;
  • x = (x / 100 + 0.5) * 100.0;