Preparing for Autumn Recruitment | Strong Training for Written Examination 17

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. Assuming that A is an abstract class, the following statement () is correct

A. int fun(A);

B. A Obj;

C. A fun(int);

D. A *p;

2. Can virtual functions be overloaded inline?

A. Yes

B. No

C. Syntax error

3. Which of the following descriptions of virtual functions is wrong?

A. The function of virtual functions is to realize inheritance

B. The function of virtual functions is to implement "dynamic binding", that is, to dynamically select appropriate member functions during the running phase of the program.

C. Compared with static member functions of classes and non-class member functions, virtual functions are less efficient

D. To implement virtual functions correctly, you can only use a base class pointer or reference to point to a derived class object.

4. The following code outputs ()

#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
    vector<int>array;
    array.push_back(100);
    array.push_back(300);
    array.push_back(300);
    array.push_back(300);
    array.push_back(300);
    array.push_back(500);
    vector<int>::iterator itor;
    for(itor=array.begin();itor!=array.end();itor++)
    {
        if(*itor==300)
        {
            itor=array.erase(itor);
        }
    }
    for(itor=array.begin();itor!=array.end();itor++)
    {
        cout<<*itor<<"";
    }
    return 0;
}

A. 100 300 300 300 300 500

B. 100 300 300 300 500

C. 100 300 300 500

D. 100 300

5. In C++, ( ) is used to implement dynamic polymorphism.

A. Inline functions

B. Overloaded functions

C. Template function

D. virtual function

6. When calling a member function, the situation of using dynamic binding is ()

A. Call a virtual function through an object

B. Call a virtual function through a pointer or reference

C. Call static functions through objects

D. Call a static function through a pointer or application

7. Which of the following descriptions of inheritance, polymorphism, and composition is incorrect?

A. Encapsulation, encapsulating objective things into abstract classes, and classes can only allow trusted classes or objects to operate their data and methods, and hide information from untrusted ones.

B. Inheritance can use all the functions of an existing class and extend these functions without rewriting the original class.

C. Hiding means that the function in the derived class blocks the function with the same name in the base class.

D. Coverage means that different functions use the same function name, but the number or type of parameters of the function is different.

8. What is the relationship between "reference" and polymorphism?

A. There is no relationship between the two

B. References can be used as a means to produce polymorphic effects

C. A reference to a base class cannot point to an instance of its derived class.

D. None of the above is correct

9. What is the output of the following program?

class A
{
public:
    A(){p();}
    virtual void p(){printf("A")}
    virtual ~A(){p();}
};
class B:public A
{
public:
    B(){p();}
    void p(){printf("B")}    
    ~B(){p();}
};
int main(int, char**)
{
    A* a=new B();
    delete a;
    return 0;
}

A. AABB

B.BBAA

C. ABAB

D.ABBA

10. The running result of the following code is ()

class Base 
{
public:
    Base() 
    {
        echo();
    }
    virtual void echo() 
    {
        printf("Base");
    }
};
class Derived:public Base 
{
public:
    Derived() 
    {    
        echo();
    }
    virtual void echo() 
    {
        printf("Derived");
    }
};
int main() 
{
    Base* base = new Derived();
    base->echo();
    return 0;
}

A. DerivedDerivedDerived

B. DerivedBaseDerived

C. BaseDerivedBase

D. BaseDerivedDerived

2. Programming questions

1.   Link to the deformation question of Yang Hui’s triangle

 2. Calculate the number of occurrences of a certain character.   Link to the question

3. Solutions to multiple choice questions

1. Assuming that A is an abstract class, the following statement () is correct

A. int fun(A);

B. A Obj;

C. A fun(int);

D. A *p;

Correct answer: D

answer:

         Abstract classes cannot instantiate objects; option A, passing parameters by value must call copy construction, and instantiating the object is an error; option B, defining a class A, also instantiates a class A object; option C, returning by value must When calling copy construction, the object must be instantiated; D option, defining the pointer will not instantiate the object;

2. Can virtual functions be overloaded inline?

A. Yes

B. No

C. Syntax error

Correct answer: B

 answer:

        Virtual functions cannot be overloaded as inline functions, because inline functions do not have addresses and are only called to expand;

3. Which of the following descriptions of virtual functions is wrong?

A. The function of virtual functions is to realize inheritance

B. The function of virtual functions is to implement "dynamic binding", that is, to dynamically select appropriate member functions during the running phase of the program.

C. Compared with static member functions of classes and non-class member functions, virtual functions are less efficient

D. To implement virtual functions correctly, you can only use a base class pointer or reference to point to a derived class object.

Correct answer: A

answer:

         Virtual function is one of the conditions for realizing polymorphism and has nothing to do with inheritance; therefore option A is wrong;

4. The following code outputs ()

#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
    vector<int>array;
    array.push_back(100);
    array.push_back(300);
    array.push_back(300);
    array.push_back(300);
    array.push_back(300);
    array.push_back(500);
    vector<int>::iterator itor;
    for(itor=array.begin();itor!=array.end();itor++)
    {
        if(*itor==300)
        {
            itor=array.erase(itor);
        }
    }
    for(itor=array.begin();itor!=array.end();itor++)
    {
        cout<<*itor<<"";
    }
    return 0;
}

A. 100 300 300 300 300 500

B. 100 300 300 300 500

C. 100 300 300 500

D. 100 300

Correct answer: C

answer:

         Here we examine the iterator failure problem when using the erase function in vector. When we delete an element in the vector, what is returned is the iterator of the next element position of the deleted element. When the vector has 100 300 300 300 300 500, we enter the loop. , first judge whether the first element is equal to 300, the result is not equal to 300, so use ++ on the iterator to reach the position of the second element, and enter the next loop; continue to judge and find that the value pointed by the iterator is equal to 300, so Delete this element and return the position of the next element. At this time, please note that after adding the if statement, we performed ++ on the iterator again, and just skipped a 300, and so on. This means that 300 was not deleted completely. The reason is, the final answer is C;

5. In C++, ( ) is used to implement dynamic polymorphism.

A. Inline functions

B. Overloaded functions

C. Template function

D. virtual function

Correct answer: D

answer:

         We achieve static polymorphism through function overloading, and dynamic polymorphism through rewriting of virtual functions; so choose D;

6. When calling a member function, the situation of using dynamic binding is ()

A. Call a virtual function through an object

B. Call a virtual function through a pointer or reference

C. Call static functions through objects

D. Call a static function through a pointer or application

Correct answer: B

answer:

         The so-called dynamic binding means that during compilation of the program, it is impossible to determine which function to call. It can only be known during running. To put it simply, isn't this the polymorphism in our C++, and there are two types of polymorphism? Conditions, and one is indispensable, one is the rewriting of the virtual function; the other is the pointer or reference call of the parent class; so choose B;

7. Which of the following descriptions of inheritance, polymorphism, and composition is incorrect?

A. Encapsulation, encapsulating objective things into abstract classes, and classes can only allow trusted classes or objects to operate their data and methods, and hide information from untrusted ones.

B. Inheritance can use all the functions of an existing class and extend these functions without rewriting the original class.

C. Hiding means that the function in the derived class blocks the function with the same name in the base class.

D. Coverage means that different functions use the same function name, but the number or type of parameters of the function is different.

Correct answer: D

answer:

         D option, overwriting is the rewriting of virtual functions, requires the same function return value, the same function name, and the same function parameter list (except for the special cases of covariance and destructor); therefore D is wrong;

8. What is the relationship between "reference" and polymorphism?

A. There is no relationship between the two

B. References can be used as a means to produce polymorphic effects

C. A reference to a base class cannot point to an instance of its derived class.

D. None of the above is correct

Correct answer: B

answer:

         One of the conditions for polymorphism is to use a pointer or reference from the parent class to call a virtual function;

9. What is the output of the following program?

class A
{
public:
    A(){p();}
    virtual void p(){printf("A")}
    virtual ~A(){p();}
};
class B:public A
{
public:
    B(){p();}
    void p(){printf("B")}    
    ~B(){p();}
};
int main(int, char**)
{
    A* a=new B();
    delete a;
    return 0;
}

A. AABB

B.BBAA

C. ABAB

D.ABBA

Correct answer: D

answer:

         Looking at the question carefully, we have a pair of parent-child classes, whose constructors and destructors both call the virtual function p, and p is rewritten in the subclass. To do this question, first we need to know the difference between the constructor and the destructor. In a function, when we call a virtual function, there will be no polymorphism; for example, when the constructor is called, the virtual table has not yet been generated, so there is no polymorphism; when we are looking at the question, we first create a new subclass object, and the object will first Call the constructor of the parent class. There will be no polymorphism in calling the function in the constructor, so we will print A; then call the constructor of the subclass, also call the p function, and print B; then we delete the base object, which is the parent class. pointer, and it points to a subclass object. Polymorphism is achieved at this time. The subclass is destructed first, and then the parent class is destructed. The function called in the destructor will not be polymorphic, so B and A are printed respectively. ;Final answer D;

10. The running result of the following code is ()

class Base 
{
public:
    Base() 
    {
        echo();
    }
    virtual void echo() 
    {
        printf("Base");
    }
};
class Derived:public Base 
{
public:
    Derived() 
    {    
        echo();
    }
    virtual void echo() 
    {
        printf("Derived");
    }
};
int main() 
{
    Base* base = new Derived();
    base->echo();
    return 0;
}

A. DerivedDerivedDerived

B. DerivedBaseDerived

C. BaseDerivedBase

D. BaseDerivedDerived

Correct answer: D

answer:

         Like the ninth question, the virtual function is called inside the constructor, and there will be no polymorphism; therefore, Base and Derived are printed separately; then we call the virtual function through the pointer of the parent class object, and the virtual function is rewritten to achieve polymorphism. , print Derived; the final answer is D;

4. Programming problem solving

1. Deformation of Yang Hui’s triangle

Idea: This question is about finding a pattern. If you go back a few more rows, you will find that the odd-numbered rows are all the second columns with even numbers. The even-numbered rows are divided into two situations. The first is a multiple of 4. This When the fourth column is an even number, the second case is a multiple of 2, and the degree is that the third column is an even number;

#include <iostream>
using namespace std;

int main() 
{
    int n;
    cin >> n;

    if(n < 3)
        cout << -1 << endl;
    else if(n % 2== 1)
        cout << 2 << endl;
    else if((n - 2) % 4 == 0)
        cout << 4 << endl;
    else
        cout << 3 << endl;
    return 0;
}

2. Count the number of occurrences of a certain character

Idea: We first enter a string, which includes letters, numbers and spaces; then we enter a character, and we count the number of times the character appears in the string; if the search is for letters, it is not case-sensitive; at this time We have an idea, when creating a character, if the original character is an uppercase letter, the character is the corresponding lowercase letter; if the original character is a lowercase letter, the character is the corresponding uppercase letter; if it is not a letter, the character is the original character; Then perform a loop to determine the number of characters;

#include <iostream>
#include <string>
using namespace std;

int main() 
{
    string str;
    char ch1;
    getline(cin, str);
    cin >> ch1;
    int pos = 0;
    int count = 0;
    char ch2 = ch1;
    if(ch1 >= 'a' && ch1 <= 'z')
        ch2 = ch1 - 32;
    else if(ch1 >= 'A' && ch1 <= 'Z')
        ch2 = ch1 + 32;
    while(pos < str.size())
    {
        if(str[pos] == ch1 || str[pos] == ch2)
            count++;
        pos++;
    }
    cout << count << endl;
    return 0;
}

Guess you like

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