Sogou C + + written test

Source: Internet
Author: User

int func () {char b[2] = {0};strcpy (b, "AAAA");}

Which of the following statements is correct:

A Debug version crashes, release version normal

B Debug Version Normal, release version crashes

C Debug version crashes, release version crashes

D Debug Version Normal, release version normal

Select a. Because the assert assertion is protected in debug, it crashes, and the Assert is deleted in release, so it will run normally. But this is not recommended because it overwrites the memory that does not belong to itself, which is the train that crashed the program

Class A

{

Public

virtual void foo () {}

};

Class B

{

Public

virtual void foo () {}

};

Class C:public A,public B

{

Public

virtual void foo () {}

};

void Bar1 (* pa)

{

b* pc = Dynamic_cast<b*> (PA);

}

void Bar2 (* pa)

{

b* pc = Static_cast<b*> (PA);

}

void Bar3 ()

{

c C;

A * pa = &c;

b* PB = Static_cast<b*> (static_cast<c*> (PA));

}

A BAR1 cannot be compiled by compiling B bar2

C BAR3 cannot be run correctly by compiling D bar1, but the wrong cast method is used

Select B. Dynamic_cast is traversing the inheritance tree at run time, so there is no error at compile time. But because A and B have nothing to do with it, the run times are wrong, dynamic, so the runtime transitions (so A and D are all wrong). Static_cast: Static, compile-time conversions, any type conversions that the compiler implicitly executes can be done by it, and if two classes are irrelevant, an error occurs (so B is correct), and if there is an inheritance relationship, you can make any transformation between the base and derived classes, without errors during compilation. So BAR3 can be compiled (the C option is wrong).

Question 12th: Which of the following template instantiation is used, will cause a compilation error?

Template
class stack;

void fi (stack); A

Class Ex
{
stack& rs; B

Stack si; C
};

int main ()
{
Stack* SC; D
Fi (*SC); E
int i = sizeof (stack); F

return 0;
}

Select C E F; Note that both stack and fi are just declarations that are not defined. I thought I would define it in other places after it was declared here, Pit Daddy.

Because the stack is just a declaration, C is wrong, and the stack cannot define the object. E is also the same, the stack is only declared, so the copy constructor cannot be executed, as for F, because the stack is only declared, do not know the size of the stack, so the error. If the stack is defined, it will be all right.

#include <stdio.h>
#include <string>
#include <iostream>
using namespace Std;
Class A
{
Public
String A;
void F1 () {printf ("Hello World");}
void F2 () {a = "Hello world"; printf ("%s", A.c_str ());}
virtual void F3 () {printf ("Hello World");}
virtual void F4 () {a = "Hello World";p rintf ("%s", A.c_str ());}

static int i;
static void F5 () {Cout<<i<<endl;}
};
void Main ()
{
A * aptr = NULL;APTR->F1 ();
A * aptr = NULL;APTR->F2 ();
A * aptr = NULL;APTR->F3 ();
A * aptr = NULL;APTR->F4 ();
}

There is no public in Class A, so it is all private so that F1,F2,F3,F4 will fail. If you change to public then: only A is correct, because a does not use any member variable, and the member function is not part of the object, so a is correct. In fact, a * aptr = Null;aptr->f5 () is also correct, because static members also do not belong to any object. As for BCD, member variables are used in B, and member variables can only exist on objects, and C has a virtual table pointer, so it only exists in the object. D is even more the same.

On the Inter CPU, the following multithreading operates on the int variable x, which is not an atomic operation, assuming that the address of the variable is aligned.

A x = y B x + + C ++x D x=1

Only the D option is an atomic operation.

Question eighth: about the C + + Standard Template Library, the following statements are wrong:

A std::auto_ptr type of object that can be placed in Std::vector > containers;

B std::shared_ptr types of objects, can be placed in the Std::vector > container;

C higher than the execution efficiency of the object Tobj,++tobj and tobj++ of complex type T

D when creating an object with the new operator, the new operator returns NULL if there is not enough memory space to cause the creation to fail.

Select: A,d. A in auto is to give something to others and not themselves (AUTO_PTR and the share_ptr in the Boost library are different, AUTO_PTR does not consider reference counting, so an object can only be owned by one auto_ptr, when assigning to other auto_ptr, Will transfer this ownership relationship. )。 Therefore does not meet the requirements of the vector. and B can. C does not explain. The D option is correct in rectification's C + + program design (rectification may be correct when writing a book, when the new standard for C + + has not yet come out), but in fact the city is wrong. Because: There are three new operators, (1) plain new/delete is operator New/delete. When this is normalized, the new operator in C + + always returns NULL to indicate that the allocation failed, just like malloc, so the programmer has to check its return value. Standard C + + revised the semantics of new, plain new throws the standard exception std::bad_alloc instead of returning null after the failure. However, many programmers using C + + now think that new is the same as before, so it is obviously futile to judge whether the allocation succeeds by checking that the return value is null, (2) nothrow new is the form of operator new that does not throw exceptions, nothrow New returns null on failure, so using it does not require setting the exception handler. Instead, check that the return value is null, as in the past. (3) Placement New/delete: The constructor and destructor are called at the specified memory address and do not cause memory allocations.

#include <iostream>
#include <stdio.h>
using namespace Std;
Class A
{
Public
void foo () {}
};

Class B:virtual Public A
{
Public
void foo () {}
};

Class C:virtual Public A
{
Public
void foo () {}
};

Class D:public B,public C
{
Public
void foo () {}
};

int main (int argc, char *argv[])
{
The size of cout<< "A" is: "<<sizeof (a) <<endl;
The size of cout<< "B" is: "<<sizeof (b) <<endl;
cout<< "C is the size of:" <<sizeof (c) <<endl;
The size of cout<< "D is:" <<sizeof (d) <<endl;
return 0;
}

Output 1,4,4,8, empty class has only one byte, each virtual inheritance to save a base class of the virtual table address

Class A
{
Public
int b;
char c;
virtual void print ()
{cout<< "This is a father ' s function!" <<endl;}
};

Class B:p ublic A
{
Public
virtual void print ()
{cout<< "This is a children ' s function!" <<endl;}
};
int main ()
{
Cout<<sizeof (A) << "<<sizeof (B) <<endl;

return 0;
}

The output is 12,12. If char c is removed, then output 8, 8, because char is aligned by byte, so the original problem requires 12 bytes.

Subclasses inherit the virtual table of the parent class and overwrite the entries therein. and pointers to virtual tables have only one in each class, if the subclass is virtual inheriting the parent class B:virtual public A, then the child analogy parent is 4 more bytes

#include
using namespace Std;

int main ()
{
Http://www.sogou.com
cout<< "Welcome to Sogou" <<endl;

return 0;
}

A: Compile time, B: runtime; C: Compile and run error; D program is running normally

Choose D. Because http://www.sogou.com in//is followed by a comment, preceded by a label (like Goto label)

Sogou C + + written test

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.