1 The is called whenever a class object is no longer in use
Solution
1. The Destructor is called whenever a class object is no longer in use. It is used for garbage collection.
(When an object is no longer in used a destructor is called to deallocated the memory allocated to the object)
2. 01 class A {
02 string x;
03 } ;
For the above class, which of the following is a general set accessor method declaration for its field.
Answer: void setX(string)
(Setters are used to set values to the private member of a class, the general syntax is void setX(string), using this method we can set value of X with the string passed as parameter)
3. Fields and methods of a class with protected visibility are only accessible by the class, derived classes and friends.
(protected members are visible to the own class, derived class and friend class)
4. Which of the following is the correct way to define a static const floating point field named str
Answer: c) static const double str = 3.14;
(A static constant is defined as above, the value of str is set to 3.14 and remains unchanged through out the program)
Refer to the following class for problems 5 to 6
01 class B {
02 int x, y;
03 public:
04 B( );
05 B( int x, int y);
06 };
5. Which of the following shows the correct way to define the overloaded constructor on line 05 using the initialization list”
d) B( int x, int y) { this->x = x, this->y = y; }
(In order to avoid variable ambiguity we use this->x=x, if using different parameters, say xVal, then we can assign as x=xVal)
6.Which of the following shows the correct way to define the default constructor with the values 2 and 3 for x and y respectively by calling the overloaded constructor.
Answer: B():B(2,3){ }
(the above statement calls the parameter constructor from the default constructor)
7. Which catch statement catches all exceptions and re-throw them.
Answer: catch() {throw;}
(If we don’t mention the type of exception in the catch its will call all types of exception, we re-throw the exception using throw keyword)
8. 01 class C {
02 static int x;
03 public:
04 C()
05 };
Which of the following is the correct way to assign 3 to static field.
Answer: C() {x=3;}
(As well already defined x as static, we can just assign 3 to x )
9. 01 class D {
02 public:
03 D();
04 D(int y=2);
05 D(int x,int y);
06 D(int y=2,string str);
07 };
Which anonymous object will result in an error.
Answer: D(“Help”)
(For the above statement the compiler checks for a constructor which takes only a string as parameter, but as we don’t have that constructor its returns an error)
10. 01 class E {
02 string x;
03 };
For the above class, which of the following is a general get accessor method declaration for its field.
Answer: string getX();
(The above method is an accessor which returns the value of x)

