C++ Final Exam Program Analysis Questions 30 Questions

1. The result of the following program is ____

   #include<iostream>

   using namespace std;

   class Base

{public:

   Base(){ cout << "0"; }

};

Class Base1: virtual Base

{public:

   Base1(){ cout << "1"; }

};

Class Base2: virtual Base

{public:

   Base2(){ cout << "2"; }

};

class Derived: public Base1, public Base2

{public:

   Derived(){ cout << "3"; }

};

void main()

{  Derived obj;

}

A. 0123      B. 3120      C. 0312       D. 3012

Answer: A

 

2. Write the result of the program running

#include<iostream>

   using namespace std;

   int i = 0;

   void fun()

   {

       {

           static int i = 1;

           std::cout << i++ << ',';

       }

       std::cout << i << ',';

}

int main()

{

    fun();    fun();

    return 0;

}

Answer: 1,0,2,0,

 

3. The underlined sentence in the following program should be filled in as ____

   class Base

{public:

   void fun(){ cout << "Base of fun" << endl; }

};

class Derived: public Base

{public:

   void fun()

   {

      ______ //Call the member function fun() of the base class

      cout << "Derived of fun" << endl;

}

};

A. fun();                    B. Base.fun();   

C. Base::fun();          D. Base->fun() ;

Answer: C

 

4. Write out the program running results

#include<iostream>

   using namespace std;

   class Base

{public:

   int x;

   void Print(){ cout << "Base::x is " << x << endl; }

};

class Derived1: public Base

{public:

   void Print()

{  cout << "Derived1::x is " << x << endl;

}

};

class Derived2: private Base

{public:

   Derived2(){ Base::x = 4; }

   int x;

   void Print()

{  cout << "Derived2's Base::x is " << Base::x << endl;

   cout << "Derived2::x is " << x << endl;

}

};

void main()

{  Base objB;

   Derived1 objD1;

   Derived2 objD2;

objD2.x = 1 + (objD1.x = 1 + (objB.x = 1));

objB.Print();

objD1.Print();

objD2.Print();

}

Answer: Base::x is 1

           Derived1::x is 2

           Derived2’s Base::x is 4

           Derived2::x is 3

 

5. Write out the program running results

   #include<iostream>

   using namespace std;

   class A

{public:

    A(){ cout << "constructor of A" << endl; }

    ~A(){ cout << "destructor of A" << endl; }

};

class B

{public:

    B(){ cout << "constructor of B" << endl; }

    ~B(){ cout << "destructor of B" << endl; }

};

int main()

{   B b;

    static A a;

   }

Answer: constructor of B

           constructor of A

           constructor of B

           constructor of A

 

6. Write the result of the program running

   #include<iostream>

   using namespace std;

   class Coord

{public:

   Coord(int i = 0, int j = 0){ x = i; y = j; }

   void Print(){ cout << "x=" << x << ", y=" << y << endl; }

   friend Coord operator++(Coord op);

private:

   int x, y;

};

Coord operator ++(Coord op)

{ ++op.x;

   ++op.y;

   return op;

}

void main()

{  Coord obj(1, 2);

   obj.Print();

   ++obj;

   obj.Print();

}

Answer: x = 1, y = 2

           x = 1, y = 2

 

7. Write out the program running results

#include <iostream>

using namespace std;

class Expt{};

class Expt1: public Expt{

public: Expt1() { cout<<"The constructor of Expt1 is called."<<endl; }

Expt1(Expt1 & e) {cout<<"The copy constructor of Expt1 is called."<<endl; }

~Expt1() { cout<<"The destructor of Expt1 was called."<<endl; }

};

class Expt2: public Expt1 {

public: Expt2() { cout<<"The constructor of Expt2 is called."<<endl; }

Expt2(Expt2 & e) { cout<<"The copy constructor of Expt2 is called."<<endl; }

~Expt2() { cout<<"Expt2's destructor was called."<<endl; }

};

void MyFunc() {    Expt1 e; throw e; }

void main() {

    try  {

        cout << "In the try block, call MyFunc()." << endl;

        MyFunc();

        cout << "In the try block, MyFunc() execution ends." << endl;

    }

    catch( Expt E ) { cout << "Caught an exception of type Expt."<<endl; }

catch( Expt2 E ) { cout << "Caught an exception of type Expt2."<<endl; }

    cout << "Back to main function." << endl;

}

Answer: In the try block, call MyFunc().

           The constructor of Expt1 is called.

           Expt1 's copy constructor is called.

           The destructor of Expt1 is called.

           An exception of type Expt was caught.

           The destructor of Expt1 is called.

           Back to the main function.

 

8. What is wrong with the following program?

   class MyClass

{

public:

    void MyClass(int a){ x = a; }

    int f(int a, int b){ x = a; y = b; }

    int f(int a, int b, int c = 0){ x = a; y = b; z = c; }

    static void g(){ x = 10; };

private:

    int x, y, z;

};

Answer: Constructors cannot have return values ​​and return value types; static function g cannot directly access non-static data member x.

 

9. Write out the program running results

#include<iostream>

using namespace std;

class Shape{

public:

Shape() { cout<<"Shape's constructor has been called."<<endl; }

void Display() { cout<<"Shape's display function is called."<<endl; }

virtual ~Shape() { cout<<"Shape's destructor was called."<<endl; }

};

class Rectangle: public Shape {

public: Rectangle () {

Side=Hight=0;     

   cout<<"Rectangle's default constructor is called."<<endl;     

   }

            Rectangle(int xx,int yy)   {

   Side=xx;   Hight=yy;    

   cout<< "The constructor of Rectangle is called."<<endl;     

}

            void Display() {

cout<<" The side length and height of Rectangle are: "<<Side<<","<<Height<<endl;

}

    ~Rectangle() { cout<<"Rectangle's destructor was called."<<endl; }

         int GetSide() {return Side;}

         int GetHight() {return Hight;}

 private:

       int  Side, Hight;

};

void main()

{

     Shape *ptr1 = new Rectangle[2];

     (*(ptr1+1)).Display();

 delete [] ptr1;

}

Answer: Shape's constructor is called.

           Rectangle's default constructor is called.

           Shape's constructor is called.

           Rectangle's default constructor is called.

           Shape's display function is called.

           Rectangle's destructor is called.

           Shape's destructor is called.

           Rectangle's destructor is called.

           Shape's destructor is called.

 

10. Write the result of the program running

   #include<iostream>

   using namespace std;

   template<class T>

   class Sample

{private:

   T n;

public:

   Sample(T i){ n = i; }

   void operator++();

void disp(){ cout << "n = " << n << endl; }

};

template<class T>

void Sample<T>::operator++()

{  n += 1;

}

void main()

{  Sample<char> s('a');

   s++;

   s.disp();

}

Answer: n = b

 

11. Write the result of the program running

#include <iostream>

using namespace std;

char* increase(char* ch) {return ++ch;}

char& increase(char& ch) {return ++ch;}

int main(){

char buf[16] = "IloveC++!";

char* p = buf;

for (int i = 0; i < 4; i++) {

p = increase(p);

increase(*p);

}

cout << buf << endl;

return 0;

}

Answer: Impwf C++!

 

12. Write out the program running results

 #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    Sample(int a = 0, int b = 0){ x = a; y = b; }

    friend void square_x(Sample &s){ s.x = s.x * s.x; }

    void disp(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    Sample s(2, 3);

    square_x(s);

    s.disp();

}

Answer: x = 4, y = 3

 

13. Write out the program running results

   #include<iostream>

   using namespace std;

   class A

{

    int whether;

public:

    A(int i){ num = i; }

    A(A &a){ num = a.num++; }

    void print(){ cout << num; }

};

void main()

{

    A a(1), b(a);

    a.print();

    b.print();

}

Answer: 21

 

14. Write the program running results

   #include<iostream>

   using namespace std;

   class Base

   {public:

      int n;

      Base(int x){ n = x; }

      virtual void set(int m){ n = m; cout << n << ' '; }

   };

   class DerivedA: public Base

{public:

   DerivedA(int x): Base(x){  }

   void set(int m){ n += m; cout << n << ' '; }

};

class DerivedB: public Base

{public:

   DerivedB(int x): Base(x){  }

   void set(int m){ n += m; cout << n << ' '; }

};

int main()

{ DerivedA da(1);

   DerivedB db(3);

   Base * pBase;

   pBase = &da;

   pBase->set(1);

   pBase = &db;

   pBase->set(2);

}

Answer: 2 5

 

15. Write out the program running results

#include <iostream>

using namespace std ;

class Base

{public:

virtual void fun(int a) { cout << "Base: " << a << endl; }

void fun(char *p) { cout << "Base: " << p << endl; }

void fun() { cout << "Base: no parameter!" << endl; }

};

class Derived: public Base

{public:

void fun(int a) { cout << "Derived:" << a << endl; }

void fun(char *p) { cout << "Derived:" << p << endl; }

};

void main()

{ Derived* pd = new Derived;

Base *pb = pd;

pb->fun(); pb->fun(0); pb->fun("c++");

pd->fun(1); pd->fun("object");

}

Answer: Base: no parameter!

           Derived:0

           Base: c++

           Derived:1

           Derived:object

 

16. Write the result of the program running

   #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    Sample(int a = 0, int b = 0){ x = a; y = b; }

    ~Sample(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    Sample s;

    s.~Sample();

}

Answer: x = 0, y = 0

           x = 0, y = 0

 

17. Write the program running results

   #include<iostream>

   Using namespace std;

   class First

{protected:

   int first;

public:

   First(int f = 0){ first = f; }

   void show(){ cout << first << endl; }

};

class Second: public First

{private:

   int second;

public:

   Second(int f, int s): First(f), second(s) {  }

   void show(){ cout << first << " " << second << endl; }

};

int main()

{

   Second s(5, 3);

   s.show();

}

A. 5 3             B. 3           C. 5             D. 3 5

Answer: A

 

18. Write the result of the program running

   #include<iostream>

   using namespace std;

   class Base

   {int n;

   public:

      Base(int a)

      {  cout << "constructing base class" << endl;

         n = a;

         cout << "n = " << n << endl;

      }

      ~Base(){ cout << "destructing base class" << endl; }

   };

   class Derived: public Base

   {  Base base;

      int m;

   public:

      Derived(int a, int b, int c) : Base(a), base(c)

      {  cout << "constructing derived class" << endl;

         m = b;

         cout << "m = " << m << endl;

}

      ~Derived(){ cout << "destructing derived class" << endl; }

};

void main(){ Derived d(1, 2, 3); }

答案:constructing base class

           n = 1

           constructing base class

           n = 3

           constructing derived class

           m = 2

           destructing derived class

           destructing base class

           destructing base class

 

19. Write the result of the program running

#include <iostream>

using namespace std;

class B1 {

public: B1(int i) {cout<<"The constructor of B1 is called:"<<i<<endl;}

~B1() {cout<<"The destructor of B1 was called."<<endl;}

};

class B2 {

public: B2(int j) {cout<<"The constructor of B2 is called:"<<j<<endl;}

~B2() {cout<<"The destructor of B2 was called."<<endl;}

};

class C: public B2, public B1 {

public:  

C(int a, int b, int c, int d):B1(a),memberB2(d),memberB1(c),B2(b) {

cout<<"The constructor of C is called."<<endl;

 }

~C() { cout<<"C's destructor was called."<<endl;}

private:  

B1 memberB1;  B2 memberB2;

};

void main(){

C obj(1,2,3,4); 

}

Answer: B2's constructor is called: 2

           The constructor of B1 is called: 1

           B1's constructor is called: 3

           B2's constructor is called: 4

           C's constructor is called.

           C's destructor is called.

           B2's destructor is called.

           The destructor of B1 is called.

           The destructor of B1 is called.

           B2's destructor is called.

 

20. Write out the program running results

#include<iostream>

using namespace std;

void func(int a)

{

    static int m = 0;

    m += a;

    cout << m << " ";

}

void main()

{

    int k = 6;

    func(k);

    func(k);

    cout << endl;

}

Answer: 6 12

 

21. The output of the following program is____

   #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    Sample(int a = 0, int b = 0){ x = a; y = b; }

    ~Sample(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    Sample s;

    exit(0);

}

Answer: (no output)

 

22. Write the result of the program running

#include <iostream>

using namespace std;

class point {

protected:

int x, y;

public:  

point(int xx, int yy) {

x = xx, y = yy;

cout << "construct point: " << x << "," << y << endl;

}

~point() { cout << "destroy point" << endl;}

};

class circle {

protected:

point center;

int   radius;

public:  

circle(int x, int y, int r):center(x, y),radius(r) {

cout << "construct circle: " << radius << endl;

  }

~circle(){ cout << "destroy circle " << endl;}

};

int main() {

circle c(1,2,3);

return 0;

Answer: construct point: 1,2

           construct circle:3

           destroy circle

           destroy point

 

23. The execution result of the following program is:

#include<iostream>

using namespace std;

class Sample

{  int x;

public:

    Sample(int a)

    {  x = a;

        cout << "constructing object: x = " << x << endl;

    }

};

void fun(int n)

{  static Sample obj(n);

}

void main()

{  fun(1); fun(10);

}

答案:constructing object: x = 1

 

24. The execution result of the following program is ____

   #include<iostream>

   using namespace std;

   class Sample

{

public:

    int x, y;

    void disp(){ cout << "x = " << x << ", y = " << y << endl; }

};

void main()

{

    int Sample::*pc;

    Sample s;

    pc = &Sample::x;

    s.*pc = 10;

    pc = &Sample::y;

    s.*pc = 20;

    s.disp();

}

Answer: x = 10, y = 20

 

25. There are the following procedures:

   #include<iostream>

   using namespace std;

   class MyClass

{

   public:

       MyClass(int n){ number = n; }

       MyClass(MyClass & other){ number = other.number; }

       ~MyClass(){}

   private:

       int number;

};

   MyClass fun(MyClass p)

   {

       MyClass temp(p);

       return temp;

   }

   int main()

   {

       MyClass obj1(10), obj2(0);

       MyClass obj3(obj1);

       obj2 = fun(obj3);

       return 0;

   }

When the program is executed, how many times is the copy constructor of MyClass called?

Answer: 4 times

 

26. Has the following definitions:

   class Point

{

public:

    Point(int x = 0, int y = 0){ _x = x; _y = y; }

    void Move(int xOff, int yOff)

    { _x += xOff; _y += yOff; }

    void Print() const

    { cout << '(' << _x << ',' << _y << ')' << endl; }

private:

    int _x, _y;

};

A compilation error will occur in the following statement is ____

A. Point pt;    pt.Print();

B. const Point pt;    pt.Print();

C. Point pt;    pt.Move(1, 2);

D. const Point pt;    pt.Move(1, 2);

Answer: D

 

27. Write the result of the program running

  #include<iostream>

using namespace std;

char *x[] = {"first", "second", "third"};

void fun(char *z[])

{

    cout << *++z << endl;

}

int main()

{

    char **y;

    y = x;

    fun(y);

    return 0;

}

Answer: second

 

28. Write the result of the program running

#include <iostream>

using namespace std;

class counter {

protected:

static int count;

int id;

public:

counter() {

id = ++ count;

cout << "id = " << id << ", count = " << count << endl;

}

};

int counter::count = 0;

static counter c[3];

int main() {

cout << "initialize program" << endl;

return 0;

}

Answer: id=1, count=1

           id=2,count=2

           id=3,count=3

           initialize program

 

29. Write the result of the program running

#include <iostream>

using namespace std;

class Expt{};

class Expt1: public Expt{

public: Expt1() { cout<<"The constructor of Expt1 is called."<<endl; }

Expt1(Expt1 & e) {cout<<"The copy constructor of Expt1 is called."<<endl; }

~Expt1() { cout<<"The destructor of Expt1 was called."<<endl; }

};

class Expt2: public Expt1 {

public: Expt2() { cout<<"The constructor of Expt2 is called."<<endl; }

Expt2(Expt2 & e) { cout<<"The copy constructor of Expt2 is called."<<endl; }

~Expt2() { cout<<"Expt2's destructor was called."<<endl; }

};

void MyFunc() {    Expt1 e; throw e; }

void main() {

    try  {

        cout << "In the try block, call MyFunc()." << endl;

        MyFunc();

        cout << "In the try block, MyFunc() execution ends." << endl;

    }

    catch( Expt E ) { cout << "Caught an exception of type Expt."<<endl; }

catch( Expt2 E ) { cout << "Caught an exception of type Expt2."<<endl; }

    cout << "Back to main function." << endl;

}

Answer: In the try block, call MyFunc().

           The constructor of Expt1 is called.

           Expt1 's copy constructor is called.

           The destructor of Expt1 is called.

           An exception of type Expt was caught.

           The destructor of Expt1 is called.

           Back to the main function.

 

30. There is the declaration of the dynamic array template class Array as follows, please fill in the most appropriate content in the blank.

_(1)_ <class T>

class Array

{protected:

    int size; //The length of the dynamic array

     (2)  elements; //The first address of the dynamic array

public:

    Array(int i);

     (3)  ~Array(); //destructor

    Array(  (4)  a); // copy constructor

     (5)  operator[](int index); // overload subscript operator

     (6)  ostream & operator <<(ostream & o, Array & a);

};

(1) Blank (1) should be filled in 【 】

A. virtual    B. template     C. void     D. class

(2) Blank (2) should be filled in 【 】

A. T *         B. T             C. int *    D. int

(3) Blank (3) should be filled in 【 】

A. void     B. int        C. virtual   D. template

(4) Blank (4) should be filled in 【 】

A. Array    B. Array *   C. Array &   D. const Array &

(5) Blank (5) should be filled in 【 】

A. T         B. T *        C. T &       D. void

(6) Blank (6) should be filled in 【 】

A. friend   B. const     C. virtual   D. void

答案:B A C D C A

 

 

Guess you like

Origin blog.csdn.net/m0_63947712/article/details/128336468