C/C ++ questions (from Zhou xing's blog)

Source: Internet
Author: User
Haha, I don't know if it is helpful for some pen/interview :)
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?

2. Where 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 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

3. The followingCodeIs there a problem with the usage of the two sizeof in? [C ease]
Void uppercase (char STR []) // converts lowercase letters in STR to uppercase letters.
{
For (size_t I = 0; I & ltsizeof (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;

4. What is the problem with the following code? [C hard]
Void char2hex (char c) // represents the character in hexadecimal notation
{
Char CH = C/0x10 + '0'; If (CH> '9') CH + = ('A'-'9'-1 );
Char Cl = C % 0x10 + '0'; If (Cl> '9') Cl + = ('A'-'9'-1 );
Cout <ch <CL <'';
}
Char STR [] = "I love China ";
For (size_t I = 0; I & ltstrlen (STR); ++ I)
Char2hex (STR [I]);
Cout <Endl;

5. 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 ();
}

6. What is the problem with the following code? [C ++ ease]
Cout <(true? 1: "1") <Endl;

7. 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];

8. 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;

9. Which class member functions are generated by default for empty classes in C ++? [C ++ ease]
answer:
class empty
{< br> Public:
Empty (); // default constructor
Empty (const empty &); // copy the 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? [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?

11. 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;
}

12. 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 );
}

13. 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 & ltchar *> (DEST );
Const char * psrc = static_cast & ltconst char *> (SRC );
If (pdest> psrc & pdest & ltpsrc + 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 & ltcount; ++ I)
Pdest [I] = psrc [I];
}
Return DEST;
}
Int main (void)
{
Char STR [] = "0123456789 ";
Mymemcpy (STR + 1, STR + 0, 9 );
Cout <STR <Endl;

System ("pause ");
Return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.