Skip Navigation Links
C C++
C#
Data Structures
Unix
Java
Puzzles
H1b 2009
Contact us
Advertise
C C++ Questions & Answers
Add new question
Q.1 What are Functors ?
Modify answer       Delete (after enough votes question will be removed)

  Functors are functions with a state. In C++ you can realize them as a class with one or more private members to store the state and with an overloaded operator () to execute the function. Functors can encapsulate C and C++ function pointers employing the concepts templates and polymorphism. You can build up a list of pointers to member functions of arbitrary classes and call them all through the same interface without bothering about their class or the need of a pointer to an instance. All the functions just have got to have the same return-type and calling parameters. Sometimes functors are also known as closures. You can also use functors to implement callbacks.

Read more at http://www.newty.de/fpt/functor.html



Q.2 Write a program to find out if a given integer is power of 2 without looping.
Modify answer       Delete (after enough votes question will be removed)

  bool isPowOf2(int i)
 {
   return i > 0 && (i & (i - 1)) == 0;
 }



Q.3 What is the difference between map and hash_map in STL?
Modify answer       Delete (after enough votes question will be removed)

Map: 
1 . Uses Key and Data to store values 
2 . Implemented mostly in a balanced tree structure 
3 . If key is a string then overloaded "<" ">" should be able to tell us where to store this object in a tree 
4 . Does not allow for duplicates to be stored in the map,multimaps should be used 
5 . For custom objects you would need at the minimum the following operators to store data in a map "<" ">" "==" and of course the other stuff for deep copy. 
6 . Performance would mostly be o(lgn) due to the implementation of a balanced tree. Also performance would degrade as the key size increases. Mainly balance operations on large key ranges would kill performance. 

Hash: 
1 . Use key and Data to store values 
2 . Could be implemented via linked lists (but bad performance) or Vectors . 
3 . I am not sure if trees could be used to implement an hash function , due to missing index, priority queue ?? 
4 . Supports duplicates via open addressing but w.c o(n) performance 
5 . For custom objects, would need to specify the hash function and also the equality operator to check for collisions. 
6. Performance amortized over the lifespan would be o(1) if we have a good table size and large range of keys. Listen more at - ListenVoice


Q.4 Write a program to detect a palindrom from a given string.
Modify answer       Delete (after enough votes question will be removed)

Reverse the string and compare it.


Q.5 Write factorial program using template meta programming.
Modify answer       Delete (after enough votes question will be removed)

will do


Q.6 How can you implement a class whose object can not be created using new?
Modify answer       Delete (after enough votes question will be removed)

Declare all the constructors of the class explicitly as private.




Q.7 What are the problems with singleton pattern?
Modify answer       Delete (after enough votes question will be removed)

will do


Q.8 Write a simple refcounted smart pointer class.
Modify answer       Delete (after enough votes question will be removed)

will do


Q.9 Can we store auto_ptr in STL containers?
Modify answer       Delete (after enough votes question will be removed)

STL Container may move elements around during reallocation. So The element should be copy constructible or assignable.
But auto_ptr is neither.


Q.10 When do you use volatile and register keywords?
Modify answer       Delete (after enough votes question will be removed)

The value of volatile variable can be changed even from outside of process. esp, in firmware programs the hardware writes/updates memory location of process.
Register - telling the compiler to use CPU register to hold that variable.  It upto the compiler decision to use register, other wise it simple ignore the keyword.


Q.11 What is stack unwinding?
Modify answer       Delete (after enough votes question will be removed)

During exception handling when the destructor is called for all local objects in the stack between the place where the exception was thrown and where it is caught.


Q.12 Write a simple thread pool class.
Modify answer       Delete (after enough votes question will be removed)

will do


Q.13 My test question
Modify answer       Delete (after enough votes question will be removed)

Hello

Alternate Answer       Approve answer(After enough votes answer will be approved)
Hello




 
  
Copyright 2009 GetMeValue