C++技术面试题目

来源:互联网 发布:淘宝热区代码生成工具 编辑:程序博客网 时间:2024/05/16 08:25
 □第一题
class Base{
public:
  Base();
  ~Base();
  int getBaseNum();

private:
  int baseNum;
};

class A:public Base{
public:
  A();
  ~A();
  float getBaseNum();

private:
  float baseNum;
}
Question1: What concept is represented(表现) by the sample code above?
Choice 1: Polymorphism(继承)
Choice 2: Inheritance(多态)
Choice 3: Function overloading(函数覆盖)
Choice 4: Recursion(递归)
Choice 4: Virtual Function(虚函数)
解答:因为函数名参数相同,如果是覆盖则基类里面必须要有virtual关键字。

□第二题
typedef const char* monthTable[3];
Question2:Referring to the code above, which one of the following choices creates two monthTable arrays and initializes one of the two?
Choice1: monthTable,winter, spring;
Choice2: monthTable winter,spring;
Choice3: monthTable(winter,spring={"March","April","May"});
Choice4: monthTable winter,spring={"March","April","May"};
Choice5: monthTable,winter,spring={"March","April","May"};

□第三题
class myclass
{
  int a;
public:
  myclass(int x);
  int get();
}

myclass::myclass(int x){a=x;}
int myclass::get(){return a;}

int main()
{
  myclass ob(120);
  myclass *p;
  p = &ob;
  return 0;
}
Question3:Referring to the sample code above, how do you print the value of the object "ob"?
Choice1 cout << ob->get();
Choice2 cout << ob.get();
Choice3 cout << ob->&get();
Choice4 cout << p.get(ob);
Choice5 cout << get(ob.int a());

□第四题
class C1{
public:
  C1(){};
  C1(const C1& rhs){}
  void sayHello(){ std::cout << "Hello";}
};

class C2: public C1{
public:
  C2(){};
  C2(const C2& rhs){}
  void sayHello1(C1& rhs){rhs.sayHello();};
  void sayHello2(C1 c1){c1.sayHello();};
};

int main()
{
  C2 c2;
  C1 c1(c2);

  c2.sayHello1(c1);
  c2.sayHello2(c1);
  return 0;
}
Question:From the sample code above,how many times are the copy constructors invoked;
Choice1: C1's copy constructor is invoked zero times.
         C2's copy constructor is invoked one time.
Choice2: C1's copy constructor is invoked one time.
         C2's copy constructor is invoked two times.
Choice3: C1's copy constructor is invoked two times.
         C2's copy constructor is invoked zero times.
Choice4: C1's copy constructor is invoked two times.
         C2's copy constructor is invoked one time.
Choice5: C1's copy constructor is invoked three times.
         C2's copy constructor is invoked zero times.

□第五题
void f()
{
  int n = std::rand() % 3;
  if (n == 1)
    throw "1";
  if (n == 2)
    throw "2";
  if (n == 3)
    throw 4.5;
}

int main()
{
  try{
   f();
  }catch(...){
    return 1;
  }catch(int i){
    return 2;
  }catch(const char* cp){
    return 3;
  }
}

Question5 What is the result of executing the sample code above?
Choice1: It is unknown;it depends on the random number generated in "f()".
Choice2: The return code will always be a 1 since all exceptions will be caught at the first catch().
Choice3: The return code will always be a 2 since the random number generator was not seeded properly.
Choice4: It is unknown;it is implementation-specific.
Choice5: There is a syntax error because catch(...) is not the last catch clause.
解答:此题应该选择5。

□第六题
int f(int i){
  static int c=0;
  c++;
  switch(i){
  case 1:return f(4); break;
  case 2:return c; break;
  case 3:break;return 0;
  case 4:return f(5); break;
  case 5:return f(7); break;
  default: return c;
  }
  return f(2);
}

Question5: Referring to the sample code above, what is the return value for the function "f" when called for the first time with an initial value of 1?
Choice1: 1
Choice2: 2
Choice3: 3
Choice4: 4
Choice5: 5
解答:此题应该选择4。

□第七题
Question7: Which one of the following declarations is legal C++?
Choice1: using::std;
Choice2: using namespace std::;
Choice3: using std namespace;
Choice4: using namespace ::std::rel_ops;
Choice5: using std::;

□第八题
std::string s="abcdefabcabc";
Question8: Referring to the sample code shown above, which one of the following statements returns the position within "s" that marks the beginning of the last occurrence of "abc"?
Choice1: s.rfind("abc", std::string::npos);
Choice2: s.rfind("abc", 0, s.length());
Choice3: s.find_last_of("abc",std::string::npos,std::string::npos);
Choice4: s.find_last_of("abc",0,s.length());
Choice5: s.rfind("abc", 0, std::strlen("abc"));
解答:find_last_of返回最后一个位置;rfind逆向搜索;此题应该选择1。

□第九题
int x = 1;

for (int i=1; i < 5; i++)
{
  if (i = 3)
    continue;
  else if (i = 4)
    break;
  x++;
}

Question9: What is true about the sample code above?
Choice1: At the end of the loop,"x" holds the value 1.
Choice2: At the end of the loop,"x" holds the value 2.
Choice3: At the end of the loop,"x" holds the value 3.
Choice4: At the end of the loop,"x" holds the value 4.
Choice5: "x" is never incremented.

□第十题
class A
{
public:
  A():i(1){}
  int i;
};

class B : public A
{
public:
  B():j(2){}
  int j;
};

int f(A *p,int count)
{
  int total = 0;
  for (int i = 0; i< count; ++i)
  {
    total += p++->i;
  }
  return (total);
}

int main()
{
  B b[11];
  cout << f(b, 10);
  return 0;
}
Question10: Given the sample code above, what is the output?
Choice1: 10
Choice2: 11
Choice3: 15
Choice4: The output is undefined.
Choice5: The code does not compile.

□第十一题
An error occurred when compiling the following code:

class Foo{
public:
  Foo(int i){}
};
class Bar : virtal Foo {
public:
  Bar(){}
};

Bar b;

Question11:Referring to the scenario above,which one of the following changes allows the code to compile successfully?
Choice1: Adding "Foo(0)" to the "Bar::Bar" initializer list
Choice2: Adding "Foo(0)" to the "Bar" constructor
Choice3: Adding a constructor to "Bar" that takes an int parameter
Choice4: Adding a copy constructor to the "class Foo"
Choice5: Adding a virtual constructor to the "class Bar"
分析:虚继承,指出希望共享其虚基类的状态,对给定虚基类,无论该类在派生层次中作为虚基类出现多少次,只继承一个共享的基类子对象。
解答:没有默认构造函数则代码出错。Bar():Foo(0){}答案是Choice1。

□第十二题
class Myclass{};
static Myclass* MyFunction(int*);
Question12: Refering to the sample code above, which one of the following is the correct declararion for a function pointer to MyFunction?
Choice1: Myclass* (*f)(int*);
Choice2: (static Myclass*) (*f)(int*);
Choice3: (Myclass* f)(int*);
Choice4: Myclass* (*f)int*;
Choice5: Myclass* (*f(int*));
解答:此题答案是Choice2。

□第十三题
std::string something;
/*....*/
if ("STRING LITERAL" == something)
  std::cout << "String are Equal" << std::endl;
Question13: How does a C++ compiler interpret(解释说明)the == operator in the sample code above?
Choice1: As the == operator overloaded by the string class
Choice2: As a built-in operator
Choice3: As a cast(投掷) from std::string to (char*) followed by a comparison(比较).
Choice4: As an overloaded == operator.
Choice5: As a syntax error.
解答:这道题应该选4。

□第十四题
Question14: Which one of the following expressions is an example of a definition?
Choice1: namespace mystd = std;
Choice2: extern int i;
Choice3: typedef unsigned char byte;
Choice4: byte f();
Choice5: struct mystruct;
解答:这道题应该选4。Choice1没有这种用法,Choice2不是定义而是外部声明,Choice3是声明一个新的类型。
     Choice5 这个显然不成立,应该struct mystruct aa;才算定义。

□第十五题
for (int i=0,r = std::strlen(buf)-1;i < r;i++,r--)
{
  buf[i]= buf[r]^buf[i];
  buf[r]= buf[r]^buf[i];
  buf[i]= buf[r]^buf[i];
}
Question15: Referring to the sample code above, what are the contents of "buf" after the code is executed?
Choice1: The original content of "buf" has been reversed.
Choice2: The buffer has remained unchanged.
Choice3: Each member of the array "buf" has been incremented by 10.
Choice4: Each member of the array "buf" has been set to zero.
Choice5: The buffer has been ciphered using a caesar cipher algorithm.
解答:此题答案应该是1。

□第十六题
char* p = "hello";
char p2[] = "world";
char *p3 = new char[12];

std::strcpy(p2,p1);
std::strcpy(p3+5," ");
std::strcpy(p3+std::strlen(p2),p2);
std::strcpy(p1,p2);
std::strcpy(p2,p1);
Question16: Referring to the sample code above, which numbered line of code causes a runtime error?
Choice1: Line5
Choice2: Line6
Choice3: Line7
Choice4: Line8
Choice5: Line9
解答:此题答案是4。

□第十七题
A function threw an exception of type std::unexpected.

Question17:Referring to the scenario(想定)above,what is a logical(必然合乎逻辑)cause?
Choice1: An exception was thrown in which the type was not in the function's exception specification.
Choice2: An exception thrown is not yet caught and the destructor for an object throws an exception during the stack unwinding.
Choice3: An exception of type std::unexpected can never be thrown in a well-formed C/C++ program.
Choice4: The application was targeting one operating system,but another operating system is being used,which generated the exception.
Choice5: There was no catch all enabled in scope closest to the function call.


□第十八题
class X{
public:
 const X& operator=(const X& rhs){
   this->n = rhs.n;
   return *this;
 }
 
 const X& operator+(const X& rhs){
   this->n += rhs.n;
   return *this;
 }

 const X& operator+(const int m){
   this->n += m;
   return *this;
 }

private:
 int n;
};

int main()
{
   X a,b,c;
   //statement goes here.
}

Question18: Given the sample code above, which, if any, of the following statements CANNOT be performed?
Choice 1: a = b;
Choice 2: a = b + c;
Choice 3: a = b + 5;
Choice 4: a = b + c + 5;
Choice 5: They are all legal.
解答:此题答案是4。

□第十九题
Question 19: Which one of the following statements replaces all elements in an integer sequence that is delimited by start and end and in which the value is less than 10 with 10?
Choice 1: std::replace_if(start, end, std::bind2nd(std::less<int>(),10),10);
Choice 2: std::replace(start, end, std::bind2nd(std::less<int>(),10),10);
Choice 3: std::replace_if(start, end, std::bind2nd(std::less<int>,10),10);
Choice 4: std::replace(start, end, std::bind2nd(std::less<>(10)),10);
Choice 5: std::replace_if(start, end, std::bind2nd(std::less(10)),10);
解答:此题答案是2。

□第二十题
class A
{
public:
 A(int j=0):i(j){}
 A(const A& that) : i(that.i){}
 A& operator=(const A& that)
 {
  i = that.i; return (*this);
 }
 friend class B;
private:
 int i;
}
class B : public A
{
public:
  B(const B& that){std::cout << that.i;}
};
Question20: Given the class declaration shown above, what is the purpose of the friend declatation?
Choice 1: It serves no purpose and is unnecessary.
Choice 2: It gives "A" access to "that.i" in the copy constructor for "A"
Choice 3: It gives "B" access to "that.i" in the copy constructor for "B"
Choice 4: It gives "B" access to "A()"
Choice 5: It gives "A" access to "that.i" in the assignment operator for "A".

□第二十一题
try{
  //do something that generates an exception
}
catch(...){
  // ???
}
Question 21: Referring to the sample code above, which statement shall replace "//???" in order to send the caught(捕捉) exception to the next handler?
Choice 1: raise;
Choice 2: rethrow();
Choice 3: throw;
Choice 4: throw();
Choice 5: throw(...);

□第二十二题
class A
{protected:
  virtual void Func(){}
};

class B : virtual A
{public:
 void Func(int i = 0){}
}

class C : virtual B
{public:
 void Func(int i = 0){}
}

int main()
{
 C c;
 c.Func();
 return 0;
}

Question 22: Given the above sample code, which one of the following statements it true?
Choice 1: A::Func() is visible through class C.
Choice 2: B::Func() overrides A::Func();
Choice 3: C::Func() overrides B::Func();
Choice 4: A::Func() is visible using a class B variable.
Choice 5: C::Func() overrides A::Func();

□第二十三题
1: int i;
2: long double d;
3: unsigned float f;
4: char const *s;
5: signed const int *p;
Question 23:Referring to the sample code above, which one of the following definitions is NOT valid?(有充分证据的)
Choice 1: Line 1
Choice 2: Line 2
Choice 3: Line 3
Choice 4: Line 4
Choice 5: Line 5

原创粉丝点击