偶前几天的C++面试题
偶前几天的C++面试题(省略了编程题),有兴趣的朋友看看!!!!一同分享,呵呵
C++笔试题(满分100分,时间40分钟)
将答案写在指定的答卷纸内,不能在考卷上作答。
一.填空(每小题4分,共40分)。
1. struct stru_type
{ int a;
double d;
char *pch;
stru_type *stru;
};
double x;
int n;
sizeof(stru_type)+sizeof(x)+sizeof(n)= ______。.
2. 在32位的操作系统中, char *pst=”this a string”;
(1) sizeof(pst)=_______。
(2) strlen(pst)=_______。
3. int a[6]={10,3,2, 7,9,5};
*a*(a+2)[1]=______。
4. 简述一下const与#define的异同。
5. void main()
{ char ch[10], *pstr=”this a test”:
strcpy(ch, pstr);
cout<<ch<<endl;
}
请问ch的输出结果是什么,为什么?
6. C++有4种存储类型,分别是自动、外部、静态和寄存器型。请回答以下问题:
int a;
static int sa;
执行以上程序,a和sa的值是多少?为什么?
7. 类的继承是面向对象一个非常重要的概念,继承方式有公有继承、保护继承和私有继承,请运用类的继承访问权限的理论,完成下面问题:
class A
{ public:
A();
fun1();
protected:
fun2();
};
class B:protected A
{ public:
B();
fun3();
protected:
fun4();
private:
int m_i;
}:
(1)派生类B的成员可否访问fun1和fun2? 并说明理由。
(2)派生类B的对象可否访问fun2? 如果不能,请说明理由。
(3)类B的对象可否访问fun4? 如果不能,请说明理由。
8. class A
{ public:
A(){ b++; }
void sets(int i){ s = i; }
int getb(){ return b; }
int gets(){ return s; }
private:
static int b;
int s;
};
int A::b=0;
void main()
{
A a[5];
for(int j=0; j<5; j++)
a[j].sets(j);
for(int i=0; i<5;i++)
{
cout<<”a[“<<i<<”].b=”<<a[i ].getb()<<endl;
cout<<”a[“<<i<<”].s=”<<a[i].gets()<<endl;
}
}
请写出执行以上程序后输出的结果。
9.数据20, 8, 12, 4, 30, 9依次入栈,不可能的出栈顺序是______。
A 12,30,9,4,8,20 B 8,9,30,4,12,20
C 20,12,4,9,8,30 C 20,4,9,30,12,8
10. 写出下面程序的输出结果。
void fun(char **p)
{
*p=*p+3;
}
void main()
{
char *ps="tested function";
fun(&ps);
cout<<ps<<endl;
}
二、改错题.,指出程序中的错误并加以纠正(每小题5分,共10分)。
1. void main()
{ int maxsize=20, a[maxsize]={10,5,12,30,45,7,50,63,2};
for(int i=0; i<5; i++)
cout<<a[i]<<endl;
}
2. #define N 50
void main()
{
int i=0, count=0;
char *pst=”this is a tested string”, str[N];
str=pst;
while(str[i]!=’\0’)
{
if(str[i]==’t’)
count++;
i++;
}
}
三、程序阅读题, 写出程序执行结果(每小题10分,共20分).。
1. void main()
{
int arr[]={6,7,8,9,10};
int *ptr=arr;
*(ptr++)=*(ptr++)+100;
printf("%d,%d",*ptr,*(++ptr));
}
2. class A
{
public:
A(int a)
{
m_v=a;
cout<<"constructor A called"<<endl;
}
~A()
{
cout<<"destructor A called"<<endl;
}
void fun1()
{
cout<<"A's fun1() called"<<endl;
}
int fun2(int b)
{
m_v=b;
return m_v;
}
protected:
int m_v;
};
class B:public A
{
public:
B(int b, int n):A(n)
{
m_b=b;
cout<<"constructor B called"<<endl;
}
~B()
{
cout<<"destructor B called"<<endl;
}
int fun3(int m)
{
return(A::m_v+m);
}
private:
int m_b;
};
void main()
{ B b(10, 9);
A *pa;
pa=&b;
cout<<pa->fun2(5)<<endl;
cout<<b.fun3(20)<<endl;
}
[解决办法]
- C/C++ code
/*//算法设计题一struct node{ int data; node *pre, *next; }; void DeleteNode(node *phead, int x ) ;void main(){ node *phead=NULL,*p=NULL,*ptmp; int ndate; p = new node; phead = p; p->pre = NULL; p->next = NULL; p->data = -1; while(true) { cin>>ndate; if(ndate==-1) break; ptmp = new node; ptmp->data = ndate; //设置节点的值 ptmp->next = NULL; ptmp->pre = p; p->next = ptmp; p = ptmp; } p = phead; while(p!=NULL) { if(p==phead) { p = p->next; continue; } cout<<p->data<<"->"; p=p->next; } cout<<endl; DeleteNode(phead,15); cin>>ndate; //15->35->4->17->10->15->20 cout<<"aa"<<endl;}void DeleteNode(node *phead, int x ) { node *p = NULL,*pStmp=NULL; p = phead; while(p!=NULL) { if(p==phead) { p = p->next; continue; } if(p->data==x) { pStmp = p; p->pre->next = p->next; p->next->pre = p->pre; p=p->next; delete pStmp; continue; } cout<<p->data<<"->"; p=p->next; }}//算法设计题二struct node{ char data; node *pre, *next; }; int matchstring(node *phead, char *ps);void main(){ node *phead=NULL,*p=NULL,*ptmp; char ndate; p = new node; phead = p; p->pre = NULL; p->next = NULL; p->data = -1; while(true) { cin>>ndate; if(ndate=='!') break; ptmp = new node; ptmp->data = ndate; //设置节点的值 ptmp->next = NULL; ptmp->pre = p; p->next = ptmp; p = ptmp; } p = phead; while(p!=NULL) { if(p==phead) { p = p->next; continue; } cout<<p->data<<"->"; p=p->next; } cout<<endl; int n = matchstring(phead,"aoe"); cout<<n<<endl; cin>>ndate; //a->o->p->e->h->f->a->o->e->u->k->a->o->e->k->b, cout<<"aa"<<endl;}int matchstring(node *phead, char *ps){ int n = 0; node *p = NULL,*pStmp=NULL; p = phead; while(p!=NULL) { if(p==phead) { p = p->next; continue; } if(p->next!=NULL) { if(p->next->next!=NULL) { if(p->data==*ps&& p->next->data==*(ps+1) && p->next->next->data == *(ps+2) ) { n++; p=p->next->next->next; continue; } } else { break; } } else { break; } p=p->next; } return n;}//算法设计题四struct node{ char data; node *next; };class CStack{public: void ins_static(node* pNode);//入栈 node* get_static();//出栈 void delete_top();//删除栈顶元素 CStack(); ~CStack();protected:private: node *m_top;}; CStack::CStack(){ m_top = NULL;}CStack::~CStack(){ node *p; p=m_top; while(p!=NULL) { m_top = p->next; free(p); p = m_top; } p=NULL; m_top = NULL;}void CStack::ins_static(node* pNode){ if(m_top==NULL) { m_top = new node; m_top->data = pNode->data; m_top->next = NULL; } else { node *p; p = new node; p->data = pNode->data; p->next = m_top; m_top = p; }}node* CStack::get_static(){ return m_top;//返回栈顶元素}void CStack::delete_top(){ node *p; p=m_top; m_top = p->next; delete p;}void main(){ CStack mystk; node mynode; for(int i=0;i<10;i++) { mynode.data = i; mystk.ins_static(&mynode); } node *p; p = mystk.get_static(); mystk.delete_top(); p = mystk.get_static(); return;}*/