Preparing for Autumn Recruitment | Strong Training for Written Examination 15

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 case of public derivation, member functions defined in the derived class can only access () of the original base class.

A. Public and private members

B. Private Members and Protected Members

C. Public and Protected Members

D. Private members, protected members and public members

2. There is the following C++ code: the output answer is

struct A
{
    void foo(){printf("foo");}
    virtual void bar(){printf("bar");}
    A(){bar();}
};
struct B:A
{
    void foo(){printf("b_foo");}
    void bar(){printf("b_bar");}
};

int main()
{
    A *p = new B;
    p->foo();
    p->bar();
    return 0;
}

A. barfoob_bar

B. foobarb_bar

C. barfoob_foo

D. foobarb_fpp

3. What is wrong with the description of abstract classes and pure virtual functions?

A. The declaration of a pure virtual function ends with "=0;"

B. A class with pure virtual functions is called an abstract class and cannot be used to define objects.

C. If a derived class of an abstract class does not implement pure virtual functions, it is also an abstract class.

D. A pure virtual function cannot have a function body

4. The output result of the following program is ()

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

A. A->0

B. B->1

C. A->1

D. B->0

5. The output of the following program is ()

class A
{
public:
    void foo()
    {
        printf("1");
    }
    virtual void fun()
    {
        printf("2");
    }
};
class B: public A
{
public:
    void foo()
    {
        printf("3");
    }
    void fun()
    {
        printf("4");
    }
};
int main(void)
{
    A a;
    B b;
    A *p = &a;
    p->foo();
    p->fun();
    p = &b;
    p->foo();
    p->fun();
    A *ptr = (A *)&b;
    ptr->foo();
    ptr->fun();
    return 0;
}

A. 121434

B. 121414

C. 121232

D. 123434

6. If class B inherits class A, A::x() is declared as a virtual function, and B::x() overloads the A::x() method, which x() method will be called in the following statement ()

B b;
b.x();

A. A::x()

B. B::x()

C. A::x() B::x()

D. B::x() A::x()

7. The following description of virtual functions is wrong

A. Add the virtual modification in front of the member function declaration to declare the function as a virtual function

B. After a virtual function is declared in the base class, the corresponding function in the derived class must also be declared as a virtual function.

C. A virtual function can be a friend function of another class, but it cannot be a static member function.

D. Pure virtual functions described in the base class must be implemented in any derived class that needs to be instantiated.

8. Which of the following is the correct declaration of a pure virtual function ()

A. void virtual print()=0;
B. virtual void print()=0;
C. virtual void print(){};
D. virtual void print()\;

9. What problems will occur when the following code is run?

class A
{
public:
    void f()
    {
        printf("A\n");
    }
};
class B: public A
{
public:
    virtual void f()
    {
        printf("B\n");
    }
};
int main()
{
    A *a = new B;
    a->f();
    delete a;
    return 0;
}

A. No problem, output B

B. Unexpected output A

C. The program is incorrect

D. None of the above answers are correct

10. What will the following code print?

class A
{
public:
    A()
    {
        printf("A ");
    }
    ~A()
    {
        printf("deA ");
    }
};
class B
{
public:
    B()
    {
        printf("B ");
    }
    ~B()
    {
        printf("deB ");
    }    
};
class C: public A, public B
{
public:
    C()
    {
        printf("C ");
    }
    ~C()
    {
        printf("deC ");
    }
};
int main()
{
    A *a = new C();
    delete a;
    return 0;
}

A. A B C deA

B. C A B deA

C. A B C deC

D. C A B deC

2. Programming questions

1. Find   the link to the question of the number of 1’s in the input integer binary system

 2. Gloves   question link

3. Solutions to multiple choice questions

 

1. In the case of public derivation, member functions defined in the derived class can only access () of the original base class.

A. Public and private members

B. Private Members and Protected Members

C. Public and Protected Members

D. Private members, protected members and public members

Correct answer: C

answer:

         The permissions of the derived class to access the members of the parent class are the smaller value of the inheritance method and the permissions of the members in the original base class . However, if the parent class members are privately modified, they will not be visible to the subclass; so choose C

2. There is the following C++ code: the output answer is

struct A
{
    void foo(){printf("foo");}
    virtual void bar(){printf("bar");}
    A(){bar();}
};
struct B:A
{
    void foo(){printf("b_foo");}
    void bar(){printf("b_bar");}
};

int main()
{
    A *p = new B;
    p->foo();
    p->bar();
    return 0;
}

A. barfoob_bar

B. foobarb_bar

C. barfoob_foo

D. foobarb_fpp

Correct answer: A

answer:

         This question examines virtual functions and polymorphism; first of all, we need to know when our virtual table is generated; virtual tables, also called virtual function tables, are generated during our compilation phase; and our virtual table pointers are created when objects are created. It is generated when it is running, which is the running state; when our subclass constructs an object, it first calls the default constructor of the parent class to construct the part that initializes the parent class, and then assigns a value to our virtual table pointer to point to the virtual table; then Go through the initialization list, and finally go through the subclass constructor body; therefore, when we create a new subclass in this question, we first call the parent class constructor. There are virtual functions called in the parent class constructor body, but at this time the virtual function table of our subclass has not yet been generated, so we can only call the virtual function bar of the parent class, and then put the address of the created B object into p. The following two calls are polymorphic. The foo parent class is not declared as a virtual function, so the parent class's foo, then the bar parent class defines a virtual function, and the subclass rewrites it, so the subclass's bar is called, and finally A is selected;

3. What is wrong with the description of abstract classes and pure virtual functions?

A. The declaration of a pure virtual function ends with "=0;"

B. A class with pure virtual functions is called an abstract class and cannot be used to define objects.

C. If a derived class of an abstract class does not implement pure virtual functions, it is also an abstract class.

D. A pure virtual function cannot have a function body

Correct answer: D

answer:

         The description of option D is wrong. A pure virtual function can have a function body, but it is meaningless;

4. The output result of the following program is ()

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

A. A->0

B. B->1

C. A->1

D. B->0

Correct answer: B

answer:

         This question examines the details related to polymorphic rewriting; we analyze the question and find that both func and test are declared as virtual functions in the parent class; while func is rewritten in the subclass, test has not completed the rewriting; we use the subclass's The pointer calls the test function inherited from the parent class. The test function has a hidden parameter, which is the this pointer, and the type of this pointer is actually A* const this. This type; at this time we call func, func in the test function In fact, one this was omitted earlier, that is, the parent class pointer calls func, and func is a virtual function and has been rewritten in the subclass. One feature of rewriting is that only the implementation is rewritten, so we just rewrite the function Body replacement, the default value remains unchanged, so option B is printed;

5. The output of the following program is ()

class A
{
public:
    void foo()
    {
        printf("1");
    }
    virtual void fun()
    {
        printf("2");
    }
};
class B: public A
{
public:
    void foo()
    {
        printf("3");
    }
    void fun()
    {
        printf("4");
    }
};
int main(void)
{
    A a;
    B b;
    A *p = &a;
    p->foo();
    p->fun();
    p = &b;
    p->foo();
    p->fun();
    A *ptr = (A *)&b;
    ptr->foo();
    ptr->fun();
    return 0;
}

A. 121434

B. 121414

C. 121232

D. 123434

Correct answer: B

answer:

         First we look at the type of p, which is a pointer of class A. We assign the address of a to p, and then call foo that has not completed the rewriting and fun that has completed the rewriting, then print 1 and 2 respectively, and then we will The address is assigned to p. At this time, it is called with a subclass pointer to complete the polymorphic tuning. Then it prints 1 and 4 when called separately. Finally, it only performs forced type conversion and prints 1 and 4, so choose B;

6. If class B inherits class A, A::x() is declared as a virtual function, and B::x() overloads the A::x() method, which x() method will be called in the following statement ()

B b;
b.x();

A. A::x()

B. B::x()

C. A::x() B::x()

D. B::x() A::x()

Correct answer: B

answer:

         There are two indispensable conditions for polymorphism. 1 is the rewriting of virtual functions. The question has been provided. 2 is the pointer or reference of the parent class to call. This is not achieved. Therefore, polymorphism is not formed. The x of class B is still called. Function; so choose B;

7. The following description of virtual functions is wrong

A. Add the virtual modification in front of the member function declaration to declare the function as a virtual function

B. After a virtual function is declared in the base class, the corresponding function in the derived class must also be declared as a virtual function.

C. A virtual function can be a friend function of another class, but it cannot be a static member function.

D. Pure virtual functions described in the base class must be implemented in any derived class that needs to be instantiated.

Correct answer: B

answer:

         Option B, the function corresponding to the derived class can be added virtual or not; there is no mandatory requirement;

8. Which of the following is the correct declaration of a pure virtual function ()

A. void virtual print()=0;
B. virtual void print()=0;
C. virtual void print(){};
D. virtual void print()\;

Correct answer: B

answer:

         For grammatical requirements, choose B;

9. What problems will occur when the following code is run?

class A
{
public:
    void f()
    {
        printf("A\n");
    }
};
class B: public A
{
public:
    virtual void f()
    {
        printf("B\n");
    }
};
int main()
{
    A *a = new B;
    a->f();
    delete a;
    return 0;
}

A. No problem, output B

B. Unexpected output A

C. The program is incorrect

D. None of the above answers are correct

Correct answer: B

answer:

         Because f is not declared as a virtual function in our parent class, the rewriting of the virtual function is not successfully completed, and when we call it using the pointer of the parent class, we still call the f function of the parent class, so we choose B;

10. What will the following code print?

class A
{
public:
    A()
    {
        printf("A ");
    }
    ~A()
    {
        printf("deA ");
    }
};
class B
{
public:
    B()
    {
        printf("B ");
    }
    ~B()
    {
        printf("deB ");
    }    
};
class C: public A, public B
{
public:
    C()
    {
        printf("C ");
    }
    ~C()
    {
        printf("deC ");
    }
};
int main()
{
    A *a = new C();
    delete a;
    return 0;
}

A. A B C deA

B. C A B deA

C. A B C deC

D. C A B deC

Correct answer: A

answer:

        When we construct a subclass, we will first construct the part of its parent class, and the order of constructing the parent class is only related to the order of inheritance, and has nothing to do with the order of distribution in the initialization list, so we will print ABC in sequence; then we call delete , destructs the pointer a of type A, so the destructor of class A is called and deA is printed;

4. Programming problem solving

1. Find the number of 1’s in the input integer binary

Idea: We can cleverly use bit operations for statistics. We use num &= num - 1 to remove the last 1 in the num binary, and repeat until all are removed;

#include <iostream>
using namespace std;

int main() 
{
    int num;
    while(cin >> num)
    {
        // 统计二进制1的个数
        int count = 0;
        while(num)
        {
            num &= num - 1;
            count++;
        }
        cout << count << endl;
    }
}

2. Gloves

Idea: Can we ensure that there must be one of each left or right hand, then take another one from the other side to ensure that the whole can definitely form a pair, as shown below;

        We take the smaller one of the left or right hand that covers every possible number of gloves, and then +1 (any one picked from the other side can be matched into a pair), think carefully about whether this is the case, but The problem cannot be completely solved at this point. We have not yet considered the situation where a certain color on a certain side is 0; as shown below;

        Based on the above, we can write the following code;

class Gloves {
public:
    int findMinimum(int n, vector<int> left, vector<int> right) 
    {
        int left_sum = 0, left_min = INT_MAX;
        int right_sum = 0, right_min = INT_MAX;
        int sum = 0;
        for(int i = 0; i < n; i++)
        {
            if(left[i] == 0 || right[i] == 0)
                sum += left[i] + right[i];
            else
            {
                left_sum += left[i];
                left_min = min(left_min, left[i]);
                right_sum += right[i];
                right_min = min(right_min, right[i]);
            }
        }
        sum += min(left_sum - left_min + 1, right_sum - right_min + 1) + 1;
        return sum;
    }
};

Guess you like

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