面试题积累(三)

来源:互联网 发布:做单页用什么软件 编辑:程序博客网 时间:2024/04/29 16:31

题目10:Which of the following statements describe the results of executing the code sippet below in C++?

        

C/C++ codeint i = 1;void main(){    int i = i;}


A. The i within main will have an undefined value.

B. The i within main will have a value of 1.

C. The compiler will not allow this statement.

D. The i within main will have a value of 0.

分析:

       选 A,main()中的i与外面的i无关,是一个未定义的值。


题目11:What does the following program print ?

#include <iostream> using namespace std; int main()  {      int x=2,y,z;   x *=(y=z=5); cout << x << endl;     z=3;      x ==(y=z);   cout << x << endl;     x =(y==z);   cout << x << endl;     x =(y&z);    cout << x << endl;     x =(y&&z);   cout << x << endl;     y=4;   x=(y|z);     cout << x << endl;     x=(y||z);    cout << x << endl;     return 0; }


分析:

(1) x *=(y=z=5)的意思是说5赋值给z,z再赋值给y,x=x*y,所以x为2*5=10。

(2) x ==(y=z)的意思是说z赋值给y,然后看x和y相等否?不管相等不相等,x并未发生变化,仍然是10。

(3) x =(y==z)的意思是说首先看y和z相等否,相等则返回一个布尔值1,不等则返回一个布尔值0。现在y和z是相等的,都是3,所以返回的布尔值是1,再把1赋值给x,所以x是1。

(4) x =(y&z)的意思是说首先使y和z按位与。y是3,z也是3。y的二进制数位是0011,z的二进制数位也是0011。所以y&z的二进制数位仍然是0011,也就是还是3。再赋值给x,所以x为3。

(5) x =(y&&z) 的意思是说首先使y和z进行与运算。与运算是指如果y为真,z为真,则(y&&z)为真,返回一个布尔值1。这时y、z都是3,所以为真,返回1,所以x为1。

(6) x =(y|z) 的意思是说首先使y和z按位或。y是4,z是3。y的二进制数位是0100,z的二进制数位是0011。所以y&z的二进制数位是0111,也就是7。再赋值给x,所以x为7。

(7) x =(y||z) 的意思是说首先使y和z进行或运算。或运算是指如果y和z中有一个为真,则(y||z)为真,返回一个布尔值1。这时y、z都是真,所以为真,返回1。所以x为1。

     



0 2
原创粉丝点击