Related Topics
C++ Programing
- Question 5
What is the use of sizeof operator in C++ ?
- Answer
In C++, the sizeof operator is used to determine the size of a data type, an object, or a class.
The syntax of the sizeof operator is as follows:
sizeof (data_type)
For example, to find the size of an int data type, you can use the sizeof operator in the following way:
int a;
cout << "Size of integer variable 'a' is " << sizeof(a) << " bytes." << endl;
This will print the size of the integer variable a in bytes.
You can also use the sizeof operator to find the size of an array. For example:
int arr[10];
cout << "Size of array 'arr' is " << sizeof(arr) << " bytes." << endl;
This will print the size of the entire array in bytes, which is the size of one element multiplied by the number of elements in the array.
You can also use the sizeof operator to find the size of a class. For example:
class MyClass {
int a;
double b;
char c;
};
cout << "Size of class 'MyClass' is " << sizeof(MyClass) << " bytes." << endl;
This will print the size of the MyClass object in bytes, which is the sum of the sizes of its member variables. Note that the size of a class may be different on different platforms, due to differences in alignment and padding.
- Question 6
How many keywords are in C++ ?
- Answer
In C++, there are a total of 91 reserved keywords that have a specific meaning and cannot be used as variable names or identifiers in a C++ program. These keywords are defined in the C++ standard, and they are used to define the syntax and semantics of the language.
Here is the list of all 91 keywords in C++:
and and_eq asm auto bitand
bitor bool break case catch
char class compl const constexpr
const_cast continue decltype default delete
do double dynamic_cast else enum
explicit export extern false float
for friend goto if inline
int long mutable namespace new
noexcept not not_eq nullptr operator
or or_eq private protected public
register reinterpret_cast return short
signed sizeof static static_assert static_cast
struct switch template this throw
true try typedef typeid typename
union unsigned using virtual void
volatile wchar_t while xor xor_eq
It’s important to note that the keyword typedef is technically not a reserved keyword in C++17, and is considered a contextual keyword. However, it is still a keyword in earlier versions of the language.
- Question 7
Which are the variables we use to store floating-point values in C++ ?
- Answer
In C++, there are two main types of variables that can be used to store floating-point values: float and double.
The float data type is a single-precision floating-point number that can store values with up to 7 digits of precision. It takes up 4 bytes of memory in most systems.
The double data type is a double-precision floating-point number that can store values with up to 15 digits of precision. It takes up 8 bytes of memory in most systems.
- Question 8
Why we use constant keyword in C++ ?
- Answer
In C++, the const
keyword is used to declare a variable or parameter as constant. When a variable is declared as const
, its value cannot be changed during the program execution.
Here are some reasons why you might use the const
keyword in C++:
To prevent accidental modification: When you declare a variable as
const
, you are telling the compiler that the value of that variable should not be modified. This can help prevent errors caused by accidental modification of the variable.To enforce program logic: You can use
const
to enforce program logic. For example, if you have a function that takes a parameter that should not be modified, you can declare it asconst
to ensure that the function does not modify the parameter.To optimize code: In some cases, the
const
keyword can help the compiler optimize code. For example, if the compiler knows that a variable’s value cannot change, it can optimize code that relies on that variable.To enable the use of
const
functions:const
member functions are functions that do not modify the object on which they are called. If you want to define aconst
member function, you must use theconst
keyword in the function declaration.
Overall, using the const
keyword can help improve code quality, readability, and maintainability by preventing accidental modification of variables and enforcing program logic.