C++ CBSE Class 12 Chapter 1 Part 1 Questions 1 to 15

Indian CBSE C++ class 12 Sumita Arora questions Chapter 1 solved part 1.Here from question 1 to 15 are solved .

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

Answer is given below

 (i) cout<< "="a ; 
(ii) m=5, n=12, o=15;
(iii) cout<< "x" << x;
(iv) cin >> y >> j ;
(v) cin >> y;
(vi) cout >> "\n abc";
(vii)a = b + c;
(viii) break = x*y;

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

Answer is given below

Difference 1)

Fundamental types are the most basic data type so they are not composed of other data types. The derived data are built from fundamental data type so they compose of the fundamental data types.

Difference 2)


Fundamental types come with the compiler and what they represent cannot be changed. The derived type can be changed according to our needs and desire. For instance, the derived type can have none or one or more derived data type.

Difference 3)

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

The derived types are: array , function , pointer , reference , constant, class, structure, union and enumeration.Example: int a=10; float b; Example: float marks[50]; Const int a=5;

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

The Header files provide a programmer with the declaration of functions, definitions of functions, declaration of data types and constants used with the library functions. They also provide derived data types.

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

C++ offer four types of integers : short, int , long and long long. .They can be also written with the qualifier signed and unsigned. Including the qualifeir they are written as signed short , short , unsigned short ,signed int, int, signed int , signed long , long int , unsigned long and signed long long , long long , unsigned long long>

5.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 used to make an object or a variable a constant type. Once the object or variable becomes const type their vallue cannot be changed again. The general form of constant declaration is as follows:

const type name = value.

const int a=10 ;
 /* a=90; //Error! */

(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 &reference_variable = variable_name;

Where type is any valid c++ data type, reference_variable is the name of the reference that will point to variable denoted by variable_name. Example:

int new=90,
 &ref=total; //note the '&' sign

cout<< "New=" << new << "\n";

cout<< "Ref=" << ref ;

In above program, ‘New’ and ‘ref’ refer to the same data object in the memory, thus, they print the same value.

(iii) variable: Variables represent the name of the memory in our program. Using the variable we can access the value present in the memory. Variables are generally declared as:
Type name;

where ‘Type’ is any C++ data type and ‘name’ is the name of the variabl. A variable can be initialized during its declaration or can be initialized later as shown below:

int size=100 ; //initialize during declaration

float new_size;

new_size= 10.46; //initialize later

(iv) union: A union is a memory which is shared by two or more different variables, the types of the variable are usually different. Defining a union is similar to defining a structure. Consider the code below:

union share{
int ii ;

char c;

union share Un;

The union ‘Un’ can be referred to as either an integer or character. To assign the integer 19 to element ii of ,Un, do it as

Un.ii = 20;

To print the value of element ‘c’ of Un, write

cout<< Un.c;

6. 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:

(ii) During declaration: Example:

int age = 10;

(i) Using assignment operator ‘=’ : Example:

int new ;

new = 29;

(iii) Run-time initialization: Example:

cin>> x >> " " >> y ;

float avg = x/y;

7. How are the following two statements different?

char pcode = 75;
char pcode = ‘K’;

The first statement treats 75 as ASCII (integer) value and converts it into its corresponding character which is ‘K’.

Whereas the second statement stores the character ‘K’ and does not make any conversion.

8. How are the following two statements different?

char pcode = 75; 
short pcode = 75;

The first statement treats 75 as an ASCII value and converts it into its corresponding character ‘K’.

The second statement treats 75 as a number. Since ‘short’ represents the ‘short int’ type.

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

char code = value;

Ys it is a valid Statement!

10. 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 the size of the float type. It also stores floating-point numbers with much larger range and precision. It stands for double precision floating-point. You cn test the size of the float and double using .

cout<< sizeof( float ) << " " << sizeof(double) ;

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

The access modifier const make the variable value constant. Thus, the value of a constant variable cannot be altered during the program run whereas the value of the variable can be changed anytime while running the program.

void main()
const int a=10;

int b=20;

/* a++; //Erro cannot change the value */

cout<< "a=" << a;

b++; //work fine

cout<< "b=" << b ;

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

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 while Binary operators (”bi” as in “two”) have two operands or two operands are required to perform an operation with Binary operators.

The Unary Operators are ++,–,&,*,+, – etc. The Binary operators are +, -, &&, <<, ==, >> etc.

//Unary operator 
short x = 987;
x = ++x;

//Binary operator 
int x, y = 5, z;

z = 10;

x = y % z;

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 increment operator, ‘++’ increment its operand by 1 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 and Postfix version difference

The prefix version performs either incrementing or decrementing the operand value first and then the operatin is performed. Whereas, the postfix version performs the operation firat and increments the value of the oeprand.

//Using prefix 
int  sum1 = 10;
c1 = 5;
sum1 = sum1 + (++c1);

cout<< sum << endl; 

//Using postfix
int sum2 = 10;
c2 = 5;
sum2 = sum2 + (c2++);

Output is,


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

For the first expression if the variable ‘val’ is assigned a value and the ‘asm’ is declared then the expression becomes invalid because there is no semi-colon at the end. Another reason it can be invalid is if the expression is treated as a comparison expression in which case the operator ‘=’ should be ‘==’.

(ii) The second expression has a variable’s name missing after ‘&&’ operator.
(iii) The third expression has both ‘||’ and ‘&&’ operators written consecutively. There must be one variable in between them.
(iv) The thirs expression uses ‘!>’ operator which is invalid and also the operator !x>20 should be written in parenthesis like (!x>20) .

15. What is the difference between Type Casting and Automatic Type conversion? Also, give a suitable C++ code to illustrate both.

Type casting is an explicit form of converting one data type to another. In such a casting method, the type you want to cast is mentioned explicitly inside the bracket or the value is placed inside the bracket and the type name outside.

Automatic casting is an implicit form of casting from one data type to another. Such casting is performed by the compiler automatically.

int A=1, B=2;
float C = (float)A/B ; //Type Casting

/* or 
float C = A(flaot)/B; //works fine, same as above 

cout<< C << endl ;

//Automatic casting
int N = 65;

char C = N ; //Automatic type conversion

cout<<C ;