1. 关于堆的说法正确的是_______
(A) 堆需要手动释放
(B) 堆能自动释放
(C) 申请堆内存会触发全局锁
(D) 申请堆内存不会触全局锁
2. 关于栈的说法正确的是_______
(A) 栈需要手动释放
(B) 栈能自动释放
(C) 申请栈内存会触发全局锁
(D) 申请栈内存不会触全局锁
3. 函数foo()运行过程中,处于栈上的变量有_______
(A) a
(B) b
(C) c
(D) d
(E) foo的返回值
int foo(int a){
static int b=0;
int c=1;
int* d=new int(2);
return 3;
}
4. 函数foo()运行后屏幕输出的内容是________________________________________
class A{
public:
void foo1(){
std::cout<<"A1,";
}
virtual void foo2(){
std::cout<<"A2,";
foo1();
}
void foo3(){
std::cout<<"A3,";
foo2();
}
};
class B : public A{
public:
void foo1(){
std::cout<<"B1,";
}
void foo2(){
std::cout<<"B2,";
foo1();
}
void foo3(){
std::cout<<"B3,";
foo2();
}
};
void foo(){
B b;
A& a=dynamic_cast<A&>(b);
a.foo3();
b.foo3();
}
5. 下面函数foo()的输出值是______
(A) 1
(B) 2
(C) 3
(D) 4
int foo(const char* a){
return 1;
}
int foo(const char* a,int b){
return 2;
}
int foo(const std::string& a){
return 3;
}
int foo(const std::string& a,int b){
return 4;
}
int foo(){
return foo("hello");
}
6. foo()运行期间类C1可能会发生深拷贝的成员变量是_____________
(A) a
(B) b
(C) c
(D) d
(E) e
(F) f
(G) g
(H) h
class C1{
public:
int a;
std::string b;
int* c;
std::string* d;
static int e;
static std::string f;
static int* g;
static std::string* h;
};
void foo(){
C v1,v2;
v1=v2;
}
7. 下面函数foo2()运行后屏幕显示值是______
(A) a
(B) b
(C) c
(D) 随机值
std::string& foo(int id){
std::string default_result="c";
static std::map<int,std::string> v1{{1,"a"},{2,"b"}};
auto iter=v1.find(id);
if(iter!=v1.end()){
return iter->second;
}else{
return default_result;
}
}
void foo2()
{
std::cout<<foo(3);
}
8. 关于foo1()和foo2()运行速度的说法正确的是______________
(A) 一样快
(B) foo1快
(C) foo2快
(D) 不确定
void foo1()
{
std::vector<int> a={1,2,3,4};
a.insert(a.begin(),5);
}
void foo2(){
std::list<int> b={1,2,3,4};
b.insert(b.begin(),5);
}
9. 下面函数foo(0x120)的返回值是_________
int foo(int x)
{
x = x & (x - 1);
return x;
}
10. 下面函数foo()的返回值是_______
int foo()
{
int a[] = {1,2,3,4,5,6,7,8};
char* pa = reinterpret_cast<char*>(a);
auto b = (*pa) + *(pa + 4)+*(pa+6);
return b;
}
Qt测试题(客观题,闭卷,5分钟)
1. 若signal和slot函数分属不同线程,则connect中可以使用的连接类型是_____
(A) Qt::AutoConnection
(B) Qt::DirectConnection
(C) Qt::QueuedConnection
2. 下面函数foo()的返回值是_________
(A) 0
(B) 1
(C) 2
(D) 随机值
int foo(){
QVector<int> a, b;
a.append(1);
QVector<int>::iterator i = a.begin();
b = a;
a[0] = 2;
b.clear();
return *i;
}
C++主观题(开卷,可以用手机上网查资料,30分钟)
1. Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.
Hint: You must implement a solution with a linear runtime complexity and use only constant extra space.Consider xor operation please.
int find_single_number(std::vector<int>& numbers){
// implement this function here
}
2. 请使用STL的std::remove_if算法删除std::vector<int> 容器中大于5的数字。
C++客观题
1. 答案:AC
2. 答案:BD
3. 答案:ACD
4. 答案:A3,B2,B1,B3,B2,B1
5. 答案:1
6. 答案:B
7. 答案:D
8. 答案:C
9. 答案:0x100
10. 答案:3
Qt客观题
1. 答案:AC
2. 答案:D
C++主观题:
1. 答案(答案不唯一,供参考):
int find_single_number(std::vector<int>& numbers){
int result = 0;
for (auto n : numbers)
{
result = result ^ n;
}
return result;
}
2. 答案(答案不唯一,供参考):
std::vector<int> v = { 1,2,4,3,2,1,3,7 };
auto iter = std::remove_if(v.begin(), v.end(), [](int x) {
return x > 5;
});
v.erase(iter, v.end());