[关闭]
@JackLiuKK 2017-06-11T06:15:51.000000Z 字数 2431 阅读 7203

Welcome to the C/C++ Quiz!


please write down your answers on a new white paper. You can use either Chinese or English.

A. Show me the code

1. Please create a "String" class to process char strings. This string class should satisfy the following requirements:

In other words, your "String" class could be used in the following code and be complied successfully by a standard C++ compiler.

  1. void foo(String x)
  2. {
  3. }
  4. void bar(const String& x)
  5. {
  6. }
  7. String baz()
  8. {
  9. String ret("world");
  10. return ret;
  11. }
  12. int main()
  13. {
  14. String s0;
  15. String s1("hello");
  16. String s2(s0);
  17. String s3 = s1;
  18. s2 = s1;
  19. foo(s1);
  20. bar(s1);
  21. foo("temporary");
  22. bar("temporary");
  23. String s4 = baz();
  24. std::vector<String> svec;
  25. svec.push_back(s0);
  26. svec.push_back(s1);
  27. svec.push_back(baz());
  28. svec.push_back("good job");
  29. }


2. Imagine we have a single linked list, please write a function which could remove a specified node in the list.


B. Basic Part

1. What are wrong in the following code? Please point out.

  1. void main()
  2. {
  3. char a = 'a';
  4. int b = 0;
  5. int *pInt1 = &b;
  6. int c = *pInt1;
  7. pInt1 = (int*)(&a);
  8. int *pInt2 = pInt1 + 1;
  9. int d = *pInt2;
  10. void *pV = &a;
  11. pV++;
  12. char e = *pV;
  13. }


2. What are wrong in the following code? Please provide your FIX.

Common.h

  1. int var1;
  2. void foo(int input)
  3. {
  4. // some code
  5. }

TestA.h

  1. #include "Common.h"
  2. #include "TestB.h"
  3. class CTestA
  4. {
  5. private:
  6. CTestB m_b;
  7. };

TestB.h

  1. #include "Common.h"
  2. #include "TestA.h"
  3. class CTestB
  4. {
  5. private:
  6. CTestA m_a;
  7. }

TestA.cpp

  1. #include "TestA.h"
  2. // some code

TestB.cpp

  1. #include "TestB.h"
  2. // some code

C. STL

1. Errors, inefficiency and potential bugs exsit in the following code, please point them out.

  1. int foo(std::map<int, int>& mapIn, std::set<int>& setIn)
  2. {
  3. std::vector<int> va(10);
  4. std::vector<int> vb;
  5. std::copy(va.begin(), va.end(), vb.begin());
  6. std::vector<int> vc(100);
  7. auto iter = va.begin() + 5;
  8. int varInt = *iter;
  9. va.push_back(vc.begin(), vc.end());
  10. varInt = *(++iter);
  11. if (mapIn[4] == 0)
  12. {
  13. // do something
  14. }
  15. auto itVec = std::find(vc.begin(), vc.end(), 100);
  16. if (itVec != vc.end())
  17. {
  18. // do something
  19. }
  20. auto itSet = std::find(setIn.begin(), setIn.end(), 10);
  21. if (itSet == setIn.end())
  22. {
  23. // do something
  24. }
  25. }


2. Please see the following code, TypeA could be either a function pointer or a functor, please try to provide the definition for TypeA in both function pointer way and functor way.

  1. void foo(TypeA processor)
  2. {
  3. int paraInt = 0;
  4. const char* pParaStr = "Test";
  5. int rtn = processor(paraInt, pParaStr);
  6. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注