Interview training Hai Tao 54 questions

Source: Internet
Author: User

In the running C ++ code, what is the output?

#include <iostream> class A{private:        int n1;        int n2;public:        A(): n2(0), n1(n2 + 2)        {        }         void Print()        {                std::cout << "n1: " << n1 << ", n2: " << n2 << std::endl;        }}; int _tmain(int argc, _TCHAR* argv[]){        A a;        a.Print();         return 0;}

Class constructor variables are initialized according to the defined sequence.

Therefore

n2(0), n1(n2 + 2)

Of course, initialize N1 first, and then initialize N2. of course, N2 is a random value, so N1 is a random value, and N2 is 0.

Question (13): Compile the running C ++ code. What is the result? (A) Compilation error; (B) successful compilation, program crash during running; (c) normal compilation and 10 output. Select the correct answer and analyze the cause.

#include <iostream> class A{private:        int value; public:        A(int n)        {                value = n;        }         A(A other)        {                value = other.value;        }         void Print()        {                std::cout << value << std::endl;        }}; int _tmain(int argc, _TCHAR* argv[]){        A a = 10;        A b = a;        b.Print();         return 0;}

Looking back at C ++

Hai Tao's answer

Answer: Compilation error. The input parameter in the copy constructor is an instance of. The copy constructor is called to copy the form parameter to the real parameter. Therefore, if the copy constructor is allowed to pass values, an endless recursion will be formed and stack overflow will occur. Therefore, the C ++ standard does not allow copying constructors to pass value parameters. Instead, it must be a reference or constant reference. In the visual
In studio and GCC, compilation fails.

Question (14): What is the output of the running C ++ code?

int SizeOf(char pString[]){        return sizeof(pString);} int _tmain(int argc, _TCHAR* argv[]){        char* pString1 = "google";        int size1 = sizeof(pString1);        int size2 = sizeof(*pString1);         char pString2[100] = "google";        int size3 = sizeof(pString2);        int size4 = SizeOf(pString2);         printf("%d, %d, %d, %d", size1, size2, size3, size4);         return 0;}

Of course, sizeof (pstring1) is a pointer, of course, 4

Sizeof (* pstring1) actually * pstring1 executes the first character. Of course it is 1

Sizeof (pstring2) Of course, the array is 100

The last one is unclear.

According to Hai Tao's answer, when an array is passed as a function parameter, the array degrades to a pointer. Of course, the result is 4.

Question (15): Running code. What is the output result? What is the problem with this code?

#include <iostream> class A{public:        A()        {                std::cout << "A is created." << std::endl;        }         ~A()        {                std::cout << "A is deleted." << std::endl;        }}; class B : public A{public:        B()        {                std::cout << "B is created." << std::endl;        }         ~B()        {                std::cout << "B is deleted." << std::endl;        }};int _tmain(int argc, _TCHAR* argv[])  {          A* pA = new B();          delete pA;             return 0;  }

Class B inherits Class A. Of course, Class A is initialized first, and then Class B is initialized.

The last part of the delete PA is of course a class.

However, class B is not destructed. Because it is not a virtual function, Class A pointer PA certainly cannot refer to Class B destructor.

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.