[关闭]
@JackLiuKK 2017-06-11T16:58:45.000000Z 字数 2873 阅读 5332

C/C++ Quiz


For your answers, either Chinese or English are OK.

A. Coding

1. Please create a class to represent Int. Only declaration is required. Please give the declaration of all member functions, friend functions, and member variables.


2. Imagine we have a single linked list, please write a function which could move a specified node to the tail of the list.


B. Read and Answer

1. What are wrong in the following code?

  1. void foo(short startIndex)
  2. {
  3. short buffer[50000] = { 0 };
  4. for (auto i = startIndex; i < 40000; ++i)
  5. {
  6. buffer[i] = (char)i;
  7. }


2. Mark all lines which are wrong and provide the FIX.

  1. class CBase
  2. {
  3. public:
  4. CBase()
  5. : m_pVar1(NULL)
  6. {
  7. m_pVar2 = new int(4);
  8. }
  9. ~CBase()
  10. {
  11. delete m_pVar2;
  12. delete m_pVar1;
  13. }
  14. void Init(int* pVar)
  15. {
  16. m_pVar1 = pVar;
  17. }
  18. private:
  19. int* m_pVar1;
  20. int* m_pVar2;
  21. }
  22. class CDerive : public CBase
  23. {
  24. public:
  25. CDerive(int var) : m_var(var) {};
  26. ~CDerive() {};
  27. private:
  28. int m_var;
  29. }
  30. int main()
  31. {
  32. CDerive* pDerives = new CDerive[10];
  33. int *pVar = new int(10);
  34. for (int i = 0; i < 10; ++i)
  35. {
  36. pDerive[i].Init(pVar);
  37. }
  38. delete pDerives;
  39. delete pVar;
  40. }

3. The following code could not be compiled, give TWO ways to fix it.

  1. class CFoo
  2. {
  3. public:
  4. CFoo() : m_var(0) {};
  5. ~CFoo() {};
  6. bool AddAndCheck() const
  7. {
  8. m_var += 5;
  9. return m_var < 10;
  10. }
  11. private:
  12. int m_var;
  13. }

4. What are wrong in the following code? Provide your fix.

  1. #define INCREASE(a) ++a
  2. void foo()
  3. {
  4. int a = 3;
  5. int b = 7;
  6. cout<<INCREASE(a * b)<<endl;
  7. }

5. What are wrong in the following code? Why?

  1. char* GetStaticBuffer()
  2. {
  3. char buffer[100] = { 0 };
  4. return buffer;
  5. }

6. With default configuration, in the following code, function fooA is OK but fooB has issues, why?

  1. const int TEN_MEGA_BYTES = 10 * 1024 * 1024;
  2. void fooA()
  3. {
  4. char *pBuffer = new char[TEN_MEGA_BYTES];
  5. }
  6. void fooB()
  7. {
  8. char buffer[TEN_MEGA_BYTES] = { 0 };
  9. }

7. In the following code, line 32, I want to give "student_teacher" as the input parameter, but by mistake, I typed "student". The compiling succeeded anyway. Why? If I want the compiling be failed in this condition, how to do?

  1. class CStudent
  2. {
  3. public:
  4. CStudent() {};
  5. ~CStudent() {};
  6. }
  7. class CTeacher
  8. {
  9. public:
  10. CTeacher(CStudent student) {};
  11. CTeacher(CStudent student)
  12. : m_student(student)
  13. {
  14. }
  15. ~CTeacher() {};
  16. void Teach() {};
  17. private:
  18. CStudent m_student;
  19. }
  20. void foo(CTeacher teacher)
  21. {
  22. teacher.Teach();
  23. }
  24. int main()
  25. {
  26. CStudent student;
  27. CTeacher student_teacher;
  28. foo(student);
  29. }

8. What is the output of the following code?

  1. int fooVar = 10;
  2. void foo(int *pVar, int& var)
  3. {
  4. pVar = &fooVar;
  5. var = fooVar;
  6. }
  7. int main()
  8. {
  9. int var1 = 1;
  10. int var2 = 2;
  11. foo(&var1, var2);
  12. cout<<var1<<":"<<var2<<endl;
  13. }

9. How to run some code before main function is called?
















10. What the difference are between

std::vector::resize() and std::vector::reserve()
std::vector::size() and std::vector::capacity()







11. Template greater<> is often used when operating the STL containers. Please give an implementation of greater<>.

Hints: Two template parameters. One is a number to be compared as base, another is the element to be compared.























12. Say we have a class named Printer which has a member function as Print(). Now there's a container as vector. Please give a lambda expression which could be used in for_each() to call Print() function for every instance in vector.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注