Wednesday 12 April 2017

Tips for C++ Revision Tour

1) Know keywords, identifiers, literals, punctuators, operators 

2) Know header files and associated functions

3) Identify the errors and rewrite corrected code

4) Predict the output:

   Be mindful of the following:
   i) function call - actual parameters and formal parameters
   ii) order of actual parameters
   iii) call by value and call by reference
   iv) If call by reference, be mindful of the changes in both actual and formal arguments
   v) Look out for endl or "\n" operators, if any, in output statements

5) Predict the output - character array

   Follow the code from main() function.
   Be mindful of the original string and the character, if any that is passed as actual parameters.
   In the function, manually dry run the changes that are taking place in the original string based on the if condition.
   These changes will be reflected in the original string.
   Display the updated string
   string[i-1] signifies prev character, string[i]-1 signifies decrementing the character by 1

Predict the output - numeric array
Read code in main()
Be mindful of the actual parameters and formal parameters
Reflect the changes in array as required
Write the contents of the updated array as specfied in cout statement.

6) Random question
Identify the minimum and maximum values variable can take while storing random numbers.
Start from option 1 onwards, and do the elimination process
Finally arrive at the ones which cannot be eliminated


      

FUNCTION OVERLOADING ARGUMENT MATCHING


Function name
Formal
Actual
Remarks
CHECK
int, int   - #1
int,double
Ambiguity

float,double - #2
float,int
Ambiguity


int,char
char,int
int,int
char,char
#1
#1
#1
#1


double,double
#2


Friday 7 April 2017

Review of C++ Exercise 1 - Key

REVIEW OF C++ - CLASS TEST  - KEY
a) Name the header file to which the following belong:  (1)
i. pow( )
ii. random( )
i) math.h   ii) stdlib.h
b) Find the output of the following program: (2)
#include<iostream.h>
void main()
{ long Number = 7583241;
int First=0, Second=0;
do
{ int R=Number%10;
if (R%2==0)
First+=R;
else
Second+=R;
Number /=10;
} while (Number>0);
cout<<First-Second;
}
-2
c) Illustrate the use of local and global variables in C++.  (2)
int x=10; // global variable
void FUNCTION()
{
int x=5;  // local variable
cout<<x<<::x; // displays value in local and global variable
}
d) Differentiate between a Run Time Error and Syntax Error. Also give suitable
examples of each in C++. (2)
Errors occurring during execution of the program is run time error. Eg: logical error in the code like the infinite loop or hardware fault like insufficient memory.
When the rules of the programming language is violated, it results in compilation error.
Eg: cout>>10;
e) Name the header file(s) that shall be needed for successful compilation of the
following C++ code  : (1)
void main ( )
{
char String [20];
gets (String);
strcat (String, “CBSE”);
puts (String);
}
String.h, stdio.h
f) Rewrite the following program after removing the syntactical error(s) if any.
Underline each correction. (2)
# include <iostream.h>
const int Max 10;
void main ( )
{
int Numbers [Max];
Numbers = { 20, 50,10, 30,40 } ;
for (Loc= Max-1 ; Loc > = 0 ; Loc - -)
cout>>Numbers [Loc];
}
g) Find the output of the following program : (3)
# include<iostream.h>
void Withdef (int HisNum = 30)
{
for (int I=20 ; I<=HisNum; I+=5)
cout<<I<<" ";
cout<<endl;
}
void Control (int &MyNum)
{
MyNum+=10;
Withdef(MyNum);
}
void main ()
{
int YourNum=15;
Control (YourNum);
Withdef();
cout<<"Number="<<YourNum<<endl;
}

20 25
20 25 30
Number=25
h) In the following C++ program what is the expected value of MyMarks from
Options (i) to (iv) given below. Justify answer. (2)
#include<stdlib.h >
# include<iostream.h>
void main ()
{
randomize ();
int Marks []= {99, 92, 94, 96, 93, 95}, MyMarks;
MyMarks = Marks [1 + random (2) ];
cout<<MyMarks<<endl;
}
(i) 99                                                                            (ii) 94
(iii) 96                                                                          (iv) None of the above
ii)
i)Differentiate between a Logical Error and Syntax Error. Also give suitable examples
of each in C++. (2)
j) Name the header file(s) that shall be needed for successful compilation of the
following C++ code :  (1)
void main( )
{
char Text[40];
strcpy(Text,”AISSCE”);
puts(Text);}

stdio.h, string.h
K) Rewrite the following program after removing the syntactical error(s), if any.
Underline each correction.  (2)
#include <iostream.h>
const int Size 5;
void main()
{
int Array[Size];
Array = {50,40,30,20,10};
for(Ctr=0; Ctr<Size; Ctr++)
cout>>Array[Ctr];
}
l) In the following C++ program what is the expected value of Myscore from Options
(i) to (iv) given below. Justify your answer.   (2)
#include<stdlib.h>
#include<iostream.h>
void main( )
{
randomize();
int Score[] = {25,20,34,56, 72, 63}, Myscore;
Myscore = Score[2 + random(2)];
cout<<Myscore<<endl; }
(i) 25
(ii) 34
(iii) 20
(iv) None of the above
ii)
m) Find the output of the following program : (3)
#include<iostream.h>
struct POINT
{int X, Y, Z;};
void StepIn(POINT & P, int Step=1)
{
P.X+=Step;
P.Y -=Step;
P.Z+=Step;
}
void StepOut(POINT & P, int Step=1)
{
P.X-=Step;
P.Y+=Step;
P.Z-=Step;
}
void main ( )
{
POINT P1={15, 25, 5}, P2={10, 30, 20};
StepIn(P1);
StepOut(P2,4);
cout<<P1.X<<","<<P1.Y<<","<<P1.Z<<endl;
cout<<P2.X<<","<<P2.Y<<","<<P2.Z<<endl;
StepIn(P2,12);
cout<<P2.X<<","<<P2.Y<<","<<P2.Z<<endl;
}

16,24,6
6,34,16

18,22,28

Review of C++ - Exercise 1

REVIEW OF C++ - CLASS TEST  - TOTAL MARKS – 25 – DURATION – 1 HR

a) Name the header file to which the following belong:  (1)
i. pow( )
ii. random( )
b) Find the output of the following program: (2)
#include<iostream.h>
void main()
{ long Number = 7583241;
int First=0, Second=0;
do
{ int R=Number%10;
if (R%2==0)
First+=R;
else
Second+=R;
Number /=10;
} while (Number>0);
cout<<First-Second;
}
c) Illustrate the use of local and global variables in C++.  (2)
d) Differentiate between a Run Time Error and Syntax Error. Also give suitable
examples of each in C++. (2)
e) Name the header file(s) that shall be needed for successful compilation of the
following C++ code  : (1)
void main ( )
{
char String [20];
gets (String);
strcat (String, “CBSE”);
puts (String);
}
f) Rewrite the following program after removing the syntactical error(s) if any.
Underline each correction. (2)
# include <iostream.h>
const int Max 10;
void main ( )
{
int Numbers [Max];
Numbers = { 20, 50,10, 30,40 } ;
for (Loc= Max-1 ; Loc > = 0 ; Loc - -)
cout>>Numbers [Loc];
}

g) Find the output of the following program : (3)
# include<iostream.h>
void Withdef (int HisNum = 30)
{
for (int I=20 ; I<=HisNum; I+=5)
cout<<I<<" ";
cout<<endl;
}
void Control (int &MyNum)
{
MyNum+=10;
Withdef(MyNum);
}
void main ()
{
int YourNum=15;
Control (YourNum);
Withdef();
cout<<"Number="<<YourNum<<endl;
}

h) In the following C++ program what is the expected value of MyMarks from
Options (i) to (iv) given below. Justify answer. (2)
#include<stdlib.h >
# include<iostream.h>
void main ()
{
randomize ();
int Marks []= {99, 92, 94, 96, 93, 95}, MyMarks;
MyMarks = Marks [1 + random (2) ];
cout<<MyMarks<<endl;
}
(i) 99                                                                            (ii) 94
(iii) 96                                                                          (iv) None of the above
i)Differentiate between a Logical Error and Syntax Error. Also give suitable examples
of each in C++. (2)
j) Name the header file(s) that shall be needed for successful compilation of the
following C++ code :  (1)
void main( )
{
char Text[40];
strcpy(Text,”AISSCE”);
puts(Text);}

K) Rewrite the following program after removing the syntactical error(s), if any.
Underline each correction.  (2)
#include <iostream.h>
const int Size 5;
void main()
{
int Array[Size];
Array = {50,40,30,20,10};
for(Ctr=0; Ctr<Size; Ctr++)
cout>>Array[Ctr];
}
l) In the following C++ program what is the expected value of Myscore from Options
(i) to (iv) given below. Justify your answer.   (2)
#include<stdlib.h>
#include<iostream.h>
void main( )
{
randomize();
int Score[] = {25,20,34,56, 72, 63}, Myscore;
Myscore = Score[2 + random(2)];
cout<<Myscore<<endl; }
(i) 25
(ii) 34
(iii) 20
(iv) None of the above
m) Find the output of the following program : (3)
#include<iostream.h>
struct POINT
{int X, Y, Z;};
void StepIn(POINT & P, int Step=1)
{
P.X+=Step;
P.Y -=Step;
P.Z+=Step;
}
void StepOut(POINT & P, int Step=1)
{
P.X-=Step;
P.Y+=Step;
P.Z-=Step;
}
void main ( )
{
POINT P1={15, 25, 5}, P2={10, 30, 20};
StepIn(P1);
StepOut(P2,4);
cout<<P1.X<<","<<P1.Y<<","<<P1.Z<<endl;
cout<<P2.X<<","<<P2.Y<<","<<P2.Z<<endl;
StepIn(P2,12);
cout<<P2.X<<","<<P2.Y<<","<<P2.Z<<endl;
}



Object Oriented Programming Structure - Features

Object Oriented Programming Structure (OOPS) – total – 12 marks
Theory – 2/12  marks
1)      What is Data Hiding?

Data hiding is the OOPS(Object Oriented Programming Structure) concept that enables data to be hidden from the outside world i.e. the main() function. By default , a  class is private and all the private members cannot be accessed by the object of the class. It can only be accessed indirectly through its member functions. In this sense, data is hidden.
There is no possibility that someone maintaining the code may inadvertently point to or otherwise access the wrong data unintentionally. Thus, all data not required by an object can be said to be "hidden."

Eg:
class sample
{
 int x;           // data hidden
 float y;   // data hidden
 public:
 void enter()
 {
  cin>>x>>y;
 }
 void display()
 {
  cout<<x<<y;
}};
 void main()
 { sample s;
   s.enter();  //x and y accessed indirectly through public member function enter()
 }

2)      What is data abstraction
Data abstraction is the act of representing only essential features and hiding background details.
For eg, we know the operation of a Television, but we may not know the internal hardware structure and the working of Television.


C++ example:
class sample
{
 int x;           // data abstraction
 float y;   // These details are completely abstracted from the class to the outside world
 public:
 void enter()
 {
  cin>>x>>y;
 }
 void display()
 {
  cout<<x<<y;
}};
 void main()
 { sample s;
   s.enter();  //object s accesses enter(), without knowing the details of x and y
 }


3)      What is an entity?
A thing with physical and independent existence. In C++ , it is termed as an instance of a class or an object.

4)      What are properties and behavior of the class?
The properties are the attributes of the class i.e. data members
The member functions are the behavior of the class.

5)      What is a class?
A class is a user-defined data type that contains within itself data members and member functions together called as members i.e. a class binds the data that describes an entity and functions together.

6)      What is meant by instantiation? Why is the object called instance of the class?
Instantiation is the process of a variable being constructed by the subroutines of the class.


7)      What is an object?
An object is an instance of a class. A class’s functions may be implemented only by way of the object.

Example of class and object
class sample
{
 int x;
 float y;
 public:
 void enter()
 {
  cin>>x>>y;
 }
 void display()
 {
  cout<<x<<y;
}};
 void main()
 { sample s;
   s.enter();
 }

8)      What is encapsulation? Illustrate with an example in C++?
The wrapping up of data and functions into a single unit is encapsulation.

Eg:

class sample
{
 int x;            
 float y;    
 public:
 void enter()
 {
  cin>>x>>y;
 }
 void display()
 {
  cout<<x<<y;
}};
 void main()
 { sample s;
   s.enter();  //s , the object and enter(), the function are both wrapped together as a 
}                    // single unit to execute the member function enter()

9)      What is the difference between a concrete class and an abstract class?
The only real difference is that a concrete class can be instantiated because it provides (or inherits) the implementation for all of its methods. An abstract classcannot be instantiated because at least one method has not been implemented.Abstract classes are meant to be extended.
10)  What is polymorphism? How is it implemented?
The act of representing a message in more than one form is called polymorphism. Poly means many, morph means changing from one form to another.
Polymorphism is represented by way of function overloading.
Eg:
Eg:
void prnsqr(int i);//overloaded for integer
void prnsqr(char c);// overloaded for character
void prnsqr(float f);//overloaded for float
void prnsqr(double d);//overloaded for double


void prnsqr(int i)
{
cout<<"Integer "<<i<<"\n";
}
void prnsqr(char c)
{
cout<<c<<" is a character "<<"\n";
}
void prnsqr(float f)
{
cout<<" Float "<<f"\n";
}
void prnsqr(double d)
{
cout<<"Double "<<d<<"square is"<<i*i<<"\n";
}