OF...  · Web viewmodifier is keyword introduced by the C++ compiler to prevent the variable being...

25
REVIEW OF CLASS XI Short questions: PART- I(SOLVED) 1. What are literals in C++.How many types of literals you know in C+ +. Ans Literals (also known as constants) are expressions with a fixed value. These are the data items whose value never change during execution of the program. It is often referred as literals. Five types of literals are there in C++ Integer literals , Floating point literals, character literal, string literals, Boolean values . 2. What is the difference between ‘a’ and “a”? Ans ‘a’ is simply a character constant that occupies one byte in memory while “a” is string literal that stores a+ \0 character in memory that means it stores 2bytes in memory. 3. What is the size of the following :’\n’, ”hello\t”, ”Saima”, ’ \’. Ans ’\n’ : 1 byte ,it is an escape sequence ”hello\t” : 7bytes (hello takes 5 bytes ,\t is an esacpe sequence takes 1byte , one for null character) ”Saima” : 6bytes( 5 bytes for saima and one for null charcter ) ’ \’ : 1 byte ,it is character constant . 4. Predict the output or error(s) for the following: void main() { int const p=5; p = p+1 ; cout<<p; } Ans Compiler error: Cannot modify a constant value.

Transcript of OF...  · Web viewmodifier is keyword introduced by the C++ compiler to prevent the variable being...

REVIEW OF CLASS XI Short questions:

PART- I(SOLVED)

1. What are literals in C++.How many types of literals you know in C++.

Ans Literals (also known as constants) are expressions with a fixed value. These are the data items whose value never change during execution of the program. It is often referred as literals.Five types of literals are there in C++Integer literals , Floating point literals, character literal, string literals, Boolean values .

2. What is the difference between ‘a’ and “a”?

Ans ‘a’ is simply a character constant that occupies one byte in memory while “a” is string literal that stores a+ \0 character in memory that means it stores 2bytes in memory.

3. What is the size of the following :’\n’, ”hello\t”, ”Saima”, ’ \’.

Ans ’\n’ : 1 byte ,it is an escape sequence

”hello\t” : 7bytes (hello takes 5 bytes ,\t is an esacpe sequence takes 1byte ,

one for null character)

”Saima” : 6bytes( 5 bytes for saima and one for null charcter )

’ \’ : 1 byte ,it is character constant .

4. Predict the output or error(s) for the following:

void main() {int const p=5;p = p+1 ;cout<<p;}

Ans Compiler error: Cannot modify a constant value.

5. What is lvalue and rvalue in variable.

Ans lvalue: (pronounced as “ell-value”) is the address in the memory in which value of the data is stored . Here in this example address value =103.

rvalue : (pronounced as “are-value”) is the value of the data entered in the variable. In this example rvalue is 10

6. What are constants.

Ans A constant is a value of any type that has the same value and can never change. It is of three types:

literals : A literal is an unnamed constant used to specify data. If we know that the data cannot be changed ,then we simply code the data value itself in a statement.

For example : char x= ’a’ ; // where ‘a’ is a character literal

Defined constants: In certain types of programs ,the same constant may be used many times .For instance ,if you are using the value of PI in your program for several different calculations.

For example: # define PI 3.14

Const modifier is keyword introduced by the C++ compiler to prevent the variable being modified in the program. It is used to tell the compiler that variable that follows const cannot be modified .

example: const int x=9;7. What is enum ?

Ans Enumerated data type (also known as enum type) is one of the user defined data type which gives you an opportunity to define your own data type and define what values of these variables can take.

8. Differentiate between structure and an array.Ans Structure is a collection of heterogenous data while an array is a collection

of homogenous data. Example:struct sample

{ float price; int prod_id;};

array :int a[5] ; ///storing 5 values of type int.

9. What do you understand by the term type casting?Ans Converting an expression of a given type into another type is known as type-casting.

Example int x=9Float y= float (x) ;//now x is float not int

1. What is the difference between while and do while. Ans

while do while It is an entry controlled loop It is an exit controlled loop.If the condition is true then only it executes the body of the loop.

If the condition is false then also it gets executed at least once.

while loop  : Do

 while (condition)

{

Statements;}

{

Statements;

}while(condition);

2. What is the difference between exit() and break.Ans exit() brings the control out of the program It is mainly used in

menu driven programs and for error checking condition where we don’t want to check for any other condition if the invalid input is entered .break : is used in loops and so on, and stops the current code block before running onto the next portion of code. (If used in a for loop that is to run 20 times for example, if break is used on the 8th iteration, the for loops ends and moves on to run whatever code comes next).

3. What is the wrong with the following code #include <iostream.h>int main (){int n;for (n=1 ; n<9; n++){cout << n << ", "; if (n==3) {cout << "countdown aborted!"; }break;}return 0;}

Ans break cannot come outside the loop therefore it is an error in C++ as misplaced break.

4. Underline the error and rewrite the corrected code : int k = 10;

do ; { cout << k++;

while k < 50 ; }

Ans Correction:

wrong code corrected code int k = 10;

do ; { cout << k++;

while k < 50 ;

int k = 10;do //semicolon is removed

{ cout << k++;

while ( k < 50) ;

} // while must have braces }

5. Name the header file(s) that shall be needed for successful compilation of the following C++ code :

void main( ){char Text[40];strcpy(Text,”AISSCE”);puts(Text);}

Ans strcpy() is defined in<string.h>and puts() is defined in <stdio.h>

6. In the following program, if the value of N given by the user is 15, what maximum

and minimum values the program could possibly display? 2

#include <iostream.h>#include <stdlib.h>void main(){int N,Guessme;randomize();cin>>N;Guessme=random(N)+10;cout<<Guessme<<endl;}

Ans Maximum Value: 24 Minimum Value: 10

7. 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;}

Ans Maximum Value: 19 Minimum Value: 10

8. In the following C++ program what is the expected value of Myscore from

Options (i) to (iv) given below. Justify your answer. #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

Ans (ii) It can generate 34 because

score[2+ random(2) ] where random (2) will generate random numbers from 0to1

therefore value of myscore can either be 34 or 56

(remember : indexing of array is from zero)

9. What are the two ways of calling a function?Ans Call by Value: In this method, the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. In pass by value, the changes made to formal arguments in the called function have no effect on the values of actual arguments in the calling functionCall by Reference: In this method, the addresses of actual arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses, we would have an access to the actual arguments and hence we would be able to manipulate them.

10. What are the differences between formal arguments and actual arguments?

Ans Actual arguments:The arguments that are passed in a function call are called actual arguments. These arguments are defined in the calling function.

Formal arguments:The formal arguments are the parameters/arguments in a function declaration. The scope of formal arguments is local to the function definition in which they are used. Formal arguments belong to the called function. Formal arguments area copy of the actual arguments. A change in formal arguments would not be reflected in the actual arguments.

11. main(){show();

}void show(){cout<<"I'm the greatest";}

Ans Compiler error: Type mismatch in redeclaration of show.Explanation:When the compiler sees the function show it doesn't know anything about it. So the default return type (ie, int) is assumed. But when compiler sees the actual definition of show mismatch occurs since it is declared as void. Hence the error.The solutions are as follows:1. declare void show() in main() .2. define show() before main().3. declare extern void show() before the use of show().26. How local variable differs from global variable in terms of declaration.

Ans

12. Declare a structure customer to store the following data :Customer name ( string), phoneno(integer),age(integer) and use address in the structure.Ans struct customer

{ char name[20];int phoneno;

int age;address a;}cust;

13. Find the syntax error in the following code:Main()

{ Struct employee { int empno;

char Ename[20];float Basic,hra,da ;double Netpay;

} ; emp1,emp2}

Ans Terminator should come after emp1 and emp2 i.emain()

{ struct employee { int empno;

char ename[20];float basic,hra,da ;double netpay;

} emp1,emp2;}

14. write a macro for finding maximum number and print the maxvalue .

Ans // function macro#include <iostream.h>#define getmax(a,b) ((a)>(b)?(a):(b))

int main(){ int x=10, y; y= getmax(x,2); cout << y << endl; cout << getmax(70,x) << endl; return 0;}

Output:

1070

15. Write a program to create a function to accept student data and pass this structure by reference to function where student is the structure with the fields as name and class[4](characterarray),age and rollno as an integer.

Ans. #include<iostream.h>

#include<conio.h>struct student {

char name [20]; int age; int roll; char clas[4];};void getdata(student &st);void main(){ student stud; clrscr();

getdata(stud);cout<<”\nname is “<<stud.name;cout<<”\n age is”<<stud.age;cout<<”\n roll is ”<<stud.roll;cout<<”class is “<<stud.clas;

}//end of main

void getdata( student & st){ cout<<”name “; cin>> st.name; cout<<”age “; cin>> st.age; cout<<”roll “; cin>> st.roll; cout<<”class “; cin>> st.clas;}//end of function

PART -IIHOTS (HIGH ORDER THINKING QUESTIONS):1. What would be contents of following after array initialization?

int A[5]={3,8 ,9}Ans:

3 8 9 0 02. Suggest storage class for following variables

½ each

1. a normal variable.

2. very heavily used variable.3. a variable that should retain its value after function is over.4. a variable that spanes multiple files.5. a variable global in one & not available in another file.

3. Find the output of the following code:main( )

{ int n[3][3] = { 2, 4, 3, 6, 8, 5, 3, 5, 1 } ; cout<< n[2][1] ;

} 4. Find the output of the following code:

main( ) { int twod[ ][ ] = { 2, 4, 6, 8 } ; cout<< twod ; }

5. Point out the errors, if any, in the following program segments: (a) /* mixed has some char and some int values */

int char mixed[100] ; main( ) { int a[10], i ; for ( i = 1 ; i <= 10 ; i++ ) { cin>> a[i] ; cin>> a[i] ; } }

6. What is the difference between Object Oriented Programming and Procedural

Programming?

Ans:

7. Write the names of the header files to which the following belong: (i) frexp() (ii) isalnum()

8. Write the names of the header files to which the following belong: 1

(i) strcmp() (ii) fabs()9. Find the output of the following program: #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] <<’#’;}

10. Find the output of the following program: #include <iostream.h>struct PLAY{ int Score, Bonus;};void Calculate(PLAY &P, int N=10){P.Score++;P.Bonus+=N;}void main(){PLAY PL={10,15};Calculate(PL,5);cout<<PL.Score<<”:”<<PL.Bonus<<endl;Calculate(PL);cout<<PL.Score<<”:”<<PL.Bonus<<endl;Calculate(PL,15);cout<<PL.Score<<”:”<<PL.Bonus<<endl;}

11. What is the difference between these two statements ?a. int arr[5]; b. int arr[5]=90;

Ans: a. This statement declares and array of base type int to store five elements.

b. This statement initializes 6th element of an array with value 90.12. Give the output of the following code :

#include<iostream.h>void main(){

char mat[3][2]={‘b’ ,’a’ ,’n’ ,’g’ ,’h’ ,’i’ };cout<< mat[2];

}Ans : Output is : hi ( it starts reading from 2row and 2 column) 13. What is difference between getch and getche()?

Ans getch() : waits for the user to input a character and displays the output till the user enters a character.As soon as the user enters a character it transfers the control back to the main function, without displaying what character was entered.

getche() : does the same thing but it displays the character entered. Here e stands for echo.

14. What is the difference between strcmp() and strcmpi()?15. What is purpose of including <time.h >while generating

random numbers. 16. In the following program, if the value of N given by the user is

30, 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;}

17. Name three types of debugging errors found commonly in C++.

18. What is the difference between run time error and logical error?

19. What is the difference between syntax error and logical error?20. Have a look at this C++ Program.find the problem in this code?

#include<iostream.h> void main(void) { int base,height,area; cout<<"Enter the base and height of the triangle:"; cin>>base>>height; area=0.5*base*height; cout<<endl<<"AREA:"<<area; }

21. Find the syntax error in the following code:struct employee{ int empno;

char Ename[20];float Basic,hra,da ;float Netpay=100;

}22. Declare a macro for calculating area of circle Ans: #define CIRCLE_AREA(x) (PI* (x) * (x) )

23. Find the output of the following program : #include<iostream.h>struct Package{int Length, Breadth, Height;};void Occupies(Package M){cout<<M.Length<<“x“<<M.Breadth<<”x“;cout<<M.Height<<endl;}void main(){Package Pl={100,150,50}, P2, .P3;++P1.Length;Occupies(P1);P3 = P1;++P3.Breadth;P3.Breadth++;Occupies (P3);P2 = P3;P2.Breadth+=50;P2. Height--;Occupies(P2);}

24. Find the output of the following program : #include<iostream.h>#include<string.h>#include<ctype.h>void Change(char Msg[],int Len){for (int Count =0; Count<Len; Count++ ){if (islower(Msg[Count]))Msg[Count]= toupper{Msg[Count]);else if (isupper(Msg[Count]))Msg[Count]= tolower(Msg[Count]);else if (isdigit(Msg[Count]))Msg[Count]= Msg[Count] + 1;else Msg[Count] = ‘*’ ;}}void main(){char Message [] = "2011 Tests ahead";int Size = strlen(Message);Change(Message,Size);cout<<Message<<endl;for (int C = 0,R=Size-l;C<=Size/2; C++,R--){

char Temp = Message[C];Message[C]= Message[R];Message[R]= Temp;}cout<<Message<<endl;}

25. Observe the following program GAME.CPP carefully, if the value of Num entered by the user is 14, choose the correct possible output(s) from the options from (i) to (iv),and justify your option. //Program : GAME.CPP#include<stdlib.h>#include<iostream.h>void main(){randomize();int Num, Rndnum;cin>>Num;Rndnum = random (Num) + 7;for (int N = 1; N<=Rndnum ; N++)cout<<N<<" ";}Output Options :(i) 1 2 3(ii) 1 2 3 4 5 6 7 8 9 10 11(iii) 1 2 3 4 5(iv) 1 2 3 4

26. (a)Rewrite the following program after removing the syntactical error(s),

if any. Underline each correction. #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];}27. Rewrite the following program after removing all the syntax error(s),if

any. Underline each correction.

#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(Point 1);Pixels Point2 = Point1Color.Point1+=2;ShowPoint(Point2);

}28. Define the function SwapArray(int[ ], int),that would expect a 1D

integer arrayNUMBERS and its size N. the function should rearrange the array in such a way that the values of that locations of the array are exchanged. (Assume the size of the array to be even).Example :If the array initially contains {2, 5, 9, 14, 17, 8, 19, 16}Then after rearrangement the array should contain {5, 2, 14, 9, 8, 17, 16, 19}

Ans:void SwapArray(int NUMBERS[ ], int N){ int i,j,temp;/* cout<<”\nThe elements before doing the desired alterations…”;for(i=0;i<N;i++)cout<<NUMBERS[i]<<’\t’; */for(i=0;i<N-1;i+=2){ temp=NUMBERS[i]; NUMBERS[i]=NUMBERS[i+1]; NUMBERS[i+1]=temp;}/* cout<<”\nThe elements after completed the desired alterations…”;for(i=0;i<N;i++)cout<<NUMBERS[i]<<’\t’; */}

29. (a) Which C++ header file(s) will be essentially required to be included to run /execute the following C++ code: void main(){char Msg[ ]="Sunset Gardens";for (int I=5;I<strlen(Msg);I++)puts(Msg);}

Ans

(a) (i) string.h (ii) stdio.h (b) Rewrite the following program after removing the syntactical errors (if any).

Underline each correction. #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();}

Ans #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;M.Register();M.Display();} (c) Find the output of the following program: #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;}Ansoutput

#agaSbarr

(d) In the following program, if the value of Guess entered by the user is 65, whatwill be the expected output(s) from the following options (i), (ii), (iii) and (iv)? #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 Ans (i) ABBC 30. (a) What is the difference between Actual Parameter and

Formal Parameters?Also, give a suitable C++ code to illustrate both .AnsActual Parameter Fromal parameterIt is a parameter, which is used in function call to send the value to function header parameters that is formal parameters

It is a parameter, which is used in function header, to receive the value from the calling environment that is from actual parameters

#include <iostream.h>void Calc(int T) //T is formal parameter{cout<<5*T;}void main(){int A=45;Calc(A); //A is actual parameter

}

(b) Write the names of the header files to which the following belong: (i) frexp() (ii) isalnum()Ans (i) math.h (ii) ctype.h (c) Find the output of the following program: #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;}AnsTiLeP550 2AiLJP430

31. (a) What is the difference between call by value and call by reference? Give an

example in C++ to illustrate both.Ans Call by value Call by reference Call by value is used to create a temporary copy of the data coming from theactual parameter into the formal parameter. The changes done in the function in formal parameter are not reflected back in the calling environment. It does not use ‘&’ sign.

Call by reference is used to share the same memory location for actual andformal parameters and so changes done in the function are reflected back inthe calling environment. It uses ‘&’ sign.

void Compute(int A, int &B){A++;B++;cout<<”In the function”<<endl;cout<<”A=”<<A<<“&”<<“B=”<<B<<endl;

}void main (){int I=50,J=25;cout<<”Before function call “<<endl;cout<<”I=”<<I<<”&”<<”J=”<<J <<endl;Compute (I,J) ;cout<<”After function call “<<endl;cout<<I=”<<I<<”&”<<”J=”<<J <<endl;}OUTPUTBefore function callI=50&J=25In the functionA=51&B=26After function callI=50&J=26

(c) Rewrite the following program after removing the syntactical errors (if any).Underline each correction.

#include [iostream.h]#include [stdio.h]class Employee{int EmpId=901;char EName [20] ;publicEmployee(){}void Joining() {cin>>EmpId; gets (EName);}void List () {cout<<EmpId<<” : “<<EName<<endl;}};void main (){Employee E;Joining.E();E.List()} (d) Find the output of the following program : #include <iostream.h>#include <ctype.h>void Encode (char Info [ ], int N) ;void main ( ){char Memo [ ] = “Justnow” ;Encode (Memo, 2) ;cout<<Memo<<endl ;}

void Encode (char Info [ ], int N){for (int I = 0 ; Info[I] !=‘\0’ ; 1++)if (1%2= =0)Info[I] = Info[I] –N ;else if (islower(Info[I]))Info[I] = toupper(Info[I]) ;elseInfo[I] = Info[I] +N ;}(e) Study the following program and select the possible output from it : #include <iostream.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;}(i) 103#102#101#100#(ii) 100#101#102#103#(iii) 100#101#102#103#104#(iv) 104#103#102#101#100#(b) Study the following program and select the possible output from it :#include <iostream.h>#include <stdlib.h>const int MAX=3 ;void main ( ){randomize( ) ;int Number ;Number = 50 + random{MAX) ;for (int P=Number; P>=50; P– –)cout<<p<< “ # ” ;cout<<endl;}(i) 53#52#51#50#(ii) 50#51#52#(iii) 50#51#(iv) 51#50# (c) 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();

} (d) Find the output of the following program:

#include <iostream.h>#include <ctype.h>void Encrypt(char T[]){

for (int i=0;T[i]!='\0';i+=2)if (T[i]=='A' || T[i]=='E') T[i]='#';

else if (islower(T[i])) T[i]=toupper(T[i]);else T[i]='@';

}void main(){

char Text[]="SaVE EArtH";//The two words in the string Text//are separated by single space

Encrypt(Text);cout<<Text<<endl;

}(b) Find the output of the following program: 2

#include<iostream.h>#include<string.h>#include<ctype.h>void Convert(char Str[],int Len){for (int Count =0; Count<Len; Count++ ){if (isupper (Str [Count] ) )Str[Count]= tolower(Str[Count]);else if (islower (Str [Count] ) )Str[Count]= toupper(Str[Count]);else if (isdigit (Str [Count]))Str[Count]= Str[Count] + 1;else Str[Count] = ‘*’;}}void main (){

char Text [] = “CBSE Exam 2005”;int Size=strlen(Text);Convert(Text,Size);cout<<Text<<endl;for (int C = 0,R=Size-l;C<=Size/2; C++,R--){char Temp = Text[C];Text [C] = Text [R] ;Text [R] = Temp;}cout<<Text<<endl;}

Long questions:1. Write a program to find sum of the following series.

x3/!4 -x5/!6 +x7/!8………….X n/ !(n+1)2. write a program to capitalize 1st letter of each word defining a

function passing array as a parameter.3. Write a program to check whether the string is palindrome or not.4. Write a program to Reverse a String Source5. To count the number of repetitive characters in a string.6. write a program to Concatenate two Strings 7. Write a program to store book details of three books and display

the information according to the bookno(isbn) given .8. An interactive c program to read basic salary of 15 persons. each

person gets 25% of basic as HRA, 15%of basic as conveyance allowances, 10%of basic as entertainment allowances.The total salary is calculated by adding basic+HRA+CA+EA.Calculate how many out of 15 get salary above 10,000.Rs also print the salary of each employee

9. Write a program to add length of the two roads provided in feet and inches using structures.