Question-57 What is the difference between const int*, const int * const, int const * in
C/C++?
Answer: Read it backwards...
int* pointer to int
int const * pointer to const int
int * const const pointer to int
int const * const const pointer to const int
For example, consider the following declarations:
We have to read pointer declarations right-to-left.
1. const Item * ptr means “ptr points to an Item that is const” — that is, the Item object
can’t be changed via ptr.
2. Item. * const ptr means “ptr is a const pointer to an Item” — that is, you can change
the Item object via ptr, but you can’t change the pointer ptr itself.
3. const Item * const ptr means “ptr is a const pointer to a const Item” — that is, you
can’t change the pointer ptr itself, nor can you change the Item object via ptr.
Now the first const can be on either side of the type so:
Question-58 What are the differences between .dll and .lib?
Answer: A dll is a library of functions that are shared among other executable programs. Just look
in your windows/ system32 directory and you will find many of them. When your program creates
a dll it also normally creates a lib file so that the application *.exe program can resolve symbols
that are declared in the dll.
A .lib is a library of functions that are statically linked to a program. They are not shared by other
programs. Each program that links with a *.lib file has all the code in that file. If you have two
programs X. exe and Y. exe that link with Z. lib then each X and Y will both contain the code in Z.
lib.
How you create dlls and libs depend on the compiler you use. Each compiler does it differently.
Question-59 What is the difference between big and little endian?
Answer: The big-endian and little-endian refer to which bytes are most significant in multi-byte
data types and describe the order in which a sequence of bytes is stored in a computer memory.
If the hardware is built so that the lowest, least significant byte of a multi-byte scalar is stored
first, at the lowest memory address, then the hardware is said to be little-endian; the little end of
the integer gets stored first, and the next bytes get stored in higher (increasing) memory locations.
Little-Endian byte order is “littlest end goes first (to the littlest address)”.
Machines such as the Intel/AMD x86, Digital VAX, and Digital Alpha, handle scalars in Little-
Endian form.
If the hardware is built so that the highest, most significant byte of a multi-byte scalar is stored
first, at the lowest memory address, then the hardware is said to be big-endian; the big end of the
integer gets stored first, and the next bytes get stored in higher (increasing) memory locations.
Big-Endian byte order is “biggest end goes first (to the lowest address)”.
Machines such as IBM mainframes, the Motorola 680x0, Sun SPARC, PowerPC, and most RISC
machines, handle scalars in Big-Endian form.
Four-byte Integer Example
Consider the four-byte integer 0x44332211. The “little” end byte, the lowest or least significant
byte, is 0x11, and the “big” end byte, the highest or most significant byte, is 0x44. The two
memory storage patterns for the four bytes are:
Here is sample code to determine what is the type of your machine
Question-60 Explain the concept of C++ Containers and STL.
Answer: The C++ STL (Standard Template Library) is a set of C++ template classes to provides
general-purpose templatized classes and functions that implement many popular and commonly
used algorithms and data structures like vectors, lists, queues, and stacks. A container is an object
that stores a collection of other objects (its elements). They are implemented as class templates,
which allows a great flexibility in the types supported as elements.
The container manages the storage space for its elements and provides member functions to
access them, either directly or through iterators (reference objects with similar properties to
pointers).
Containers replicate structures very commonly used in programming: dynamic arrays (vector),
queues (queue), stacks (stack), heaps (priority_queue), linked lists (list), trees (set), associative
arrays (map), etc...
Many containers have several member functions in common, and share functionalities. The
decision of which type of container to use for a specific need does not generally depend only on
the functionality offered by the container, but also on the efficiency of some of its members
(complexity). This is especially true for linear containers, which offer different trade-offs in
complexity between inserting/removing elements and accessing them.
Container Classes
• vector: Vectors are sequence containers representing arrays that can
change in size.
• deque: Array which supports insertion/removal of elements at
Sequences beginning or end of array. It is a double ended queue with pop and
push at both ends.
• list: Linked list of variables, struct or objects. It is a randomly
changing sequence of items.
• set (duplicate data not allowed in set), multiset (duplication
allowed): It is an unordered collection of items.
Associative
• map (unique keys), multimap (duplicate keys allowed): Associative
Containers
key-value pair held in balanced binary tree structure. An collection of
pairs of items indexed by the first one.
• stack [LIFO]: A sequence of items with pop and push at one end only.
• queue [FIFO]: A Sequence of items with pop and push at opposite
Container Adapters
ends.
• priorityqueue: It returns element with highest priority.
• string: Character strings and manipulation.
String
• rope: String storage and manipulation.
• bitset: Contains a more intuitive method of storing and manipulating
Bits
bits.
• iterator: STL class to represent position in an STL container. An
iterator is declared to be associated with a single container class
type.
Operations/Utilities
• algorithm: Routines to find, count, sort, search, ... elements in
container classes.
• autojptr: Class to manage memory pointers and avoid memory leaks.
Question-61 Explain the concept of smart pointers in C++.
Answer: The power of C++ comes with pointers and objects. In C++, pointers are very commonly
used, but the built-in pointers may give unexpected results if they were not used properly. When a
built-in pointer is created, it is not automatically set to NULL. If an uninitialized pointer is then
compared to NULL (pointer == NULL) the test will pass, and any dereferencing will result in
undefined behavior.
It’s fairly easy to remember to set a pointer to NULL when you create it, so this issue isn’t that
important, but what if you call a function that returns a pointer? If the memory was allocated on
the heap (i.e. came from a call to new or malloc) then someone has to delete it, or it will be a
memory leak. It’s up to the programmer to read the documentation and figure it out.
What about in a multi-threaded environment? It’s very easy for two threads to share the same data,
but what if both of them are using the same pointer, and one thread calls delete on the pointer
while the other thread is still using it?
Finally, if you’re using exceptions, you’ve probably had a pretty hard time making sure that each
time an exception is thrown, all of the allocated memory gets freed. Take the following code:
What if Foo throws an exception? You have to make sure that each and every exception that Foo
(or any function called by Foo) throws will be caught, in order to delete ptr; otherwise you’ll
have a memory leak. Certainly there must be a better solution than using these built-in pointers.
The answer for such problems is smart pointers.
The idea behind this smart pointer implementation is to have a set of objects that wrap the
functionality of a built-in pointer. These smart pointers either point to an object or they equal
NULL (they never point to memory that has been deleted and they are always initialized).
• The pointers must always point to valid memory, or be NULL
• The pointers will be reference counted and handle freeing the memory being pointed
to (so they can be exception safe while at the same time eliminating memory leaks)
• The pointers should be as similar to the built-in pointers as possible
Also, in order for the smart pointers to act like the built-in types, it was necessary for the
implementation to be non-intrusive. An intrusive smart pointer is one that requires a common base
class be used in any object that will be pointed to. When you create your custom objects, you
would have to inherit from a base class the gives reference counting functionality to your object.
This approach only works for user defined types, and the smart pointers would never be able to
point to built-in types (like int and float). A non-intrusive approach requires no changes to a
defined type (whether built-in or user defined) because the pointer has a more intelligent means of
keeping track of the reference count.
With smart pointers, the programmer doesn’t have to worry about memory management (never see
the word delete again!), the pointers are copy safe, thread safe, exception safe, and they never
point to memory that has already been deleted. If used properly, they avoid circular references,
and they work almost identically to the built-in pointers.
Question-62 Explain the concept of auto_ptr in C+ + .
Answer: The standard C++ library comes with a smart pointer called auto_ptr, for “automatic
pointer.” The auto_ptr smart pointer owns the object it holds a pointer to. That is, it releases the
memory associated to it upon destruction; it does not do any allocation by itself, nor does it keep
a reference counter of the memory involved.
The auto_ptr class overloads the * and -> operators so as to allow transparent access to the
dynamic object. To access the raw pointer itself, use the get method. Consider a simple example:
It is important to note in the example above that auto_ptr’s constructor cannot fail (it is declared
as throws()) If it could, the code would need to be more complex in order to catch those failures,
defeating in part the purpose of the smart pointer. The automatic pointer also provides the release
method, which detaches itself from the memory object, returning a raw pointer to it. This allows
the use of an automatic pointer in a critical section only, falling back to manual management once
that delicate code is over. As an example, imagine a function that returns a raw pointer to a
dynamically allocated object. This function needs to do multiple initialization tasks and has
several exit points if errors happen. You can use an automatic pointer to simplify its code:
Automatic pointers are not copyable. If the developer attempts to copy an instance of auto_ptr, the
object it points to will be transferred to the new smart pointer, invalidating the old one.
Therefore, using this class together with STL collections is dangerous; don’t do it, because it does
not follow the required semantics.
Furthermore, if two automatic pointers hold a reference to the same memory object, the behavior
is undefined (but typically the application will simply crash).
Note: In the C++11 standard, std:: unique_ptr is used instead of std:: autojptr.
Question-63 What is Serialization and Deserialization in Java?
Answer: We can convert a Java object to an Stream that is called Serialization. Once an object is
converted to Stream, it can be saved to file or send over the network or used in socket
connections. The object should implement Serializable interface and we can use
[Link] to write object to file or to any OutputStream object. The process of
converting stream data created through serialization to Object is called deserialization.
Question-64 Why Java is not pure Object Oriented language?
Answer: Java is not said to be pure object oriented because it support primitive types such as int,
byte, short, long etc. I believe it brings simplicity to the language while writing our code.
Obviously Java could have wrapper objects for the primitive types but just for the representation,
they would not have provided any benefit.
As we know, for all the primitive types we have wrapper classes such as Integer, Long etc. that
provides some additional methods.
Question-65 What is difference between path and classpath variables?
Answer: PATH is an environment variable used by operating system to locate the executable.
That’s why when we install Java or want any executable to be found by OS, we need to add the
directory location in the PATH variable.
Classpath is specific to Java and used by Java executables to locate class files. We can provide
the classpath location while running Java application and it can be a directory, ZIP files, JAR
files etc.
Question-66 Can we have multiple public classes in a Java source file?
Answer: We can’t have more than one public class in a single Java source file. A single source
file can have multiple classes that are not public.
Question-67 What is final keyword?
Answer: final keyword is used with Class to make sure no other class can extend it, for example
String class is final and we can’t extend it.
We can use final keyword with methods to make sure child classes can’t override it.
final keyword can be used with variables to make sure that it can be assigned only once.
However the state of the variable can be changed, for example we can assign a final variable to
an object only once but the object variables can change later on.
Java interface variables are by default final and static.
Question-68 What is static keyword?
Answer: static keyword can be used with class level variables to make it global i.e all the
objects will share the same variable.
static keyword can be used with methods also. A static method can access only static variables