今天面试了深圳一家公司的C++开发,线上笔试,感觉题目做的不是很好,来这里复盘一下
总体题目是15个选择+10个编程题
抽象类可以定义友元函数
私有继承基类中的pulibc成员在派生类中仍然是private的
保护继承基类中的pulibc成员在派生类中仍然是protected的
公有继承基类中的pulibc成员在派生类中仍然是public的
x=0;for(i=0;i<99;i++) if(i) x++;求i值等于多少
最终i为98
while(!x)中的!x 与什么等价?
x==0
int a=10,b=11,c=12,求表达式(a+b)<c&&b==c的值
0
x=1,y=1,z=1,执行w=++x ||++y && ++z,求xyz的值分别为多少
x=2,y=1,z=1
int a=3,b=4,c=5;求(a+b)>c &&b == c
0
int a = 5,b=3,求a+=a*=++b*3;
120
int x=5,x+=1.9,求x为多少?
6
代码题
一个简单的swap函数
这个我不可能错
一个getMemory函数,指针访问出错,并且修改好
一个数组错误题
指出下列程序中的错误
void test1(){
char string[10];
char* st1 = "0123456789";
strcpy(string,str1);
}
应该是11,数组过小放不进去
试题1字符串str1需要11个字节才能存放下(包括末尾的’\0’),而string只有10个字节的空间,strcpy会导致数组越界;
#include <iostream>
using namespace std;
class basic
{
private:
int a, b;
public:
basic(int i, int j)
{
a = i;
b = j;
}
void add(int x, int y)
{
a += x;
b += y;
}
void print()
{
cout << "a=" << a << ",b=" << b << endl;
}
};
class child : public basic
{
private:
int c, d;
public:
child(int x, int y, int z, int w) : basic(x, y)
{
c = z;
d = w;
}
void cadd(int i, int j)
{
c += i;
d += j;
add(-i, -j);
}
void cprint()
{
basic::print();
cout << "c=" << c << ",d=" << d << endl;
}
};
int main()
{
basic k1(10, 20);
k1.print();
child k2(10, 20, 30, 40);
k2.cadd(100, 200);
k2.basic::print();
k2.print();
k2.cprint();
return 0;
}
求这段程序的运行结果,不能使用编译器
a=10,b=20
a=-90,b=-180
a=-90,b=-180
a=-90,b=-180
c=130,d=240