2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors

You can Download Chapter 9 Constructors and Destructors Questions and Answers, Notes, 2nd PUC Computer Science Question Bank with Answers Karnataka State Board Solutions help you to revise complete Syllabus and score more marks in your examinations.

Karnataka 2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors

2nd PUC Computer Science Constructors and Destructors One Mark Questions and Answers

Question 1.
What is a constructor?
Answer:
It is a special member function that is used to initialize the data members of an object.

Question 2.
Write one reason which defines the need to use a constructor.
Answer:
The constructors are used to initialize the object automatically when an object is created. This reduces a separate function call to a member function used for such purpose. It is called constructor because it constructs the values of data members of the class.

Question 3.
What should be the access parameters for constructor declaration?
Answer:
The constructors should be declared in public section i.e., public is the access parameter for constructor declaration.

a

Question 4.
Can a constructor return a value to a calling function?
Answer:
No, constructors cannot return value to a calling function.

KSEEB Solutions

Question 5.
How many types of constructors are there?
Answer:
There are three types of constructors.

Question 6.
What is a default constructor?
Answer:
A constructor which does not take any arguments is called a zero argument constructor.

Question 7.
What is the drawback of default constructor?
Answer:
All objects of a class with default constructor are initialized to same set of values.

Question 8.
Is it possible to overload a default constructor?
Answer:
Since no arguments are there, the default constructor cannot be overloaded.

KSEEB Solutions

Question 9.
What is a parameterized constructor?
Answer:
A constructor that takes one or more arguments is called a parameterized constructor.

Question 10.
Write any one feature of parameterized constructor.
Answer:
The parameterized constructor can be overloaded.

Question 11.
Name two methods through which constructors can be invoked.
Answer:
The two methods through which constructors can be invoked are implicit call and explicit call.

Question 12.
What is an explicit call?
Answer:
It is a method of invoking a function where the declaration of object is followed by assignment operator followed by a constructor followed by argument list enclosed within parentheses.

Question 13.
What is an implicit call with reference to constructors?
Answer:
In this method, the declaration of object is followed by the argument list enclosed within parentheses.

Question 14.
When is = used with constructors?
Answer:
The = is used for the parameterized constructor with exactly one argument.

Question 15
What is a copy constructor?
Answer:
It is a parameterized constructor using which one object can be copied into another object.

KSEEB Solutions

Question 16.
Write the syntax for declaration of copy constructor.
Answer:
The syntax for declaration of copy constructor:
Classname :: Classname (Classname &ptr)

Question 17.
Can a copy constructor be invoked explicitly?
Answer:
No, a copy constructor cannot be invoked explicitly.

Question 18.
What is meant by constructor overloading?
Answer:
If many constructors differ by number of arguments or/and by type of arguments in a class is called constructor overloading.

Question 19.
What is a destructor?
Answer:
It is a special member function that destroys the objects that have been created by a constructor, when they no longer required.

Question 20.
Which operator is used with destructor?
Answer:
The operator tilde sign (~) is used with destructor.

2nd PUC Computer Science Constructors and Destructors Two Mark Questions and Answers

Question 1.
What is a constructor? Give an example.
Answer:
It is a special member function that is used to initialize the data members of an object,
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 1

Question 2.
Why are constructor needed in a program? Justify.
Answer:
The objects are not automatically initialized when created. The explicit call to initialization member function can only initialize the object. This method proves to be inconvenient when large number of objects need to be initialized by giving separate function call. This problem can be overcome by automatically initializing object when they are created using constructor.

KSEEB Solutions

Question 3.
Write the syntax and example for default constructor.
Answer:
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 2

Question 4.
Mention the features of parameterized constructors.
Answer:
The features of parameterized constructors are

  1. parameterized constructors can be overloaded
  2. parameterized constructors can have default arguments and default values.

Question 5.
Which are the different methods through which constructors are invoked?
Answer:
The different methods through which constructors are invoked are

  • Explicit call
  • Implicit call
  • Using = operator

KSEEB Solutions

Question 6.
Write an example to show the use of parameterized constructor through explicit call.
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 3
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 4

Question 7.
When is copy constructor used in a program?
Answer:
The copy constructor takes an object as argument and is used to copy values of data members of one object into other object.

Question 8.
Write syntax and example for copy constructor.
Answer:
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 5
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 6

2nd PUC Computer Science Constructors and Destructors Three Mark Questions and Answers

Question 1.
Mention three types of constructors.
Answer:
The three types of constructors are

  1. Default constructor
  2. Parameterized constructor
  3. Copy constructor

Question 2.
What are the features of default constructors?
Answer:
The features of default constructors are

  • All objects of a class are initialized to same set of values
  • These constructors has no arguments
  • These constructors are automatically called when every object is created.

Question 3.
What are the disadvantages of default constructor?
Answer:
The disadvantages of default constructor are

  • Different objects cannot be initialized with different values.
  • Declaring a constructor with arguments, hides default constructor.

KSEEB Solutions

Question 4.
Write short note for constructor overloading.
Answer:
The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor. The constructors are no different from other functions. Therefore constructors can also be overloaded.

Overloading a constructor means having many constructors in a class with different types arguments and/or different number of arguments. The compiler decides which version of the constructor to invoke during object creation based on number of arguments and type of arguments passed in a program.

2nd PUC Computer Science Constructors and Destructors Five Mark Questions and Answers

Question 1.
Write the rules for writing a constructor function.
Answer:
The rules for writing a constructor functions are

  • They should be declared in the public section.
  • They are invoked automatically when the objects are created.
  • They should not have return types, therefore they cannot return values.
  • They cannot be inherited.
  • They can have default arguments.
  • Cannot refer to addresses.
  • These cannot be static.
  • An object of a class with a constructor cannot be used as a member of a union.

Question 2.
Explain default constructor with syntax and example.
Answer:
This constructor has no arguments in it. Default Constructor is also called as no argument constructor. They initialize data members with common values for all objects belongs to similar class.
The features of default constructors are

  • All objects of a class are initialized to same set of values
  • These constructors has no arguments
  • These constructors are automatically called when every object is created.

2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 7

KSEEB Solutions

Question 3.
Explain parameterized constructor with syntax and example.
Answer:
The constructors that can take arguments are called parameterized constructors. When a constructor is parameterized, we must pass arguments to the constructor.
When a constructor is parameterized, the object declaration without parameter may not work. We must pass the initial values as arguments to the constructor. This can be done in two ways:

1. By implicit call – The implicit call is implemented as follows:
student mk (1200,19); /””implicit call*/

This method is also called the shorthand method, and is used very often as it is shorter, looks better and easy to implement. In the above example, student is a class name and mk is name of object and passed arguments are 1200, 19. One can notice that function name is not taken to invoke constructor.

2. By Explicit Call – The following statement illustrates the explicit call for the parameterized constructor
Student dushyanth = Student (1201, 20); /””explicit call*/

In the above example, Student is class name and dushyanth is object name.
After the = symbol, the name Student refers to parameterized constructor with argument
1201,20.
The parameterized constructor syntax:
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 8

KSEEB Solutions

Question 4.
With an example show how constructors are used with = operator.
Answer:
The constructor argument if limited to single argument then = operator can be used to pass the values to these constructors. This is explained in the following example,
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 9
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 10

In the above program, c1 and c2 are objects with argument 10 and 20 respectively passed using = operator.

Question 5.
Explain the features of copy constructor.
Answer:
The features of copy constructor are

  1. The copy constructor should have at least one argument of the same class and this argument must be passed as a constant reference type.
  2. If additional arguments are present in the copy constructor, then it must contain default arguments.
  3. Explicit function call of copy constructor is not allowed.
  4. Copy constructor is also called automatically, when an object is passed to a function using pass by value.
  5. If a new object is declared and existing object is passed as a parameter to it in the declaration itself, then also the copy constructor is invoked.

Question 6.
Explain destructors with syntax and example.
Answer:
It is a special function used to release the memory space allocated by the object.
→ Name of the Destructor is similar to the class, which it belongs.
→ It does not have argument(s) and doesn’t return any value (no return type)
→ Destructor is preceded by ~ (tilde) sign.
Following points should be kept in mind while defining and writing the syntax for the destructor:

  • A destructor function must be declared with the same name as that of the class to which it belongs.
  • The first character of the destructor name must begin with a tilde (~).
  • A destructor function is declared with no return types specified (not even void).
  • A destructor function must have public access in the class declaration.

General Syntax of Destructors:
~ classname();
The above is the general syntax of a destructor. In the above, the symbol tilde ~ represents a destructor which precedes the name of the class.
For example,
2nd PUC Computer Science Question Bank Chapter 9 Constructors and Destructors 11

a