Wednesday, June 23, 2010

C++ interview Questions (1)

1) Using for loop, print out even number between 1 and 50.


#include<iostream>
using namespace std;

int main()
{


for (int i=1; i<=50;i++)

if(i%2==0) cout <<"even i="<<i<<endl;

return(0);
}

2)How do you know what shell you are running?
echo $0

3) What is object-oriented design?
In object-oriented design, an object is a fundamental entity.
The three basic principles of object-oriented are as follow:
a) Encapsulation-The ability to combine data, and operations on that
data, in a single unit.
b) Inheritance -The ability to create new objects from existing objects.
c) Polymorphism-The ability to use the same expression to denote
different operations.




4) Write a program to input 5 number between 1 and 10 and output average.
#include<iostream>
using namespace std;

int main()
{
double number;
double total = 0;

for(int i=1; i<=5;i++)
{
cout <<"please input a number between 1 and 10"<<endl;

cin>>number;
while(number<1 ||number>10)
{
cout<<"you number is not between 1 and 10, enter again"<<endl;

cin>>number;
}
total = total + number;

}

cout<<"the average of your 5 inputs are "<< total/5<<endl;
return(0);

}

~                    

5. Reverse a linked list
node * reverse(node *head)
{

node *curr = *head, *prev = NULL, *nxt_fwd = NULL;

while(curr)
{
nxt_fwd = curr->link;
curr->link = prev;

prev =curr;
curr=nxt_fwd;
}
head = prev; /*save the new head */

return head;
}

A linked list is a data structure that consists of a sequence of 
data records such that in each record there is a field that contains 
a reference (i.e., a link) to the next record in the sequence. 
 
6. What is difference between #define and const?

#define is macro and don't need to state the data type,
const is a modifier and need to state the data type.

7. What is difference between public, private and protected variables?
private variables can only be accessed by member functions and
friends of that class.
protected variables can be accessed by member fucntions and friends of
that class and its derived class.
public variables can be accessed by anyone.

8. What is difference between pointer and reference?
Reference is the other name given to the same variable. Pointer is used to
contain address of another variable. For pointer
a) Its not necessary to initialize the pointer at the time of declaration.
b) You can create the array of Pointer.
c) You can assign NULL to the pointer
d) You can use pointer to pointer.
But reference can not.

9. What C++ library are you proficient with?
STL, Boost and openmp.



10. What is virtual basic class and friend class?
Virtual Base Class: Used in context of multiple inheritance in C++. If
you plan to derive two classes from a class, and further derive one
class from the two classes in the second level, you
need to declare the uppermost base class as 'virtual' in the inherited classes.
This prevents multiple copies of the uppermost base class data members when
an object of the class at the third level of hierarchy is created.


Friend class: When a class declares another class as its friend, it is
giving complete access to all its data and methods including private
and protected data and methods to the friend
class member methods.



11. What is the difference between a .lib and .dll? 



DLL : as the name states is a dynamic link library, that can allows functions/

classes/ implmented within XXX.dll to be called at run time. Hence the calling

.exe or .dll links with XXX.dll at run time.



LIB : is static linking. I.e when the calling exe or dll calls the XXX.lib

the exporting functions/classes etc are pulled in at compile time, opposed

to run time.

No comments:

Post a Comment