65,208
社区成员
发帖
与我相关
我的任务
分享
以下试题假设环境为Win32、VC6.0开发工具,且代码已包含正确的头文件及命名空间。
时间40分钟
1 下面程序的运行结果是什么。
#define SQR(X) X*X
int main()
{
int a = 2;
int b = 1;
cout << a*SQR(a+b)/SQR(a+b) << endl;
return 0;
}
2 下面程序的运行结果是什么。
int main(void)
{
int a = 0;
if(!(a = 5) || a || (a *= 3))
a *= 2;
cout << ++a << endl;
cout << a++ << endl;
return 0;
}
3 下面程序的运行结果是什么。
int Fun(int a)
{
static int b = 1;
return a * b++;
}
int main(void)
{
cout << Fun(2) * Fun(3);
return 0;
}
4 下面程序运行结果是什么。
struct STest
{
int a;
char c;
};
void Test(char str1[100])
{
char str2[] = "Hello";
char *pStr2 = str2;
STest stest;
cout << sizeof(str1) <<" ";
cout << sizeof(str2) << endl;
cout << sizeof(pStr2) << endl;
cout << strlen(str2) << endl;
cout << strlen(pStr2) << endl;
cout << sizeof(stest) << endl;
}
int main()
{
char p[100];
Test(p);
}
5 下面三条语句有什么区别。
(1)const char * p
(2)char const * p
(3)char * const p
6 下面三个函数的运行结果分别是什么。
void Test1()
{
char *str1="0123456789";
strcpy(str1, "hello");
cout << str1 << endl;
}
void Test2()
{
char string[10];
char *str1="0123456789";
strcpy(string, str1);
cout << str1 << endl;
}
void Test3()
{
char string[5], str1[5];
for(i=0; i<5;i++)
str1[i] ='a';
strcpy(string, str1);
cout << str1 << endl;
}
7 下面程序的运行结果是什么。
struct A{int a;};
void CreateA(A* a)
{
a = new A;
}
int main()
{
A* pA = NULL;
CreateA(pA);
cout << pA->a << endl;
delete pA;
}
8 下面程序的运行结果是什么。
class A
{
public:
virtual void print(void)
{
cout << "A::print()" << endl;
}
};
class B : public A
{
public:
virtual void print(void)
{
cout << "B::print()" << endl;
};
};
class C : public B
{
public:
void print(void)
{
cout << "C::print()" << endl;
}
};
void print(A a)
{
a.print();
}
int main(void)
{
A a, *pa,*pb,*pc;
B b;
C c;
pa=&a;
pb=&b;
pc=&c;
a.print();
b.print();
c.print();
pa->print();
pb->print();
pc->print();
print(a);
print(b);
print(c);
return 0;
}
9 下面代码有什么问题。
int main()
{
vector<int> array;
array.push_back(1);
array.push_back(2);
array.push_back(3);
array.push_back(2);
//删除array数组中所有的2
vector<int>::iterator iter=array.begin();
for(; itor!=array.end(); ++iter)
{
if(2 == *iter)
array.erase(iter);
}
return 0;
}
10 下面语句中的 “_T()” 的作用是什么。
CString str = _(“Hello”);
11 编写类String的构造函数、析构函数和赋值函数。
已知类String的原型为:
class String
{
public:
// 普通构造函数
String(const char *str = NULL);
// 拷贝构造函数
String(const String &other);
// 析构函数
~String(void);
// 赋值函数
String & operate =(const String &other); private:
// 用于保存字符串
char *m_data;
};
12 简述下面API的做用,写出创建窗口流程,并说明窗口函数是在何时指定的。
RegisterClass
CreateWindow
GetMessage
TranslateMessage
DispatchMessage
以下试题假设环境为Win32、VC6.0开发工具,且代码已包含正确的头文件及命名空间。
时间40分钟
1 下面程序的运行结果是什么。
#define SQR(X) X*X
int main()
{
int a = 2;
int b = 1;
cout << a*SQR(a+b)/SQR(a+b) << endl;
return 0;
}
// 9
2 下面程序的运行结果是什么。
int main(void)
{
int a = 0;
if(!(a = 5) || a || (a *= 3))
a *= 2;
cout << ++a << endl;
cout << a++ << endl;
return 0;
}
// 11
// 11
3 下面程序的运行结果是什么。
int Fun(int a)
{
static int b = 1;
return a * b++;
}
int main(void)
{
cout << Fun(2) * Fun(3);
return 0;
}
// 12
4 下面程序运行结果是什么。
struct STest
{
int a;
char c;
};
void Test(char str1[100])
{
char str2[] = "Hello";
char *pStr2 = str2;
STest stest;
cout << sizeof(str1) <<" ";
cout << sizeof(str2) << endl;
cout << sizeof(pStr2) << endl;
cout << strlen(str2) << endl;
cout << strlen(pStr2) << endl;
cout << sizeof(stest) << endl;
}
int main()
{
char p[100];
Test(p);
}
// 4
// 6
// 4
// 5
// 5
// 8
5 下面三条语句有什么区别。
(1)const char * p // p所指向区域不可修改
(2)char const * p // 同上
(3)char * const p // p不能改变它的指向
6 下面三个函数的运行结果分别是什么。
void Test1()
{
char *str1="0123456789";
strcpy(str1, "hello");
cout << str1 << endl;
}
// segmentation fault
void Test2()
{
char string[10];
char *str1="0123456789";
strcpy(string, str1);
cout << str1 << endl;
}
// 0123456789
// maybe the desired result, but overflowed
void Test3()
{
char string[5], str1[5];
for(i=0; i<5;i++)
str1[i] ='a';
strcpy(string, str1);
cout << str1 << endl;
}
// maybe aaaaa five 'a', but overflowed
7 下面程序的运行结果是什么。
struct A{int a;};
void CreateA(A* a)
{
a = new A;
}
int main()
{
A* pA = NULL;
CreateA(pA);
cout << pA->a << endl;
delete pA;
}
// segmentation fault
// void CreateA(A **a) { *a = new A; } CreateA(&pA);
// void CreateA(A* &a) { a = new A; } CreateA(pA);
8 下面程序的运行结果是什么。
class A
{
public:
virtual void print(void)
{
cout << "A::print()" << endl;
}
};
class B : public A
{
public:
virtual void print(void)
{
cout << "B::print()" << endl;
};
};
class C : public B
{
public:
void print(void)
{
cout << "C::print()" << endl;
}
};
void print(A a)
{
a.print();
}
int main(void)
{
A a, *pa,*pb,*pc;
B b;
C c;
pa=&a;
pb=&b;
pc=&c;
a.print();
b.print();
c.print();
pa->print();
pb->print();
pc->print();
print(a);
print(b);
print(c);
return 0;
}
// A::print()
// B::print()
// C::print()
// A::print()
// B::print()
// C::print()
// A::print()
// A::print()
// A::print()
9 下面代码有什么问题。
int main()
{
vector<int> array;
array.push_back(1);
array.push_back(2);
array.push_back(3);
array.push_back(2);
//删除array数组中所有的2
vector<int>::iterator iter=array.begin();
for(; itor!=array.end(); ++iter)
{
if(2 == *iter)
array.erase(iter);
}
return 0;
}
// segmentation fault, dut to dynamic change the array during iterating
10 下面语句中的 “_T()” 的作用是什么。
CString str = _(“Hello”);
// unicode and ascii char compatible
11 编写类String的构造函数、析构函数和赋值函数。
已知类String的原型为:
class String
{
public:
// 普通构造函数
String(const char *str = NULL);
// 拷贝构造函数
String(const String &other);
// 析构函数
~String(void);
// 赋值函数
String & operate =(const String &other); private:
// 用于保存字符串
char *m_data;
};
// too long --!!
12 简述下面API的做用,写出创建窗口流程,并说明窗口函数是在何时指定的。
RegisterClass // 注册窗口类,窗口函数在这里指定
CreateWindow // 创建窗口
GetMessage // 从进程的消息队列得到一个消息
TranslateMessage // 转换虚拟按键到字符
DispatchMessage // 发配消息至窗口进程