📜  C++ 程序的输出 |设置 40

📅  最后修改于: 2022-05-13 01:56:10.890000             🧑  作者: Mango

C++ 程序的输出 |设置 40

Q.1 以下程序的输出是什么?

#include 
using namespace std;
int main()
{
    int i, j, k;
    int sum[2][4];
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 3; j++)
            sum[i][j];
    }
    cout << sum[i][j];
    return 0;
}

选项
一) 3 3
b) 2 0
c) 垃圾值
d) 2 3

Answer : c

说明:在这个程序中,在给定的数组中,我们没有分配任何值,因此它将指向任何垃圾值。

Q.2 以下程序的输出是什么?

#include 
using namespace std;
int main()
{
    char m;
    switch (m) {
    case 'c':
        cout << "Computer Science";
    case 'm':
        cout << "Mathematics";
        break;
    case 'a':
        cout << "Accoutant";
        break;
    default:
        cout << "wrong choice";
    }
    return 0;
}

选项
a) 计算机科学、数学
b) 数学
c) 错误的选择
d) 错误



Answer : c

说明:在这个程序中,m 是一个变量,它没有被赋值,这意味着 – m 有任何垃圾值。所以,它不匹配任何大小写,默认情况下它会打印“错误的选择”。

Q.3 以下程序的输出是什么?

#include 
using namespace std;
#include 
using namespace std;
int main()
{
    int i, x[5], y, z[5];
    for (i = 0; i < 5; i++) {
        x[i] = i;
        z[i] = i + 3;
        y = z[i];
        x[i] = y++;
    }
    for (i = 0; i < 5; i++)
        cout << x[i] << " ";
    return 0;
}

选项
a) 3 4 5 6 7
b) 4 5 6 7 8
c) 2 3 4 5 6
d) 以上都不是

Answer : a

解释:在这个循环中,简单地首先分配 y 的值,然后递增所以没有变化。

Q.4 以下程序的输出是什么?

#include 
using namespace std;
int max(int& x, int& y, int& z)
{
    if (x > y && y > z) {
        y++;
        z++;
        return x++;
    } else {
        if (y > x)
            return y++;
        else
            return z++;
    }
}
int main()
{
    int A, B;
    int a = 10, b = 13, c = 8;
    A = max(a, b, c);
    cout << a++ << " " << b-- << " " << ++c << endl;
    B = max(a, b, c);
    cout << ++A << " " << --B << " " << c++ << endl;
    return 0;
}

选项
a) 10 14 8 或 14 13 8
b) 10 13 8 或 11 14 9
c) 10 14 9 或 14 12 9
d) 11 12 8 或 13 14 8

Answer : c

说明:这里max函数是返回最大值,第一次打印时b的值递减,剩下的两个a和c在第二次调用函数保持不变。

Q.5 以下程序的输出是什么?

#include 
using namespace std;
int main()
{
    for (int i = 10; i > 6; i = i - 2)
        cout << i;
    for (int i = -5; i > -7; i--)
        cout << i + 1;
    return 0;
}

选项
a) 10 8 6 -5 -6
b) 10 8 -4 -5
c) 10 8 -5 -6
d) 8 6 -4 -5

Answer : b

说明:执行第一个循环并打印 10、8,然后条件变为假,因此循环退出。然后执行第二个循环并检查条件并在条件变为假之后打印 -4, -5 然后它被终止。