Classes and objects: book back answers
15 solved questions from the end of this chapter, Class 11 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for Classes and objectsSECTION – A Choose the correct answer
The variables declared inside the class are known as
- (A) data
- (B) inline
- (C) method
- (D) attributes
Try it, then show the answerAnswer
(D) attributes
Data members are also called attributes.
From the book: “data members are also called as attributes.”
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
Try it, then show the answerAnswer
(C) nesting of member function
The chapter calls a member function calling another member function of the same class nesting of member functions.
From the book: “When a member function is called by another member function of the same class , it is calledas nesting of member functions.”
The member function defined within the class behave like ........ functions
- (A) inline
- (B) Non inline
- (C) Outline
- (D) Data
Try it, then show the answerAnswer
(A) inline
A member function defined inside a class behaves like an inline function.
From the book: “When a member function is defined inside a class, it behaves like inline functions.”
Which of the following access specifier protects data from inadvertent modifications?
- (A) Private
- (B) Protected
- (C) Public
- (D) Global
Try it, then show the answerAnswer
(A) Private
Private members cannot be accessed from outside the class, protecting the data from direct external changes.
From the book: “A private member cannot be accessed from outside the class.”
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
Try it, then show the answerAnswer
(C) 5
The array x1[4] creates four objects, and x2 creates one more object. The total is 4 + 1 = 5.
From the book: “Even an array of objects can be created for a class.”
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
Try it, then show the answerAnswer
(C) False, True
The chapter says constructors are executed automatically when objects are created. It does not say they should be declared private; it describes constructors as class member functions, and examples declare them under public.
From the book: “The constructor is executed automatically when the object is created”
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
Try it, then show the answerAnswer
(C) Copy constructor
A constructor that has a reference to an already existing object of its own class is a copy constructor.
From the book: “A constructor having a reference to an already existing object of its own class is called copy constructor.”
SECTION-B
What are called members?
Try it, then show the answerAnswer
Data members and member functions are called members of a class.
A class comprises data members, which represent its properties, and member functions, which perform its tasks.
From the book: “Class comprises of members. Members are classified as Data Members and Member functions.”
Differentiate structure and class though both are user defined data type.
Try it, then show the answerAnswer
The members of a structure are public by default, whereas the members of a class are private by default.
This is the stated difference between structure and class in C++.
From the book: “The only difference between structure and class is the members of structure are by default public where as it is private in class.”
What is the difference between the class and object in terms of oop?
Try it, then show the answerAnswer
A class defines the properties and operations of a user-defined type. An object is an instance of that class, created to use the class.
A class is the specification; objects are class-type variables or instances that can be created from it.
From the book: “Objects are also called as instance of class.”
Why it is considered as a good practice to define a constructor though compiler can automatically generate a constructor ?
Try it, then show the answerAnswer
A user-defined constructor lets the programmer initialize the data members of an object with the required values when it is created. The automatically generated constructor does not provide those chosen initial values.
Constructors initialize class objects, and parameterized constructors allow objects to be created with different initial values.
From the book: “This type of constructor helps to create objects with different initial values.”
Write down the importance of destructor.
Try it, then show the answerAnswer
A destructor frees resources acquired by an object during its lifetime and removes the object's memory when its lifetime ends.
The destructor is called when the object goes out of scope and destroys the object.
From the book: “The purpose of the destructor is to free the resources that the object may have acquired during its lifetime.”
Write with example how will you dynamically initialize objects?
Try it, then show the answerAnswer
Dynamic initialization supplies an object's initial values at runtime, usually by passing runtime values to a parameterized constructor. Example: X x(a,b); where a and b are read at runtime and X has a constructor X(int p, float q) that initializes its data members.
In the chapter's example, input values are read into variables and then passed to the constructor when the object is created.
From the book: “When the initial values are provided during runtime then it is called dynamic initialization.”
Given the following C++ code, answer the questions (i) & (ii). class TestMeOut public: ~TestMeOut() //Function 1 {cout<<“Leaving the exam hall”<<endl;} TestMeOut() //Function 2 {cout<<“Appearing for exam”<<endl;} void MyWork() //Function 3 {cout<<“Answering”<<endl;} }; (i) In Object Oriented Programming, what is Function 1 referred as and when doesit get invoked / called ? (ii) In Object Oriented Programming, what is Function 2 referred as and when doesit get invoked / called ? Explain in det
Try it, then show the answerAnswer
(i) Function 1 is a destructor. It is invoked when the class object goes out of scope. (ii) Function 2 is a constructor. It is invoked automatically when an instance of the class is created or comes into scope.
The tilde before the class name identifies the destructor. The constructor has the class name and runs automatically when an object is created.
From the book: “When a class object goes out of scope, a special function called the destructor gets executed.”
Explain in detail
Mention the differences between constructor and destructor
Try it, then show the answerAnswer
A constructor and a destructor are special member functions of a class. They differ in when they are called and in their purpose.
- Purpose: A constructor initializes the class object, while a destructor frees resources acquired by the object during its lifetime.
- Time of execution: A constructor is executed when an object is created. A destructor is executed when the object goes out of scope or its lifetime ends.
- Function name: The constructor has the same name as the class. The destructor has the same name, prefixed with the tilde character (~).
- Arguments: A constructor can have a parameter list. A destructor cannot have arguments.
- Overloading: Constructors can be overloaded, but destructors cannot be overloaded.
- Return type: Neither function has a return type; both return nothing and are not associated with any data type.
Example: In the chapter’s simple class, simple() initializes a and b to 0, and ~simple() displays “Destructor is executed” when the object’s lifetime ends.
Thus, the constructor prepares an object for use, whereas the destructor performs cleanup when the object’s lifetime ends.
From the book: “The destructor cannot have arguments”