Impact of access modifier const over variables.

Q17. Explain the impact of access modifier const over variables. Support your answer with examples.

Ans. If the access modifier const is used before variable’s definition, it modifies its access types, that is, the access of the constant variable is readable only, it can no more be written on to. For instance,

int val = 10;

declares a variable val with initial value 10. However, the value of val can be changed in the program at any time. But, if we modify the above definition of variable as follows:

const int val = 10;

the variable val becomes constant and its value remains 10 throughout the program, it can never be changed during program run.

Variable Initialization.

Q16. How many ways can a variable be initialized into? Give examples for each types of initialization.

Ans. A first value (initial value) may be specified in the definition of a variable. A variable with a declared first value is said to be an initialized variable. C++ supports two forms of variable initialization at the time of variable definition:

int val = 1001;

int val (1001);

In both cases, val is initialized with s first value of 1001. Following are some more examples of initialized variables:

double price = 214.70, discount = 0.12; Read More …

Explain the function and usage of variables.

Q15. Explain the function and usage of variables with example.

Ans. Variables represent named storage locations, whose values can be manipulated during program run. For instance, to store name of a student and marks of a student during a program run, we require storage locations that too named so that these can be distinguished easily. Variables, called as symbolic variables, serve the purpose. The variables are called symbolic variables because these are named locations. For instance, the following statement declares a variable i of the data type int:

int i;

Why are so many data types provided in C++.

Q14. Why are so many data types provided in C++?

Ans. The reason for providing so many data types is to allow programmer to take advantage of hardware characteristics. Machines are significantly different in their memory requirements, memory access times (time taken to read memory), and computation speeds. Suppose, a program that earlier was working with 2-byte int efficiently, on shifting to a machine that provides 4-byte int and 2-byte short can easily work with short of the new machine thereby not increasing the memory requirements of the program.

Explain the function and usage of a union.

Q13. Explain the function and usage of a union giving an example.

Ans. 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 declaration declares a union share having two variables (integer and character type) and creates a union object cnvt of union type share:

union share


int i;

char ch;


union share cnvt;


the keyword union is used for declaring and creating a union cnvt, both integer I and character ch share the same memory location. (of course, i (being integer) occupies 2 bytes and ch (being character) uses only 1 byte.



Byte 0 Byte 1



Figure: Union elements I and ch utilizing union cnvt


At any point, you can refer to the data stored in a cnvt as either an integer or a 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



Declaration for a student class.

Last Questions 11.

Q12. Write declaration for a class that holds the information of Q11. For associated operations, make suitable assumptions.


class stud


int roll_no;

char name[20];

int class;

float marks;

char grade;



percent();  //to calculate the percentage of student

calgrade();  //calculate grade

prntreport();  //prints the report of the student


Declaration for a student structure in C++.

Q11. Write declaration for a structure that holds information of a student like roll no, name, class, marks and grade.


struct stud


int roll_no;

char name[20];

int class; Read More …

Declaration of structure for branch.

Last Question 9.

Q10. Write declaration for a structure that holds same information as that of the class mentioned in question 9.


struct branch


int branch_num,num_emp;  //branch number,                //number of employee

char area[50],head[50];      //area,head

}x;  //structure object named x

Declaration for class that holds properties of branch.

Q9. Write declaration for a class that holds properties of a branch. The class holds information like branch-number, area, number of employees, head and the operations associated like branch report printing, branch data modification, pending work reporting, forecast reporting.


class branch


int branch_num,num_emp;  //branch number,                //number of employee

char area[50],head[50];      //area,head


//operations associated with branch


prntreport(); //branch report printing

modifydata();  //branch data modification

pendingworkrept();  //pending work report

forecastrept();  //forecast reporting


Concept of constant variable.

Q8. Explain with example the concept of constant variable.

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

where const is the keyword that must be used for declaring a constant, type is any valid C++ data type, name is the name of the constant and value is the constant value of the data type type. For instance,

const int upperage = 50;

declares a constant named a upperage of type integer that holds value 50;

a constant must be initialized at the time of declaration. If you give only const in place of const int, it means the same.