Some English Quiz

来源:互联网 发布:canopy算法 编辑:程序博客网 时间:2024/05/29 12:04

1.Give an example of implementing a Stack in the template way(only tmplate class declaration without detail definition and realization)

template<class T,int Size=100>class Stack{public:Stack():top(0){};~Stack(){};int push(T& data);int pop(T* pdata);int clear();int peek(T* pdata);bool isEmpty();private:T Stack[Size];int top;};int Stack::push(T& data)//这里为什么要用引用?{if(top>=Size){cout<<"the stack is full"<<endl;return top;}top++;Stack[top]=data;return top;}int Stack::pop(){if(top<=0){cout<<"No data"<<endl;return top;}//Stack[top]=NULL;top--;return top;}int Stack::clear(){top=0;return 0;}int Stack::peek(T* pdata)//取栈顶元素{*pdata=Stack[top];return 0;}bool Stack::isEmpty(){if(top==0)return true;else return false;}

2.What's the difference between String and StringBuffer?

String是不可变对象,一旦创建就不能改变它的值,对已经存在的String对象的修改都是重新创建一个对象然后将值赋进去,原来的对象会被垃圾回收,影响性能

StringBuffer是可变对象,修改的时候不用重新创建对象,只能通过构造函数来建立

字符串连接StringBuffer比String效率要高,原因是String的字符串拼接(String Str="";Str+="Hello")动作是通过建立一个StringBuffer,然后调用append,最后将StringBuffer.ToString(),所以String操作比Stringbuffer多了一些附加操作


3.Which came the first,chicken or the egg?


4.In C++,there're four types of Casting Operators,please enuerate and explain them especially the difference.

reinterpret_cast:转换一个指针为其他类型的指针,能够在非相关类型之间转换,操作结果是简单地从一个指针到另一个指针的二进制拷贝,类型之间指向的内容不作任何检查和转换。(如果是指针到整数的拷贝,结果与系统相关)

class A{};class B{};A *a = new A();B *b = reinterpret_cast<B*>(a);//reinterpret_cast就像传统的类型转换一样对待所有指针的类型转换
static_cast:能执行任意的隐式转换和相反的转换动作

//static_castclass Base{};class Derived:public Base{};Base* base = new Base();Derived derived = static_cast<Derived*>(base);//也能用于基础类型之间的显式转换double d = 3.14159265;int i = static_case<int>(d);
dynamic_cast:只用于对象的指针和引用。在多态里允许任意的隐式转换及相反的过程,与static_cast不同的是会检查操作是否有效(检查是否返回一个被请求的有效完整对象),若不是有效完整的对象指针,则返回NULL。

//dynamic_castclass Base{virtual dummy(){}};class Derived:public Base{};Base* b1=new Derived;Base* b2=new Base;Derived* d1=dynamic_cast<Derived*>(b1);//successDerived* d2=dynamic_cast<Derived*>(b2);//fail,return NULL//对于引用类型转换则抛出bad_cast异常class Base{virtual dummy(){}};class Derived:public Base{};Base* b1=new Derived;Base* b2=new Base;Derived d1=dynamic_cast<Derived &*>(b1);//successDerived d2=dynamic_cast<Derived &*>(b2);//fail,抛出bad_cast异常

const_cast:操作传递对象的const属性,或者是设置或者是移除(const_cast也能改变一个类型的volitale qualifier)

//const_castclass C{};const C *a = new C;C *b = const_cast<C*>(a);


5.Let's say we have a database with 1 one-column table.It contains same records. Could you please give at least 1 solution to help us get records between line 5 and 7. No line number,row id or index etc.

select  the records which in the top 7 records but not in the top 4.

select the top 7 records and make the order desc, then get the top 3 records 

6.Please write a program to realize the model discribed in the figure. You should design your program asgeneric as possible so that we can enhance the model in the future easily without making too much change in your program.

Process1:X=A+B;

Process2:Y=C+D;

Process3:if E>=0 Z=E;

Process4:if E<0Z=E/B;



原创粉丝点击