Preparing for Autumn Recruitment | Strong Training for Written Examination 16

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. The output result of the following C++ code is ()

#include <iostream>
class Base
{
public:
    int Bar(char x)
    {
        return (int)(x);
    }
    virtual int Bar(int x)
    {
        return (2 * x);
    }
};
class Derived : public Base
{
public:
    int Bar(char x)
    {
        return (int)(-x);
    }
    int Bar(int x)
    {
        return (x / 2);
    }
};
int main(void)
{
    Derived Obj;
    Base *pObj = &Obj;
    printf("%d,", pObj->Bar((char)(100)));
    printf("%d,", pObj->Bar(100));
    return 0;
 }

A. 100,-100

B. 100,50

C. 200,-100

D. 200,50

2. The correct description of the function is ()

A. A virtual function is a static function

B. The virtual function of the derived class and the virtual function of the base class have different number and types of parameters.

C. A virtual function is a non-member function

D. After a virtual function is declared in the base class, the corresponding function in the derived class does not need to be declared as a virtual function.

3. After the code is executed, what are the values ​​of a and b respectively?

// 32位环境下
class Test
{
public:
    int a;
    int b;
    virtual void fun() {}
    Test(int temp1 = 0, int temp2 = 0)
    {
        a=temp1 ;
        b=temp2 ;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
};
int main()
{
    Test obj(5, 10);
    // Changing a and b
    int* pInt = (int*)&obj;
    *(pInt+0) = 100;
    *(pInt+1) = 200;
    cout << "a = " << obj.getA() << endl;
    cout << "b = " << obj.getB() << endl;
    return 0;
}

A. 200 10

B. 5 10

C. 100 200

D. 100 10 

4. When a function of a class is declared as virtual, then the same prototype function in all derived classes of the class _____?

A. Virtual functions are only recognized when they are respecified

B. A virtual function is only virtual when it is respecified as virtual.

C. None of them are virtual functions

D. They are all virtual functions

5. Which of the following statements about the difference between virtual functions and non-virtual functions is incorrect?

A. When the pointer of a subclass accesses a virtual function, it accesses the method of the subclass.

B. When the pointer of a subclass accesses a non-virtual function, it accesses the method of the subclass.

C. The pointer of the parent class accesses the virtual function and accesses the method of the parent class.

D. The pointer of the parent class accesses the non-virtual function and accesses the method of the parent class.

6. The output of the following program:

#include <iostream>
using namespace std;
class A
{
public:
    void print()
    {
        cout << "A:print()";
    }
};
class B: private A
{
public:
    void print()
    {
        cout << "B:print()";
    }
};
class C: public B
{
public:
    void print()
    {    
        A:: print();
    }
};
int main()
{
    C b;
    b.print();
    return 0;
}

A. A:print()

B. B:print()

C. Compilation error

7. Which of the following descriptions about C++ is correct ()

A. Any pointer must point to an instance

B. Subclass pointers cannot point to parent class instances

C. Any reference must point to an instance

D. The instance pointed to by the reference cannot be invalid

8. The following description of polymorphism is incorrect ()

A. Polymorphism in C++ language is divided into compile-time polymorphism and run-time polymorphism.

B. Compile-time polymorphism can be achieved through function overloading

C. Runtime polymorphism can be achieved through templates and virtual functions

D. The mechanism for achieving runtime polymorphism is called dynamic binding

9. Write the output of the following program

class A
{
public:
    void FuncA()
    {
        printf( "FuncA called\n" );
    }
    virtual void FuncB()
    {
        printf( "FuncB called\n" );
    }
};
class B : public A
{
public:
    void FuncA()
    {
        A::FuncA();
        printf( "FuncAB called\n" );
    }
    virtual void FuncB()
    {
        printf( "FuncBB called\n" );
    }
};
void main( void )
{
     B b;
     A *pa;
     pa = &b;
     A *pa2 = new A;
     pa->FuncA(); ( 3)
     pa->FuncB(); ( 4)
     pa2->FuncA(); ( 5)
     pa2->FuncB();
     delete pa2;
     return 0;
}

A. FuncA called FuncB called FuncA called FuncB called

B. FuncA called FuncBB called FuncA called FuncB called

C. FuncA called FuncBB called FuncAB called FuncBB called

D. FuncAB called FuncBB called FuncA called FuncB called

10. In a 32-bit environment, what is the output of the above program?

#include<iostream>
using namespace std;
class Base
{
public:
    virtual int foo(int x)
    {
        return x * 10;
    }
    int foo(char x[14])
    {
        return sizeof(x) + 10;
    }
};
class Derived: public Base
{
    int foo(int x)
    {
        return x * 20;
    }
    virtual int foo(char x[10])
    {
        return sizeof(x) + 20;
    }
} ;
int main()
{
    Derived stDerived;
    Base *pstBase = &stDerived;
    char x[10];
    printf("%d\n", pstBase->foo(100) + pstBase->foo(x));
    return 0;
}

A. 2000
B. 2004
C. 2014
D. 2024

2. Programming questions

1. Complete number calculation   question link

2. Poker card size   question link

3. Solutions to multiple choice questions 

1. The output result of the following C++ code is ()

#include <iostream>
class Base
{
public:
    int Bar(char x)
    {
        return (int)(x);
    }
    virtual int Bar(int x)
    {
        return (2 * x);
    }
};
class Derived : public Base
{
public:
    int Bar(char x)
    {
        return (int)(-x);
    }
    int Bar(int x)
    {
        return (x / 2);
    }
};
int main(void)
{
    Derived Obj;
    Base *pObj = &Obj;
    printf("%d,", pObj->Bar((char)(100)));
    printf("%d,", pObj->Bar(100));
    return 0;
 }

A. 100,-100

B. 100,50

C. 200,-100

D. 200,50

Correct answer: B

answer:

         First, we observe the topic. There are two functions called bar in the parent class and the subclass. The one with int as a formal parameter is a virtual function and has been rewritten. The one with char as a formal parameter forms an overload with this virtual function; we In the main function, we instantiate a subclass object and point to it with the parent class pointer. Then we call the interface of the parent char class and return 100; then we call the int interface, which is declared as a virtual function. , and it is called using the pointer of the parent class, which constitutes polymorphism, but the pointer originally points to a subclass object, so the virtual function of the subclass is called here and returns and prints 50; so choose B;

2. The correct description of the function is ()

A. A virtual function is a static function

B. The virtual function of the derived class and the virtual function of the base class have different number and types of parameters.

C. A virtual function is a non-member function

D. After a virtual function is declared in the base class, the corresponding function in the derived class does not need to be declared as a virtual function.

Correct answer: D

answer:

         Option A, the virtual function cannot be a static member function, because the static member function does not have this pointer; Option B, the rewriting of the virtual function must satisfy the same return value, the same function name, and the same function parameters (except for covariance and destruction); Option C, virtual functions must be member functions, non-member functions do not have this pointers; Option D, correct;

3. After the code is executed, what are the values ​​of a and b respectively?

class Test
{
public:
    int a;
    int b;
    virtual void fun() {}
    Test(int temp1 = 0, int temp2 = 0)
    {
        a=temp1 ;
        b=temp2 ;
    }
    int getA()
    {
        return a;
    }
    int getB()
    {
        return b;
    }
};
int main()
{
    Test obj(5, 10);
    // Changing a and b
    int* pInt = (int*)&obj;
    *(pInt+0) = 100;
    *(pInt+1) = 200;
    cout << "a = " << obj.getA() << endl;
    cout << "b = " << obj.getB() << endl;
    return 0;
}

A. 200 10

B. 5 10

C. 100 200

D. 100 10 

Correct answer: A

answer:

         There is a small pitfall here. Many people may choose option C. This question actually has a virtual function. Since the virtual function is declared in the class, there must be a virtual table pointer, and the virtual table pointer is generally placed at the beginning of the class. location; therefore we take the address of the class and cast it to int* type, which points to the virtual table pointer at the beginning. We first change the virtual table pointer to 100; then change the second element a to 200; initialize b in the construction is 10; therefore, choose A for this question;

4. When a function of a class is declared as virtual, then the same prototype function in all derived classes of the class _____?

A. Virtual functions are only recognized when they are respecified

B. A virtual function is only virtual when it is respecified as virtual.

C. None of them are virtual functions

D. They are all virtual functions

Correct answer: D

answer:

         As long as it is declared as a virtual function in the parent class, then all functions of the same type in its subclasses will be virtual functions;

5. Which of the following statements about the difference between virtual functions and non-virtual functions is incorrect?

A. When the pointer of a subclass accesses a virtual function, it accesses the method of the subclass.

B. When the pointer of a subclass accesses a non-virtual function, it accesses the method of the subclass.

C. The pointer of the parent class accesses the virtual function and accesses the method of the parent class.

D. The pointer of the parent class accesses the non-virtual function and accesses the method of the parent class.

Correct answer: C

answer:

         Option C says that the pointer of the parent class accessing the virtual function must be the method of accessing the parent class. In fact, it is not necessarily the case. What if it constitutes polymorphism? Possible access to functions of the same type in subclasses;

6. The output of the following program:

#include <iostream>
using namespace std;
class A
{
public:
    void print()
    {
        cout << "A:print()";
    }
};
class B: private A
{
public:
    void print()
    {
        cout << "B:print()";
    }
};
class C: public B
{
public:
    void print()
    {    
        A:: print();
    }
};
int main()
{
    C b;
    b.print();
    return 0;
}

A. A:print()

B. B:print()

C. Compilation error

Correct answer: C

answer:

         Observe carefully, when B inherits the parent class, it is a private inheritance method and is invisible to the subclass. Therefore, the print function of class A cannot be called in class C. Therefore, there is a compilation error. Select C;

7. Which of the following descriptions about C++ is correct ()

A. Any pointer must point to an instance

B. Subclass pointers cannot point to parent class instances

C. Any reference must point to an instance

D. The instance pointed to by the reference cannot be invalid

Correct answer: C

answer:

         Option A, you can have a pointer to null; Option B, you can point the subclass pointer to the parent class through forced type conversion or dynamic_cast; Option C, the description is correct; Option D, if the referenced object space is dynamically applied for on the heap , then when the space is destructed, the instance pointed to by the reference is invalid;

8. The following description of polymorphism is incorrect ()

A. Polymorphism in C++ language is divided into compile-time polymorphism and run-time polymorphism.

B. Compile-time polymorphism can be achieved through function overloading

C. Runtime polymorphism can be achieved through templates and virtual functions

D. The mechanism for achieving runtime polymorphism is called dynamic binding

Correct answer: C

answer:

         Option C, templates have nothing to do with polymorphism;

9. Write the output of the following program

class A
{
public:
    void FuncA()
    {
        printf( "FuncA called\n" );
    }
    virtual void FuncB()
    {
        printf( "FuncB called\n" );
    }
};
class B : public A
{
public:
    void FuncA()
    {
        A::FuncA();
        printf( "FuncAB called\n" );
    }
    virtual void FuncB()
    {
        printf( "FuncBB called\n" );
    }
};
void main( void )
{
     B b;
     A *pa;
     pa = &b;
     A *pa2 = new A;
     pa->FuncA(); ( 3)
     pa->FuncB(); ( 4)
     pa2->FuncA(); ( 5)
     pa2->FuncB();
     delete pa2;
     return 0;
}

A. FuncA called FuncB called FuncA called FuncB called

B. FuncA called FuncBB called FuncA called FuncB called

C. FuncA called FuncBB called FuncAB called FuncBB called

D. FuncAB called FuncBB called FuncA called FuncB called

Correct answer: B

answer:

         First, observe the title. FuncA is not a virtual function. In the subclass, FuncA of the parent class is hidden; FuncB is a virtual function and has been rewritten. First, pa is an A-type pointer, pointing to the subclass, and pa2 is also an A-type pointer. , pointing to the parent class; when calling FuncA and FuncB respectively, when pa calls the FuncA function, it calls FuncA in class A. When calling FuncB, it constitutes polymorphism and calls FuncB of the subclass. Then pa2 itself points to the parent class, so calling FuncA and FuncB always calls the parent class; so choose B;

10. In a 32-bit environment, what is the output of the above program?

#include<iostream>
using namespace std;
class Base
{
public:
    virtual int foo(int x)
    {
        return x * 10;
    }
    int foo(char x[14])
    {
        return sizeof(x) + 10;
    }
};
class Derived: public Base
{
    int foo(int x)
    {
        return x * 20;
    }
    virtual int foo(char x[10])
    {
        return sizeof(x) + 20;
    }
} ;
int main()
{
    Derived stDerived;
    Base *pstBase = &stDerived;
    char x[10];
    printf("%d\n", pstBase->foo(100) + pstBase->foo(x));
    return 0;
}

A. 2000
B. 2004
C. 2014
D. 2024

Correct answer: C

answer:

        The function declaration in this question is very similar to our first question; the two foo functions in the parent class and the subclass are each overloaded. The foo function of the int interface in the parent class is defined as a virtual function and is defined in the subclass. The rewriting is completed; then we look at the main function, which defines a subclass and uses the parent class pointer to point to the subclass; in the printf function, we first pass in an integer and call the int interface of the subclass The foo function constitutes polymorphism; it returns 2000; the second one passes in a character array and calls the foo function of the parent class character array interface. sizeof calculates 4+10 and returns 14 (I don’t know why it is 4 You can see C language pointer enhancement ); so choose C;

4. Programming problem solving

1. Complete number calculation

Idea: We traverse from 1 to n in sequence and record the complete number. There is no skill;

#include <iostream>
using namespace std;

bool is_absolute(int num)
{
    int sum = 0;
    for(int i = 1; i < num; i++)
    {
        if(num % i == 0)
            sum += i;
    }
    if(num == sum)
        return true;
    else
        return false;
}

int main() 
{
    int n;
    cin >> n;
    int count = 0;
    for(int i = 1; i <= n; i++)
    {
        if(is_absolute(i))
            count++;
    }
    cout << count << endl;
    return 0;
}

2. Poker card size

Idea: This question mainly examines the use of string, and there is no algorithm. We first determine whether there is a king bomb, and return directly if there is one; then continue to filter, first we calculate the first card and the number of cards in the two hands respectively; Then use string to save the comparison order, and then if the number of cards in the hands is the same, directly compare the position of the first card in the sequence again; if the number of cards in the hands is not the same, check whether one side has a bomb, and if so, return the bomb to the hand of the other side. If not, return ERROR;

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

string find_max(string& cards)
{
    if(cards.find("joker JOKER") != string::npos)
        return "joker JOKER";
    // 分离两手牌
    int pos = cards.find('-');
    string play1 = cards.substr(0, pos);
    string play2 = cards.substr(pos + 1);
    // 统计两手牌个数
    int card_num1 = count(play1.begin(), play1.end(), ' ') + 1;
    int card_num2 = count(play2.begin(), play2.end(), ' ') + 1;
    // 统计他两第一张牌
    string card1 = play1.substr(0, play1.find(' '));
    string card2 = play2.substr(0, play2.find(' '));
    // 手牌比较依据数组
    string str = "345678910JQKA2jokerJOKER";
    if(card_num1 == card_num2)
    {
        if(str.find(card1) > str.find(card2))
            return play1;
        else
            return play2;
    }
    else 
    {
        if(card_num1 == 4)
            return play1;
        else if(card_num2 == 4)
            return play2;
        else
            return "ERROR";
    }
}

int main() 
{
    string cards;
    getline(cin, cards);
    cout << find_max(cards) << endl;
    return 0;
}

Guess you like

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