问题1.该项目的产出是什么?
#include <iostream> using namespace std; void fun( int & a, int b) { a += 2; b += 1; } int main() { int x = 10, y = 2; fun(x, y); cout << x << " " << y << " " ; fun(x, y); cout << x << " " << y; return 0; } |
选项 a) 102 102 b) 12 2 14 2 c) 123143 d) 123143
Answer : b
说明: 在这个程序中,在main()中,我们在fun()中传递两个值x或y,fun()收到x的引用,所以它的值递增,但y已经递增,但不是一个引用,所以y在主块中是相同的。
问题2.该项目的产出是什么?
#include <iostream> using namespace std; void f2( int p = 30) { for ( int i = 20; i <= p; i += 5) cout << i << " " ; } void f1( int & m) { m += 10; f2(m); } int main() { int n = 20; f1(n); cout << n << " " ; return 0; } |
选项 a) 25 30 35 20 b) 20253020 c) 25 30 25 30 d) 2025300
Answer : d
说明: 在这个程序中,main()调用f1()并在f1()中传递n的值,f1()收到n的引用,并增加其值的10,现在n是30。再次调用f2()并在f2()中传递m的值,f2()接收m的值并检查条件并打印值。
问题3.该项目的产出是什么?
#include <iostream> using namespace std; void fun( char s[], int n) { for ( int i = 0; s[i] != ' ' ; i++) if (i % 2 == 0) s[i] = s[i] - n; else s[i] = s[i] + n; } int main() { char str[] = "Hello_World" ; fun(str, 2); cout << str << endl; return 0; } |
选项 a) EgjnmaTqpnb b) FgjnmaUqpnb c) Fgjnm_Uqpnb d) EgjnmaTqpnb
Answer : b
说明: 在main()中,调用fun()并传递字符串,一个整数是2。在fun()中,循环i被迭代并检查条件,如果i为偶数,则将索引值减少2,如果i为奇数,则将索引值增加2。
问题4.该项目的产出是什么?
#include <iostream> using namespace std; int main() { int x[] = { 12, 25, 30, 55, 110 }; int * p = x; while (*p < 110) { if (*p % 3 != 0) *p = *p + 1; else *p = *p + 2; p++; } for ( int i = 4; i >= 1; i--) { cout << x[i] << " " ; } return 0; } |
选项 a) 110 56 32 26 b) 110573027 c) 110 56 32 25 d) 100553025
Answer : a
说明: 在这个程序中,指针p包含数组x的基址,所以*p包含x[0]的值,p++是下一个元素的指向地址。
问题5 这个程序的输出是什么?
#include <iostream> using namespace std; int main() { char * str = "GEEKSFORGEEK" ; int * p, arr[] = { 10, 15, 70, 19 }; p = arr; str++; p++; cout << *p << " " << str << endl; return 0; } |
选项 a) 10 Eeksforgek b) 15 Geeksforgek c) 15 Eeksforgek d) 10 Geeksforgek
Answer : C
说明: 在这种情况下,str包含字符串的基址,指针p包含数组的基址。如果字符串和指针的值增加1,那么它将指向字符串和数组的下一个地址值。
本文由 贾亚克·贾因 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。