Sunday, August 29, 2010

C++ interview questions (4)

22. what is importance of const. pointer in copy constructor?
 When we try to  copy one object into another using copy constructor,we need to maintain the original copy of original object (which we are copying) so while passing object we make it constant and we pass it as a by reference.


 23. What is a memory leak? How can we avoid it?
A memory leak or leakage in computer science occurs when a computer program consumes memory but is unable to release it back to the operating system.
A memory leak can be avoided by making sure that whatever memory has been dynamically allocated will be cleared after the use of the same. for example
#include<iostream>
using namespace std;

void func()
{
int *a = new int; //on heap

int b; // on stack
//avoid memeory leakage
delete a; //deleting memory on heap
}

int main()
{

func();
cout<<"end of main"<<endl;
return (0);
}


 
24. Why always array starts with index 0?
  This boils down to the concept of Binary digits.
Take an array size of 64 for example. We start from 0 and 
end at 63. We require 6 bits.But, if we were to start from 
1 and end at 64, we would require 7 bits to store the 
same number, thus increasing the storage size.
 
25 What is  stringstream? Give an example.
stringstream provides an interface to manipulate strings 
as if they were input/output streams. 

Members inherited from istream

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );
Following is a sample program using a stringstream 
to read numbers from a csv file named "input.txt" into
a 6 row by 5 column int array and
then prints the array.

It can take data from an input (text) file that
looks like this: 
1,3,10,3,1
1,10,10,3,10
1,,,,
0,0,0,1,10
1,1,0,2,2
10,1,0,1,10

 

 
#include <iostream>
#include <fstream>

#include<sstream>
using namespace std;

int ROWS = 6;

int COLS = 5;
int BUFFSIZE = 80;

main() {
 int array[ROWS][COLS];
 char buff[BUFFSIZE]; // a buffer to temporarily park the data

 ifstream infile("input.txt");
 stringstream ss;
 for( int row = 0; row < ROWS; ++row ) {

   // read a full line of input into the buffer (newline is
   //  automatically discarded)
   infile.getline( buff,  BUFFSIZE );
   // copy the entire line into the stringstream

   ss << buff;
   for( int col = 0; col < COLS; ++col ) {

     // Read from ss back into the buffer.  Here, ',' is
     //  specified as the delimiter so it reads only until
     //  it reaches a comma (which is automatically
     //  discarded) or reaches the 'eof', but of course
     //  this 'eof' is really just the end of a line of the
     //  original input.  The "6" is because I figured
     //  the input numbers would be 5 digits or less.
     ss.getline( buff, 6, ',' );

     // Next, use the stdlib atoi function to convert the
     //  input value that was just read from ss to an int,
     //  and copy it into the array.
     array[row][col] = atoi( buff );
   }

   // This copies an empty string into ss, erasing the
   //  previous contents.
   ss << "";
   // This clears the 'eof' flag.  Otherwise, even after
   //  writing new data to ss we wouldn't be able to
   //  read from it.
   ss.clear();
 }

 // Now print the array to see the result
 for( int row = 0; row < ROWS; ++row ) {

   for( int col = 0; col < COLS; ++col ) {

     cout << array[row][col] << " ";
   }
   cout << endl;
 }

 infile.close();
}

2 comments:

  1. Hi, Peter:

    Thanks for your comments! Glad to know you like this post. Welcome to visit my blog often.

    Jiansen Lu

    ReplyDelete
  2. Hi There,


    Brilliant article, glad I slogged through the C++ interview questions it seems that a whole lot of the details really come back to from my past project.

    I need a Program In C or either Pascal, including 2 conditions of (if-else) + loops (fixed and non fixed both) in Modular Programming Perspective?

    what I tried as I don't know c very well but some how what I did is
    By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).


    Grazie,
    Yamuna

    ReplyDelete