Chapter: 01 C++ Revision Tour
Jan 17, 2016
Chapter: 01 C++ Revision Tour
HEADER FILE
• Name the header file to be included for the use of following built-in functions:
(i) frexp()
(ii) toupper()
(iii) getc()
(iv) strcat()
(v) isupper()
(vi) setw()
(vii) exp()
(viii) strcmp()
(ix) isdigit()
(x) log()
(xi) scanf()
(xii) system()
(xiii)getchar()
(xiv)clrscr()
(xv) strcpy()
(xvi)getxy()
(xvii)puts()
(xviii) gets()
(xix)cos()
(xx) gotoxy()
(xxi)isalnum()
(xxii)fabs()
(xxiii)strlen()
(xxiv)exit()
HEADER FILEAnswers:
(i) frexp() math.h
(ii) toupper() ctype.h
(iii) getc() stdio.h
(iv) strcat() string.h
(v) isupper() ctype.h
(vi) setw() iomanip.h
(vii) exp() math.h
(viii) strcmp() string.h
(ix) isdigit() ctype.h
(x) log() math.h
(xi) scanf() stdio.h
(xii) system() stdlib.h
(xiii)getchar() stdio.h
(xiv)clrscr() conio.h
(xv) strcpy() string.h
(xvi)getxy() conio.h
(xvii)puts() stdio.h
(xviii)gets() stdio.h
(xix)cos() math.h
(xx) gotoxy() conio.h
(xxi)isalnum() ctype.h
(xxii)fabs() math.h
(xxiii) strlen() string.h
(xxiv) exit() process.h
Differentiation
• What is the difference between Local Variable and Global Variable? Also, give a suitable C++ code to illustrate both.
Local Variable Global Variable
1. It is a variable which is declared within a function or within a block.
1. It is variable which is declared outside all the functions
2. It is accessible only with in a function/block in which it is declared
2. It is accessible throughout the program
Differentiation
• What is the difference between #define and const? Explain with suitable example.
#define const
1. It can only define simple constants.
1. It can define any type of C++ constant including class, structure constant
2. Values assigned before compilation.
2. Values assigned while program starts running.
3. Example: #define MAX 10
3. Example: const int MAX = 10
Differentiation
• What is the difference between call by value and call by reference? Also, give a suitable C++ code to illustrate both.
• Distinguish between an object and a class• Reusability of classes is one of the major properties of
OOP. How is it implemented in C++• Differentiate between Data Hiding and Encapsulation.• What is the purpose of header file in a program?• Differentiate between the post-increment and pre-
increment
Differentiation
• Differentiate between a Run Time and Syntax Error. Also give suitable examples of each in C++.
• Differentiate between a Logical Error and Syntax Error. Also give suitable examples of each in C++.
• How is an entry controlled loop different from exit controlled loop?
• What is the difference between Actual Parameter and Formal Parameter? Also, give a suitable C++ code to illustrate both.
QUESTIONS RELATED TO ERRORSQ1. Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include[iostream.h]typedef char Text(80);void main(){Text T= “Indian”;int Count = strlen(T);cout<< T << ’has’ << Count << ‘characters’ <<endl;}
QUESTIONS RELATED TO ERRORSQ2. Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include<iostream.h>void main(){first = 4, second =6;show(first ; second);show(first);}void show(int a, int b = 30){
a = a + b;cout<< a >> b;
}
QUESTIONS RELATED TO ERRORSQ3. Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include<iostream.h>#define SIZE =10void main( ){
int a[SIZE]={10,20,30,40,50};float x=2;SIZE=5;for(i=0, i<SIZE; i++)cout<<a[i]%x ;
}
QUESTIONS RELATED TO ERRORSQ4. Rewrite the following program after removing the syntactical errors (if any). Underline each correction.
#include [iostream.h]class PAYITNOW{int Charge;PUBLIC:
void Raise(){cin>>Charge;}void Show{cout<<Charge;}
};void main(){
PAYITNOW P;P.Raise();Show();
}
QUESTIONS RELATED TO ERRORSQ5.
class code{ int num; char branch;
static int count;code (int i =0, char b);
public:code (code A){ num = A.num;
branch = A.branch;}~ code ( ) { cout<< “Destroying Object” << --count <<endl; }
};code (int i, char b){ num = i;
branch = b;}void main( ){ code x1, x2; }
QUESTIONS RELATED TO ERRORSQ6.
#include <iostream.h> struct Value { int Color,Style; } void main() {
Value Point1=(5,3); Displayelements(Point1); Value Point2=Point1; Color.Point1+=2; Displayelements(Point2);
} void Displayelements(Value P) { cout<<P.Color<<P.Style<<endl; }
QUESTIONS RELATED TO ERRORSQ7.
#include<iostream.h>
void main( )
{
struct test
{
char test_name[20];
char type;
int tickets=10;
} object;
gets(test_name);
puts(test_name);
}
QUESTIONS RELATED TO OUTPUTSQ1. Give the output of the following Program:
#include <iostream.h>int g=20;void func(int &x ,int y){
x=x-y; y=x*10;cout<<x<<“,”<<y<<“\n”;
}void main( ){
int g=7;func(g,::g);cout<<g<“,”<<::g<<“\n”;func(::g,g);cout<<g<“,”<<::g<<“\n”;
}
QUESTIONS RELATED TO OUTPUTSQ2. Give the output of the following Program:
#include<iostream.h>int a = 3;void demo (int &x, int y, int *z){
a + = x;y * = a;*z = a + y;cout<<a<<”, “<<x<< “,”<<y<<”,“<<*z<<endl;
}
void main( ){
int a = 2, b=5;demo(::a,a,&b);cout<<::a<<”,”<<a<<”,”<<b;
}
QUESTIONS RELATED TO OUTPUTSQ3. Give the output of the following Program:
#include <iostream.h> #include<conio.h> void Updatevalues(int Arr[ ], int Count) { for (int C=1;C<Count;C++)
Arr[C-1]+=Arr[C]; } void main() { clrscr(); int A[]={8,9,10},B[]={100,200,300,400},C[]={90,120}; Updatevalues(A,3); Updatevalues(B,4); Updatevalues(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] <<'#';
}
QUESTIONS RELATED TO OUTPUTSQ4. Give the output of the following Program:
#define i 5class TEMP{ static int a;
float b;public:TEMP( ){ b=10; }void INTEMP( ){
a++;b=a+10;
}void OUTTEMP( ){ cout<<a*i<<"$"<<b-3<<endl; }
};
int TEMP::a=2;void main(){
TEMP ob[5];for(int x=1;x<5;x++)
ob[x].INTEMP( );for(x=1;x<5;x++)
ob[x].OUTTEMP( );}
QUESTIONS RELATED TO OUTPUTSQ5. Give the output of the following Program:
#include<iostream.h>#include<stdio.h>#include<conio.h>void TRANSFER(char *s1,char *s2){
int n,j=0;for(int i=0;*(s1+i)!='\0';i++){
n=*(s1+i);if(n%2==0)*(s2+j++)=*(s1+i);
} *(s2+j) = ‘\0’;
}
void main(){
char *p="ChaRlesBabBaGe",q[80];TRANSFER(p,q);cout<<q<<endl;
}
QUESTIONS RELATED TO OUTPUTSQ6. Give the output of the following Program:
#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 = 30;
Control(YourNum );withdef( );cout<< “Number=”<<YourNum<<endl;
}
QUESTIONS RELATED TO OUTPUTSQ7. Give the output of the following Program:
void main( ){
char * NAME = “inForMATion”;for( int x=0;x<strlen(NAME);x++)
if(islower(NAME[x])NAME[x] = toupper(NAME[x]);
else if(isupper (NAME[x]))if(x%2==0)
NAME[x] = NAME[x -1];else
NAME[x]--;cout<<NAME <<endl;
}
QUESTIONS RELATED TO OUTPUTSQ8. Give the output of the following Program:
#include<iostream.h>void change(int &a, int b=10){ if(b%10==0)
a+=5;for(int i=5; i<=a; i++)
cout<< b++ <<‘:’;cout<<endl; }void Disp(int x){ if(x%3==0)
change(x);else
change(x, 3); }
void main(){
Disp(3);Disp(4);change(2,20);
}
QUESTIONS RELATED TO RANDOM NUMBERS
Q1. Study the following program and select the possible output from it:
#inlude<iostream.h>#include<conio.h>#include<stdlib.h>const int LIMIT = 4;void main( ){
randomize( );int Points;Points = 100 + random(LIMIT);for ( int p = Points; p>=100; p--)cout<<p<<”#”;cout<<endl;
}
1. 103#102#101#100#2. 103#102#101#1003. 101#102#103#104#4. 104#103#102#101#100#
QUESTIONS RELATED TO RANDOM NUMBERS
Q2. Observe the following program carefully, choose the possible incorrect output(s) from the options from(i) to (iv), and justify your option:
#include<iostream.h>#include<stdlib.h> const int LOW=100; void main() {
randomize( ); int POINT=10, Number; for(int i=1;i<=4;i++)
{Number=LOW+random(POINT);cout<<Number<<“ " ;POINT--;
} }
(i) 109 101 104 104(ii) 106 107 105 102(iii) 107 106 108 106(iv) 101 104 104 105
QUESTIONS RELATED TO RANDOM NUMBERS
Q3. Observe the following program carefully & choose the correct possible output from the options (i) to (iv) justifying your answer:
#include<iostream.h>#include<conio.h>#include<stdlib.h>void main( ){
clrscr( );randomize( );int RN;RN=random(4)+5;for(int i=1; i<=RN; i++)
cout<<i<<' ';getch();
}
i) 0 1 2ii) 1 2 3 4 5 6 7 8iii) 4 5 6 7 8 9iv) 5 6 7 8 9 10 11 12
QUESTIONS RELATED TO RANDOM NUMBERS
Q4. Observe the following program carefully & choose the correct possible output from the options (i) to (iv) justifying your answer:
#include<iostream.h>#include<stdlib.h>void main( ){
randomize( );int G,H=5;G=random(H)+30;for(int i=35; i>G; i--)
cout<<i<<’$’;cout<<i;
}
i) 35$34$33$32$31$30$
ii) 35$34$33$32$31
iii) 30$31$32$33$34$35$36
iv) 35$34$33$32$31$30
QUESTIONS RELATED TO RANDOM NUMBERS
Q5. In the following program , find the correct possible output(s) from the options:
#include<stdlib.h>#include<iostream.h>void main(){ randomize( );Char Weekdays[][5]={”Sun”,”Mon”,”Tue”,”Wed”,”Thu”,”Fri”,”Sat”};int Num;for(int I=0;I<3;I++){
Num =random(3)+1;cout<<Weekdays[Num]<<” : ”;
}}
Outputs:a)Wed : Thu : Fri :b)Tue : Wed : Tue :c)Sun : Mon : Tue :d)Tue : Wed : Mon :
QUESTIONS RELATED TO RANDOM NUMBERS
Q6. If the value of VAL entered by the user is 10, choose the correct possible output(s) from the options from i) to iv) and justify your option.
#include<iostream.h>#include<stdlib.h>void main(){
randomize();int VAL, Rnd; int n=random(2);Rnd=8 + random(sizeof(VAL)) * 1;while(n<Rnd)
{cout<<n<< “\t”;n++;
}}
Output options:
i) 1 2 3 4 5 6 7 8 9 10 11 12 13 ii) 0 1 2 3 4 5 6 7 8 iii) 1 2 3 4 5 iv) 1 2 3 4 5 6 7 8
QUESTIONS RELATED TO RANDOM NUMBERS
Q7. Observe the following program and find out, which option or options out of (i) to (iv) will not be expected output(s) from the program? What will be the minimum and maximum value assigned to the variable Sequence.
#include<iostream.h>#include<stdlib.h>void main( ){
int sequence, select[4] = {25, 90, 30, 45};randomize( );for ( int c= 0; c < 4 ; c++){
sequence = random( 4 - c);cout<< select[sequence] << "@";
}}
Outputs:a)45@90@30@25@b)90@25@90@25@c)30@30@25@25@d)30@30@90@25@