C++ Programming tips &Technical Interview Questions Set 3

Posted by , No Comments

Welcome to FreshersPlane, FreshersPlane Provide you latest Job Notifications , Career Opportunities, Government Jobs Information,

Don't forget to subscribe to email notifications to get all latest jobs directly to your email daily. We post more than 1000+ jobs every week.So,

cplus21.How do I get the current position of the file pointer?

Ans: We can get the current position of the file pointer by using the tellp( ) member function of ostream class or tellg( ) member function of istream class. These functions return (in bytes) positions of put pointer and get pointer respectively.

22.What are put and get pointers?

Ans: These are the long integers associated with the streams. The value present in the put pointer specifies the byte number in the file from where next write would take place in the file. The get pointer specifies the byte number in the file from where the next reading should take place.

23. What do the nocreate and noreplace flag ensure when they are used for opening a file?

Ans: nocreate and noreplace are file-opening modes. A bit in the ios class defines these modes. The flag nocreate ensures that the file must exist before opening it. On the other hand the flag noreplace ensures that while opening a file for output it does not get overwritten with new one unless ate or app is set. When the app flag is set then whatever we write gets appended to the existing file. When ate flag is set we can start reading or writing at the end of existing file.

24. What is the limitation of cin while taking input for character array?

Ans: To understand this consider following statements,
char str[5] ;
cin >> str ;
While entering the value for str if we enter more than 5 characters then there is no provision in cin to check the array bounds. If the array overflows, it may be dangerous. This can be avoided by using get( ) function. For example, consider following statement,cin.get ( str, 5 ) ;
On executing this statement if we enter more than 5 characters, then get( ) takes only first five characters and ignores rest of the characters. Some more variations of get( ) are available, such as shown below:
get ( ch ) – Extracts one character only

get ( str, n ) – Extracts up to n characters into str

get ( str, DELIM ) – Extracts characters into array str until specified delimiter (such as ‘n’). Leaves delimiting character in stream.

get ( str, n, DELIM ) – Extracts characters into array str until n characters or DELIM character, leaving delimiting character in stream.

25.What is the purpose of istream class?

Ans: The istream class performs activities specific to input. It is derived from the iosclass. The most commonly used member function of this class is the overloaded >> operator which canextract values of all basic types. We can extract even a string using this operator.

26.Would the following code work?

void main( )
ostream o ;
o << “Dream. Then make it happen!” ; } Ans: No! This is because we cannot create an object of the ostream class since its constructor and copy constructor are declared private.

27.Can we use this pointer inside static member function?

Ans: No! The this pointer cannot be used inside a static member function. This is because a static member function is never called through an object.

Find the Latest Jobs in our site for freshers , experience .List of walkings , fresher jobs, latest jobs,Govt Jobs, Sarkari Naukri for all engineering branches, all graduates, Apply for all jobs mentioned in our site.

28.What is strstream?

Ans: strstream is a type of input/output stream that works with the memory. It allows using section of the memory as a stream object. These streams provide the classes that can be used for storing the stream of bytes into memory. For example, we can store integers, floats and strings as a stream of bytes. There are several classes that implement this in-memory formatting. The class ostrstream derived from ostream is used when output is to be sent to memory, the class istrstream derived from istream is used when input is taken from memory and strstream class derived from iostream is used for memory objects that do both input and output.

29.When we want to retrieve the streams of bytes from memory we can use istrestream.

The followingexample shows the use of istrstream class. #include void main( ) { int age ; float salary ; char name[50] ; char str[] = “22 12004.50 K. Vishwanatth” ; istrstream s ( str ) ; s >> age >> salary >> name ;

cout << age << endl << salary << endl << name ;
cout << endl << s.rdbuf( ) ; } Here, s is the object of the class istrstream. When we are creating the object s, the constructor of istrstream gets called that receives a pointer to the zero terminated character array str. The statement s >> age >> salary >> name ; extracts the age, salary and the name from the istrstream object s. However, while extracting the name, only the first word of name gets extracted. The balance is extracted using rdbuf( ).

30.When the constructor of a base class calls a virtual function, why doesn’t the override function of the derived class gets called?

Ans: While building an object of a derived class first the constructor of the base class and then the constructor of the derived class gets called. The object is said an immature object at the stage when the constructor of base class is called. This object will be called a matured object after the execution of the constructor of the derived class. Thus, if we call a virtual function when an object is still immature, obviously, the virtual function of the base class would get called. This is illustrated in the following example.

class base
protected :
int i ;
public :
base ( int ii = 0 )
i = ii ;
show( ) ;

virtual void show( )
cout << “base’s show( )” << endl ;
} ;

class derived : public base
private :
int j ;
public :
derived ( int ii, int jj = 0 ) : base ( ii )
j = jj ;
show( ) ;
void show( )
cout << “derived’s show( )” << endl ;
} ;

void main( )
derived dobj ( 20, 5 ) ;
The output of this program would be:
base’s show( )
derived’s show( )



Enter your email address:

Delivered by Apply Now