You are on page 1of 3

Question – What are modifiers?

Ans – Modifiers are the keywords which is used to modify the datatype and their meaning
such as signed, unsigned, long, short

Modifier signed int a = 25; Value

Datatype Variable
name

Question – Can we print a global variable inside a function when there is a variable
having same name?
ANS – Yes! Using Scope Resolution operator (::) we can call the global variable inside the
function when there is already exiting variable having same name.
#include <iostream> Output
using namespace std; The value of local variable is: 50
The value of local variable is: 25
int a = 25;
int main()
{
int a = 50;
cout << "The value of local variable is: " << a << endl;
cout << "The value of global variable is: " << ::a;
return 0; Scope Resolution
Operator
}
The scope Resolution operator (::) is used to access global variable inside a function.

Ques. What are Literals in C++?


Ans – The literals are used to automatically define the type of value when we don’t assign
the datatype to it. So, it will automatically assume the datatype of value.
Example = If we add some number into a variable then the compiler automatically assume
that it is an int, or double.
𝑐𝑜𝑢𝑡 ≪ 𝑥 + 2;
Literal
Variable
In case we use the decimal number then it will automatically assume the number ad double.

Ques. Can we change the type of literals without declaration of variable?


Ans – YES! We can change the type of literals in C++ with using F, L etc.
#include<iostream>
using namespace std;

int main (){


//we can concatenate F, L with the value to change the type of literals.
cout <<10; //It will define the value as int
cout<<14.5F; // the value 14.5 is now a float
cout<<15.888895654L; // the value of 15.88889564 is now a LONG
return 0;
}
Ques. What do you mean by Reference Variable?
Ans- Reference Variable can be defined as another name of already existing variable.
These variables are also called an ALIAS
Such as – There are different names of Addition i.e. Sum, Add, plus, total, etc.
So, we can assign other names to the already existing variables.
Example –
#include<iostream> Output
using namespace std; The value of add is: 52
The value of sum is: 52
int main() The value of total is: 52
{
int addition = 52; // Decelaration of variables
//assignment of different names to a variable
int & add = addition;
int & sum = addition;
int & total = addition;
//printing the variable
cout<< “The value of add is: ” <<add<<endl;
cout<< “The value of sum is: ” << sum<<endl;
cout<< “The value of total is: ” << total<<endl;
return 0;
}

Ques. What do you mean by typecasting?


Ans – Typecasting means converting one datatype to another.
We have two methods to typecast the value.
#include<iostream> Output
using namespace std; The value of float b as int is: 3
The value of int a as float is: 4
int main() The value of float b as int is: 3
{ The value of int a as float is: 4
int a = 4;
float b = 3.14;
cout<< "The value of float b as int is: "<<int(b) <<endl;
cout<< "The value of int a as float is: "<<float(a)<<endl;
//Another method of typecasting in C++
cout<< "The value of float b as int is: "<<(int)b <<endl;
cout<< "The value of int a as float is: "<<(float)a<<endl;
return 0;
}

Program 2 –
#include <iostream> Output
using namespace std; the sum of float a and int b is: 80
the sum of int a and int b is: 80
int main() the sum of float a and float b is: 80.54
{ the sum of double a and float b is: 80.54
int a = 45;
float b = 35.54;
cout<<"the sum of float a and int b is: "<<float(a)+int(b)<<endl;
cout<<"the sum of int a and int b is: "<<int(a) + int(b)<<endl;
cout<<"the sum of float a and float b is: "<<float(a)+float(b) <<endl;
cout<<"the sum of double a and float b is: "<<double(a)+float(b)<<endl;
return 0;
}
Ques. What is constants in C++?
Ans – Constants are the non-variable values ones assigned; cannot be manipulate (change)
by a function or method. The keyword ‘const’ is used to create a constant.

#include<iostream> Output:
using namespace std;
mian.cpp: In function 'int main()':
int main() mian.cpp: 7:6: error: assignment of read-only variable 'x'
{ x+=50;
const int x = 10; //declaration a constant int ^
x+=50; //changing the value of constant integer
cout<<x; // pass an error
return 0;
}

Ques. – What are manipulators in C++?


Ans – Manipulators are used to formatting the output. The most commonly used
manipulators are ‘endl’ and ‘setw’.
‘endl’ is used for next line and ‘setw’ is used to set width of the output.
#include<iomanip> header is used to manipulate the output.
#include<iostream> OUTPUT
#include<iomanip>
using namespace std; without setw: 3
without setw: 94
int main() without setw: 1235
{
int a = 3, b = 94, c = 1235; with setw: 3
//without setw with setw: 94
cout<<"without setw: "<<a<<endl; with setw: 1235
cout<<"without setw: "<<b<<endl;
cout<<"without setw: "<<c<<endl;
//with setw
cout<<"with setw: "<<setw(4)<<a<<endl;
cout<<"with setw: "<<setw(4)<<b<<endl;
cout<<"with setw: "<<setw(4)<<c<<endl;

return 0;
}

You might also like