The small test sledgehammer of the C + + engineer Comprehensive Practice Roll

Source: Internet
Author: User

Objective

Step into central China, postgraduate study immediately also has a small year time, suddenly bright time flies. In the coming year, it will be the work of Alexander. Stupid bird must fly first, and the opportunity is always left to prepare the person, thus, as an IT field rookie Cheng, from now, open genius mode, sail a voyage!

The experience of the first set of exercises

Just registered the customer account of the cattle network, ready to measure the level, a good temper. See the recommended practice, "C + + Engineer Comprehensive Practice Roll", oh,20, 2 hours. Because of the wood experience, a good tension ~
Results took 20 minutes, finished 20 choice questions, really is too hasty, did not return to check, hand shake on the roll!!!
The result is conceivable, only to do half of the topic, really is to cry ... Back to the head to think about, the feeling of the topic is the basis of the solid level, or the basic knowledge is not solid!
After all, is the first time, can not be discouraged, in the next year, must be steady, regular test themselves, bit by bit of accumulation and progress!

Analysis and summary of the wrong problem

1. Add the following function code:

If two pieces of memory overlap, using the memcpy function may cause the behavior to be undefined. While the Memmove function avoids this problem, here is a way to do it, please add code.

#include <iostream>using namespace STD;void* Memmove (void* STR1,Const void* str2,size_t N) {Char* Pstr1= (Char*) str1;Const Char* Pstr2= (Const Char*) str2;if( ) { for(size_t i=0; i!=n;++i) {* (pstr1++) =* (pstr2++); }    }Else{pstr1+=n-1; pstr2+=n-1; for(size_t i=0; i!=n;++i) {* (pstr1--) =* (pstr2--); }    }return( );}

A. PStr1 < PSTR2 str1

B. pstr1+n < PSTR2 str2

C. pstr1+n < PSTR2 | | Pstr2+n < PSTR1 str2

D. pstr2+n < PSTR1 str1

Analysis: This is a memory copy related exercises, obviously, str2 as the source address, str1 as the destination address, divided into two cases copy:

I. If pStr1 < PSTR2, copy from front to back

II. If the PStr1 > PSTR2 is also copied from back to front, it may result in PSTR1 's initial value being overwritten by the contents of the PSTR2, so it should be copied sequentially from the last element.

Answer: A

Summary: See this topic, do not think deeply, do not know what to think, choose the wrong answer C.

3. The following is the reason why a template class is used in C + +, which describes the error?

A. Data structures that can be used to create dynamic growth and reduction

B. It is type-independent and therefore highly reusable

C. It checks the data type at runtime to ensure type safety

D. It is platform-independent, portability

Analysis: Template classes Check data types at compile time, ensuring type safety.
Answer: C
4. After the following function is executed, func (1) =?

intfunc(int a){    int b;    switch (a)    {        case 1: b = 30;        case 2: b = 20;        case 3: b = 16;        default: b = 0;    }    return b;}

A. 30

B. 20

C. 16

D. 0

Analysis: This topic examines the C language Foundation, the switch and case combination, the correct expression way is, case N: statement; break, and the lack of a break in the topic, the decision does not work, the program will be executed until the last one, return 0.
Answer: D

Summary: This question selected A, still thinking how there will be such a simple topic, alas, jumped into the pit! Only blame themselves, the foundation is not reliable, do not carefully!

5. On a 32-bit machine, compile the following code with GCC to find out how much sizeof (A) sizeof (B) is?

class A{        int a;        short b;        int c;        char d;};class B{        double a;        short b;        int c;        char d;};

A. 12 16

B. 12 12

C. 16 24

D. 16 20

Analysis: Following three conditions (1) The size of the struct is equal to the integer multiples of the maximum member size in the structure body, and (2) the first address of the member in the structure is an integer multiple of its type size, as opposed to the first address of the struct, for example, the address offset of a double member relative to the first address of the struct should ; (3) in order to satisfy rules 1 and 2, the compiler will make a byte fill after the struct member;
In the title, the structure a accounted for 4b,b accounted for 2B, but because C accounted for 4B, in order to meet the conditions 2,b more occupy 2B, in order to meet the conditions 1,d occupy 4B, so the final occupation 16B;
The structure of the b,a accounted for 8b,b accounted for 2B, but because C occupies 4B, then B will occupy more 2B, then ABC occupies 8+4+4=16b, in order to meet the conditions 1,d occupy 8B, so the final occupation of 24B.

Answer: C

Summary: The wrong choice of a, do understand, storage alignment and byte fill this matter, but not enough detail, not strong enough!

7. What's wrong with the following about C + + thread safety?

A. Line shuo are all caused by global variables and static variables.

B. If there are only read operations for global variables and static variables in each thread, and there is no write operation, this global variable is generally thread-safe, and if multiple threads write at the same time, it is generally necessary to consider thread synchronization, otherwise it may affect thread safety

C. The string inside the C + + standard library is thread-safe

D. POSIX threading standards require that most functions in the C standard library have thread safety

Analysis: The understanding of thread safety issues, refer to the http://blog.csdn.net/ghevinn/article/details/37764791, and for the C + + standard library string, check some information, there is no clear statement, Since it is wrong here, it is not possible to say that string is thread-safe.

Answer: C

Summary: Wrong choice A, not aware of thread safety issues.

8. What is the output of the following program?

Class Base { Public:Base(intJ):I(j) {}Virtual~base () {}voidFunc1 () {i *=Ten;    Func2 (); }intGetValue () {returnI }protected:Virtual void Func2() {i++; }protected:inti;}; Class Child: PublicBase { Public: Child(intJ):Base(j) {}voidFunc1 () {i *= -;    Func2 (); }protected:void Func2() {i + =2; }};intMain () {Base * PB =NewChild (1);    Pb->func1 (); cout << pb->getvalue () << Endl; Delete PB; }

a.11

B. 101

c.12

d.102

Analysis: To investigate the base class and the derived class function call problem, to see whether it is a base class object or a derived class object, in addition to the base class which functions are virtual, will be overridden by the derived class, the topic PB is a derived class object, call the child constructor->base constructor, initialized to 1, when the call function func1 (), the call is a function of the base class, Func1 () is not a virtual function, so pb->func1 () executes the FUNC1 function of the base class, i= 10, and then calls the Func2 () function; Here Func2 is a virtual function, to go down the derivation of the class to find, When found, executes Func2 () in the derived class, at which point i = 12; finally executes Pb->getvalue () and the result is 12.

Answer: C

Summary: Wrong choice A, the knowledge of inheritance is ambiguous.

9. Under the description of static data members, right?

A. Static data members can be initialized within a class

B. Static data members cannot be called by objects of the class

C. Static data members are not affected by the private control character

D. Static data members can be called directly with the class name

Analysis: This topic examines static data members within a class, which are initialized outside the class, usually at the time of definition, but can be initialized in the class when the type is const static. It is the same as normal data members, and can be called directly using the class name or using object invocation.

Answer: D

Summary: The problem is outrageous, seemingly wrong option, anyway is sloppy! The wrong choice C, Woo-woo ....

The C + + class system cannot be inherited by a derived class?

A. Constructors

B. Static member functions

C. Non-static member functions

D. Assignment Action function

Analysis: Constructors cannot be inherited, but can be called, if the parent has redefined the constructor, i.e. there is no default constructor, the subclass must explicitly invoke the constructor of the parent class when creating its own constructor function. The rest of the three can be inherited in our usual use.

Answer: A

Summary: The wrong choice D, wrong for the derived class call the base class constructor, can be said to inherit, correct this error!

18. On the 32-bit small end machine, what is the output of the following code?

Char array[ A] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08}; Short*pshort= (short*)Arrayint *pint= (int *)Array Int64*pint64= (Int64*)Arrayprintf("0x%x , 0x%x , 0x%x , 0x%x",*pshort,*(pshort+2) ,*pint64,*(pint+2));

A. 0x201, 0x403, 0x807060504030201, 0x0
B. 0x201, 0x605, 0x807060504030201, 0x0
C. 0x201, 0x605, 0x4030201, 0x8070605
D. 0x102, 0x506, 0x102030405060708, 0x0

Analysis:
The data high byte of the small end machine is placed at the high address, and the low byte is placed at the lower address. The x86 structure is a small-end mode.
The Pshort occupies 2 bytes, and the 16 binary in memory is the 0x01 0x02, and the corresponding 16 binary number is 0x0201.
Pshort + 2 points to the array array's subscript 4 element, occupies 2 bytes, 16 in memory is 0x05 0x06, the corresponding 16 binary number is 0x0605.
The int64 type of pint64 is indeterminate, but according to the name you can see that it occupies 8 bytes, and the corresponding 16 binary form is 0x807060504030201.
Pint + 2 takes 4 bytes, points to an array with the subscript 8 element, 8-11 elements do not specify an initialization value for the array, the default is 0, so * (pint + 2) corresponds to 16 in 0.

Answer: B

Summary: The wrong choice C, Meng ...

Summarize

The first time to do a written examination of the wrong pile of piles, although very frustrated, but also aroused the courage to catch up, refueling!

The small test sledgehammer of the C + + engineer Comprehensive Practice Roll

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.