Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects

Students can Download Computer Science Chapter 14 Classes and Objects Questions and Answers, Notes Pdf, Samacheer Kalvi 11th Computer Science Book Solutions Guide Pdf helps you to revise the complete Tamilnadu State Board New Syllabus and score more marks in your examinations.

Tamilnadu Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects

Samacheer Kalvi 11th Computer Science Classes and Objects Text Book Back Questions and Answers

PART – 1
I. Choose The Correct Answer

Question 1.
The variables declared inside the class are known as data members and the functions are known as ………………..
(a) data functions
(b) inline functions
(c) member functions
(d) attributes
Answer:
(c) member functions

Question 2.
Which of the following statements about member functions are True or False?
(i) A member function can call another member function directly with using the dot operator.
(ii) Member function can access the private data of the class.
(a) (i) – True, (ii) – True
(b) (i) – False, (ii)-True
(c) (i) – True, (ii) – False
(d) (i) – False, (ii) – False
Answer:
(a) (i) – True, (ii) – True

Question 3.
A member function can call another member function directly, without using the dot operator called as ………………..
(a) sub function
(b) sub member
(c) nesting of member function
(d) sibling of member function
Answer:
(c) nesting of member function

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 4.
The member function defined within the class behave like ………………..
(a) inline function
(b) Non – inline function
(c) Outline function
(d) Data function
Answer:
(a) inline function

Question 5.
Which of the following access specifier protects data from inadvertent modifications?
(a) Private
(b) Protected
(c) Public
(d) Global
Answer:
(b) Protected

Question 6.
class x
{
int y; public:
x(int z){y=z;}
}x1[4];
int main()
{x x2(10);
return 0;}
How many objects are created for the above program?
(a) 10
(b) 14
(c) 5
(d) 2
Answer:
(c) 5

Question 7.
State whether the following statements about the constructor are True or False.
(i) constructors should be declared in the private section.
(ii) constructors are invoked automatically when the objects are created.
(a) True, True
(b) True, False
(c) False, True
(d) False, False
Answer:
(c) False, True

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 8.
Which of the following constructor is executed for the following prototype?
add display( add &);  // add is a class name
(a) Default constructor
(b) Parameterized constructor
(c) Copy constructor
(d) Non Parameterized constructor
Answer:
(d) Non Parameterized constructor

Question 9.
What happens when a class with parameterized constructors and having no default constructor is used in a program and we create an object that needs a zero – argument constructor?
(a) Compile – time error
(b) Domain error
(c) Runtime error
(d) Runtime exception
Answer:
(d) Runtime exception

Question 10.
Which of the following create a temporary instance?
(a) Implicit call to the constructor
(b) Explicit call to the constructor
(c) Implicit call to the destructor
(d) Explicit call to the destructor
Answer:
(b) Explicit call to the constructor

PART – 2
II. Answers to all the questions

Question 1.
What are called members?
Answer:
Class comprises of members. Members are classified as Data Members and Member functions. Data members are the data variables that represent the features or properties of a class. Member functions are the functions that perform specific tasks in a class.

Question 2.
Differentiate structure and class though both are user defined data type.
Answer:
The only difference between structure and class is the members of structure are by default public where as it is private in class.

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 3.
What is the difference between the class and object in terms of OOP?
Answer:
Object:

  • Object is an instance of a class.
  • Object is a real world entity such as pen, laptop, mobile, chair etc.
  • Object allocates memory when it is created.

Class:

  • Class is a blueprint or template from which objects are created.
  • Class is a group of similar objects.
  • Class doesn’t allocate memory when it is created.

Question 4.
Why it is considered as a good practice to define a constructor though compiler can automatically generate a constructor?
Answer:
Declaring a constructor with arguments hides the compiler generated constructor. After this we cannot invoke the compiler generated constructor.

Question 5.
Write down the importance of destructor.
The purpose of the destructor is to free the resources that the object may have acquired during its lifetime. A destructor function removes the memory of an object which was allocated by the constructor at the time of creating a object.

PART – 3
III. Answers to all the questions

Question 1.
Rewrite the following program after removing the syntax errors if any and underline the errors:
Answer:
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 1

Question 2.
Write with example how will you dynamically initialize objects?
When the initial values are provided during runtime then it is called dynamic initialization.
Example to illustrate dynamic initialization
Answer:
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 2
Output:
Enter the Roll Number 1201
Enter the Average 98.6
Roll number:- 1201
Average :- 98.6

Question 3.
What are advantages of declaring constructors and destructor under public accessability?
Answer:
When constructor and destructor are declared under public:

  1. we can initialize the object while declaring it.
  2. we can explicitly call the constructor.
  3. we can overload constructor and therefore use multiple constructors to initialize objects automatically.
  4. we can destroy the objects at the end of class scope automatically (free unused memory).

However, some C++ compiler and Dev C++ do not allow to declare constructor and destructor under private section. So it is better to declare constructor and destructor under public section only.

Question 4.
Given the following C++ code, answer the questions (i) & (ii).
Answer:
class TestMeOut
{

public:
~TestMeOut() //Function 1 {cout << “Leaving the examination hall” << endl;}
TestMeOut() //Function 2
{cout<<“Appearing for examination”<<endl;}
void MyWork() //Function 3
{cout<<“Attempting Questions//<<endl;}

};
1. In Object-Oriented Programming, what is Function 1 referred as and when does it get invoked/called?
2. In Object-Oriented Programming, what is Function 2 referred as and when does it get invoked/called?
Answer:
1. Function 1 is the ‘destructor’.
It is executed automatically when an object of the class TestMeOut goes out of scope.

2. Function 2 is called the default ‘constructor’.
It is executed automatically when an instance of the class TestMeOut comes into the scope or when objects of the class TestMeOut are created.

Question 5.
Write the output of the following C++ program code:
Answer:
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 3
Output:
B#0
I#1
G#1

PART – 4
IV. Answers to all the questions

Question 1.
Explain nested class with example.
Answer:
When one class becomes a member of another class then it is called Nested class and the relationship is called containership. When a class is declared within another class, the inner class is called a Nested class (i.e. the inner class) and the outer class is known as the Enclosing class. The nested class can be defined in private as well as in the public section of the Enclosing class.

Classes can be nested in two ways:

  1. By defining a class within another class
  2. By declaring an object of a class as a member to another class
  3. By defining a class within another class

C++ program to illustrate the nested class
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 4
Output:
The product of 3*2 = 6
The product of 4*4 = 16
The product of 2*2=4

By declaring an object of a class as a member to another class Whenever an object of a class is declared as a member of another class, it is known as a container class. In the containership, the object of one class is declared in another class.
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 5

Question 2.
Mention the differences between constructor and destructor.
Answer:
Constructor:

  • The name of the constructor must be the same as that of the class.
  • No return type can be specified for the constructor.
  • A constructor can have a parameter list.
  • The constructor function can be overloaded.
  • They cannot be inherited but a derived class can call the base class constructor.
  • The compiler generates a constructor, in the absence of a user-defined constructor.

Destructor:

  • The destructor has the same name as that of the class prefixed by the tilde character
  • It has no return type.
  • The destructor cannot have arguments
  • Destructors cannot be overloaded i.e., there can be only one destructor in a class.
  • They cannot be inherited.
  • In the absence of a user-defined destructor, it is generated by the compiler.

Question 3.
Define a class RESORT with the following description in C++ :
Answer:
Private members:

  1. Rno // Data member to storeroom number.
  2. Name // Data member to store user name.
  3. Charges // Data member to store per day charge.
  4. Days //Data member to store the number of days.
  5. Compute () // A function to calculate a total amount as Days * Charges and if the //total amount exceeds 11000 then the total amount is 1.02 * Days ’’’ Charges.

Public member:
getinfo() // Function to Read the information like name, room no, charges and days dispinfo () // Function to display all entered details and total amount calculated
//using COMPUTE function
//include
//include
//include
Class RESORT
{

Private:
int Rno;
char name [20];
float charges; int days; float compute();
Public:
void getinfoO;
void dispinfo();

}
void RESORT: : getinfo()
{

cout << “Enter Registration number:”; cin >> Rno.;
cout << “\n Enter name:”; gets(name);
cout << “\n Enter per day charges:”; cin >> charges;
cout << “\n Enter number of days:”; cin >> days;

}
void RESORT: : dispinfo()

{

cout << “\n1. Registration number:” << Rno << “\n2. Name:”; puts(name);
cout << “\n3. charges per day:” << charges << “\n4. Number of days:” <<days;
cout<< “\n5. Amount:” << compute();

}
long float;
amount = charges*days;
if (amount> 11000)
amount = 1,02*days*charges;
return amount;
}
RESORT obj;
void main()
{

clrscr();
obj.getinfo();
obj.dispinfoQ;
getch();

}

Question 4.
Write the output of the following.
Answer:
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 6
Output:
Constructing Subject
Constructing Student
Constructing Admission
Back to main()
Subject number Days:
Constructing the object
d= 150, Sn= 12
Constructing the object of students
Enter the roll number and the marks secured
mo: 12345 marks: 438
Constructing the object of admission
fees: 20000
Back in main()

Question 5.
Write the output of the following.
Answer:
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 7
Output:
Constructor of class P
Constructor of class P
Constructor of class Q
Constructor of class R
Constructor of class Q
Constructor of class P
Destructor of class P
Destructor of class Q
Destructor of class R
Destructor of class Q
Destructor of class P
Destructor of class P

Samacheer Kalvi 11th Computer Science Arrays and Structures Additional Questions and Answers

PART – 1
I. Choose the correct answer

Question 1.
The most important feature of C++ is the ………………………
a) Class
b) Array
c) Structures
d) All the above
Answer:
a) Class

Question 2.
Objects are also called as ………………..
(a) instance of class
(b) class
(c) function
(d) scope
Answer:
(a) instance of class

Question 3.
……………… is a feature of OOP languages.
a) Abstraction
b) Encapsulation
c) Inheritance and Polymorphism
d) All the above
Answer:
d) All the above

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 4.
Objects are passed as arguments to a ………………..
(a) call by value
(b) call by reference
(c) member function
(d) global variable
Answer:
(c) member function

Question 5.
……………….. are needed to represent real-world entities that not only have data type properties but also have associated operations,
a) Classes
b) Arrays
c) Structures
d) All the above
Answer:
a) Classes

Question 6.
When a class is declared within another class, the inner class is called ……………….. and the outer class is called ………………..
(a) enclosing class, nested class
(b) nested class, enclosing class
(c) first-class, second class
(d) A class, B class
Answer:
(b) nested class, enclosing class

Question 7.
The body of the class is terminated by……………………
a) Semicolon
b) List of declarations at the end
c) Either A or B
d) None of these
Answer:
c) Either A or B

Question 8.
A constructor which can take arguments is called ………………..
(a) parameterised constructor
(b) default constructor
(c) copy constructor
(d) destructor
Answer:
(a) parameterized constructor

Question 9.
The members of the class are by default …………………..
a) Private
b) Public
c) Protected
d) None of these
Answer:
a) Private

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 10.
the name of the symbol is ………………..
(a) hash
(b) arrow
(c) tilde
(d) bracket
Answer:
(c) tilde

Question 11.
If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at ………………….
a) Run Time
b) Compile time
c) Both A and B
d) None of these
Answer:
b) Compile time

Question 12.
When Member function defined outside the class, and then it is be called as ………………….
member function.
a) Outline
b) Non-inline
c) Either A or B
d) None of these
Answer:
c) Either A or B

Question 13.
When Member function defined outside the class using …………….. operator.
a) Scope resolution
b) Membership
c) Reference
d) Conditional
Answer:
a) Scope resolution

Question 14.
The class variables are called ………………….
a) Object
b) Attributes
c) Procedures
d) None of these
Answer:
a) Object

Question 15.
Objects are also called as …………………. of class.
a) Instant
b) Instance
c) Attributes
d) None of these
Answer:
b) Instance

Question 16.
Objects can be created in ………………… methods.
a) Three
b) Four
c) Two
d) Five
Answer:
c) Two

Question 17.
Objects can be created as ……………….
a) Global object
b) Local object
c) Either A or B
d) None of these
Answer:
c) Either A or B

Question 18.
…………….. objects can be used by any function in the program.
a) Global object
b) Local object
c) Either A or B
d) None of these
Answer:
a) Global object

Question 19.
If an object is declared outside all the function bodies or by placing their names immediately after the closing brace of the class declaration then it is called……………….
a) Global object
b) Local object
c) Either A or B
d) None of these
Answer:
a) Global object

Question 20.
If an object is declared within a function then it is called ……………….
a) Global object
b) Local object
c) Either A or B
d) None of these
Answer:
b) Local object

Question 21.
……………… can be defined either inside class definition or outside the Class definition.
a) Constructor
b) Destructor
c) Data abstraction
d) Member functions
Answer:
a) Constructor

Question 22.
A constructor can be defined in ……………… section of a class.
a) Private
b) Public
c) Either Private or Public
d) None of these
Answer:
c) Either Private or Public

Question 23.
If a constructor is defined in ………………… section of a class, then only its object Can be created in any function.
a) Private
b) Public
c) Either Private or Public
d) None of these
Answer:
b) Public

Question 24.
The main function of the constructor is ……………………….
a) To allocate memory space to the object
b) To initialize the data member of the class object
c) Either A or B
d) None of these
Answer:
c) Either A or B

Question 25.
A constructor that accepts no parameter is called …………………… constructor.
a) Null
b) Default
c) Empty
d) None of these
Answer:
b) Default

Question 26.
Identify the correct statement from the following with respect to the constructor.
a) If a class does not contain an explicit constructor (user-defined constructor) the compiler automatically generates a default constructor implicitly as an inline public member.
b) In the absence of a user-defined constructor the compiler automatically provides the default constructor. It simply allocates memory for the object.
c) Parameterized constructor is achieved by passing parameters to the function.
d) All the above
Answer:
d) All the above

Question 27.
A constructor which can take arguments is called ……………….. constructor.
a) Parameterized
b) Default
c) Empty
d) None of these
Answer:
a) Parameterized

Question 28.
……………….. type of constructor helps to create objects with different initial values.
a) Parameterized
b) Default
c) Empty
d) None of these
Answer:
a) Parameterized

Question 29.
Declaring a constructor with arguments hides the ……………………
a) Data members
b) Compiler generated constructor
c) Member functions
d) None of these
Answer:
b) Compiler generated constructor

Question 30.
………………… Constructor is used to creating an array of objects.
a) Default
b) Parameterized
b) Overloaded
d) None of these
Answer:
a) Default

PART – 2
II. Very Short Answers

Question 1.
What are the features of OOP languages?
Answer:
The four features commonly present in OOP languages: Abstraction, Encapsulation, Inheritance, and Polymorphism.

Question 2.
What is a global object?
Answer:
If an object is declared outside all the function bodies or by placing their names immediately after the closing brace of the class declaration then it is called a Global object. These objects can be used by any function in the program.

Question 3.
Define class.
Answer:
Class is a way to bind the data and its associated functions together.

Question 4.
In the absence of a user-defined constructor, what will the constructor do?
Answer:
In the absence of a user-defined constructor, the compiler automatically provides the default constructor. It simply allocates memory for the object.

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 5.
Write a note on data hiding.
Answer:
Data hiding feature of Object-Oriented Programming preventing the functions of a program to access directly the internal representation of a class type. The access restriction to the class members is specified by public, private, and protected sections within the class body.

Question 6.
What will be the output of the following program?
Answer:
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 8
Output:
151515

Question 7.
What will be the output for the following program?
Answer:
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 9

PART – 3
III. Short Answers

Question 1.
Write the syntax of the class definition.
Answer:
The General Form of a class definition:
{
private:
variable declaration;
function declaration;

protected:
variable declaration;
function declaration;

public:
variable declaration;
function declaration;
};

Question 2.
Explain scope resolution operator with an example.
Answer:
If there are multiple variables with the same name defined in separate blocks then:: (scope resolution) operator will reveal the hidden file scope (global) variable.
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 10
Output:
120

Question 3.
Compare global object and local object.
Answer:
Global Object:
If an object is declared outside all the function bodies or by placing their names immediately after the closing brace of the class declaration then it is called a Global object. These objects can be used by any function in the program.

Local Object:
If an object is declared within a function then it is called a local object. It cannot be accessed from outside the function.

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 4.
What are the functions of the constructor?
Answer:
As we know now that the constructor is a special initialization member function of a class that is called automatically whenever an instance of a class is declared or created. The main function of the constructor is

  1. To allocate memory space to the object and
  2. To initialize the data member of the class object

Question 5.
Write a C++ program to return memory requirements for an object.
Answer:
PROGRAM
# include<iostream>
using namespace std;
class product
{
int code, quantity;
float price;
public:
void assignData( );
void Print( );
};
int main( )
{
product p1, p2;
cout<<“\n Memory allocation for object p1 ” <<sizeof(p1);
cout<<“\n Memory allocation for object p2 ” <<sizeof(p2);
return 0;
}
Output
Memory allocation for object p1 12
Memory allocation for object p2 12

Question 6.
What are the ways to create an object using parameterized constructor?
Answer:
There are two ways to create an object using parameterized constructor:
Implicit call:
In this method, the parameterized constructor is invoked automatically whenever an object is created. For example simple s1(10, 20); in this for creating the object s1 parameterized constructor is automatically invoked.

Explicit call:
In this method, the name of the constructor is explicitly given to invoke the parameterized constructor so that the object can be created and initialized.

Question 7.
When copy constructor is called?
Answer:
A copy constructor is called:

  1. When an object is passed as a parameter to any of the member functions. Example void simple::putdata(simple x);
  2. When a member function returns an object. Example simple getdata() {}
  3. When an object is passed by reference to an instance of its own class For example, simplest, s2(s1); // s2(s1) calls copy constructor.

Question 8.
Write the output of the following program.
Answer:
#include
using namespace std;
class simple
{
private:
int a, b;
public:
simple()
{
a= 0;
b= 0;
cout << “\n Constructor of class – simple”;
}
void getdata()
{
cout << “\n Enter values for a and b (sample data 6 and 7)…”; cin >> a >> b;
}
void putdata()
{
cout << “\nThe two integers are ..” << a << ‘\t’ << b << end1;
cout << “\n The sum of the variables” << a << “+” << b << “=” << a+b; }
~simple()
{ cout << “\n Destructor is executed to destroy the object”;}
};
int main()
{
simple s;
s.getdata();
s.putdata();
return 0;
}
Output:
Constructor of class – simple
Enter values for a and b (sample data 6 and 7)… 6 7
The two integers are .. 6 7
The sum of the variables 6 + 7 = 13
Destructor is executed to destroy the object

PART – 4
IV. Explain in Detail

Question 1.
Create a class called product with the following specifications.
Answer:
Private members:
code quantity – integer data type
price – float data type
get data() – function to accept values for all data members with no return

Public members:
tax – float
dispdata() member function to display code, quantity, price and tax. The tax is calculated as if the quantity is more than 150, tax is 3000, or else 1000.
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 13

Question 2.
Explain pass by value with an example.
Answer:
When an object is passed by value the function creates its own copy of the object and works on it. Therefore any changes made to the object inside the function do not affect the original object.
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 14
Output:
Example program for Pass by value
Value of object 1 before passing 10
Value of object 2 before passing 20
Changed value of object 1 100
Changed value of object 2 200
Value of object 1 after passing 10
Value of object 2 after passing 20

Question 3.
Explain pass by reference with an example.
Answer:
When an object is passed by reference, its memory address is passed to the function so the called function works directly on the original object used in the function call. So any changes made to the object inside the function definition are reflected in original object.
Samacheer Kalvi 11th Computer Science Solutions Chapter 14 Classes and Objects 15
Output:
Example program for Pass by reference
Value of object 1 before passing 10
Value of object 2 before passing 20
Changed value of object 1 100
Changed value of object 2 200
Value of object 1 after passing 100
Value of object 2 after passing 200

Question 4.
What are the characteristics of constructors?
Answer:
Characteristics of constructors:

  • The name of the constructor must be the same as that of the class.
  • No return type can be specified for the constructor.
  • A constructor can have a parameter list.
  • The constructor function can be overloaded.
  • They cannot be inherited but a derived class can call the base class constructor.
  • The compiler generates a constructor, in the absence of a user-defined constructor.
  • Compiler generated constructor is a public member function.
  • The constructor is executed automatically when the object is created.
  • A constructor can be used explicitly to create a new object of its class type.

Samacheer Kalvi 11th Computer Science Solutions Chapter Classes and Objects

Question 5.
Write the output of the following program.
Answer:
#include
using namespace std;
class simple
{
private:
int a, b;
public:
simple() //default constructor
{
a = 0; b = 0;
cout << “\n default constructor” << endl;
}
int getdata();
};
int simple :: getdata()
{int tot;
cout << “\n Enter two values”; cin >> a >> b;
}
tot = a + b; return tot;
}
int main()
{
int sum=0; simple s1[3];
cout << “\n\t\tObject 1 with both values \n”;
for (int i=0;i<3;i++)
sum+=s 1 [i]. getdata();
cout << “\nsum of all object values is” << sum;
return 0;
}
Output:
default constructor
default constructor
default constructor
Object 1 with both values
Enter two values 10 20
Enter two values 30 40
Enter two values 50 50
sum of all object values is 200
}