Students can Download 2nd PUC Computer Science Chapter 7 Classes and Objects 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 7 Classes and Objects
2nd PUC Computer Science Classes and Objects One Mark Questions and Answers
Question 1.
What is class?
Answer:
A collection of objects having identical properties & the common behaviour.
Question 2.
What is an object?
Answer:
Real world entity with attributes & functions
Question 3.
What are the two types of members referenced in a class?
Answer:
Data members & member functions.
Question 4.
What are data members?
Answer:
Member data of a class, describe the characteristics of a class.
Question 5.
What is a member functions?
Answer:
Set of operations that are performed on the objects of the class.
Question 6.
Mention the access specifier used with a class.
Answer:
Private, protected & public.
Question 7.
Is it posible to access data outside the class?
Answer:
No.
Question 8.
Which type of data members are accessible outside a class? .
Answer:
Public.
Question 9.
Which access specifier is implicitly used in a class?
Answer:
Private.
Question 10.
Define the term public access.
Answer:
Public access means that members can be accessed by function outside the class also.
Question 11.
Mention the operator used to access members of a class.
Answer:
Scope resolution operator.
Question 12.
What is the significance of scope resolution operator (::)?
Answer:
The use of scope resolution operator implies that these member functions are defined outside the class.
Question 13.
How are objects of a class are declared? Give an Example.
Answer:
Class user – defined – name
{
private : | | members
public : | | methods
};
user – defined – name object 1, object,….;
ex :
class student { int rool no; char name [20]; char sex; int age; public: void get - data (); void display - data(); }
Question 14.
What is meant by an array of objects?
Answer:
An array having class type elements is known as array of objects.
Question 15.
Write an example to show how objects can be used as function arguements.
Answer:
void add (num n1 num n2)
{
x = n1 . x + n2 . x:
y = n1 . y + n2 . y:
}
where num is a class name.
2nd PUC Computer Science Classes and Objects Two/Three Marks Questions and Answers
Question 1.
Write the differences between class definition & class declaration.
Answer:
A class definition is a process of naming a class & data variables, & inteface operations of the class. A class declaration specifies the representation of objects of the class & set of operations that can be applied to such objects.
Question 2.
Write the syntax & example for class definition.
Answer:
Class user - defined - name { private: Member data Membaer functions protected : Member data Member functions public: Member data Member functions }:
Example:
class account { private: int accno; char name [20]; char acctype [4]; int bal - amt; public: void get - data (); void display data (); };
Question 3.
Write the syntax & example for class declaration.
Answer:
Defining a class
class class - name { private: Data members; Member function; protected: Data member; Member function public: Data members; Member functions; }; Here, class keyword used to declare Class - name a class Name of the class.
Example:
class students { private : Int Rollno: char name [10]: public: void get data (); void display () };
Question 4.
What is the significance of using access specifiers? Mention different access specifiers.
Answer:
The access specifiers define the scope of the data. The access specifiers are private, public & protected.
Question 5.
Discuss the private access specifiers with an example.
Answer:
Private access means a member data can only be accessed by the member function. Member declared under private are accessible only within the class. If no access specifier is mentioned, than by default, member are private.
ex: private:
int x;
float y;
Question 6.
Write short notes on public access specifiers.
Answer:
Public access means that members can be accessed by any function outside the class also.
ex:
class box { int length; public int width; private int height; void set - height (int i) { height = i; } int get - height () { return height; } }; int main () { box object; object.length = 10; object.width = 20; object.set - height (30); || private variable can be return 0; accessed only, only through its public method. }
Question 7.
Explain protected access.
Answer:
The members which are declared using protected can be accessed only by the member functions, friends of a class & also by the member functions derived from this class. The members cannot be accessed from outside. The protected access specifier is therefore similar to private specifiers.
Question 8.
How are class members are referenced? Discuss with the suitable example.
Answer:
The members of a class can be data or functions private & protected members of a class can be accessed only through the member functions of that class. No function outside a class can include statement to access data directly. The public data members of object of a class can be accessed using direct member access operator (.).
The syntax for accessing class member is.
class – object, member – data;
class – object, member -function (arguments);
Example:
class rectangle int l: int b; public: void get - data(); void complete- area (); void dispaly (); }; int main () { rectangle rl; rl. get - data (); rl. complete - area (); rl. display (); return 0 ; }-
Question 9.
What is meant by referencing member functions inside class definition & outside class definition & out side class definition?
Answer:
To define member faction inside a class the function declaration within the class is replaced by actual function definition inside the class. In the outside class definition member function declared as a member class definition must be defined separately outside the class.
Question 10.
Discuss how objects of the class are referenced with an example.
Answer:
Once the class is defined an object is created from that class,
Example:
class num { private: int x; inty; public: int sum (intp, int q); int diff (int p, int q); }; void main () { num s1, s2; s1, sum (200, 300); s2. diff (600,500); }
Question 11.
How can arrays be used as class members. Write an exmple.
Answer:
It is possible to use arrays as member data of class type,
class marks
{
int m [5];
int i;
public :
void setval (void);
void display (void);
};
The array variable m is private member of class marks. This can be used by the member function setval () and display () as follows.
{
cout <<‘ Enter marks :“<< endl;
for (i = 0; i < 5; i++)
cin >> m [i];
}
voi marks :: display (void)
{
cout << “The marks are ;”;
for (i = 0; : 5; i ++)
cout << setw (4) << m [i] << end ;
}
Question 12.
How are objects passed as arguments to a function? Give an example.
Answer:
Objects as function arguments:
A function can receive an object as a function arguemeni. This is similar to any other data type being seat as function arguement. An object can be passed to a function in two way.
- Copy of entire object is passed to function (pass – by – pass value)
- Only address of the object is transferred to the function (Pass – by – reference).
In pass – by – value, a copy of the object is passed to the function. The function creates its own copy of the object and uses it. Therefore changes made to the object inside the function do not affect the original object. In pass – by – reference, when an address of an object is passed to the function, the function directly works on the original object, object used in function call.
This means changes made to the object inside the function will reflect in the original object, because the function is making changes in the original object itself. Pass – by – reference is more efficient, since it requires only to pass the address of the object and not the entire object.
Example:
Program to show objects as arguements to function.
}
5 Rupees and 75 paise converted value : 575 paise
2nd PUC Computer Science Classes and Objects Five Marks Questions and Answers
Question 1.
Explain class definition & class declaration with syntax & example.
Answer:
- A class definition is a process of naming a class, data variable & operations of a class.
- A class declaration specifier the representation of objects of the class & set of operations that can be applied to such objects.
Defining a class:
class class - name { private : Data members; Member functions; protected : Data members; Member functions ; public : Data members; Member functions ; }; Here, class Keyword used to declare a class Name ot the class
Body of the class is enclosed in a pair of curled braces. Class body contains the declaration of class members [data members & member functions]. There are generally there types of members namely private, public & protected. There are called as access specifiers.
Example:
class account { private: int accno ; | | implicit by default char name [20]; char acctype [4]; int bal - amt; public : void get - date(); | | member functions void display data (); }
Question 2.
Describe access specifiers in a class.
Answer:
Private access means a member data can only be accessed by the member function. Member declared under private are accessible only within the class. If no access specifier is mentioned, then by default, members are private,
Example:
private: int x; float y; public access means that members can be accessed by any function outside the class also. ex: class box { int length; public int width; private int height; void set - height (int i) { height = i; } int get - height () { return height; } int main () { box object; Object, length = 10; object width = 20; object, set- hight (30) : // private variable can be accessed only return 0; only through its public method }
The memers which are declared using protected can be accessed only by the member function, friends of a class & also by the member functions derived from from this class. The members cannot be accessed from outside. The protected access specifier is therefore similar to private specifiers.
Question 3.
Explain member functions.
- Inside class definition
- Outside class definition
Answer:
1. Inside class definition:
To define member function inside a class the function declaration within the class is replaced by actual function definition inside the class. A function defined in a class is treated as inline function. Only small functions are defined inside class definition.
Syntax : return – type classname (member – function)
Example:
class rectangle { int length; int breath; public: void get - data () { cin >> length; cin << breath; } void put - data (void) { cout < < length cout < < breadth } };
2. Outside class function:
A function declaration as a member of a class is known as member function. Member functions within a class must be defined seperately outside the class. The definition of member function is similar to normal function. But a member function has an ‘identity table’ in the header. This table tells the complier which class the function belongs to the scope of the member function is limited to the class mentioned in the header. Scope resolution operator :: is used to define the member function.
Syntax: For member function outside a class.
return = type classname :: member function (argl, arg2 – arg)
{
function body;
}
Question 4.
What are the characteristics of member functions outside a class?
Answer:
The member functions have following characteristics:
- Type & number of arguments in member function must be same as type & number of data declared in the class definition.
- The symbol :: is known as scope resolution operator, usage of “along with class name is the header of function definition. The scope resolution operator identifier the function as a member of particular class.
- Several classes can use same function name. Membership lable can resolve their scope.
- Member function can access private data of a class. A non- member function cannot.
Question 5.
Explain how objects of a class can be defined.
Answer:
When a class is defined, it specifies the type information the objects of the class store. Once the class is defined an object is created from that class. The objects of a class are declared in the same manner like any other variable declaration.
Syntax: Class user _ defined _ name
{
Private : / / members
Public : / / methods
}
user _ defined _ name object 1, object 2, …….. ;
Example:
Class num { Private : int x ; inty; Public : int sum (int P, int q); int diff (int P, int q) ; } void main () { num S1, S2; s1.sum (200, 300) ; s2.diff (600, 500); }
Note that an object is an instance of a class template.
Question 6.
Illustrate how an array of objects an defined.
Answer:
An array having class type elements is known as arrray od objects. An array of object is defined after the class definition and is defined in the same way as any other array.
Example:
class employee { char name [10]; int age; public: void getdata () void disp data (); }; employee supervisor[3]; employee sales - executive [5]; employee team - leader [10];
Question 7.
Describe how objects ican be used as function arguements.
Answer:
Objects as function arguments:
A function can receive an object as a function arguemeni. This is similar to any other data type being seat as function arguement. An object can be passed to a function in two way.
- Copy of entire object is passed to function (pass – by – pass value)
- Only address of the object is transferred to the function (Pass – by – reference).
In pass – by – value, a copy of the object is passed to the function. The function creates its own copy of the object and uses it. Therefore changes made to the object inside the function do not affect the original object. In pass – by – reference, when an address of an object is passed to the function, the function directly works on the original object, object used in function call.
This means changes made to the object inside the function will reflect in the original object, because the function is making changes in the original object itself. Pass – by – reference is more efficient, since it requires only to pass the address of the object and not the entire object.
Example:
Program to show objects as arguements to function.
}
5 Rupees and 75 paise converted value : 575 paise
Question 8.
Let product list be a linear array of size N where each element of the array contains following fields item code, price and quality. Declare a class product list with the three data members and member functions to perform the following.
- Add values to the product list.
- Printing the total stock value.
Answer:
Question 9.
A class clock has following members a. hour b. minute create member functions.
- To initialize the data members
- Display the time
- To convert hours and minutes to minutes.
Answer:
# include < iostream. h>
# include < conio. h>
class clock
Question 10.
Write a program that receives arrival time, departure time and speed of an automobile in kilometers / hour as input to a class. Compute the distance travelled in meters / second and display the result using member functions.
Answer:
# include < io stream . h >
# include < conio.h> class travel
{
Private