微软就是微软...题目的确很有意思..特别是那几道编程题..
Test 1:
x++与++y。第一想法就是他们的结果都是11,不过觉得太简单了,怕有坑。发现想多了。
Test 2:
字符串判断想不相等。与java不同,是对象。但这里是一样的,都是相等的。
附带说明,一个正确的程序的返回值是0(return 0)。而且当时很2b地,认为(7&1)的结果不是1,犯2了。
Test 3:
虚函数,还有重载,覆盖等问题。如果是virtual,在虚表里查,所以看实际类型。其他的,由指针的类型决定。
Test 4:
这道题输出的其实是22221111,题目中一个答案都不对!所以这道题应该不选!这样都行!
理由是,setValue(C* c, int i)里的类型是C*,而C的大小是4,D的大小是8。所以,按照C的大小去进行data[i]的操作。
还有一些智力题。挺有趣的。
一个是1:1000的毒酒,要用多少老鼠可是试出来。用2进制数,给每瓶毒酒编号,为1的数让对应的老鼠喝下去。
一个较轻的铅块,还有其他的都是正常的铅块,总共有N个。只允许称3次,那些N值可以。不一定是一半半地分下去。
测试代码如下:
#include <iostream>
using namespace std;
class A
{
public:
virtual void f()
{
cout<<"A virtual f"<<endl;
}
void f() const
{
cout<<"A const f"<<endl;
}
};
class B: public A
{
public:
void f()
{
cout<<"B virtual f"<<endl;
}
void f() const
{
cout<<"B const f"<<endl;
}
};
void testVoid(const A* a)
{
a->f();
}
class C
{
public:
long c;
};
class D: public C
{
public:
long d;
};
void setValue(C* c, int i)
{
c[i].c = 2;
}
int main()
{
// test 1
cout<<"############# Test 1"<<endl;
int x = 10;
int y = 10;
x = x++;
y = ++y;
cout<<"x y is "<<x<<" "<<y<<endl;
// test 2
cout<<"############# Test 2"<<endl;
char * str = "ms";
if(str == "ms")
cout<<"Str equals ms"<<endl;
else
cout<<"Str not equals ms"<<endl;
if("ms" == "ms")
cout<<"two str equal"<<endl;
else
cout<<"two str not equal"<<endl;
// test 3
cout<<"############# Test 3"<<endl;
A * pa = new B();
pa->f();
testVoid(pa);
// test 4
cout<<"############# Test 4"<<endl;
D data[4];
int i;
for(i = 0; i < 4; i++)
{
data[i].c = 1;
data[i].d = 1;
setValue(data, i);
}
for(i = 0; i < 4; i++)
{
cout<<i<<" c is "<<data[i].c<<" d is "<<data[i].d<<endl;
}
return 0;
}
运行结果是:
############# Test 1
x y is 11 11
############# Test 2
Str equals ms
two str equal
############# Test 3
B virtual f
A const f
############# Test 4
0 c is 2 d is 2
1 c is 2 d is 2
2 c is 1 d is 1
3 c is 1 d is 1
Press any key to continue