Multiple choice questions and answers related to the definition of C++ classes

1. There are the following procedures:

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

Shōkai Takude ()【Answer: D】
A CBA
B BAC
C ACB a>
D ABC

2. The following ( ) is not a characteristic of a constructor [Answer: D]
A The function name of the constructor is the same as the class name
B The constructor Can be overloaded
C The constructor can set default parameters
D The constructor must specify a type specification

3. The incorrect statement about the constructor is ( ) [Answer: D]
A The name of the constructor is the same as the name of the class
B The constructor is automatically executed when declaring class variables
C The constructor does not have any function type
D There is only one constructor

4. The constructor of a class is automatically called and executed when ( ) of the class is defined [Answer: C]
A member function
B Data member
C Object
D Friend function

5. There are the following class statements:

class base{
       int x;
public:
       base(int n){ x=n;}
};
class derived: public base{
       int y;
public:
       derived(int a,int b);
};

For the following definition of the derived constructor, the correct one is [Answer: A].

(a) derived::derived(int a,int b):base(a),y(b){}

(b) derived::derived(int a,int b):x(a),y(b){}

(c) derived::derived(int a,int b):base(a),derived(b){}

(d) derived::derived(int a,int b):x(a),derived(b){}

6. There are the following procedures:

#include<iostream>
using namespace std;
class BASE{
public:
~BASE(){cout<<"BASE";}
};
class DERIVED:public BASE{
public:
~DERIVED(){cout<<"DERIVED";}
};
int main(){DERIVED x; return 0;}

The output result of executing the program is () [Answer: D]

A BASE
B DERIVED
C BASEDERIVED
D DERIVEDBASE

7. ( ) is the characteristic of a destructor [Answer: A]
A Only one destructor can be defined in a class
B The destructor name has nothing to do with the class name
C The destructor can only be defined within the class body
D The destructor can have one or more parameters

8. For any class, the number of destructors is at most ( ) [Answer: B]
A 0
B 1< /span> D 3
C 2

9. Among the following function prototypes, the one that can be used as the constructor of class AA is [Answer: D]
A void AA(int)
B int AA()
C AA(int)const
D AA(int)

10、、

class Point{
    int x_,y_;
    public:
    Point():x_(0),y_(0){}
    Point(int x,int y=0):x_(x),y_(y){}
};

If the statement is executed:

Point a(2),b[3],*c = new Point(3);

Then the number of times the constructor of the Point class is called is () [Answer: D]
A twice
B three times
C four times
D five times

11. In the case of public derivation, the member functions defined in the derived class can only access the original base class [Answer: C]
A public members and private members a>
B Private members and protected members
C Public members and protected members
D Private members, protected members and public members

12. The following statement is incorrect: [Answer: A]
A The protected members of the base class are still protected in the derived class
B The protected members of the base class are still protected in the public derived class
C The protected members of the base class are still private in the private derived class
D For the base Access to class members must be unambiguous

13. The following statement is incorrect: [Answer: D]
A In order to make full use of existing classes, derived classes generally use public derivation
B Access to base class members must be unambiguous
C Assignment compatibility rules also apply to multiple inheritance situations
D Public members of the base class are Still public in derived classes

14. In the following class definition, the wrong statement is ( )

class sample
{
  public:
  sample( int val);  //①
~  sample();      //②
private:
      int a=2. 5;       //③
sample();        //④

【 答案: C】
A ①②③④
B ②
C ③④
D ①②③

15. There are the following class definitions:

class SAMPLE
{
      int n;
Public:
      SAMPLE(int i=0):n(i){}
      void setValue(int n0);
};

The following implementation of the setValue member function is correct (). 【Answer: B】
A SAMEPLE::setValue(int n0){n=n0;}
B void SAMEPLE::setValue(int n0){n =n0;}
C void setValue(int n0){n=n0;}
D setValue(int n0){n=n0;}

16、

#include<iostream>
using namespace std;
class Base{
protected:
Base(){cout<<'A';}
Base(char c){cout<<c;}
};

class Derived:public Base{
public:
Derived(char c){cout<<c;}
};
int main()
{
Derived d1('B');
return 0;
}

When executing this program, the screen will display [Answer: C]
A B
B BA
C AB
D BB

17、

#include<iostream>
using namespace std;
class Base
{
private:
    void fun1()const{cout<<"fun1";}
protected:
    void fun2()const{cout<<"fun2";}
public: 
    void fun3()const{cout<<"fun3";}
} ;
class Derived:protected Base
{
publc:
    void fun4()const{cout<<"fun4";}
};
int main()
{
    Derived obj;
    obj.fun1();     //①
    obj.fun2();     //②
    obj.fun3();     //③
    obj.fun4();     //④
    return 0;
}

That's the language of the language ( ). [Answer: B]
A ①②③④
B ①②③
C ②③④
D ①④< /span>

18. There are the following class definitions:

class XA
{
int x;
public:
XA(int n){x=n;}
};
class XB:public:XA
{
int y;
public:
XB{int a,int b};
};

【 答案: B】
A XB::XB(int a,int b):x(a),y(b){}
B XB::XB(int a,int b):XA(a),y(b){}
C XB::XB(int a,int b):x(a),XB(b){}
D XB::XB(int a,int b):XA(a),XB(b){}

19. () cannot be included in the initialization list of the derived class constructor [Answer: C]
A The constructor of the base class
B Initialization of base class object members
C Initialization of derived class object members
D Initialization of new data members in derived classes

20. When a derived class object ends its life cycle () [Answer: A]
A First call the destructor of the derived class, and then call the destructor of the base class Function
B First calls the destructor of the base class, then calls the destructor of the derived class
C If the base does not define a destructor, only the derived class is called Class destructor
D If the derived class does not define a destructor, only the base class destructor will be called

21. There are the following procedures:

#inlcude<iostream>
using namespace std;
class MyClass
{
  public:
     MyClass(){cout<<“A;} 
     MyClass(char c)
     {cout<<c;}
          ~MyClass(){cout<<”B”;}
}
Int main()
{
  MyClass p1,*p2;
    P2=new MyClass(‘X’);
  delete p2;
  return 0;
}

Execute this program and the output ( ) will be displayed on the computer screen. 【Answer: D】
A ABX
B ABXB
C AXB
D AXBB< /span>

22. If there are the following statements:

class MyClass{public:MyClass(){  cout<<1;  }};

Execute the following statement: MyClass a,b[2],*p; the output result of the program is (). 【Answer: B】
A 11
B 111
C 1111
D 11111< /span>

23. There are the following class definitions:

class Test{
    public:Test(){a=0;c=0;}//①
    int f(int a)const{this->a=a;}//②
    static int g(){return a;}//③
    void h(int b){Test::b=b;}//④
private:
    int a;
    static int b;
    const int c;
};

int Test::b=0;

In the line marked with the number, the one that can be compiled correctly is (). 【Answer: D】
A ①
B ②
C ③
D ④< /span>

Supongo que te gusta

Origin blog.csdn.net/weixin_74287172/article/details/134231537
Recomendado
Clasificación