Author
others
View
23
Download
0
Embed Size (px)
C++ HOLIDAY HOMEWORK ASSIGNMENT WITH MARKING SCHEME
CHAPTER -1 C++ REVISION
1.(a)What is the difference between Global Variable and Local Variable? Also, give a
suitable C++ code to illustrate both. 2
(b) Which C++ header file(s) will be essentially required to be included to run /execute
the following C++ code: 1
void main()
{
char Msg[ ]="Sunset Gardens";
for (int I=5;I>Mno>>Fees;}
void Display{cout
GAME G={110,50};
Play(G,10);
cout
cout
for (int L=0;L
Q 3.)(i)Write the names of the header files to which the following belong :
(1/2) MARK EACH
puts(), sin(), setw(), sqrt(), strcat(), gets(), strcpy(), abs(), isupper(), pow(), random(),
strcmp(), isalnum(), isalpha(), fabs()
(ii) In the following program, find the correct possible output(s) from the options: (2)
#include
#include
void main ( )
{
randomize( );
char Area[][10]= {“NORTH”, “SOUTH”, “EAST”, “WEST”};
int ToGo;
for(int I = 0; I
cin>>N;
Guessme=random(N)+10;
cout
int a[SIZE]={10,20,30,40,50};
float x=2;
SIZE=5;
for(int i=0;i
help of random function , if the number generated by the random number is
supposed to be between the range of 20-2000 2
#include
#include
randomize();
r=___________________//statement 1
cout
void main()
{
char str[]="SUCCESS";
cout
CHAPTER – OBJECT ORIENTED PROGRAMMING
1.(a) What do you understand by Data Encapsulation and Data Hiding? Also, give a suitable
C++ code to illustrate both. 2
(b) Answer the questions (i) and (ii) after going through the following class: 2
class Seminar
{
int Time;
public:
Seminar() //Function 1
{
Time=30;cout
(NoCandidates/100+1)
Public Members
• A function SCHEDULE() to allow user to enter values for TestCode,
Description, NoCandidate & call function CALCNTR() to calculate the number of
Centres
• A function DISPTEST() to allow user to view the content of all the data members
(d) Answer the questions (i) to (iv) based on the following: 4
class PUBLISHER
{
char Pub[12];
double Turnover;
protected:
void Register();
public:
PUBLISHER();
void Enter();
void Display();
};
class BRANCH
{
char CITY[20];
protected:
float Employees;
public:
BRANCH();
void Haveit();
void Giveit();
};
class AUTHOR : private BRANCH , public PUBLISHER
{
int Acode;
char Aname[20];
float Amount;
public:
AUTHOR();
void Start();
void Show();
};
(i) Write the names of data members, which are accessible from objects belonging to class
AUTHOR.
(ii) Write the names of all the member functions which are accessible from objects
belonging to class BRANCH.
(iii) Write the names of all the members which are accessible from member functions of
class AUTHOR.
(iv) How many bytes will be required by an object belonging to class AUTHOR?
Q2. (a) What do you understand by Polymorphism? Give a suitable example of the
same. 2
(b) Answer the questions (i) and (ii) after going through the following program: 2
class Match
{
int Time;
public:
Match()
//Function 1
{
Time=0;
cout
• A data member Distance of type float
• A data member Fuel of type float
• A member function CALFUEL() to calculate the value of Fuel as per the
following criteria
Distance Fuel
void Sales_Detail();
};
(i) Write the names of data members which are accessible from objects belonging to
class CUSTOMER.
(ii) Write the names of all the member functions which are accessible from objects
belonging to class SALESMAN.
(iii) Write the names of all the members which are accessible from member functions of
class SHOP.
(iv) How many bytes will be required by an object belonging to class SHOP?
Q3.) Answer the questions (i) to (iv) based on the following code:
class Cricketer
char category[20];
protected:
float match_fee;
void calc_match_fee(float);
public:
Cricketer();
void CInput();
void CShow();
};
class Bowler : public Cricketer
char BOWLERName[20];
int Wickets;
public:
Bowler();
void BOWInput();
void BOWShow();
};
class Batsman : public Cricketer
char BASTSMANName[20];
int Centuries;
float Bat_Average;
public:
Batsman();
void BATInput();
void BATShow();
};
(i) Which type of Inheritance is shown in the above example?
(ii) How many bytes will be required by an object of the class Batsman?
(iii) Write name of all the data members accessible from member functions of the class
Bowler?
(iv) Write the name of all the member functions accessible by an object of the class
Batsman?
Q4. (a) Differentiate between a default and a parameterized constructor in context of class
and object . Give suitable example in C++. 2
(b) Answer the questions (i) and (ii) after going through the following class : 2
class Computer
{
char C_name[20];
char Config[100];
public:
Computer(Computer &obj); // function1
~Computer(); //function 2
};
(i) Write the statement(s) which will invoke the function 1.
(ii) Name the specific feature of the class shown by function 2. Also
write the time of its invoke.
(c) Define a class Shop in C++ with the description given below : 4
private members
name array of 40 characters
address array of 40 characters
type of item array of 3X20 characters
availqty array of 3 integers
totalqty array of 3 integers
public members
init() function to ask and store the values of address ,type
of item from the user and updates the totalqty and avialqty
accordingly .
display() function to display the details of the item in the
following format :
Name :
Address :
Items :
Balance Stock :
(d) Answer the questions (i) to (iv) based on the following code : 4
class Goods
{
int id;
protected :
char name[20];
long qty;
void Incr(int n);
public :
Goods();
~Goods();
void get();
};
class Food_products : protected Goods
{
char exp_dt[10];
protected :
int id;
int qty;
public :
void getd();
void showd();
};
class Cosmetics : private Goods
{
int qty;
char exp_date[10];
protected :
int id;
public :
~Cosmetics();
Cosmetics();
void show();
};
(i) Name the all protected members of class Food_products.
(ii) Name the member functions accessible through the object of class
Food_products.
(iii) From the following, Identify the member function(s) that cannot be called
directly from the object of class Cosmetics
show()
getd()
get()
(iv) If the class cosmetics inherits the properties of food_products class also,
then name the type of inheritance.
Q5.) Differentiate between data abstraction and data hiding. 2
b. Answer the questions (i) and (ii) after going through the following class : 2
class Exam
{
int Rollno;
char Cname[25];
float Marks ;
public :
Exam( ) //Function 1
{
Rollno = 0 ;
Cname=””;
Marks=0.0;
}
Exam(int Rno, char candname) //Function 2
{
Rollno = Rno ;
strcpy(Cname,candname);
}
~Exam( ) //Function 3
{
cout
A data member Cname of type string
A data members Agg_marks (Aggregate Marks) of type float
A data members Grade of type char
A member function setGrade () to find the grade as per the aggregate marks
obtained by the student. Equivalent aggregate marks range and the respective grade as
shown
below.
Aggregate Marks Grade
>=80 A
Less than 80 and >=65 B
Less than 65 and >=50 C
Less than 50 D
Public members:
A constructor to assign default values to data members:
Rno=0,Cname=”N.A”,Agg_marks=0.0
A function Getdata () to allow users to enter values for Rno. Cname, Agg_marks and call
function setGrade () to find the grade.
A function dispResult( ) to allow user to view the content of all the data members.
d. Give the following class definition answer the question that is follow: 4
class University
{
char name [20];
protected :
char vc[20];
public :
void estd();
void inputdata();
void outputdata();
}
class College : protected University
{ int regno;
protected
char principal()
public :
int no_of_students;
void readdata();
void dispdata ( );
};
class Department : public College
char name[20];
char HOD[20];
public :
void fetchdata(int);
void displaydata( ); }
i). Name the base class and derived class of college.
ii) Name the data member(s) that can be accessed from function displaydata().
iii)What type of inheritance is depicted in the above class definition?
iv) What will be the size of an object (in bytes) of class Department?
SOLVED QUESTIONS AND ANSWERS
Q1. Name the header files of following built in functions :
Strcpy(), strcat(),log(), clrscr(),setw(),fabs(),isalnum(),isupper()
Ans. Strcpy()- string.h
Strcat()- string.h
log() - math.h
clrscr() - conio.h
setw - iomanip.h
fabs() - math.h
isalpnum() - ctype.h
isupper() - ctype.h
Q 2. Q4. What are the advantages and disadvantages of inline functions?
Ans. The main advantages of inline functions is that they save on the overheads of a
function call as the function is not invoked, rather its code is replaced in the program.
The major disadvantage of inline functions is that with more function calls, more memory
is wasted as for every function call, the same function code is inserted in the program.
Repeated occurrences of same function code waste memory space.
Q3.) What are static class members?
Ans A class can have static data members as well as static member functions The static
data members are the class variables that are common for all the objects of the class. Only
one copy of static data members is maintained which is shared by all the objects of the
class. They are visible only with in the class.
Q4.) What is the significance of access specifiers in a class?
Ans A class provides three access specifiers namely private, public and protected
(a) A data member declared as private or protected remains hidden from outside world and
it can only be accessed by the member functions of the class.
(b) A data member declared as public is made available to the outside world. That is, it can
be accessed by any function, any expression in the program but only by using an object of
the same class type.
Q5.) What is destructor ? What is its need ? Ans. Destructor:
A destructor is also a member function whose name is the same as the class name but is
preceded by tilde (‘~’)
A destructor takes no arguments and no return types can be specified for it. It is called
automatically by the compiler when an object is destroyed. A destructor cleans up the
storage ( memory area of the object) that is no longer accessible.
Need for Destructors
Allocated resources (like constructor) must be de-allocated before the object is destroyed.
It works as a de-allocating and releasing memory area and it perform our works as a clean
up tasks. Therefore, a destructor is equally useful as a constructor is.
class stud
{
stud() //constructor
{
cout
Q8.) What is copy constructor?
Ans Copy constructor:
A copy constructor is a constructor that can be used to initialize one object with the values
from another object of same class during declaration.
Q9.) What is the relationship between constructor and its class? Define constructor also
Ans Both carry the same name. It is a function that automatically initializes the data
members of an object at the time of its creation.
Constructor : It removes the memory held by an object, It is the last member function to be
called. Morevoer, it carries the same name of the class with tidle (~) as prefix.
Q10. What is Inheritance ?
Ans. It is a special feature of OOPS. Inheritance is capability to inherit the properties of
one class in to another class.
The derive new class is called derived class (sub class) and old class is called based class
(super class).
The Class whose properties of data members are inherited, is called Base Class or Super
Class and the class that inherits these properties, is called Derived Class or Sub Class.
Exp1:- If Class A inherits the data of Class B then we can say A is Sub Class and B is
Super Class.
Q11. Define the needs and objectives of Inheritance.
Ans The major needs and objectives of inheritance are:
(i) It ensures the closeness with the real world models.
(ii) It extend the functionality of an existing class.
(iii) It establishes “a kind of” relationship.
(iv) It helps in reuse of an existing class by a subclass (reusability).
(v) It implements transitive nature( if a class Y inherits properties from class X, then all
subclassY will automatically inherit the properties of X)
(vi) The redundancy can be reduced by abstracting a super class from few sub classes.
(vii) It is conept of reusability.
Q12.) Give the following definitions, answer the questions that follow:-
#include
class book
{
char title[20];
char author[20];
int noof pages;
public:
void read();
void show();
};
class textbook: private textbook
{
int noofchapters, noof assignments;
protected:
int standard;
void readtextbook();
void showtextbook();
};
class physicsbook: public textbook
{
char topic[20];
public:
void readphysicsbook();
void showphysicsbook();
};
(i) Name the members, which can be accessed from the member functions of class
physicsbook.
(ii) Name the members, which can be accessed by an object of Class textbook.
(iii) Name the members, which can be accessed by an object of Class physicsbook.
(iv) What will be the size of an object (in bytes) of class physicsbook.
Ans
(i) standard , readtextbook(),showtextbook() and topic;
(ii) readtextbook() and showtextbook()
(iii) readphysicsbook(), showphysicsbook(), readtextbook() and showtextbook()
(iv) The size of object of physicsbook= size of book + size of Textbook + size of
physicsbook.
= 42+6+20 = 68 bytes
Q13.) Consider the following declarations and answer the questions given below:
Class vehicle
{
int wheels;
protected:
int passenger;
public:
void inputdata( int, int);
void outputdata();
};
class heavyvehicle: protected vehicle
{
int dieselpetrol;
protected:
int load;
public:
void readdata( int, int);
void writedata();
};
class bus:private heavyvehicle
{
char marks[20];
public:
void fetchdata(char);
void displaydata();
};
(i) Name the class and derived class of the class heavyvehicle.
(ii) Name the data members that can be accessed from function displaydata()
(iii) Name the data members that can be accessed by an object of bus class
(iv) Is the member function outputdata() accessible to the objects of heavyvehicle class.
Ans
(i) base class = vehicle, derived class = bus
(ii) The data members passenger, load, make are available to function display data
(iii) No data members can be accessed by the object of bus calss.
(iv) No member functions outputdata () is not accessible to the objects of heavy vehicle
class.
Q13 . What type of C++ class members ( data members and member functions) are not
inherited?
Ans Data member : Static data members of the base class are not inherited by the derived
class
Member functions: Constructors and destructors of base class are not inherited.
Q14.) Differentiate between Call by Value and Call by Reference. Ans. Comparison: S.N.
Parameters passed by value Parameters passed by reference
1. When we call a function by passing parameters by value, the values of actual parameters get copied into the formal parameters but not the variables themselves.
When we call a function by passing parameters by reference, formal parameters create a reference or pointer directly to the actual parameters.
2. The changes made in the values of formal parameters will not be reflected back to the actual parameters.
The changes made in the values of formal parameters will be reflected back to the actual parameters.
3. Example – // passing parameters by
value
#include
void swap (int a, int b)
{ a=a+b;
b=a-b; c=a-b; cout
Q9.) Explain the process of disambiguation in C++ program.
Q10.) Explain the scope rules of a class.
Q11.) what are inline member function? Explain its use.
Q12.) What is the significance of access specifiers in a class?
Q13.)Explain the different visibility modes in context with Object Oriented Programming.
Q14.)What is the relationship of a class and its objects? How is memory allocated to a class and its objects?
Q15.) What are static data members of a class? Explain the characteristics of a static data member.
Q16.) What do you understand by a default constructor? What is its role? How is it equivalent to a constructor
having default arguments?
Q17.)What do you understand by constructor and destructor functions used in classes? How are these functions
different from other member functions ?
Q18.) What is copy constructor? Elaborate with suitable examples.
Q19.) Discuss the various situations when a copy constructor is automatically invoked.
Q20.) What are the advantages of inheritance in Object oriented programming?
Q21.)Differentiate between members, which are present within the private visibility mode with those which are
present within the public visibility modes.
Q22.) Describe how is an object of a class that contains objects of other classes created?
Boolean Algebra
Q23.)What is the significance of Principle of Duality?
Q24.) Write the dual of the following Boolean expression
(a) (x+y’) (b) xy+xy’+x’y
(c) a+a’b+b’ (d) (x+y’+z)(x+y)
Q25.) State all the basic theorems of Boolean Algebra
Q26.) What is Logic gate? Draw all the logic gates with truth table.
Q27.) Prove De Morgans Law