Sunday, January 16, 2022

Class-11 | C++ Concept | Computer Science | Questions Answer Solutions!

 1. What is the purpose of a header file in a program?

Ans. Header files provide function prototypes, definitions of library functions, declaration of data types and constants used with the library functions.

2. What main integer types are offered by C++?

Ans. C++ offered three types of integers : short, int and long. Each comes in both signed and unsigned versions.

3. Explain the usage of following with the help of an example:

(i) constant (ii) reference (iii) variable (iv) union

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


(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;

5. What is the difference between fundamental data types and derived data types? Explain with examples.

Ans. Fundamental data types                                    

1) These are the data types that are not composed of any other data type.

2) There are five fundamental data types: char, int, float, double and void.

3) Example: int a=10; float b; 

 Derived data types

1) These are tha data types that are composed of fundamental data types.

2) These are: array, function, pointer, reference, constant, class, structure, union and enumeration.

3) Example: float marks[50]; Const int a=5;

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

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

6. How are the following two statements different?

char pcode = 75;

char pcode = ‘K’;

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

7. How are the following two statements different?

char pcode = 75; short pcode = 75;

Ans. The first statement treats 75 as ascii value and converts it into its relative character ‘K’ whereas second statement

treats 75 as number.

8. If value is an identifier of int type and is holding value 200, is the following statement correct?

char code = value

Ans. VALID Statement

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

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

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

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

}

11. What are arithmetic operators in C++? Distinguish between unary and binary arithmetic operators. Give examples for each of them.

Ans. Following are the arithmetic operators in C++: addition(+), subtraction(-), multiplication(*), division(/) and reminder(%).

Unary arithmetic operators 

1) Unary Operators has only one operand.

2) The Unary Operators are ++,--,&,*,+, - etc.

3) Example: short x = 987; x = -x;

Binary arithmetic operators

1) Binary operators (”bi” as in “two”) have two operands

2) The +, -, &&, <<, ==, >> etc. are binary operators.

3) Example: int x, y = 5, z;

z = 10;

x = y + z;


12. What is the function of increment/decrement operators? How many varieties do they come in? How are these two varieties different from one another?

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

1) Performs the increment or decrement operation before using the value of the operand.

2) 

Example: sum = 10;

ctr = 5;

sum = sum + (++ctr);


Postfix version

1) First uses the value of the operand in evaluating the expression before incrementing or decrementing the operand’s value.

2)

Example: sum = 10;

ctr = 5;

sum = sum + (ctr++);

No comments:

Post a Comment

CMS - 2024 || JAC INTERMEDIATE EXAMINATION - 2024 || 12th COMPUTER SCIENCE!

  Part-A Multiple Choice Questions 1) Who is the developer C++? a) Von Neumann b) Dennis M. Ritchie c) Charles Babbage d) Bjarne Stroustrup ...