1. What are output by the following three output statements? [C ease]
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 zone.
Their values are the first address of each storage area, not equal; str3 and str4 are the same as above, just according to the const semantics, they point to the number
The data area cannot be modified. Str5 and str6 are not arrays but character pointers and are not allocated to the storage area. The subsequent "ABC" is
The data is stored in the static data area, and they are only pointers pointing to the first address of the Area, which are equal.
12. Are there any problems with the usage of the two sizeof in the following code? [C ease]
Void uppercase (char STR []) // converts lowercase letters in STR to 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 character 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, if sizeof is used as an array, only the size of the static array can be measured.
To detect the size of dynamically allocated or external arrays. STR outside the function is a static defined array, so its size is 6,
The STR in the function is actually a pointer to the string without any additional information related to the array. Therefore, siz
EOF only acts on the pointer, and a pointer is 4 bytes, SO 4 is returned.
13. In which situations can B be implicitly converted to a for non-C ++ built-in types A and B? [C ++ medium]
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 implements the non-explicit parameter as B (you can have other default values
Value parameters) constructor
D. A & operator = (const A &); // value assignment operation. Although it is not an authentic implicit type conversion operation
Calculate one
4. What is the problem with the following code? [C ++ ease]
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? [C ++ ease]
Cout <(true? 1: "1") <Endl;
A: The ternary expression "? : "The two operands after the question mark must be of the same type.
8. Can the following code be compiled? Why? [C ++ ease]
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.
2. What are the following error methods for reverse traversing array? [STL ease]
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 of the array
{
Cout <array [I] <Endl;
}
A: First, the array definition is incorrect. Add the type parameter: vector <int> array. Second, vector: size_type is
It is defined as unsigned int, that is, the unsigned number. In this way, when I is 0 as a loop variable, minus 1 will become the largest integer.
, Resulting in loop lossControl.
9. Is the output statement in the following code 0? Why? [C ++ ease]
Struct CLS
{
Int m_ I;
CLS (int I): m_ I (I ){}
CLS ()
{
CLS (0 );
}
};
Cls obj;
Cout <obj. m_ I <Endl;
A: No. In the default constructor, the constructor with parameters is called again for user behavior rather than compiler behavior, that is, only
Execute the function call without executing the subsequent initialization expression. Only when an object is generated will the initialization expression
The corresponding constructor is called together.
10. Which class member functions are generated by default for empty classes in C ++? [C ++ ease]
A:
Class empty
{
Public:
Empty (); // default constructor
Empty (const empty &); // copy the constructor
~ Empty (); // destructor
Empty & operator = (const empty &); // value assignment operator
Empty * operator & (); // address fetch Operator
Const empty * operator & () const; // address fetch operator const
};
3. What are the outputs of the following two output statements? [C ++ difficulties]
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
Integer number. The value of this integer is 1, and (Int &) A tells the compiler to treat a as an integer (not essentially
Conversion ). 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 as those of B, but the integer form of 0 is the same as that of floating point form, so
In special cases, 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 uses
The floating point is stored in the memory. According to ieee754, the content of the floating point is 0x0000803f (Bytes reverse order has been considered ). This
That 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
The 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
T &) is only used to express a type information, meaning that cout <and = select the correct Heavy Load version.
6. What is the problem with the following code? [STL ease]
Typedef vector intarray;
Intarray array;
Array. push_back (1 );
Array. push_back (2 );
Array. push_back (2 );
Array. push_back (3 );
// Delete all 2 in the array
For (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 time you call "array. Erase (itor);", the deleted Element
The subsequent content will automatically move forward, leading to missing items in the iteration. After deleting an item, let the itor -- to move it from the previous one.
One element starts to traverse.
11. 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) can take this situation into consideration.
{
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;
}