Tuesday, May 9, 2017

CBSE Class 12 - Computer Science - C++ - OOPS, Class and Objects (Q and A) (#cbseNotes)

C++ - OOPS, Class and Objects

CBSE Class 12 - Computer Science - Questions and Answers

CBSE Class 12 - Computer Science - C++ - OOPS, Class and Objects (Q and A) (#cbseNotes)

Q1: Define class.

Answer: A class is collection of data (data member) and functions (member functions or methods)
working on the data. It can be seen as a blueprint for the object. Data define the attributes and methods define behaviours of the class.


Q2: Does memory allocated when a class is created? 

Answer: No memory is allocated when a class is created. Memory is allocated only when an object is created.


Q3: Which keyword is used to begin the definition of a class?

Answer: class




Q4: How is class body enclosed? Give an example.

Answer: Every class’s body is enclosed in a pair of braces ( { and } ) and ends with a semicolon.

class Student {
int roll_num;
    char name[40];
public:
void displayName();
};



Q5(MCQ): Which of the following does not take up computer memory?
(a) Class
(b) Object
(c) Both of these
(d) None of these

Answer: (a) Class


Q6: What is an object?

Answer: An Object is an instance of the class means have all the properties defined in the class.


Q7: What are member functions of a class?

Answer: Member functions are the methods which are declared/defined inside the class and operate upon the data member.


Q8: What is Data Abstraction?

Answer: Data abstraction means representing essential features without including background details. Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.


Q9: What do you mean by data encapsulation?

Answer: It means binds the data and its functions into a single unit called class. Encapsulation of a class makes it possible to use the class without knowing how it works. Other users of the class need to know only what it does, not how it does it.

e.g. class circle publishes its function to draw the circle. How it draws on the screen, is internal to the class.


Q10: What is Object Oriented Programming (OOP)?

Answer: Object-oriented programming is an approach to programming that uses the concept of classes and objects.


Q11: Write the syntax notation to define a class.

Answer:

class <class_name>
{
private:
declaration of data member;
declaration/definition member function;
protected:
declaration of data member;
declaration/definition member function
public:
declaration of data member;
declaration/definition member function
};


Q12: What is modularity in Object oriented programming? How does it help?

Answer: The act of partitioning a complex program into simpler fragments called modules is called as modularity.
It reduces the complexity to some degree
It creates a number of well-defined boundaries within the program.



Q13: What is data hiding? How it is achieved in C++?

Answer: Data hiding is a property of OOPS by which the crucial data of an object is made hidden from the rest of the program or outside world.
In C++ data hiding is provided by using access specifiers – Private & Protected.


Q14: Define polymorphism. How it is implemented in C++?

Answer: 'Poly' means many and 'morphs' mean form, so polymorphism means one name multiple forms. Polymorphism in object-oriented programming is the ability of objects belonging to different data types to respond to calls of methods of the same name, each one according to an appropriate type-specific behaviour.

C++ implements Polymorphism through
Function Overloading
Operator overloading
Virtual functions



Q15: What is the purpose of access specifiers in C++? List its main types in C++.

Answer: Access specifiers are used identifying access rights for the data and member functions of the class.

There are three main types of access specifiers in C++ programming language:
private
public
protected


Q16: Differentiate between Abstraction and Encapsulation.

Answer:


Abstraction Encapsulation
1. Solves the problem at the design level. 1. Solves the problem at the implementation level.
2. Used for hiding unwanted data and giving relevant data. 2. Hiding code and data into a single unit to protect data from outside access.
3. Focus on what the object does instead of how it does. 3. Hiding details or make it internal how the object does something.
4. e.g. Car Its outer layout like number of seats, dashboard, tyres etc. 4. e.g. Inner layout of car, how internal wiring is done to connect dashboard, engine etc.


Q17: How are public class members  accessed?

Answer: A data member and member function declared under public access specifier can be accessed by the objects directly i.e. using dot (.) operator.

e.g. object.datamember

Q18: Will an attempt by a function (not a member of class) to access a private member of the class result in error or not? If there is an error will it be runtime or compile time?

Answer: It will be a compilation error.

  

No comments:

Post a Comment