常见C/C++笔试题目整理(含答案)3
来源:互联网 发布:vasp软件 编辑:程序博客网 时间:2024/05/16 15:30
网上流传的一份常见C++笔试题目汇总,供各位有找工作需要的同学参考之用,因为原文较长,遂采用连载形式,有耐心的同学就请一直跟下去吧,相信一定会有所收获。提前说明一点,题目来在网络,答案仅供参考,如有同学觉得哪道题目有异议,欢迎讨论!
题目31-45
--------------------------------------------------------------------------
31. 当一个类 A 中没有声明任何成员变量与成员函数,这时sizeof(A)的值是多少,如果不是零,请解释一下编译器为什么没有让它为零。
答:肯定不是零。如果是零的话,声明一个class A[10]对象数组,而每一个对象占用的空间是零,这时就没办法区分A[0],A[1]…了。
--------------------------------------------------------------------------
32.下面的函数实现在一个固定的数上加上一个数,有什么错误,改正
int add_n(int n){static int i=100;i+=n;return i;}
答: 因为static使得i的值会保留上次的值。去掉static就可了。
--------------------------------------------------------------------------
33. 已知strcpy的函数原型:char *strcpy(char *strDest, const char *strSrc)其中strDest 是目的字符串,strSrc 是源字符串。不调用C++/C 的字符串库函数,请编写函数 strcpy。
答:
char *strcpy(char *strDest, const char *strSrc){if ( strDest == NULL || strSrc == NULL)return NULL ;if ( strDest == strSrc)return strDest ;char *tempptr = strDest ;while( (*strDest++ = *strSrc++) != ‘/0’);return tempptr;}
--------------------------------------------------------------------------
34. 已知String类定义如下:
class String{public:String(const char *str = NULL); // 通用构造函数String(const String &another); // 拷贝构造函数~ String(); // 析构函数String & operater =(const String &rhs); // 赋值函数private:char *m_data; // 用于保存字符串};
尝试写出类的成员函数实现。
答:
String::String(const char *str){if ( str == NULL ) //strlen在参数为NULL时会抛异常才会有这步判断{m_data = new char[1] ;m_data[0] = '/0' ;}else{m_data = new char[strlen(str) + 1];strcpy(m_data,str);}}String::String(const String &another){m_data = new char[strlen(another.m_data) + 1];strcpy(m_data,other.m_data);}String& String::operator =(const String &rhs){if ( this == &rhs)return *this ;delete []m_data; //删除原来的数据,新开一块内存m_data = new char[strlen(rhs.m_data) + 1];strcpy(m_data,rhs.m_data);return *this ;}String::~String(){delete []m_data ;}
--------------------------------------------------------------------------
35. static变量和static 函数各有什么特点?
答:
static变量:在程序运行期内一直有效,如果定义在函数外,则在编译单元内可见,如果在函数内,在在定义的block内可见;
static函数:在编译单元内可见。
----------------------------------------------------------------------------------
36. 根据下面代码回答问题:
int i = 1;int j = i++;if((i>j++) && (i++ == j)) i+=j;
请问i最后等于多少?
答: i = 5
--------------------------------------------------------------------------
37. 根据下面代码回答问题:
unsigned short array[]={1,2,3,4,5,6,7};int i = 3;请问 *(array + i) = ?
答: 4
--------------------------------------------------------------------------
38. 根据下面代码回答问题:
class A{ virtual void func1(); void func2();}Class B: class A{ void func1(){cout << "fun1 in class B" << endl;} virtual void func2(){cout << "fun2 in class B" << endl;}}以下四种说法中,哪个是正确的?
- (A) A中的func1和B中的func2都是虚函数.
- (B) A中的func1和B中的func2都不是虚函数.
- (C) A中的func2是虚函数.,B中的func1不是虚函数.
- (D) A中的func2不是虚函数,B中的func1是虚函数.
答: A
--------------------------------------------------------------------------
39. 简述“.h”头文件中的ifndef/define/endif 的作用?
答:防止该头文件被重复引用。
--------------------------------------------------------------------------
40. 写出下面程序结果:
#include <iostream>using namespace std;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: virtual void print(void) { cout<<"C::print()"<<endl; }};void print(A a){ a.print();}int main(int argc, const char * argv[]) { 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()
--------------------------------------------------------------------------
41. #include<file.h> 与 #include "file.h"的区别?
答:前者是从Standard Library的路径寻找和引用file.h,而后者是从当前工作路径搜寻并引用file.h。
--------------------------------------------------------------------------
42. 如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针)
struct node { char val; node* next;}bool check(const node* head) {} //return false : 无环;true: 有环
一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):
bool check(const node* head){if(head==NULL) return false;node *low=head, *fast=head->next;while(fast!=NULL && fast->next!=NULL){low=low->next;fast=fast->next->next;if(low==fast) return true;}return false;}
--------------------------------------------------------------------------
43. 写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数。
答题思路:KMP算法效率最好,时间复杂度是O(n+m)。
--------------------------------------------------------------------------
44. 文件中有一组整数,要求排序后输出到另一个文件中。
答:
#include<iostream>#include<fstream>using namespace std;void Order(vector<int>& data) //bubble sort{int count = data.size() ;int tag = false ; // 设置是否需要继续冒泡的标志位for ( int i = 0 ; i < count ; i++){for ( int j = 0 ; j < count - i - 1 ; j++){if ( data[j] > data[j+1]){tag = true ;int temp = data[j] ;data[j] = data[j+1] ;data[j+1] = temp ;}}if ( !tag )break ;}}void main( void ){vector<int>data;ifstream in("c://data.txt");if (!in){cout<<"file error!";exit(1);}int temp;while (!in.eof()){in>>temp;data.push_back(temp);}in.close(); //关闭输入文件流Order(data);ofstream out("c://result.txt");if ( !out){cout<<"file error!";exit(1);}for ( i = 0 ; i < data.size() ; i++)out<<data<<" ";out.close(); //关闭输出文件流}
--------------------------------------------------------------------------
45. 写一个函数找出一个整数数组中,第二大的数。
const int MINNUMBER = -32767 ;int find_sec_max( int data[] , int count){int maxnumber = data[0] ;int sec_max = MINNUMBER ;for ( int i = 1 ; i < count ; i++){if ( data > maxnumber ){sec_max = maxnumber ;maxnumber = data ;}else{if ( data > sec_max )sec_max = data ;}}return sec_max ;}
未完,待续。
VIEWER DISCRETION IS ADVISED!!
- 常见C/C++笔试题目整理(含答案)3
- 常见C/C++笔试题目整理(含答案)1
- 常见C/C++笔试题目整理(含答案)2
- 常见C/C++笔试题目整理(含答案)4
- 常见C/C++笔试题目整理(含答案)5
- 常见C/C++笔试题目整理(含答案)6(最终弹)
- 常见C++笔试题目整理(含答案)10
- 常见C++笔试题目整理(含答案)
- 常见C++笔试题目整理(含答案)0
- 十分常见的C/C++笔试题整理(含答案)
- c语言常见笔试题目
- C/C++笔试题目以及答案
- 常见(经典)程序练习题(含算法,c答案,java答案)
- MS-ATC笔试 题目 (含部分答案)
- C语言笔试面试常见编程题目(更新中)
- C/C++笔试面试常见题目2
- Linux 笔试面试常见题目(整理)
- c面试题总结(含答案)
- 对于数组和指针的理解
- Oracle存储过程(转)
- 从今天开始,我要尝试每天进步百分之一!
- 常见C/C++笔试题目整理(含答案)1
- 常见C/C++笔试题目整理(含答案)2
- 常见C/C++笔试题目整理(含答案)3
- java学习
- 既然来了
- Java基础:第三十四讲 包
- c#中public/protect/private/internal修饰符的区别
- 顶级程序员的心得 Coders at Work (IV)
- MySQL导入导出.sql文件方法(转载)
- 看郭克华老师的视频教学笔记(四)
- 成功解决Google app Engine 解决String500个字符限制(原创)