C++ Question
C++ Question
How do you find out if a linked-list has an end? (i.e. the list is not a cycle)
You can find out by using 2 pointers. One of them goes 2 nodes each time. The second one
goes at 1 nodes each time. If there is a cycle, the one that goes 2 nodes each time will
eventually meet the one that goes slower. If that is the case, then you will know the linked-list
is a cycle.
The free subroutine frees a block of memory previously allocated by the malloc subroutine.
Undefined results occur if the Pointer parameter is not a valid pointer. If the Pointer
parameter is a null value, no action will occur. The realloc subroutine changes the size of the
block of memory pointed to by the Pointer parameter to the number of bytes specified by the
Size parameter and returns a new pointer to the block. The pointer specified by the Pointer
parameter must have been created with the malloc, calloc, or realloc subroutines and not been
deallocated with the free or realloc subroutines. Undefined results occur if the Pointer
parameter is not a valid pointer.
Function overloading: C++ enables several functions of the same name to be defined, as long
as these functions have different sets of parameters (at least as far as their types are
concerned). This capability is called function overloading. When an overloaded function is
called, the C++ compiler selects the proper function by examining the number, types and
order of the arguments in the call. Function overloading is commonly used to create several
functions of the same name that perform similar tasks but on different data types.
Operator overloading allows existing C++ operators to be redefined so that they work on
objects of user-defined classes. Overloaded operators are syntactic sugar for equivalent
function calls. They form a pleasant facade that doesn't add anything fundamental to the
language (but they can improve understandability and reduce maintenance costs).
What is the difference between declaration and definition?
The declaration tells the compiler that at some later point we plan to present the definition of
this declaration.
E.g.: void stars () //function declaration
The definition contains the actual implementation.
E.g.: void stars () // declarator
{
for(int j=10; j > =0; j--) //function body
cout << *;
cout << endl; }
It permits code reusability. Reusability saves time in program development. It encourages the
reuse of proven and debugged high-quality software, thus reducing problem after a system
becomes functional.
Write a short code using C++ to print out all odd number from 1 to 100 using a for loop
for( unsigned int i = 1; i < = 100; i++ )
if( i & 0x00000001 )
cout << i << \",\";
while (pointer1) {
pointer1 = pointer1->next;
pointer2 = pointer2->next; if (pointer2) pointer2=pointer2->next;
if (pointer1 == pointer2) {
print (\"circular\n\");
}
}
Answer2
Virtual destructors: If an object (with a non-virtual destructor) is destroyed explicitly by
applying the delete operator to a base-class pointer to the object, the base-class destructor
function (matching the pointer type) is called on the object.
There is a simple solution to this problem – declare a virtual base-class destructor. This
makes all derived-class destructors virtual even though they don’t have the same name as the
base-class destructor. Now, if the object in the hierarchy is destroyed explicitly by applying
the delete operator to a base-class pointer to a derived-class object, the destructor for the
appropriate class is called.
What is a template?
Templates allow to create generic functions that admit any data type as parameters and return
value without having to overload the function with all the possible data types. Until certain
point they fulfill the functionality of a macro. Its prototype is any of the two following ones:
You have two pairs: new() and delete() and another pair : alloc() and free().
Explain differences between eg. new() and malloc()
Answer1
1.) “new and delete” are preprocessors while “malloc() and free()” are functions. [we dont
use brackets will calling new or delete].
2.) no need of allocate the memory while using “new” but in “malloc()” we have to use
“sizeof()”.
3.) “new” will initlize the new memory to 0 but “malloc()” gives random value in the new
alloted memory location [better to use calloc()]
Answer2
new() allocates continous space for the object instace
malloc() allocates distributed space.
new() is castless, meaning that allocates memory for this specific type,
malloc(), calloc() allocate space for void * that is cated to the specific class type pointer.
What is a class?
Class is a user-defined data type in C++. It can be created to solve a particular kind of
problem. After creation the user need not know the specifics of the working of a class.
Which recursive sorting technique always makes recursive calls to sort subarrays that
are about half size of the original array?
Mergesort always makes recursive calls to sort subarrays that are about half size of the
original array, resulting in O(n log n) time.
What is abstraction?
Abstraction is of the process of hiding unwanted details from the user.
What is the difference between an external iterator and an internal iterator? Describe
an advantage of an external iterator.
An internal iterator is implemented with member functions of the class that has items to step
through. .An external iterator is implemented as a separate class that can be "attach" to the
object that has items to step through. .An external iterator has the advantage that many
difference iterators can be active simultaneously on the same object.
What are the differences between a C++ struct and C++ class?
The default member and base-class access specifier are different.
How does throwing and catching exceptions differ from using setjmp and longjmp?
The throw operation calls the destructors for automatic objects instantiated since entry to the
try block.
Explain the ISA and HASA class relationships. How would you implement each in a
class design?
A specialized class "is" a specialization of another class and, therefore, has the ISA
relationship with the other class. An Employee ISA Person. This relationship is best
implemented with inheritance. Employee is derived from Person. A class may have an
instance of another class. For example, an employee "has" a salary, therefore the Employee
class has the HASA relationship with the Salary class. This relationship is best implemented
by embedding an object of the Salary class in the Employee class.
In C++, what is the difference between method overloading and method overriding?
Overloading a method (or function) in C++ is the ability for functions of the same name to be
defined as long as these methods have different signatures (different set of parameters).
Method overriding is the ability of the inherited class rewriting the virtual method of the base
class.
Write a program that ask for user input from 5 to 9 then calculate the average
int main()
{
int MAX=4;
int total =0;
int average=0;
int numb;
cout<<"Please enter your input from 5 to 9";
cin>>numb;
if((numb <5)&&(numb>9))
cout<<"please re type your input";
else
for(i=0;i<=MAX; i++)
{
total = total + numb;
average= total /MAX;
}
cout<<"The average number is"<<average<<endl;
return 0;
}
Answer2.
A copy constructor is used to initialize a newly declared variable from an existing variable.
This makes a deep copy like assignment, but it is somewhat simpler:
1) dynamic id operator
2) typecast operator
Answer2.
RTTI is defined as follows: Run Time Type Information, a facility that allows an object to be
queried at runtime to determine its type. One of the fundamental principles of object
technology is polymorphism, which is the ability of an object to dynamically change at
runtime.
Is C an object-oriented language?
C is not an object-oriented language, but limited object-oriented programming can be done in
C.