Page 1 :
CHAPTER-1, C++ REVISION TOUR, 1., , Ans., , 2., Ans., , 3., Ans., 4., Ans., 5., Ans., , Find out the errors, if any, in the following C++ statement:, (i) cout<<"="a;, (ii) m=5, n=12; o=15, (iii) cout<<"x";<<x;, (iv) cin>>y;>>j;, (v) cin>>"\n">>y;, (vi) cout>>"\n "abc";, (vii)a = b + c, (viii) break = x*y;, (i) cout<<"="a;, (ii) m=5, n=12; o=15, (iii) cout<<"x";<<x;, (iv) cin>>y;>>j;, (v) cin>>"\n">>y;, (vi) cout>>"\n "abc";, (vii) a = b + c, (viii) break = x*y;, What is the difference between fundamental data types and derived data types? Explain with examples., Fundamental data types, Derived data types, These are the data types that are not composed of, These are tha data types that are composed of, any other data type., fundamental data types., There are five fundamental data types: char, int,, These are: array, function, pointer, reference, constant,, float, double and void., class, structure, union and enumeration., Example: int a=10;, float b;, Example: float marks[50]; Const int a=5;, What is the purpose of a header file in a program?, Header files provide function prototypes, definitions of library functions, declaration of data types and constants used, with the library functions., What main integer types are offered by C++?, C++ offered three types of integers : short, int and long. Each comes in both signed and unsigned versions., Explain the usage of following with the help of an example:, (i) constant, (ii) reference, (iii) variable, (iv) union, (i) constant: The keyword const can be added to the declaration of an object to make that object a constant rather, than a variable. Thus, the value of the named constant cannot be altered during the program run. The general form of, constant declaration is as follows: const type name = value. Example: const int a=10;, (ii) reference: A reference is an alternative name for an object. A reference variable provides an alias for a previously, defined variable. The general form of declaring a reference variable is: Type &ref-var = var-name;, where type is any valid c++ data type, ref-var is the name of reference variable that will point to variable denoted by, var-name., Example:, int total;, int &sum=total;, total=100;, cout<<"Sum="<<sum<<"\n";, cout<<"Total="<<total<<"\n";, In above code both the variables refer to the same data object in the memory, thus, print the same value., 1
Page 2 :
6., Ans., , 7., Ans., 8., Ans., 9., Ans., 10., Ans., 11., Ans., , 12., , (iii) variable: Variables represent named storage locations whose value can be manipulated during program run., Variables are generally declared as: type name; where type is any C++ data type and name is the variable name. A, variable can be initialized either by a separate assignment statement following its declaration as shown below:, int age;, age = 10;, (iv) union: A union is a memory location that is shared by two or more different variables, generally of different types, at different times. Defining a union is similar to defining a structure. Following of declaration declares a union share, having two variables and creates a union object cnvt of union type share:, union share{, int i;, char ch;, };, union share cnvt;, union can referred to the data stored in cnvt as either an integer or character. To assign the integer 20 to element i of, cnvt, write - cnvt.i = 20;, To print the value of element ch of cnvt, write - cout<<cnvt.ch;, How many ways can a variable be initialized into? Give examples for each type of initialization., A variable can be initialized into two ways as following:, (i) By separate assignment statement: Example:, int age;, age = 10;, (ii) At the time of declaration: Example: int age = 10;, (iii) Dynamic initialization: Example:, float avg = sum/count;, How are the following two statements different?, char pcode = 75;, char pcode = ‘K’;, The first statement treats 75 as ascii value and converts it into its relative character ‘K’ whereas second statement, stores character ‘K’ and does not make any conversion., How are the following two statements different?, char pcode = 75;, short pcode = 75;, The first statement treats 75 as ascii value and converts it into its relative character ‘K’ whereas second statement, treats 75 as number., If value is an identifier of int type and is holding value 200, is the following statement correct?, char code = value, VALID Statement, The data type double is another floating-point type. Then why is it treated as a distinct data type?, The data type double is treated as a distinct data type because it occupies twice as much memory as type float, and, stores floating-point numbers with much larger range and precision. It stands for double precision floating-point. It is, used when type float is too small or insufficiently precise., Explain the impact of access modifier const over variables. Support your answer with examples., The access modifier const can be added to the declaration of an object to make that object a constant rather than a, variable. Thus, the value of the named constant cannot be altered during the program run whereas the value of the, variable can be changed during the program run., Example:, void main(){, const int a=10;, int b=20;, a++;, cout<<"a="<<a;, b++;, cout<<"b="<<b;, }, What are arithmetic operators in C++? Distinguish between unary and binary arithmetic operators. Give examples, for each of them., , 2
Page 3 :
Ans., , Following are the arithmetic operators in C++:, addition(+), subtraction(-), multiplication(*), division(/) and reminder(%)., Unary arithmetic operators, Binary arithmetic operators, Unary Operators has only one operand, Binary operators (”bi” as in “two”) have two operands, The Unary Operators are ++,--,&,*,+, - etc., The +, -, &&, <<, ==, >> etc. are binary operators., Example:, , short x = 987;, x = -x;, , Example:, , int x, y = 5, z;, , z = 10;, x = y + z;, , 13., Ans., , 14., , Ans., , 15., Ans., , What is the function of increment/decrement operators? How many varieties do they come in? How are these two, varieties different from one another?, The increment operator, ++ adds 1 to its operand and the decrement operator -- subtract 1 from its operands., The increment/decrement operator comes in two varieties as following:, (i) Prefix version and, (ii) Postfix version:, Prefix version, Postfix version, Performs the increment or decrement operation, First uses the value of the operand in evaluating the, before using the value of the operand., expression before incrementing or decrementing the, operand’s value., Example: sum = 10;, Example: sum = 10;, ctr = 5;, ctr = 5;, sum = sum + (++ctr);, sum = sum + (ctr++);, State why are following expression invalid?, (i) asm = 5100 || val < 35, (ii) age > 70 && < 90, (iii) income >= 500 ||&& val < 500, (iv) res !> 20 ||! X > 20, (i) In this expression single ‘=’ operator is used for comparison which is not valid., (ii) In this expression variable’s name is not mentioned after the ‘&&’ operator which is not valid., (iii) In this expression both ‘||’ and ‘&&’ operators are written together which is not valid., (iv) In this expression use of ‘!>’ is invalid and !x>20 should be written in parenthesis like (!x>20)., What is the difference between Type Casting and Automatic Type conversion? Also, give a suitable C++ code to, illustrate both., Type Casting, Automatic Type conversion, It is an explicit process of conversion of a data from, It is an implicit process of conversion of a data from one, one type to another., type to another., It is performed with the help of casting operator()., It is performed by compiler its own., Example:, Example:, int A=1, B=2;, int N = 65;, float C = (float)A/B;//Type Casting, char C = N; //Automatic type conversion, cout<<C;, cout<<C;, Output: 0.5, Output: A, , 16., , ., , 17., , 3
Page 4 :
40, Ans., 41, Ans., 42, Ans., , 43, Ans., , 44, Ans., , 45, , Briefly describe the importance of iostream.h file., iostream.h is most important header file for basic input and output operations. iostream.h contain built-in functions, cout and cin for input-output. So, every C++ program should include iostream.h header file for performing input and, output operations., How do the following two statements differ in operation?, cin>>ch;, cin.get(ch);, The difference between cin>>ch and cin.get(ch) is that when >> operator is used, the white spaces (e.g., tabs), spaces, etc. and new-line characters are ignored whereas it is not so with cin.get(ch)., What is an array? What is the need for array?, Array: An array is a collection of variables of the same type that are referenced by a common name., Need for array: Arrays are very much useful in a case where many variables of the same (data) types need to be, stored and processed., For example, suppose we have to write a program in which can accept salary of 50 employees. If we solve this, problem by making use of variables, we need 50 variables to store employee’s salary. Remembering and managing, these 50 variables is not an easy task and it will make the program a complex and lengthy program. This problem can, be solved by declaring 1 array having 50 elements; one for employee’s salary. Now we only have to remember the, name of that 1 array., What do you understand by two-dimensional arrays? State some situation that can be easily represented by twodimensional arrays., Two-dimensional array: A two-dimensional array is an array in which each element is itself an array. For instance, an, array A[m][n] is an M by N table with M rows and N columns containing M x N elements. Two-dimensional arrays are, used to represent tables, matrices, etc., For example, below marks table can be represented easily by 2D array :, Maths, , Physics, , Chemistry, , CS, , Englisg, , Toal, , Amit, , 60, , 64, , 64, , 82, , 79, , 349, , Chandar, , 84, , 70, , 82, , 79, , 69, , 349, , Lalit, , 79, , 68, , 86, , 87, , 85, , 698, , Can you think of a difference between an array of strings and other two-dimensional arrays? What is it? Support, your answer with examples., Array of strings, Two-dimensional array, Array of string is used to store string value., Two-dimensional array is used to store numeric value., The first index determines the number of strings and The first index determines the number of rows and the, the second index determines maximum length of, second index determines maximum columns of each string., each string., Example:, Example:, int a[2][3];, char string[3][31];, int i,j;, int i;, for(i=0;i<2;i++), cout<<"Enter 3 strings:";, {, for(i=0;i<3;i++), for(j=0;j<3;++j), cin.getline(string[i],31);, {, cout<<"Enter element:";, cin>>a[i][j];, }, }, If an array is initialized at the time of declaration, what thing one must bear in mind?, 15
Page 5 :
Ans., , 46, Ans., , 47, Ans., , 48, Ans., , 49, , Ans., , 50, Ans., , The general form of array initialization is as shown below:, type array-name[size N] = {value-list};, If an array is initialized at the time of declaration, following thing one must bear in mind:, The element values in the value-list must have the same data type as that of type, the base type of the array., In character array, you must make sure that the array you declare is long enough to include the null., What is meant by unsized array initialization in C++? What are its benefits?, Unsized array initialization means skip the size of the array I an initialization statement. The C++, then automatically, creates an array big enough to hold all the initializers present and calculates the dimensions of unsized arrays., Example:, char S1[] = “First String”;, Advantages:, Less tedious., Allow to change any of the values without fear of using incorrect array dimensions., We may lengthen or shorten the value-list without changing the array dimensions., What do you meant by function prototyping? Write down the advantages of function prototypes in C++., A function prototype is a declaration of the function that tells the program about the type of value return by the, function the number and type of arguments., Example:, int sum(int a, int b);, Advantages:, Enables a compiler to carefully compare each use of the function with the prototype to determine whether, the function is invoked properly i.e., the number and type of arguments are compared and any wrong number, or type of the arguments is reported., Tells the program about the return type, Tells the program the number and type of arguments., What are actual and formal parameters of a function?, The arguments passed to the functions while the function is called is known as the actual arguments, whereas the, arguments declared in the function header is called as formal arguments., Ex. Suppose sum() is a function., int sum(int x, int y) /*Here x and y are called formal arguments*/, {...}, void main() {, int ans;, ...., ans = sum(3,5); /*Here the arguments 3 and 5 are called actual arguments*/, ..., getch();, }, Construct function prototype for descriptions given below:, test(), takes no arguments and has no return value., convert(), takes a float argument and returns an int., promote() takes two double arguments and returns a double., sum(), takes an int array and an int value and returns a long result., check(), takes a string argument and returns an int., (i) void test();, (ii) int convert(float a);, (iii) double promote(double a, double b);, (iv) long sum(int a[], int b);, (v) int check(string s);, What do you understand by default arguments and constant argument? Write a short note on their usefulness., Default arguments: C++ allows us to assign default values to a function’s parameters which are useful in case a, matching argument is not passed in the function call statement. The default values are specified at the time of, 16
Page 6 :
51, Ans., , 52, Ans., , function declaration. Example: float interest(float principal, int time, float rate=0.10);, Constant argument: By constant argument, it is meant that the function cannot modify these arguments. In order to, make an argument constant to a function, we can use the keyword const as shown : int sum (const int a, const int b);, The qualifier const in function prototype tells the compiler that the function should not modify the argument. The, constant arguments are useful when functions are called by reference., How is call-by-value method of function invoking different from call-by-reference method? Give appropriate, examples supporting your answer., Call By Value, Call by reference, In call by value method, the called function, In call by reference method, the called function, creates its own copies of the original values send, accesses ad works with the original values using their, to it., references., The changes done in the function in formal, The changes done in the function are reflected back, parameter are not reflected back in the calling, in the calling environment., environment., It use ‘&’ sign as the reference operator., It does not use the ‘&’ sign, Example:, #include <iostream.h>, void change(int x, int y){, x = 10; /*change the value of x */, y = 20; /*change the value of y */, }, void change(int x, int y);, , Example:, #include <iostream.h>, void change(int *x, int *y){, *x = 10; /*change the value of x */, *y = 20; /*change the value of y */, }, void change(int *x, int *y);, , void main (){, // local variable declaration:, int a = 100;, int b = 200;, , void main (){, // local variable declaration:, int a = 100;, int b = 200;, , cout<<"Before value of a "<<a <<endl;, cout<<"Before value of b "<<b<< endl;, change(a, b);, cout<<"After value of a "<<a<<endl;, cout<<"After value of b "<<b<<endl;, }, , cout<<"Before value of a "<<a <<endl;, cout<<"Before value of b "<<b<< endl;, change(&a, &b);, cout<<"After value of a "<<a<<endl;, cout<<"After value of b "<<b<<endl;, }, , Discuss the similarities and difference between global and local variables in terms of their lifetime and scope., Local variable, Global variable, It is a variable which is declared within a, It is a variable which is declared outside all, function or within a compound statement., the functions., It is accessible only within a function/compound, It is accessible throughout the program, statement in which it is declared, A global variable comes into existence when the, A local variable comes into existence when, program execution starts and is destroyed when, the function is entered and is destroyed, the program terminates., upon exit., , Example:, , #include <iostream.h>, float NUM=900;, //NUM is a global variable, void LOCAL(int T), {, int Total=0;, //Total is a local variable, for (int I=0;I<T;I++), Total+=I; cout<<NUM+Total;, 17
Page 7 :
}, void main(){, LOCAL(45);, }, 53, Ans., , 54, Ans., , 55, , Ans., , What is structure? Declare a structure in C++ with name, roll number and total marks as components., A structure is a collection of variables referenced under one name. A structure is declared using the keyword struct as, in following syntax:, struct <structure tag>, {, [public:] | [private:] | [protected:], /* data members’ declarations */, /* member functios’ declarations */, };, Example:, struct Student, {, char Name[30];, int Rollno;, float Total_Marks;, };, What are Nested structures? Give an example., A structure within a structure is called nested structures., Example:, struct addr, //structure tag, {, int houseno;, char area[26];, char city[26];, char state[26];, };, struct emp, //structure tag, {, int empno;, See, address is a structure variable itself and it is, char name[26];, member of another structure, the emp structure., char desig[16];, addr address;, float basic;, };, emp worker;, // create structure variable, The structure emp has been defined having several elements including a structure address also. The, address is itself a structure of type addr. While defining such structures are defined before outer structures., Write a program that asks the user to enter two integers, obtains the two numbers from the user, and outputs the, large number followed by the words “is larger by – units than smaller number” to the system console (e.g., if the, larger number is 9 and smaller is 6, message should be “9 is larger by 3 units than smaller number”)., If the numbers are equal print the message “These numbers are equal”., #include<iostream.h>, #include<conio.h>, void main(), {, int a,b,dif;, clrscr();, cout<<"Enter a:";, 18
Page 8 :
cin>>a;, cout<<endl<<"Enter b:";, cin>>b;, if(a>b), {, dif=a-b;, cout<<a<<"is larger by"<<dif<<"units than smaller number"<<endl;, }, else if(b>a), {, dif=b-a;, cout<<b<<"is larger by"<<dif<<"units than smaller number"<<endl;, }, else, cout<<"These numbers are equal"<<endl;, getch();, 56, , Ans., , }, Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tanks of, CNG by recording the miles driven and the gallons used for each tank., Develop a C++ program that will input the kilometers driven and gallons used for each tank., The program should calculate and display the kilometers per gallon obtained for each tank of gasoline., After processing all input information, the program should calculate and print the average kilometers per gallon, obtained for all tanks., Formulate the algorithm as flowchart., Write a C++ program as instructed., Test, debug, and execute the C++ program., #include<iostream.h>, #include<conio.h>, void main(){, clrscr();, int tanks=0;, float tot_km; float avg_k_p_g;, cout<<"Enter how many tanks filled :";, cin>>tanks;, float *kms=new float[tanks];, float *gallons_used=new float[tanks];, float *k_p_g=new float[tanks];, for(int i=0;i<tanks;i++), {, cout<<"Enter how much kilometers covered for tank "<<i+1<<" ";, cin>>kms[i];, cout<<"Enter how much gallon used from tank "<<i+1<<" ";, cin>>gallons_used[i];, k_p_g[i]=kms[i]/gallons_used[i];, cout<<"KMs per Gallon obtained for tank No. "<<i+1<<" "<<k_p_g[i]<<endl;, tot_km+=k_p_g[i];, cout<<endl;, }, avg_k_p_g=tot_km/tanks;, cout<<"Average kilometers per gallon obtained for all tanks is "<<avg_k_p_g;, getch();, }, , 19