C++笔试题2
C++笔试题(第2套)
时间:20分钟 总分:100分(第1题每小题2分,其余每题10分)
姓名: 分数:
-
C++有很多关键字,其中
① 自动类型推导的关键字是____________
② 外部变量声明的关键字是____________
③ 显式重写虚函数的关键字是____________
④ 抛出异常的关键字是____________
⑤ 模板定义的关键字是____________
int main() {
int a = 5, b = 10;
int &ref = a;
ref = b;
std::cout << a << " " << b;
return 0;
}
//如上输出为____________
- 在64位平台下,
void fun(char arr[])
{
cout << sizeof(arr);
}
int main(){
char arr[] = "12345";
cout << sizeof(arr); // ①
fun(arr); // ②
return 0;
}
//如上①②输出分别为____________
int main() {
unsigned char a = 255;
a = a + 1;
cout << (int)a;
return 0;
}
//程序的输出是____________
int main() {
int arr[3] = {10, 20, 30};
__________________________ // 补全代码使下一行输出结果为10,20,30
cout << p[-1] <<”,”<< *p <<”,”<< *(p + 1);
}
class Widget {
static int count;
int id;
public:
Widget() : id(++count) { cout << "C" << id; }
~Widget() { cout << "D" << id; }
};
int Widget::count = 0;
int main() {
Widget w1;
Widget* w2 = new Widget();
delete w2;
}
//如上程序的输出是____________
class Base {
public:
virtual void show() { cout << "Base"; }
};
class Derived : public Base {
public:
void show() override final { cout << "Derived"; }
};
class Child : public Derived {
public:
void show() override { cout << "Child"; }
};
//请指出如上程序中的问题____________
template<int N>
struct Factorial {
static const int value = N * Factorial<N-1>::value;
};
template<>
struct Factorial<0> {
static const int value = 1;
};
int main() {
cout << Factorial<5>::value;
}
//如上输出结果是____________
class Resource {
public:
Resource() { cout << "R "; }
~Resource() { cout << "~R "; }
};
int main() {
auto p1 = make_shared<Resource>();
auto p2 = p1;
weak_ptr<Resource> wp = p1;
p1.reset();
cout << (wp.expired() ? "Y" : "N");
p2.reset();
cout << (wp.expired() ? "Y" : "N");
}
//如上输出结果是____________
- Lambda表达式
int main() {
int counter = 0;
auto f = [counter]() mutable {
cout << ++counter;
};
f();
f();
cout << counter;
}
//如上输出结果是____________
浙公网安备 33010602011771号