Test Tool and test tool in "C/C ++ integrated exercise for engineers"

Source: Internet
Author: User
Tags control characters

Test Tool and test tool in "C/C ++ integrated exercise for engineers"
Preface

Date:
When I stepped into central China, my postgraduate studies were about to take a year away, and my feet suddenly flew away. In the coming year, Alexander will be the task of work. The stupid bird must fly first, and the opportunity is always reserved for those who are prepared. Therefore, as a rookie programmer in the IT field, from now, start the learning overlord mode and sail!

Feeling of the first exercise

I have just registered an account of Niuke. I want to measure the level here and hone it. See the recommended exercise "C/C ++ engineer comprehensive exercise", oh, 20 questions, 2 hours. Due to experience in wood, it is quite tense ~
It took 20 minutes for the result to finish 20 multiple-choice questions. It was really too hasty. I didn't even return to check the results. I handed in the exam when my hands were shaking !!!
The score can be imagined. If you only have half the right questions, you really need to cry... When I think back, I feel that my questions are based on a solid foundation, or my basic knowledge is not solid!
After all, this is the first time. You can't be discouraged. In the next year, you must be able to perform regular tests on yourself, accumulating and improving 1.1 million drops!

Incorrect question analysis and summary

1. Add the following function code:

If two segments of memory overlap, using the memcpy function may lead to undefined behavior. The memmove function can avoid this problem. Below is an implementation method. Please add the 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 exercise. Obviously, str2 is the source address and str1 is the target address. There are two types of copies:

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

Ii. if pStr1> pStr2 is copied from the front to the back, the initial value of pStr1 may be overwritten by the content after pStr2, therefore, we should copy the last element forward in sequence.

Answer:

Conclusion: I did not think deeply about this question, but did not know what to think. I chose the wrong answer C.

3. Which of the following statements about why the template class is used in C ++ is false?

A. It can be used to create A dynamically growing and decreasing data structure.

B. It is type-independent and therefore highly reusable.

C. It checks the data type during runtime to ensure the type security.

D. It is platform-independent and portable.

Analysis: The template class checks the data type during compilation to ensure the type security.
Answer: C
4. After the following function is executed, func (1) =?

int func(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 question is based on the C language. The correct expression of switch and case is case n: Statement; break. The lack of break in the question leads to ineffective judgment, the program runs until the last one and returns 0.
Answer: D

Conclusion: I chose A for this question. I still want to know how to have such A simple question. Alas, I jumped into the trap! You can only blame yourself for the Poor Foundation and the questions are not carefully answered!

5. Compile the following code with gcc on A 32-bit machine. What are the values of sizeof (A) sizeof (B?

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: follow the following three conditions: (1) the size of the struct is an integer multiple of the maximum member size in the body; (2) the offset of the first address of a member in the structure to the first address of the structure is an integer multiple of its type. For example, the offset of the first address of the double member to the first address of the structure should be an integer multiple of 8; (3) to satisfy rules 1 and 2, the compiler fills in bytes after the struct members;
In the question, structure A occupies 4B and B occupies 2B, but as C occupies 4B, B occupies 2B to meet condition 2, and d occupies 4B to meet condition 1, therefore, 16B is used;
Struct B, a occupies 8B and B occupies 2B. But because c occupies 4B, B occupies 2B more, and abc occupies 8 + 4 + 4 = 16B, in order to meet the condition 1, d occupies 8B, so it eventually occupies 24B.

Answer: C

Conclusion: I chose A by mistake. I do know about storage alignment and byte filling, but I am not sure enough about the details!

7. Which of the following statements about C ++ thread security is false?

A. thread security is caused by global variables and static variables.

B. if each thread only performs read operations on global variables and static variables without write operations, this global variable is generally thread-safe. If multiple threads execute write operations at the same time, generally, thread synchronization needs to be considered; otherwise, thread security may be affected.

The string in the C. C ++ standard library ensures thread security.

D. POSIX thread standards require most functions in the C standard library to have thread security

Analysis: for the understanding of thread security issues, refer to the http://blog.csdn.net/ghevinn/article/details/37764791 to explain, and for the C ++ standard library in the string, check some information, there is no clear statement, since it is incorrect here, it cannot be said that string is thread-safe.

Answer: C

Conclusion: select A by mistake, but do not know about thread security issues.

8. What is the output of the following programs?

class Base {    public:    Base(int j): i(j)  {}    virtual~Base() {}    void func1() {        i *= 10;        func2();    }    int getValue() {        return  i;    }    protected:    virtual void func2() {        i++;    }    protected:    int i;};class Child: public Base {    public:    Child(int j): Base(j) {}    void func1() {        i *= 100;        func2();    }    protected:    void func2() {        i += 2;    }};int main() {    Base * pb = new Child(1);    pb->func1();    cout << pb->getValue() << endl; delete pb; }

A.11

B. 101

C.12

D.102

Analysis: evaluate the function call problem between the base class and the derived class. Check whether the base class object or the derived class object is used. In addition, check which functions of the base class are virtual and will be overwritten by the derived class, in this topic, pb is a derived class object. You can call the Child constructor> Base constructor and initialize it to 1. When the func1 () function is called, the Base class function is called. func1 () it 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, find the derived class and run func2 () in the derived class. At this time, I = 12. Finally, run pb-> getValue () and the result is 12.

Answer: C

Conclusion: Wrong A is selected, and the inherited knowledge is vague.

9. Which of the following statements about static data members is true?

A. static data members can be initialized within the class.

B. static data members cannot be called by class objects.

C. static data members are not subject to private control characters

D. static data members can be called directly by class names.

Analysis: this topic examines the static data members in the class, which are initialized outside the class. They are usually initialized during definition, but can be initialized in the class body when the type is const static. It is the same as a common data member, and can be called directly by class name or object.

Answer: D

Summary: This question is so wrong that it seems to be an incorrect option. It is so careless! Wrong C. Why ....

14. Which of the following class systems cannot be inherited by a derived class?

A. Constructor

B. static member functions

C. Non-static member functions

D. Value assignment operation function

Analysis: constructor cannot be inherited, but can be called. If the parent class re-defines the constructor, that is, the default constructor is absent, when a subclass creates its own constructor, it must explicitly call the constructor of the parent class. The other three can be inherited and used in common use.

Answer:

Conclusion: Wrong D is selected. If the derived class calls the base class constructor, it can be said to inherit and correct this error!

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

 char array[12] = {0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08};      short *pshort = (short *)array;      int *pint = (int *)array;      int64 *pint64 = (int64 *)array;      printf("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 of the Small-end machine is stored in the high byte and the low byte in the low address. The x86 structure is in the small-end mode.
Pshort occupies 2 bytes. In the memory, the hexadecimal value is 0x01 0x02, and the corresponding hexadecimal value is 0x0201.
Pshort + 2 points to the element whose subscript is 4 in the array and occupies 2 bytes. The hexadecimal value in the memory is 0x05 0x06, the hexadecimal number is 0x0605.
The int64 type of pint64 is uncertain, but according to the name, it can be seen that it occupies 8 bytes and the corresponding hexadecimal format is 0x807060504030201.
Pint + 2 occupies 4 bytes and points to an array whose subscript is 8. 8-11 elements do not specify the array initialization value. The default value is 0. Therefore, * (pint + 2) the hexadecimal value is 0.

Answer: B

Conclusion: C, Meng, and so on are incorrect...

Summary

The first time I did a written examination, I was wrong with a pile of questions. Although I was frustrated, it also aroused the courage to catch up. Come on!

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.