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<strlen(Msg);I++) puts(Msg); } (c) Rewrite the following program after removing the syntactical errors (if any).Underline each correction. 2 #include [iostream.h] class MEMBER { int Mno;float Fees; PUBLIC: void Register(){cin>>Mno>>Fees;} void Display{cout<<Mno<<" : "<<Fees<<endl;} }; void main() { MEMBER M; Register(); M.Display(); } (d) Find the output of the following program: 3 #include <iostream.h> struct GAME { int Score, Bonus;}; void Play(GAME &g, int N=10) { g.Score++;g.Bonus+=N; } void main() {
25
Embed
C++ HOLIDAY HOMEWORK ASSIGNMENT WITH MARKING …...C++ HOLIDAY HOMEWORK ASSIGNMENT WITH MARKING SCHEME CHAPTER -1 C++ REVISION 1.(a)What is the difference between Global Variable and
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
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<strlen(Msg);I++)
puts(Msg);
}
(c) Rewrite the following program after removing the syntactical errors (if any).Underline
each correction. 2
#include [iostream.h]
class MEMBER
{
int Mno;float Fees;
PUBLIC:
void Register(){cin>>Mno>>Fees;}
void Display{cout<<Mno<<" : "<<Fees<<endl;}
};
void main()
{
MEMBER M;
Register();
M.Display();
}
(d) Find the output of the following program: 3
#include <iostream.h>
struct GAME
{ int Score, Bonus;};
void Play(GAME &g, int N=10)
{
g.Score++;g.Bonus+=N;
}
void main()
{
GAME G={110,50};
Play(G,10);
cout<<G.Score<<":"<<G.Bonus<<endl;
Play(G);
cout<<G.Score<<":"<<G.Bonus<<endl;
Play(G,15);
cout<<G.Score<<":"<<G.Bonus<<endl;
}
(e) Find the output of the following program: 2
#include <iostream.h>
void Secret(char Str[ ])
{
for (int L=0;Str[L]!='\0';L++);
for (int C=0;C<L/2;C++)
if (Str[C]=='A' || Str[C]=='E')
Str[C]='#';
else
{
char Temp=Str[C];
Str[C]=Str[L-C-1];
Str[L-C-1]=Temp;
}
}
void main()
{
char Message[ ]="ArabSagar";
Secret(Message);
cout<<Message<<endl;
}
(f) In the following program, if the value of Guess entered by the user is 65, what will be
the expected output(s) from the following options (i), (ii), (iii) and (iv)? 2
#include <iostream.h>
#include <stdlib.h>
void main()
{
int Guess;
randomize();
cin>>Guess;
for (int I=1;I<=4;I++)
{
New=Guess+random(I);
cout<<(char)New;
}
}
(i) ABBC
(ii) ACBA
(iii) BCDA
(iv) CABD
2.(a) What is the difference between Actual Parameter and Formal Parameters?
Also, give a suitable C++ code to illustrate both. 2
(b) Write the names of the header files to which the following belong: 1
(i) frexp() (ii) isalnum()
(c) Rewrite the following program after removing the syntactical errors (if any).
Underline each correction. 2
#include <iostream.h>
struct Pixels
{ int Color,Style;}
void ShowPoint(Pixels P)
{ cout<<P.Color,P.Style<<endl;}
void main()
{
Pixels Point1=(5,3);
ShowPoint(Point1);
Pixels Point2=Point1;
Color.Point1+=2;
ShowPoint(Point2);
}
(d) Find the output of the following program: 3
#include <iostream.h>
void Changethecontent(int Arr[ ], int Count)
{
for (int C=1;C<Count;C++)
Arr[C-1]+=Arr[C];
}
void main()
{
int A[]={3,4,5},B[]={10,20,30,40},C[]={900,1200};
Changethecontent(A,3);
Changethecontent(B,4);
Changethecontent(C,2);
for (int L=0;L<3;L++) cout<<A[L]<<'#';
cout<<endl;
for (L=0;L<4;L++) cout<<B[L] <<'#';
cout<<endl;
for (L=0;L<2;L++) cout<<C[L] <<'#';
}
(e) Find the output of the following program: 2
#include <iostream.h>
struct Game
{
char Magic[20];int Score;
};
void main()
{
Game M={"Tiger",500};
char *Choice;
Choice=M.Magic;
Choice[4]='P';
Choice[2]='L';
M.Score+=50;
cout<<M.Magic<<M.Score<<endl;
Game N=M;
N.Magic[0]='A';N.Magic[3]='J';
N.Score-=120;
cout<<N.Magic<<N.Score<<endl;
}
(f) In the following program, if the value of N given by the user is 20, what
maximum and minimum values the program could possibly display? 2
#include <iostream.h>
#include <stdlib.h>
void main()
{
int N,Guessnum;
randomize();
cin>>N;
Guessnum=random(N-10)+10;
cout<<Guessnum<<endl;
}
Q 3.)(i)Write the names of the header files to which the following belong :
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 << “ welcome”
}
~stud() //destructor
{
}
};
stud s1;
Q6.) What are the characteristics of a Constructors?
Ans. The constructor functions have certain special characteristics:-
1. Constructor fuctions are invoked automatically when the objects are created.
2. If a class has a constructor, each object of that class will be initialized before any use is
made of the object.
3. No return type can be specified for a constructor
4. They can not be inherited through a derived class can call the base class constructor
5. A constructor may not be static.
6. It is not possible to take the address of a constructor
7. Member functions may be called from within a constructor.
Q7.) What do you understand by default constructor?
Ans A default constructor is the one that takes no arguments. It is automatically invoked
when an object is created without providing any initial values. In case, the programmer has
not defined a default constructor, the compiler automatically generates it.
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 <iostream.h>
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 <iostream.h>
void swap (int a, int b)
{ a=a+b;
b=a-b; c=a-b; cout << "Values inside
function \n”; cout<<”a=" << a << ", b="
<< b; }
int main ()
{ int x=1, y=3;
swap (x, y); cout << "Values inside
main \n”; cout << "x=" << x << ",
y=" << y; return 0; }
Output :
Values inside function
a=3, b=1
Values inside main
x=1, y=3
(Note: Changes made in formal parameters (a and b) are not reflected back to actual parameters (x and y))
Example - // passing parameters by
reference
#include <iostream.h>
void swap (int &a, int
&b) { a=a+b;
b=a-b; c=a-b; cout << "Values inside
function \n”; cout<<”a=" << a << ", b="
<< b; }
int main ()
{ int x=1, y=3;
swap (x, y); cout << "Values inside
main \n”; cout << "x=" << x << ",
y=" << y; return 0; }
Output :
Values inside function
a=3, b=1
Values inside main
x=3, y=1
(Note: Changes made in formal parameters (a and b) are reflected back to actual parameters
Theory Questions to be done in the notebook after revising all the chapters.
Q1.) Explain the following with examples
(i) Tokens
(ii) Identifiers
(iii) Literals
(iv) Pre-defined datatypes and User defined datatypes
Q2.) Differentiate between Implicit and Explicit type casting.
Q3.) Differentiate between run-time and syntax error. Give example of each.
Q4.) Differentiate between Procedural Programming and Object Oriented programming.
Q5.) Explain the basic concepts of OOP’s with examples.
Q6.) Explain the advantages and disadvantages of OOP’s
Q7.) What is base class? What is derived class? How are they interrelated?
Q8.) What is function overloading ? What is it’s need? Give an example illustrating its use in a C++ .
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.