InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 251. |
We All Know That A Const Variable Needs To Be Initialized At The Time Of Declaration. Then How Come The Program Given Below Runs Properly Even When We Have Not Initialized P? #include<iostream> Void Main( ) { Const Char *p ; P = "a Const Pointer" ; Cout << P ; } |
|
Answer» The OUTPUT of the above PROGRAM is 'A CONST pointer'. This is because in this program p is declared as 'const char*' which means that VALUE stored at p will be constant and not p and so the program works properly. The output of the above program is 'A const pointer'. This is because in this program p is declared as 'const char*' which means that value stored at p will be constant and not p and so the program works properly. |
|
| 252. |
Write My Own Zero-argument Manipulator That Should Work Same As Hex? |
|
Answer» ANSWER :This is shown in following program. #include ostream& myhex ( ostream &o ) { o.setf ( ios::hex) ; return o ; } VOID MAIN( ) { cout << endl << myhex << 2000 ; } |
|
| 253. |
What Is Meant By Forward Referencing And When Should It Be Used? |
|
Answer» Forward REFERENCING is generally REQUIRED when we make a class or a function as a friend.Consider following program: class test { public: friend void fun ( sample, test ) ; } ; class sample { public: friend void fun ( sample, test ) ; } ; void fun ( sample s, test t ) { // code } void main( ) { sample s ; test t ; fun ( s, t ) ; }On compiling this program it gives error on the following STATEMENT of test class. It gives an error that sample is undeclared identifier. friend void fun ( sample, test ) ; This is so because the class sample is defined below the class test and we are using it before its definition. To overcome this error we need to GIVE forward reference of the class sample before the definition of class test. The following statement is the forward reference of class sample. class sample;Forward referencing is generally required when we make a class or a function as a friend.Consider following program: On compiling this program it gives error on the following statement of test class. It gives an error that sample is undeclared identifier. friend void fun ( sample, test ) ; This is so because the class sample is defined below the class test and we are using it before its definition. To overcome this error we need to give forward reference of the class sample before the definition of class test. The following statement is the forward reference of class sample. |
|
| 254. |
What Is Meant By Const_cast? |
|
Answer» The const_cast is used to convert a const to a non-const. This is shown in the following program: #include VOID main( ) { const int a = 0 ; int *ptr = ( int * ) &a ; //one way ptr = const_cast_ ( &a ) ; //better way }Here, the address of the const variable a is assigned to the POINTER to a non-const variable. The const_cast is also used when we want to change the data members of a class inside the const member functions. The following code snippet SHOWS this: class sample { private: int data; public: void FUNC( ) const { (const_cast (this))->data = 70 ; } };The const_cast is used to convert a const to a non-const. This is shown in the following program: Here, the address of the const variable a is assigned to the pointer to a non-const variable. The const_cast is also used when we want to change the data members of a class inside the const member functions. The following code snippet shows this: |
|
| 255. |
Write A Program That Will Convert An Integer Pointer To An Integer And Vice-versa. |
|
Answer» The following program demonstrates this. #include<stdio.h> #include<iosream> #include<conio.h> void MAIN( ) { INT i = 65000 ; int *IPTR = reinterpret_cast ( i ) ; cout << endl << iptr ; iptr++ ; cout << endl << iptr ; i = reinterpret_cast ( iptr ) ; cout << endl << i ; i++ ; cout << endl << i ; }The following program demonstrates this. |
|