Languages-C & C Plus Plus [3i Infotech Placement]: Sample Questions 48 - 49 of 354

Glide to success with Doorsteptutor material for competitive exams : get questions, notes, tests, video lectures and more- for all subjects of your exam.

Question 48

C & C Plus Plus
Edit

Describe in Detail

Essay▾

Find the output of the following program

  1. classcomplex
  2. {
  3.     doublere;
  4.     doubleim;
  5.     public:complex() :re(1),im(0.5)
  6.     {
  7.     }
  8.     booloperator==(complex &rhs); operatorint(){}
  9. };
  10. boolcomplex::operator == (complex &rhs)
  11. {
  12.     if((this re == rhs.re) && (this im == rhs.im)) returntrue; elsereturnfalse;
  13. }
  14. intmain()
  15. {
  16.     complexc1;
  17.     cout≪ c1;
  18. }

Explanation

  • In the program
Table Showing the Program
  1. classcomplex
  2. {
  3.     doublere;
  4.     doubleim;
  5.     public:complex() :re(1),im(0.5)
  6.     {
  7.     }
  8.     booloperator==(complex &rhs);
  9.     operatorint(){}
  10. };
  11. boolcomplex::operator == (complex &rhs)
  12. {
  13.     if((this-> re == rhs.re) && (this-> im == rhs.im)) returntrue; elsereturnfalse;
  14. }
  • Define the class complex
  • Define the double variable re and im
int main () {complex c1; cout << c1;}
  • The program tries to print the complex object using output re-direction operator, which is not defined for the class.
  • But the compiler instead of giving an error sees the conversion function and converts the user defined object to standard object and prints some garbage value.

Question 49

C & C Plus Plus
Edit

Write in Short

Short Answer▾

What is the output of the following program?

  1. main ()
  2. {
  3.     while (strcmp(“some”, “some⧵0”) )
  4.     printf (“Stringsarenotequal”);
  5. }

Explanation

In the program

Table Shows the Program
while (strcmp ( “some” , “some⧵0” ) )
  • Ending the string constant with ⧵0 explicitly makes no difference.
  • “some” and “some⧵0” are equivalent.
  • So, strcmp returns 0 (false) hence breaking out of the while loop.

Developed by: