一家外企公司完整面试题和答案 Basic C/C++ (CC++ Programming interview questions and answers)

1. In C++ what is the difference between a struct and a class?

The default member and base class access specifiers are different. The only differences are that a struct defaults to public member access and public base class inheritance, and a class defaults to the private access specifier and private base class inheritance.

2. How do we do inheritance in C++?

Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

Example code as following b inheritance a

class a

{

  int x;

};

class b:public a

{

  int y;

};

3. Can we overload operator in C++, and how? What is class copy constructor?

Yes, we can.

For example:

class complex

{

public:

void operator + (complex &c)

{

}

complex operator – (complex &c)

{

}

}

copy constructor:

A copy constructor is a method that accepts an object of the same class and copies it’s data members to the object on the left part of assignment

class Point2D

{

public Point2D() : x(0) , y(0) {} //default (no argument) constructor

public Point2D( const Point2D & ) ;

};

.

4. Understand accessibility of class members and functions, public, protected, and private.

Public data members and member functions are accessible outside the class.

Protected data members and member functions are only available to derived classes.

Private data members and member functions can’t be accessed outside the class. However there is an exception can be using friend classes.

5. When is the “protected” access specifier needed in C++?

When only derived classes’ should access base class data member or method.

6. What is the primary value in using virtual functions within C++?

Primary value is implementation polymorphism.

In case of polymorphism (virtual functions) if a base class pointer(or reference) is allocated a pointer(or reference) of derived class the actual function called is determined only during runtime through the virtual table entry . This is runtime binding or late binding

7. Can constructor and destructor be virtual? What is the point in making a destructor "virtual"?

Constructor can be virtual l, but destructor can not be virtual.

Because each derived classes destructor behavior is not the same

8. What is "pure virtual" functions?

A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.

9. What are static and const members and functions?

A static data member or functions acts as a global object that belongs to its class type. Unlike other data members where each class object has its own copy, there is only one copy of a static data member per class type. A static data member is a single, shared object accessible to all objects of its class type.

The const type members transforms an object into a constant. For example,

const int bufSize = 512; // input buffer size

defines bufSize to be a constant initialized with the value 512. Any attempt to change that value from within the program results in a compile-time error. For this reason, it is referred to as read-only.

10. What is Overloading? If two functions within the same class have the same name what happens?

Overloading is the practice of supplying more than one definition for a given function name in the same scope.

When an overloaded function is called, the C++ compiler selects the proper function by examining the number, types and order of the arguments in the call. Function overloading is commonly used to create several functions of the same name that perform similar tasks but on different data types.

11. How can you tell if the new operator fails?

If new fail, the return point will be NULL. For example as following

int * ptr = new int(5);

If(ptr==NULL)

{

cout<<”new fail<<endl;

}

12. Is “has a relationship” correct in inheritance?

Yes,for example as following

Employee is derived from Person. A class may have an instance of another class. For example, an employee “has” a salary, therefore the Employee class has the HASA relationship with the Salary class. This relationship is best implemented by embedding an object of the Salary class in the Employee class.

13. How to do memory allocation in C, how to do it in C++? How to free memory in C, how to do it in C++?

For example:

In c:

char * p = (char*) malloc(100);

free (p);

p = NULL;

in c++:

char* p = new char[100];

delete[] p;

p = NULL;

14. Are the expressions *pointer++ and ++*pointer same? If the variable i = 5, what is the results of "i++ - 1"?

Is not the same.

The result is 4.

15. What is the difference between a C++ pointer and a reference?

A reference must always refer to some object and, therefore, must always be initialized; pointers do not have such restrictions. A pointer can be reassigned to point to different objects while a reference always refers to an object with which it was initialized.

16. What does the term “default parameter” mean in C++?

A default parameter is a function parameter that has a default value provided to it. If the user does not supply a value for this parameter, the default value will be used. If the user does supply a value for the default parameter, the user-supplied value is used.

17. Under what conditions is multiple inheritance not recommended?

When two base classes implement a method with the same name, or two base has the same attribute.

18. When using printf(), what value is used with "%d"?

int data type.

19. Describe how to use try and catch

Using try surrounded the code that maybe has exception, using catch hold of abnormal happen information.

Example code as following

try {

cout << "Inside try block\n";

throw 1; // throw an error

cout << "This will not execute";

}

catch (int i) { // catch an error

cout << "Caught an exception -- value is: ";

cout << i << "\n";

}

20. If a function takes a pointer to a pointer as a parameter, how would you return a new pointer?

Using the way that pointer to a pointer together with reference as the parameter,the example code as following

void test(int** & pp)

{

p = new int(5);

pp= &p;

}

int** ppt = NULL;

test(ppt);

 

好好地考察一下你的Embedded English & C language理解力吧.

1.What is achieved by prefixing the 'static' keyword to a file-levelfunction or file-level variable declaration?

2.Describe the difference between the “IS A” and “HAS A” objectrelationships. Which is the stronger relationship and why?

3.Java & C# support interfaces directly with the “interface” keyword.
C++ does not have an “interface” keyword. How do you create an interface in C++?
Where/when is the use of interfaces especially helpful?

4.If a program requires a large number of execution contexts what can bedone to minimise thread scheduling overhead?


5. What does it mean to say that a function is reentrant?
What are some of the ways of achieving re-entrancy?

 

1. Explain what does #define do.

2. Please describe the ‘diamond problem’ with regards to multiple inheritance in object oriented programming.

3. Difference between Vector and List?

4. Given the root of a binary search tree, link all the nodes at the same level, by using an additional Node* level.

5. How do you find the max depth of a binary tree?

6. What is the difference between overriding and overloading?

7. If you had 5 red balls that contained 4 red balls and those red balls contained the original 5 red balls, then how many sets of sets of balls would I take to have a double set of red balls of varying sizes inside each next largest red ball?

8. Integer shift right-left, what happens by shifting to more than the integer bit size, etc.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值