1. What are output by the following three output statements?
Char str1 [] = "abc"; char str2 [] = "abc"; const char str3 [] = "abc"; const char str4 [] = "abc "; const char * str5 = "abc"; const char * str6 = "abc"; cout <boolalpha <(str1 = str2) <endl; // What is output? Cout <boolalpha <(str3 = str4) <endl; // What is output? Cout <boolalpha <(str5 = str6) <endl; // What is output?
A: outputs false, false, and true respectively. Str1 and str2 are character arrays, each of which has its own storage area, and their values are the first addresses of each storage area, not equal; str3 and str4 are the same as above, just according to the const semantics, the data zone to which they direct cannot be modified. Str5 and str6 are not arrays but character pointers and are not distributed to the storage area. The "abc" after them are stored in the static data area as constants, and they are only pointers pointing to the first address of the area, equal.
2. Are there any problems with the usage of the two sizeof in the following code?
Void UpperCase (char str []) // converts lowercase letters in str into UpperCase letters {for (size_t I = 0; I <sizeof (str) /sizeof (str [0]); ++ I) if ('A' <= str [I] & str [I] <= 'Z ') str [I]-= ('A'-'A');} char str [] = "aBcDe"; cout <"str length: "<sizeof (str)/sizeof (str [0]) <endl; UpperCase (str); cout <str <endl;
A: There is a problem with sizeof in the function. According to the syntax, sizeof, for example, is used as an array, can only measure the size of a static array, and cannot detect the dynamically allocated or external array size. The str outside the function is a static defined array, so its size is 6. The str in the function is actually a pointer to a string without any additional array-related information, therefore, sizeof only acts as a pointer, and a pointer is 4 bytes, SO 4 is returned.
3. Where can B be implicitly converted to A for non-C ++ built-in types A and B?
A: A. class B: public {......} // The Public B inherits from A and can be indirectly inherited.
B. class B {operator A ();} // B implements implicit conversion to
C. class A {A (const B &) ;}// A constructor that implements the non-explicit parameter as B (other parameters with default values can be available)
D. A & operator = (const A &); // value assignment operation. Although it is not an authentic implicit type conversion operation, it can barely calculate
4. What is the problem with the following code?
struct Test{Test( int ) {}Test() {}void fun() {}};void main( void ){Test a(1);a.fun();Test b();b.fun();}
A: An error occurred while defining variable B. Define Objects Based on the default constructor. No parentheses are required.
5. What is the problem with the following code?
Cout <(true? 1: "1") <endl;
A: The ternary expression "? : "The two operands after the question mark must be of the same type.
6. Can the following code be compiled? Why?
unsigned int const size1 = 2;char str1[ size1 ];unsigned int temp = 0;cin >> temp;unsigned int const size2 = temp;char str2[ size2 ];
A: The str2 definition has an error. size2 is a non-compiler constant, while the array definition requires that the length be a compilation constant.
7. What are the following error methods for reverse traversing array?
Vector array; array. push_back (1); array. push_back (2); array. push_back (3); for (vector: size_type I = array. size ()-1; I> = 0; -- I) // reverse traversal array {cout <array [I] <endl ;}
A: First, the array definition is incorrect. Add the type parameter: vector <int> array. Second, vector: size_type is defined as unsigned int, that is, the number of unsigned values. If I is 0 as a cyclic variable, then 1 minus it will become the largest integer, leading to loop loss of control.
8. Is the output statement in the following code 0? Why?
struct CLS{int m_i;CLS( int i ) : m_i(i) {}CLS(){CLS(0);}};CLS obj;cout << obj.m_i << endl;
A: No. Within the default constructor, the constructor with parameters is called for user behaviors rather than compiler behaviors. That is, only function calls are executed, and subsequent initialization expressions are not executed. The initialization expression is called along with the corresponding constructor only when the object is generated.
9. Which class member functions are generated by default for empty classes in C ++?
A:
Class Empty {public: Empty (); // default constructor Empty (const Empty &); // copy constructor ~ Empty (); // destructor Empty & operator = (const Empty &); // value assignment operator Empty * operator &(); // address retrieval operator const Empty * operator & () const; // address retrieval operator const };
10. What are the outputs of the following two output statements?
Float a = 1.0f; cout <(int) a <endl; cout <(int &) a <endl; cout <boolalpha <(int) a = (int &) a) <endl; // What is output? Float B = 0.0f; cout <(int) B <endl; cout <(int &) B <endl; cout <boolalpha <(int) B = (int &) B) <endl; // What is output?
A: outputs false and true respectively. Pay attention to the conversion application. (Int) a actually constructs an integer with the floating point a as the parameter. The value of this integer is 1, (int &) a tells the compiler to regard a as an integer (no substantial conversion is made ). Because 1 is stored as an integer and its memory data is stored as a floating point, the two are not the same. The two conversions of B are the same, but the integer form of 0 is the same as that of floating point, so in this special case, the two are equal (only in numerical sense ). Note: the output of the program will show (int &) a = 1065353216. How does this value come from? As mentioned above, 1 is stored in the memory as a floating point number. According to ieee754, the content of 1 is 0x0000803F (Bytes reverse order has been considered ). This is the value of the memory unit occupied by the variable. When (int &) a appears, it is equivalent to telling its context: "treat this address as an integer! Don't worry about what it was ." In this way, the content 0x0000803F is interpreted as an integer, and its value is exactly 1065353216 (decimal number ). By viewing the assembly code, we can confirm that "(int) a is equivalent to re-constructing an integer number with a value equal to a", while (int &) the function is only to express a type information, meaning that the correct Heavy Load version is selected for cout <and =.
11. What is the problem with the following code?
Typedef vector IntArray; IntArray array; array. push_back (1); array. push_back (2); array. push_back (2); array. push_back (3); // Delete All 2for (IntArray: iterator itor = array. begin (); itor! = Array. end (); ++ itor) {if (2 = * itor) array. erase (itor );}
A: The type parameter is also missing. In addition, each call of "array. erase (itor); ", the content after the deleted element is automatically moved forward, leading to iteration leakage. After deleting an item, let the itor --, to continue traversing from the next element that has been moved forward.
12. Write a function to copy data between memories. [Whether the problem is comprehensive]
A:
Void * mymemcpy (void * dest, const void * src, size_t count) {char * pdest = static_cast <char *> (dest ); const char * psrc = static_cast <const char *> (src); if (pdest> psrc & pdest <psrc + cout) consider this situation. {for (size_t I = count-1; I! =-1; -- I) pdest [I] = psrc [I];} else {for (size_t I = 0; I <count; ++ I) pdest [I] = psrc [I];} return dest ;}