3i Infotech Papers: Sample Questions 582 - 583 of 1245

Examrace Placement Series prepares you for the toughest placement exams to top companies.

Question number: 582

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

Find the output of the following program

  1. #include <iostream> class fig2d
  2. {
  3.     int dim1;
  4.     int dim2;
  5.     publicfig2d()
  6.     {
  7.         dim1=5;
  8.         dim2=6;
  9.     }
  10.     virtual void operator<<(ostream & rhs);
  11. };
  12. void fig2d::operator<<(ostream &rhs)
  13. {
  14.     rhs <<dim1<<" "<<dim2<<" ";
  15. }
  16. void main()
  17. {
  18.     fig2d obj1obj1 << cout;
  19. }

Explanation

  • In the program

Table showing the program

Table showing the program

  1. class fig2d
  2. {
  3.     int dim1;
  4.     int dim2;
  5.     publicfig2d()
  6.     {
  7.         dim1=5;
  8.         dim2=6;
  9.     }
  10.     virtual void operator < < (ostream & rhs);
  11. };
  • Define the class fig2d
  • Define the integer variable dim1 and dim2.

fig2d () function assigns the value of dim1 = 5 and dim2 = 6

The < < operator is overloaded with ostream as arguments.

  1. void fig2d::operator < < (ostream &rhs)
  2. {
  3.     rhs < < dim1 < < ” “ < < dim2 < < ” “;
  4. }

print the value of dim1 and dim2

  1. void fig2d::operator < < (ostream &rhs)
  2. {
  3.     rhs < < dim1 < < ” “ < < dim2 < < ” “;
  4. }
  • This enables the ‘cout’ to be present at the right-hand-side.

  • Normally, ‘cout’ is implemented as global function, but it doesn’t mean that ‘cout’ cannot be overloaded as member function.

  • Overloading < < as virtual member function is handy when the class in which it is overloaded is inherited, and this becomes available to be overriden.

  • This is as opposed to global friend functions, where friends are not inherited.

Question number: 583

» Languages » C & C Plus Plus

Essay Question▾

Describe in Detail

What is the output of the following program?

  1. void main ()
  2. {
  3.     int i = 10j = 2;
  4.     int * ip = &i, * jp = &j;
  5.     int k = * ip/* jp;
  6.     printf (“%d”, k);
  7. }

Explanation

Compiler Error: “Unexpected end of file in comment started in line 5”

In the program

Table shows the program

Table shows the program

int i = 10, j = 2;

  • Given the integer variable i = 10 and j = 2

int * ip = &i, * jp = &j;

  • Pointer ip stores the address of I and pointer jp stores the address of j.

int k =* ip/* jp;

  • The programmer intended to divide two integers, but by the “maximum munch” rule, the compiler treats the operator sequence/and * as /* which happens to be the starting of comment.

  • To force what is intended by the programmer, int k =*ip/ *jp; // give space explicitly separating/and * //orint k =*ip/ (*jp); // put braces to force the intention