Tuesday, 21 March 2017

Review of C++ - Text Book Questions

Text Book P -89 Very Short/Short Answer Questions:

2) What is the difference between fundamental data types and derived data types? Explain with examples.
Fundamental data types are those data types that are not composed of other data types.
eg int x; float f;
Derived data types derive other types from the fundamental types by using declaration operators.
int arr[4]; const int x=10;

3) What is the purpose of a header file in a program?

enables us to work with all declarations/definitions which are inside the header files.

4) What main integer types are offered by C++?
short, int , long

5) Explain the usage of the following with the help of an example.
constant - value of the named constant cannot be altered during the program run.
const int x = 5;

reference -  is an alternative name for a variable. it provides an alias name for previously defined variable.
int total = 50;
int &sum=total;
total+=25;
cout<<sum; // will display 75

variable:named memory location.
eg:int x;

Union: user defined data type . it is a memory location that is shared by two or more different variables.
union share{
int i;
char ch;
";
union share cnvt;

6) How many ways can a variable be initialized into? Give examples for each type of initialization.

3 ways.
1) int x=9;
2) int x(9);
3) int x=9+3;

7)How are the following 2 statements differ?
  char pcode =75;
  char pcode = 'K';
 1st one integer constant is assigned to a character variable, 2nd a character constant is assigned.

9)If value is an identifier of int type and is holding value 200, is the following statment correct?
  char code = value;

compiler doesnt give error, but since char holds maximum 127 as value, it results in data loss.

10)The data type double is another floating point type. Then why is it treated as a distinct data type?

 because it stores maximum size in floating point

11)Explain the impact of access modifier const over variables. Support your answer with examples.
 By prefixing const to  data types, it doesnt allow to change values during program run.

const int x=5;

12) What are the arithmetic operators in C++? Distinguish between unary and binary arithmetic operators. Give examples for each of them.
=,-,*,/,%
unary - operates on a single operand -
a++;b--;
binary - operates on two operands -
c=a+b;
c=a%b;

13) 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 function of increment/decrement operators is to increment or decrement the operands/variables by 1.They come in 2 varieties - pre and post. i.e. pre-increment/pre-decrement, post-increment/post-decrement. pre-increment is based on change and use rule. post-increment is based on use and change rule.

19) Observe the following C++ code and write the name(s) of the header files which will be essentially required to run it in a C++ compiler.
void main()
{
char CH,STR[20];
cin>>STR;
CH=toupper(STR[0]);
cout<<STR<<" starts with "<<CH<<endl;
}
iostream.h, ctype.h

20) Observe the following C++ code and write the name(s) of the header files which will be essentially required to run it in a C++ compiler:
void main()
{
int Number;
cin>>Number;
if(abs(Number)==Number)
 cout<<"Positive:"<<endl;
}
iostream.h, math.h

21) string.h, iostream.h

24) #include <iostream.h>
  void main()
{
int present = 25, past=35;
void assign(int ,int=30);
void assign(past);
}
void assign(int default1, int default2=30)
{
default1=default1+default2;
cout<<default1<<default2;
}

25) i) typedef char STRING[50];
 void main()
{
STRING City;
gets(City);
cout<<City[0]<<"\t"<<City[2];
cout<<City<<endl;
}

ii) #define Convert(P,Q) P+2*Q
    void main()
{
 float A,B,Result;
 cin>>A>>B;
Result=Convert(A,B);
cout<<"Output :"<<Result<<endl;
}



28) iostream.h, ctype.h

29)
















31)










32) Point=3





















33) Min and Max value is 2 and 3
iv) is the ans

34)When Turn is 3, min and max value are 0 and 2
Ans:i) & iii)

35)Mynum - least and highest - 20,24
Ans:iv)

36)
42)








49) The benefit of using function prototype for a function is that it specifies the return type of the function and the type of arguments if any.
void check(int );// prototype for the function check which doesnt return anything and takes int as parameter.

50) cin>>ch; // ignores space
     cin.get(ch);// doesnt ignore space

51) Array is a collection of homogeneous finite number of elements stored contiguously , all of which are of a same data type. To efficiently store and retrieve data by using a single variable array name and its subscript.

52) A collection of 1-d arrays is 2-d arrays. Data is stored in form of rows and columns.
      students' marks in different subjects, prices of commodities

53) Array of strings - each string ends with '\0' null character
     other 2-d arrays - numeric arrays dont end with a null character

char str[5][12]; // 5 strings of 12 characters each
int [3][4];/ 3 rows of 4 elements each.

54)can be sized or unsized.
55) when the array is not given a size, can be used in situations where the size is unknown.
56) Function prototyping specifies the function return type and type of arguments if any.
Advantage - by knowing the prototype, it is possible to determine the function return type and type of arguments. Function can be defined anywhere in the program.
57) Actual parameters are used in function call. Formal parameters are used in function definition.
58) void test();
 int convert(float);
double promote(double,double);
long(int[],int);
int(char[]);

59)default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn't provide a value for the argument with default value. 
// A function with default arguments
void sum(int x, int y=9)
{
   cout<<x<<" "<<y<<endl;
}


void main()
{
    sum(10); // function 1
sum(5,3); // function 2
}
function 1 will produce following output
10 9
function 2 will produce foll output
5 3

In C++, an argument to a function can be declared as const as shown below:
int length(const int &s);
The qualifier const tells the complier that the function should not modify the argument. The compiler will generate an error when this condition is violated. This type of declaration is significant only when we pass arguments by reference or pointers.

60)In call by value, a copy of actual parameters is made to formal parameters. Function works only with the copy. Any change made in formal will not be reflected in the actual.
In call by reference, an alias of the actual parameters is received as formal in the function, any changes made in formal will be reflected in the actual.

Eg: call by value
void change(int a, int b)
{
a=6;b=10;
}
void main()
{
int x=1,y=2;
change(x,y);  
cout<<x<<" "<<y;// will display 1 2
}

Eg: call by reference
void change(int &a, int &b)
{
a=6;b=10;
}
void main()
{
int x=1,y=2;
change(x,y);  
cout<<x<<" "<<y;// will display 6 10
}



No comments:

Post a Comment