正文

C++ tests and answers (2)

(2024-01-18 05:49:02) 下一個

C AND C++ TESTING  (INTERMEDIATE)

 

1.      Explain the following:

        (1)  struct str *(*block())[8];

        (2)  double (*(*func)())[2][6];

2.      What's object ID?  What's persistent object?     

        How to create persistent object?

3.     What’s the type (span) of a pointer?  In the following code:

class A : public B { … };

A a; B *pb = &a;  A *pa = &a;

What’s difference between pb and pa ?

4.     Why in the iostream library the designer defined “operator void*()” for “cin” ?

  1. In the following codes, in which cases, the j and k are not initialized?

(1)  class C1 { public: int j; };

void C1_ini() { C1 c1; … }

  1. class C2 { public: C2(){} int j; … };

class C3 {public: C2 c2; int k; };

void C3_ini() { C3 c3; … }

  1. typedef struct { int j,k; } S;

S global;

S s_ini() { S local; S *ps = new S; *ps = local; delete ps; return local; }

  1. class S { public: S ( int jj = 0, int kk = 0 ) : j(jj), k(kk) {}

 private: int j, k; };

              S global;

S s_ini() { S local the; S *ps = new S; *ps = local; delete ps; return local; }

  1. What’s  the static initialization problem? How to deal with the static initialization problem?
  2. What’s “placement operator new” ?

What’s wrong in the following code:

A *pa = new ( buffer ) A;

// do something

delete pa;  // want to destroy the object but preserve the storage for reuse

pa = new ( buffer ) A;

  1. What’s namespace?
  2. What’s NRV optimization?  How to turn on NRV optimization?
  3. What’s difference between “Dynamic casts” and “Static casts”?
  4. We have:

class A { public: float x, y, z; … };

A a;

What’s difference between  “& A::z” and “& a.z”?

Assume we have a code:

float A::*p1 = 0;  float A::*p2 = & A::x;

What’s difference between p1 and p2 ?

  1. In the following code:

class A { public:         virtual ~A();

                                   int a();

                                   int b();

                                   virtual int c();  };

What are the following addresses:

       &A::~A;   &A::a;   &A::b;   &A::c;

Why?

  1. Why it is better to write:  delete p;  p = 0; 
  2. What’s wrong in the following code:

class B { public: B(); virtual ~B(); … };

class A : public B { public: A(); ~A(); … };

void main() { B *pb = new A[10];  delete [ ] pb; … }

  1. In the example.h file we have:

extern double fun (double);  // first fun()

template < class type >

class A { public:

                     void fun1() { t = fun (j); }

                     type fun2() { return fun(t) }

                 private: int j; type t;  };

In the example.cpp file we have:

extern int fun (int);  // second fun()

A <int> a;

a.fun1();      

a.fun2();      

The question is which fun() is called when a.fun1() and a.fun2() are called?

  1. The following is the code for using share memory:

void sh_use ( void* p ) {

       A *pa = new A;

       Sh_lock ( p );

       // ….

       Sh_unlock (p );

       delete pa;                           

}

How to add Exception Handling (EH) in above code?  How to  use auto_ptr object to

improve EH coding?

  1. What’s CRC?  What’s OMT? What’s CORBA?
  2. How to discover objects when designing a programming system?  What are basic

rules used to figure out objects when designing a programming system?

  1. In developing C++ programming there are three models: (1) Procedural Model, (2) Abstract

Data Type (ADT) Model (Object-Based Model) and (3) Object-Oriented (OO) Model

(Polymorphism).  What are differences between them?

  1. Explain the following keywords in C++:  explicit and mutable.
  2. What’s difference between “abstract const” and “bit const”?
  3. What’s smart pointer?  What’s reference counting?
  4. Assume we have a member function declaration:

void Stack::push(char);

How to declare a pointer to the member function?  How to initialize the pointer?

How to call the member function through the pointer?

  1.  What’s  binary compatibility?
  2. Explain:  ifstream  dataFile(“ints.dat”);  list<int> data(istream_iterator<int>(dataFile), istream_iterator<int>());

 

C AND C++ TESTING  (INTERMEDIATE)  WITH ANSWERS

 

1.      Explain the following:

        (1)  struct str *(*block())[8];

        (2)  double (*(*func)())[2][6];

 

2.      What's object ID?  What's persistent object?     

        How to create persistent object?

 

3.     What’s the type (span) of a pointer?  In the following code:

class A : public B { … };

A a; B *pb = &a;  A *pa = &a;

What’s difference between pb and pa ?

 

A: The type (span) of a pointer is the different type of  object being addressed.  The type of a pointer instructs the compiler as to how to interpret the memory found at a particular address.

Pb and pa addresses the same first byte of  A object.  The difference is that address span of  pa encompasses the entire A object, while the span of pb encompasses only the B subobject of  A.  pb cannot directly access any members other than those present within the B subobject, except through the virtual mechanism.

 

4.     Why in the iostream library the designer defined “operator void*()” for “cin” ?

A:  The code if  (cin) should evaluate to a true/false scalar value.  So operator int() was defined.  But    the programmer error: cin << intVal  // meant cout, not cin,  will produce:  int temp = cin.operator int(); temp << intVal; To avoid this unexpected behavior, replace the operator int() with operator void*().

 

  1. In the following codes, in which cases, the j and k are not initialized?

(1)  class C1 { public: int j; };

void C1_ini() { C1 c1; … }

  1. class C2 { public: C2(){} int j; … };

class C3 {public: C2 c2; int k; };

void C3_ini() { C3 c3; … }

  1. typedef struct { int j,k; } S;

S global;

S s_ini() { S local; S *ps = new S; *ps = local; delete ps; return local; }

  1. class S { public: S ( int jj = 0, int kk = 0 ) : j(jj), k(kk) {}

 private: int j, k; };

              S global;

S s_ini() { S local; S *ps = new S; *ps = local; delete ps; return local; }

 

A:  (1) and (3).

 

  1.  What’s  the static initialization problem? How to deal with the static initialization problem?

 

A:  In A.h, we have

      Class A {

      Public:

           Void* operator new(size_t) {

                Return pool.alloc();

           }

           Void operator delete(void* p) {

               Pool.free(p);

           }

      Private:

          Static Pool pool;

      };

In A.c, we have

     Pool A::pool(sizeof(A));

At global scope, we have

     A* pa = new A;

A::pool and pa are nonsimple, nonlocal, static object.  A::pool might or might not be constructed before pa is initialized.  This is the static initialization problem.  We can use Init function, Init check, and Init object to deal with the static initialization problem.

 

  1. What’s “placement operator new” ?

What’s wrong in the following code:

A *pa = new ( buffer ) A;

// do something

delete pa;  // want to destroy the object but preserve the storage for reuse

pa = new ( buffer ) A;

 

A:  The placement operator new is a predefined overloaded instance of operator new.  Delete pa applies the destructor, but it also frees the memory addressed by pa.  So, you should use pa->~A instead.

 

  1. What’s namespace?

 

  1. What’s NRV optimization?  How to turn on NRV optimization?

 

A: NRV is named return value optimization.  It is the compiler level optimization.  Usually a class has well designed copy constructor will turn on NRV optimization.

 

  1. What’s difference between “Dynamic casts” and “Static casts”?

 

A:  static_cast <type> (expr) performs well-behaved casts.  It is safer than using C-style cast.  Dynamic_cast <type> (expr) enable you to use safe, standardized, and simple downcasts in runtime.

 

  1. We have:

class A { public: float x, y, z; … };

A a;

What’s difference between  “& A::z” and “& a.z”?

Assume we have a code:

float A::*p1 = 0;  float A::*p2 = & A::x;

What’s difference between p1 and p2 ?

 

A:   & A::z is its offset within the class, whereas a.z is its actual address in memory which is the offset of z (minus 1) plus the beginning address of a.  p2 is the physical offset of x plus 1.

 

  1. In the following code:

class A { public:         virtual ~A();

                                   int a();

                                   int b();

                                   virtual int c();  };

What are the following addresses:

       &A::~A;   &A::a;   &A::b;   &A::c;

Why?

 

A:  &A::~A;  yields 1.   &A::c; yields 2.  Because the address of virtual function is unknown at compile time.  What is known is the function’s associated index into the virtual table. &A::a;  and  &A::b;  yield their actual memory locations since they are not virtual.

 

  1. Why it is better to write:  delete p;  p = 0; 

 

A: The use of p as a pointer to an address  in storage is still well defined if not setting p = 0.

 

  1. What’s wrong in the following code:

class B { public: B(); virtual ~B(); … };

class A : public B { public: A(); ~A(); … };

void main() { B *pb = new A[10];  delete [ ] pb; … }

 

A:  delete [ ] pb; is a problem.  In most case, only ~B() is called when delete [ ] pb;      instead of delete [ ] pb; the following code should be added:

For (int I = 0; I < 10; I ++) {

B *p = & ((A*) pb) [I] ;

Delete p;

}

 

  1. In the example.h file we have:

extern double fun (double);  // first fun()

template < class type >

class A { public:

                     void fun1() { t = fun (j); }

                     type fun2() { return fun(t) }

                 private: int j; type t;  };

In the example.cpp file we have:

extern int fun (int);  // second fun()

A <int> a;

a.fun1();      

a.fun2();      

The question is which fun() is called when a.fun1() and a.fun2() are called?

 

A: When fun1() is called, inside fun1() the first fun() is called because  the invocation of the first fun() is not dependent on the template argument and the invocation must be resolved from the template declaration.  When fun2() is called the second fun() is called because this instance clearly is dependent on the template argument that determines the actual type t.

 

  1. The following is the code for using share memory:

void sh_use ( void* p ) {

       A *pa = new A;

       Sh_lock ( p );

       // ….

       Sh_unlock (p );

       delete pa;                           

}

How to add Exception Handling (EH) in above code?  How to  use auto_ptr object to

improve EH coding?

 

A: void sh_use ( void* p ) {

       A *pa = new A;

    Try{

       Sh_lock ( p );

       // ….

    }

    Catch (…) {

       Sh_unlock (p );

       delete pa;

       throw;

    }

       Sh_unlock (p );

       delete pa;                           

}

Using the class template auto_ptr can avoid memory leakage in the case of exceptions.

void sh_use ( void* p ) {

     auto_ptr  < A > pa ( new A);

     Sh_lock shl(p);

}

 

  1. What’s CRC?  What’s OMT? What’s CORBA?

 

  1. How to discover objects when designing a programming system?  What are basic

rules used to figure out objects when designing a programming system?

 

  1. In developing C++ programming there are three models: (1) Procedural Model, (2) Abstract

Data Type (ADT) Model (Object-Based Model) and (3) Object-Oriented (OO) Model

(Polymorphism).  What are differences between them?

 

  1. Explain the following keywords in C++:  explicit and mutable.

 

  1. What’s difference between “abstract const” and “bit const”?

 

A:  When we use the const, we should understand how the const should be interpreted and how it should be applied.  The abstract const promises that a function will not change the abstract value of  the object , whereas the bit const promises that a function  will not write to any of the bits making up the object.  We should always use abstract const, and other possible interpretations also are not type safe.

 

  1. What’s smart pointer?  What’s reference counting?

 

A:  When we design a container, the containing object can have a handle to the representation of the contained object.  The handle object acts as pointer, so it is called smart pointer.  The class template auto_ptr is a smart pointer. If  these container class contain a reference count manipulated by the handle class, such use is called the reference counting.  Using reference counting can avoid expensive copy of  the instances.

 

  1. Assume we have a member function declaration:

void Stack::push(char);

How to declare a pointer to the member function?  How to initialize the pointer?

How to call the member function through the pointer?

 

  1. What’s  binary compatibility?

 

A:  It is also called link compatibility. The changes in the classes make all related codes to recompile.  These changes are binary incompatibility. It is typically involved in the design of the libraries.

 

  1. Explain:  ifstream  dataFile(“ints.dat”);  list<int> data(istream_iterator<int>(dataFile), istream_iterator<int>());

 

A: This declares a function, data, whose return type is list<int>.  The function data takes two parameters: The first parameter is named dataFile; its type is istream_iterator<int>.  And the second parameter has no name; its type is pointer to function taking nothing and returning an istream_iterator<int>.

 

[ 打印 ]
閱讀 ()評論 (0)
評論
目前還沒有任何評論
登錄後才可評論.