Students can Download 2nd PUC Computer Science Chapter 9 Constructors and Destructors Questions and Answers, Notes Pdf, 2nd PUC Computer Science Question Bank with Answers helps you to revise the complete Karnataka State Board Syllabus and to clear all their doubts, score well in final exams.
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:
A constructor is a special member function that is used in the classes to initialize the objects of a class automtically.
Question 2.
Write one reason which defines the need to use a constructor simplify.
Answer:
To initialize the object when it is first created
Question 3.
What should be the access parameters for constructor declaration?
Answer:
Public.
Question 4.
Can a constructor return a value to a calling function?
Answer:
No.
Question 5.
How many types of constructors are there.
Answer:
3.
Question 6.
What is a default constructor?
Answer:
The constructors which does not accept any arguments
Question 7.
What is the drawback of default constructor?
Answer:
It is not possible to initialize different objects with different to initialize different objects with different values.
Question 8.
Is it possible to overload a default constructor ?
Answer:
No.
Question 9.
What is a parameterized constructor?
Answer:
A constructor that takes onr or more arguments is called parameterized constructor.
Question 10.
Write any one feature of parameterized constructor?
Answer:
For an object created with one argument, constructor, with only one argument is invoked & executed.
Question 11.
Name two methods through which constructor can be invoked.
Answer:
Explicit call & Impicit call.
Question 12.
What is an explicite call?
Answer:
In explicit call, declaration of an object is followed by assignment operator, constructor name & argument list enclosed in parathesis.
Question 13.
What is an implicit call with reference to constructors?
Answer:
An implicit call means the declaration of the object is followed by arguments enclosed in parathesis.
Question 14.
When is = used with constructors?
Answer:
Used for the constructor with exactly one argument.
Question 15.
What is a copy constructors?
Answer:
Copy constructor is a parameterized constructor using which one object can copied to another object.
Question 16.
Write the syntax for declaration of copy constructor.
Answer:
Class – name :: class – name (class – name & ptr)
Question 17.
Can a copy constructor can be invoked explicitly?
Answer:
No.
Question 18.
What is meant by constructor overloading?
Answer:
Constructor overloading means specifying additional meaning to it by passing required arguments.
Question 19.
What is a destructor?
Answer:
Destructors are the member functions that destroy an object automaticaly.
Question 20.
Which operator is used with the destructor?
Answer:
~ (tilde) operator.
2nd PUC Computer Science Constructors and Destructors Two Marks Questions and Answers
Question 1.
What is a constructor? Give an example.
Answer:
A constructor is a special member function that is used in the classes to initialize the objects of a class automatically,
ex:
class test { int m; public: int x, y; ' test () | | constructor inside the class definition { x = 0; y = 0; } };
Question 2.
Why are constructors needed in a program? Justify.
Answer:
It is sometimes convenient if an object can initialize itself when it is first created without the need to make a separate call to a member function. This is possible with the special member functions. Automatic initialize is carried out using such a special member function is called constructor. Generally, we can say, a constructor constructs the data members of an object.
Ex: defines a values to the data members.
Question 3.
Write the syntax & example for default constructor.
Answer:
Syntax: class – name :: class – name (int = 0) | | constructor.
Ex : Students () {}.
Question 4.
Mention the features of parameterized constructors.
Answer:
- Parameterized constructors can be overloaded.
- For an object created with one argument, constructor with only one argument is invoked & executed.
- The parameterized constructor can have default arguments and default values.
Question 5.
Which are the different methods through which constructors are invoked?
Answer:
- Explicit call
- Implicit call
- Initialed at the time of declaration with = operator.
Question 6.
Write the example to show the use to parameterized constructor through explicit call.
Answer:
# include < iostream. h> class num { private : int a,b; public : num (int p, int q) (a = p, b = q:) void display () { cout << " a = " << " &b = "<< end1; } }; void main () { num obj 1 = num (10, 20); num obj2 = num (40, 50); cout << " First construction : " ; obj1. display (); cout << " second construction :: obj 2. display (); }
Question 7.
When is a copy constructor used in a program?
Answer:
- To initialize an object with the values of already existing objects.
- When objects must be returned as function values.
- To state objects as by value parameters of a function.
Question 8.
Write syntax & example for copy constructor.
Answer:
Syntax : classname :: classname (classname & ptr)
Ex: x :: x (x & ptr)
Here, x is a class name & ptr is a pointer to a class object.
2nd PUC Computer Science Constructors and Destructors Three Marks Questions and Answers
Question 1.
Mention the three types of constructors?
Answer:
- Default constructor
- Parameterized constructor
- Copy constructor.
Question 2.
What are the features of default constructors?
Answer:
- For every object created, this constructor is automatically called.
- All objects of a class are initialized to same set of values by the default constructors.
- If the different values to be initialized to different values, it cannot be done using the default constructors.
Question 3.
What are the disadvantages of default constructors?
Answer:
- When many objects of the same class are created, all the objects are initialized to same set of values by default constructions.
- It is not possible to initialize different objects with different initial values using the default constructions.
Question 4.
Write a short note on construction overloading.
Answer:
Constructor overloading means specifying additional meaning to it by passing the required arguments. Depending on the requirement one can define any number of overloaded constructors in a single class. Depending on the type & number of arguments passed the complier decides which version of the constructor to invoke during object creation.
2nd PUC Computer Science Constructors and Destructors Five Marks Questions and Answers
Question 1.
Write the rules for writing a constructor function.
Answer:
- A constructor always has name that is same as the class name of which they are the members. This will help the complier to identity that they are the constructors.
- There is no return type for the constructor (not even viod). Since the constructors are called automatically by the system there is no program for it to return anything a return value world not make sense.
- A constructors should be declared in the public section.
- A constructors is invoked automatically when objects are created. Constructors can have default arguments.
- It is not possible to refer to the address of the constructros.
- The constructors make implicit class to the operators new & delete when memory allocation is required.
Question 2.
Explain the default constructor with system example.
Answer:
- For every object created, this constructor is automatically called.
- All objects of a class are initialized to same set of values by the default constructors.
- If the different values to be initialized to different values it cannot be done using the default constructors.
Syntax: class – name:: class – name (int = 0) | | constructor without arguments
ex: students () {}.
Question 3.
Explain the parameterized constructor with syntax & Example.
Answer:
- Parameterized constructors can be overloaded.
- For an object created with one argument, constructor with only one argument is invoked & executed.
- The parameterized constructor can have default arguments & default values,
Example:
# include < iostream. h> class num { private : int a, b; public : num (int p, into q ) { a = p, b = q;} void display () { cout << "a = " a << " & b = " << b << end 1; } }; { num obj 1 = num (10, 20); num obj 2 = num (40, 50); cout << " first construction obj 1. display (); cout << " second construction obj 2. display (); }
Question 4.
With the example show how constructors, are used with = operator.
Answer:
This method is used for the constructor with exactly one argument. In this declaration is followed by assignment operator & value to be initialized, ex:
# include < isotream.h> class num { private : int a ; public: num (int m) (a = m ;) void display () { cout << a << end 1; } }; void main () { num obj 1 = 100 ; num obj 2 = 200 ; cout <<" object 1 = obj 1. display (); cout << " object 1 = obj 1. display (); }
Question 5.
Explain the features of copy constructors.
Answer:
1. Copy constructors is not invoked explicitly.
2. Copy constructor is invoked automatically when a new object is created & equated to an already existing object in the declaration statement itself.
ex:
x a1; | | default constructor
x a2; | | copy constructor al, display ();
The above example shows the use of copy constructor to create a new object a2 using existing object a1.
3. When a new object is declared & existing object is passed instructor is invoked,
ex:
x a1 (100, 200) ; | | parameterized constructor
x a2 (a) | | copy constructor is invoked for
| | object a2 with a1 as parameter
4. When an object is passed to a function using pass – by value, copy constructor is automatically called.
ex:
void test (x b)
{
}
main ()
{
x a;
test (a); | | copy constructor is invoked.
}
5. Copy constructor is invoked when as object returns a value.
Question 6.
Explain destructors with syntax & examples.
Answer:
class classaname { private : int counter ; public : counter () | | constructor { counter = 0; } ~ counter () | | Destructor {} }
- The destructor name is same as that of class. The first character must be tide (~).
- Destructor do not have a return value.
- They take no arguments. Therefore destructors cannot be overloaded.
- The most common use of destructors is to de – locate memory that was allocated for the Object by the constructor. Also, they are declaration public.
Example:
class account { private : float balance; float rate; public: account () ; | | constructor ~ account (); | | destructor }