Preparing for Autumn Recruitment | Strong Training for Written Examination 18

Table of contents

1. Multiple choice questions

2. Programming questions

3. Solutions to multiple choice questions

4. Programming problem solving


1. Multiple choice questions

1. In the following description of STL, () is wrong

A. STL containers are thread-unsafe

B. When the capacity is not enough, a typical implementation of STL is to double the internal memory expansion method of vector.

C. std::sort is a stable sort

D. std::bitset is not an STL container

2. Which of the following STL containers stores data must be sorted ()

A. vector

B. accordingly

C. list

D. map

3. Which of the following objects in STL containers are stored continuously ()

A. list

B. vector

C. map

D. set

4. Which structure in STL may cause the storage location of the original members to change when adding members ()

A. map

B. set

C. list

D. vector

5. The first-level containers in STL are:

A. vector, deque, list, set, multiset, map, multimap.

B. Sequence container, association container, container adapter

C. set, multiset, map, multimap.

D. vector, deque, list.

 6. What are the underlying data structures used by unordered_map and priority_queue in STL ()

A. rbtree,queue

B. hashtable,heap

C. rbtree,heap

D. hashtable,queue

7. Which of the following descriptions of iterator invalidation is incorrect ()

A. The insertion operation of vector will not cause the iterator to expire

B. The insertion operation of map will not cause the iterator to expire

C. The deletion operation of vector will only cause the iterator pointing to the deleted element and the following iterator to become invalid.

D. The deletion operation of map will only cause the iterator pointing to the deleted element to become invalid.

8. How to catch exceptions so that the code can be compiled?

class A 
{
public:
    A(){}
};
void foo()
{
    throw new A;
}

A. catch (A && x)

B. catch (A * x)

C. catch (A & x)

D. All of the above

9. If there is the following program segment: then the program output:

#include <iostream>
using namespace std;
class A 
{
public:
    ~A() 
    {
        cout << "~A()";
    }
};
class B
{
public:
    virtual ~B() 
    {
        cout << "~B()";    
    }
};
class C: public A, public B 
{
public:
    ~C() 
    {
        cout << "~C()";
    }
};
int main() 
{
    C * c = new C;
    B * b1 = dynamic_cast<B *>(c);
    A * a2 = dynamic_cast<A *>(b1);
    delete a2;
    return 0;
}

A. ~C()~B()~A()

B. ~C()~A()~B()

C. A)B) are all possible

D. None of the above is correct

10. The output result of the following program is ___

class A
{
public:
    A ():m_iVal(0){test();}
    virtual void func() { std::cout<<m_iVal<<‘ ’;}
    void test(){func();}
public:
    int m_iVal;
};
class B : public A
{
public:
    B(){test();}
    virtual void func()
    {
        ++m_iVal;
        std::cout<<m_iVal<<‘ ’;
    }
};
int main(int argc ,char* argv[])
{
    A*p = new B;
    p->test();
    return 0;
}

A. 1 0

B. 0 1

C. 0 1 2

D. 2 1 0

E. Unpredictable

F. None of the above is correct

2. Programming questions

1. Count the total number of rabbits per month   . Link to the question

2. String wildcard   question link

3. Solutions to multiple choice questions

1. In the following description of STL, () is wrong

A. STL containers are thread-unsafe

B. When the capacity is not enough, a typical implementation of STL is to double the internal memory expansion method of vector.

C. std::sort is a stable sort

D. std::bitset is not an STL container

Correct answer: C

answer:

        sort is an unstable sort, and the bottom layer is the idea of ​​using quick sort; STL provides a stable sort, stable_sort, and the bottom layer is the idea of ​​​​merging sort; 

2. Which of the following STL containers stores data must be sorted ()

A. vector

B. accordingly

C. list

D. map

Correct answer: D

answer:

         The bottom layer of the map is a red-black tree, which is a type of binary search tree;

3. Which of the following objects in STL containers are stored continuously ()

A. list

B. vector

C. map

D. set

Correct answer: B

answer:

        Only vector applies for a continuous space on the heap for management; 

4. Which structure in STL may cause the storage location of the original members to change when adding members ()

A. map

B. set

C. list

D. vector

Correct answer: D

answer:

         Vector applies for a continuous space, and stores data in this continuous space in sequence; when data is inserted at a certain position, the data behind the insertion position must be moved;

5. The first-level containers in STL are:

A. vector, deque, list, set, multiset, map, multimap.

B. Sequence container, association container, container adapter

C. set, multiset, map, multimap.

D. vector, deque, list.

Correct answer: D

answer:

         First of all, we need to understand the concept of a first-level container. The so-called first-level container means that the data type stored cannot be combined; such as map, which stores KeyValue pairs; therefore, D is selected for the question;

 6. What are the underlying data structures used by unordered_map and priority_queue in STL ()

A. rbtree,queue

B. hashtable,heap

C. rbtree,heap

D. hashtable,queue

Correct answer: B

answer:

         The bottom layer of unordered_map is a hash table, and the bottom layer of priority_queue is a heap;

7. Which of the following descriptions of iterator invalidation is incorrect ()

A. The insertion operation of vector will not cause the iterator to expire

B. The insertion operation of map will not cause the iterator to expire

C. The deletion operation of vector will only cause the iterator pointing to the deleted element and the following iterator to become invalid.

D. The deletion operation of map will only cause the iterator pointing to the deleted element to become invalid.

Correct answer: A

answer:

         Insertion into the vector will cause the iterator to fail; it will affect the insertion and all elements after the insertion position;

8. How to catch exceptions so that the code can be compiled?

class A 
{
public:
    A(){}
};
void foo()
{
    throw new A;
}

A. catch (A && x)

B. catch (A * x)

C. catch (A & x)

D. All of the above

Correct answer: B

answer:

         The question examines exceptions in C++. Throw is followed by new, a class A object, and a pointer of type A is returned. If it fails, the parameter in catch must be a pointer of type A; so choose B;

9. If there is the following program segment: then the program output:

#include <iostream>
using namespace std;
class A 
{
public:
    ~A() 
    {
        cout << "~A()";
    }
};
class B
{
public:
    virtual ~B() 
    {
        cout << "~B()";    
    }
};
class C: public A, public B 
{
public:
    ~C() 
    {
        cout << "~C()";
    }
};
int main() 
{
    C * c = new C;
    B * b1 = dynamic_cast<B *>(c);
    A * a2 = dynamic_cast<A *>(b1);
    delete a2;
    return 0;
}

A. ~C()~B()~A()

B. ~C()~A()~B()

C. A)B) are all possible

D. None of the above is correct

Correct answer: D

answer:

         This question examines polymorphism. We create a class C object. The class C object inherits classes A and B. We assign the address of the new object to c, and then assign the value from the slice to the subclass b1. This is what we will do again. Assign b1 to a class A pointer a2 whose type does not matter, and finally delete a2, which will cause the operation to crash;

10. The output result of the following program is ___

class A
{
public:
    A ():m_iVal(0){test();}
    virtual void func() { std::cout<<m_iVal<<‘ ’;}
    void test(){func();}
public:
    int m_iVal;
};
class B : public A
{
public:
    B(){test();}
    virtual void func()
    {
        ++m_iVal;
        std::cout<<m_iVal<<‘ ’;
    }
};
int main(int argc ,char* argv[])
{
    A*p = new B;
    p->test();
    return 0;
}

A. 1 0

B. 0 1

C. 0 1 2

D. 2 1 0

E. Unpredictable

F. None of the above is correct

Correct answer: C

answer:

         We have mentioned in the previous questions that when we call virtual functions in the constructor, there will be no polymorphism. Therefore, the virtual table pointer is initialized when the constructor initializes the object. Let's look at the question again. First, new a subclass , the parent class constructor will be called first, the parent class constructor calls test, test calls the parent class's func, and prints 0; then the subclass part is initialized, the subclass constructor calls test, test calls the subclass func, and m_iVal++ And print, print 1; finally, hand the new object to the parent class pointer to point to, the parent class pointer calls test, test calls func, causing polymorphism, the parent class pointer calls the virtual function, calls the subclass func, and prints 2; so choose C;

4. Programming problem solving

1. Count the total number of rabbits every month

Idea: Read the question carefully, it is not difficult to find that the question asks us to find the nth term of the Fibonacci sequence; the following picture shows the number of rabbits aged one, two and three months from January to July. How much; the discovery is our Fibonacci sequence;

#include <iostream>
using namespace std;

int main() 
{
    int n;
    cin >> n;
    int f1 = 1;
    int f2 = 1;
    int f3;
    for(int i = 3; i <= n; i++)
    {
        f3 = f1 + f2;
        f1 = f2;
        f2 = f3;
    }
    cout << f3 << endl;
    return 0;
}

2. String wildcard

Idea: We can use recursive thinking to solve this problem. When we encounter '?', we will move the pointers pointing to the two strings backward; if we encounter '*', we can divide There are three cases, the first case is * does not match any characters, the second case * matches one character, and the third case * matches two or more characters; see the code for details;

#include <cctype>
#include <iostream>
using namespace std;
bool strMach(const char* p, const char* q)
{
    // 双方都走完才算匹配
    if(*p == '\0' && *q == '\0')
        return true;
    // 只有其中一方走完不算匹配
    if(*p == '\0' || *q == '\0')
        return false;
    if(*p == '?' && (isalnum(*q) || isalpha(*q)))
        return strMach(p + 1, q + 1);
    else if(*p == '*' && (isalnum(*q) || isalpha(*q)))
    {
     while(*p=='*')
        {
          p++;
        } 
        p--;
        return strMach(p + 1, q)  // 匹配0个
        || strMach(p + 1, q + 1)  // 匹配1个
        || strMach(p, q + 1);     // 匹配多个
    }
    else if(tolower(*p) == tolower(*q))
        return strMach(p + 1, q + 1);
    return false;
}
 
int main()
{
    string pattern, str;
    cin >> pattern >> str;
    bool ret = strMach(pattern.c_str(), str.c_str());
    if(ret)
        cout << "true" << endl;
    else
        cout << "false" << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/Nice_W/article/details/131996758