Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203205
  • 博文数量: 32
  • 博客积分: 1025
  • 博客等级: 少尉
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-13 15:54
文章分类

全部博文(32)

文章存档

2011年(30)

2010年(2)

我的朋友

分类: C/C++

2011-04-01 01:04:10

1.    Difference between heap and stack?

The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored.

The heap is the section of computer memory where all the variables created or initialized at runtime are stored. heap is an area of memory used for dynamic memory allocation .

Stack: 
- local variables (variables declared inside a function) are put on the stack - unless they are also declared as 'static' or 'register'
- function parameters are allocated on the stack
- local variables that are declared on the stack are not automatically initialized by the system so they usually have garbage in them until you set them
- variables on the stack disappear when the function exits (thus, if a function is called multiple times, it's local variables and parameters are recreated and destroyed each time the function is called end exited).

Heap:
- declared variables (as opposed to dynamically created ie new, malloc) are created on the heap before program execution begins, they exist the entire life of the program (although scope may prevent access to them - they still exist) and they are initialized to all zeros
- global variables are on the heap
- static local variables are on the heap (this is how they keep their value between function calls)
- memory allocated by new, malloc and calloc are on the heap
 
2.    The difference between structs and classes in c++?

The members and base classes of a struct are public by default, while in class , they default to private . Note: you should make your base classes explicitly public , private , or protected , rather than relying on the defaults.

struct and class are otherwise functionally equivalent. 
3.    What is virtual function?

A virtual function is a member function that you expect to be redefined in derived classes. 
4.    How does virtual function implemented? 
Vtable: 
http://blog.csdn.net/yyyy1985/archive/2009/05/07/4156746.aspx

5.    What’s abstract class?

An abstract class, or abstract base class (ABC), is a class that cannot be instantiated.

Abstract classes are closely related to interfaces. They are classes that cannot be instantiated, and are frequently either partially implemented, or not at all implemented. One key difference between abstract classes and interfaces is that a class may implement an unlimited number of interfaces, but may inherit from only one abstract (or any other kind of) class. 
6.    What is pure virtual function?

An abstract class contains at least one pure virtual function. Specify a virtual function as pure by placing = 0 at the end of its declaration. You don't have to supply a definition for a pure virtual function. 
7.    What’s the difference between virtual function and pure virtual function?

There are two major differences between a virtual and a pure virtual function, these are below:

  • There CAN’T be a definition of the pure virtual function in the base class.

  • There MUST be a definition of the pure virtual function in the derived class.

In other words, because pure virtual function has to be declared as =0 in the base class without function body; and then be redefined in the derived class to override the declaration in base class. 
8.    What are the differences and similarities between thread and process? 
Differences

In Space: Threads share the address space  of the process that created it; Process  have their own address. 
Data Access: Threads have direct access  to the data segment of its process; P rocesses   have their own copy of the data segment of the parent process.  
Commu: Threads can directly  communicate   with other threads of its process;  processes   must use inter-process communication to  communicate  with sibling   processes . 
Overhead: Threads have almost no overhead;  processes   have considerable overhead. 
Creation/Call: New threads are easily created; new  processes   require duplication of the parent process. 
Threads can exercise considerable control over threads of the same process;  processes   can only exercise control over child  processes . 
Interaction: Changes to the main thread (cancellation,  priority   change, etc.) may affect the behavior of the other threads of the process, changes to the parent process does not affect child  processes .

Similarities:

Both have an id, set of registers, state, priority, and scheduling policy. 
Both have attributes that describe the entity to the OS.
Both have an information block.
Both share  resources   with the parent process. 
Both function as independent entities from the parent process.
The creator can exercise some control over the thread or process.
Both can change their attributes.
Both can create new resources. 
Neither can access the  resources   of another process.

9.    What’s the difference between mutex and semaphore? 
A mutex is a binary semaphore, usually including extra features like ownership or priority inversion protection.

The differences between mutexes and semaphores are operating system dependent . Mutexes are meant to be used for mutual exclusion only and binary semaphores are meant to be used for event notification and mutual exclusion

10.    What is the difference between new and operator new?

  1. void * p =  new  (1024);  //compilation error    
  2. void * p =  operator   new  (1024);  //works   

The C++ keyword "new" represents the new operator.It is used:
1. to allocate the memory
2. to call the constructor.

The (1)st job is done using C++'s inbuilt 'operator new'. (Bad overloading of terminologies!). The prototype of "operator new" is void* operator new (size_t);
Thus, In the first example, the new operator expects the 'type' and not the size, since it also needs to call the constructor on that type. Hence the first line gives compilation error. (1024 is not a type.)
On the second line, you are explicitly calling the 'operator new'. In other words, what you are doing is only the first step (allocating the memory). The 'operator new' requires size_t which you have provided properly (1024), and hence the code compiles properly.
 

11.    What is the difference between assignment and initialization? 
Assignment changes the value of the object that has already been constructed.

Assignment operation can be executed many times; 
Initialization constructs a new object and gives it a value at the same time.

Initialization operation can only be executed once.  

For example, you can initialize a const type object, but you can not do assignment on it.

12.    What is polymorphism? How does it implemented?

In programming languages,  polymorphism   means that some code or operations or objects behave differently in different contexts. It is an ability to call different  functions   by using only one type of function call. Typically, polymorphism   in C++ is implemented by using  virtual   methods.

(一篇简短又易懂的文章形像的介绍了如何用虚函数实现了多态http://www.cublog.cn/u/5391/showart_203729.html )
13.    What is encapsulation? 
Encapsulation is the process to hide the implementation details of the object and the only thing that remains externally visible is the interface of the object.

14.    Copy constructor

The copy constructor lets you create a new object from an existing one by initialization. A copy constructor of a class A is a non-template constructor in which the first parameter is of type A& , const A& , volatile A& , or const volatile A& , and the rest of its parameters (if there are any) have default values.

15.    Difference between copy constructor and assignment .

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. There is no need to test to see if it is being initialized from itself.
  2. There is no need to clean up (eg, delete) an existing value (there is none).
  3. A reference to itself is not returned.

16.    What’s the difference between pointer and reference? 
Pointer *

1) Its not necessary to initialize the pointer at the time of declaration. Like

  1. /* 1st way to initialize a pointer */   
  2. int  a = 10;  
  3. int  *P = &a;  //It is not necessary    
  4.   
  5. /* 2nd way to initialize a pointer */   
  6. int  a = 10;  
  7. int  *P;  
  8. P = &a;   
 
2) You can create the array of Pointer.
3) You can assign NULL to the pointer like
  1. int  *P = NULL;  //Valid   
 
4) You can use pointer to pointer.
Reference &
1) Its necessary to initialize the Reference at the time of declaration. Like
  1. int  &a = 10;  
  2. int  &a;  //Error here but not in case of Pointer.   
 
2) You can not create the Array of reference.
3) You can not assign NULL to the reference like
  1. int  &a = NULL;  //Error   
 
4) You can not use reference to reference.
 

17.    What is friend function?

A friend of a class is X a function or class that is not a member of X, but is granted the same access to X as X's members. Functions declared with "friend" label in a class member list are called "Friend Functions"; Classes declared with the "friend" label in the member list of another class are called "Friend Classes" of that class.

18.    What’s the binary search tree? (data structure)

A  binary tree   where every  node's   left  subtree   has  keys   less than the node's key, and every right subtree has keys greater than the node's key. 
19.    What’s the stack? 
The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored.

20.    What’s LIFO (Last In First Out)? 
In a LIFO structured linear list, elements can be added or taken off from only one end, called the "top ".

21.    Given a stack, and 5 variables, require you put the 5 variables into the stack. How would you test it before putting them in?

Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. A destructor is a member function with the same name as its class prefixed by a ~ (tilde).

(A constructor can be declared as virtual or static; but a destructor can be declared virtual or pure virtual)

30.  What is copy constructor? When it is called?

copy constructor is called whenever a new variable is created from an object. This happens in the following cases (but not in assignment).

  • A variable is declared which is initialized from another object
  • A value parameter is initialized from its corresponding argument.
  • An object is returned by a function

31.  Overloading and overriding difference 
Overriding is the example of run-time polymorphism and Overloading is the example of compile-time polymorphism.
Overriding 
■The return type must exactly match that of the overridden method.
■The access level must not be more restrictive than that of the overridden method.
■The access level can be less restrictive than that of the overridden method.
■The overriding method must not throw new or broader checked exceptions than those declared by the overridden method. 
■The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares.
■You cannot override a method marked final.
■If a method can’t be inherited, you cannot override it. 
Overloaded method 
■Overloaded methods must change the argument list.
■Overloaded methods can change the return type.
■Overloaded methods can change the access modifier.
■Overloaded methods can declare new or broader checked exceptions.
■A method can be overloaded in the same class or in a subclass.

32.    what is recurcive function? Implement one recurcive funcition.

Recursive function is a function which contains a call to itself.

For example: find n!

  1. unsigned  int  fact(unsigned  int  n)  
  2. {  
  3.    if (n==1)  
  4.       return  1;  
  5.    else   
  6.       return  fact(n-1)*n;  
  7. }  
 
33.   Declare a pointer point to an interger;

         int *a;
point to 20 intergers;

         int (*a)[20];
point to 20 pointers which point to interger

         int *a[20];
34.  What’s virtual destructor?

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.

( )
35.    How is the source file compiled to executable(from source file -> binary file)?

A compiler is likely to perform many or all of the following operations: => => => semantic analysis=> => .

For GCC, the sequence of commands executed by a single invocation of GCC consists of the following stages:

36.    After the source file is compiled to binary file, how is the binary file orgnized and stored? 

37.    What is overloading? How does the computer know which function is according to which implementation since they have the same name?

The  operator   keyword declares a function specifying what  operator-symbol means when applied to instances of a class. This gives the operator more than one meaning, or "overloads" it. The compiler distinguishes between the different meanings of an operator by examining the types of its operands . 
38.    Memory leakage: 
A  memory leak   or  leakage   in  computer science  is a particular type of memory consumption by a  computer program  where the program is unable to release  memory  it has acquired.

How you ever met memory leakage program, is it cause severe problem? 
For firefox, the memory leakage is not a bug, it it a feature!

How did you find the leakage? 

Have you ever used any software to detect the memory leakage?

https://developer.mozilla.org/en/Debugging_memory_leaks 
39.    c++ file in Linux: which compiler do you use?

GCC
40.    Given a file, tell me the steps of reading the file. 
41.    What is “IS” and “Has” in C++? What’s the difference?  
Is-a   is a relationship where one class  D   is a  subclass  of another class  B   (and so  B   is a  superclass  of  D );

Has-a   is a relationship where one object "belongs" to (is  a part or member of) another object. In simple words,  has-a   relationship in an object is called member field of an object.

Is-a   relationship constitutes a hierachy of subtyping/subtype polymorphism; Multiple  has-a   relationships will combine to form a possessive hierarchy.

(参考http://blog.chinaunix.net/u2/85848/showart_1962718.html )

42.    the disadvantage of using stored procedures with parameters. 
43.    when inserting a record into a table, how can you let the relatived tables change accordingly?

44. 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.

 The definition contains the actual implementation. (contains body)

Cisco 试题:

What will print out?

  1. Main()   
  2. {   
  3.     char  *p1=“name”;   
  4.     char  *p2;   
  5.     p2=(char *)malloc(20);   
  6.     memset (p2, 0, 20);   
  7.     while (*p2++ = *p1++);   
  8.     printf(“%sn”,p2);   
  9. }   

Answer:empty string.

 

What will be printed as the result of the operation below:

 

  1. main()   
  2. {   
  3.     int  x=20,y=35;   
  4.     x=y++ + x++;   
  5.     y= ++y + ++x;   
  6.     printf(“%d%dn”,x,y);   
  7. }   

Answer : 5794

 

What will be printed as the result of the operation below:

  1. main()   
  2. {   
  3.     int  x=5;   
  4.     printf(“%d,%d,%dn”,x,x<<2,x>>2);   
  5. }   

Answer: 5,20,1

 

What will be printed as the result of the operation below:

  1. #define swap(a,b) a=a+b;b=a-b;a=a-b;    
  2.   
  3. void  main()   
  4. {   
  5.     int  x=5, y=10;   
  6.     swap (x,y);   
  7.     printf(“%d %dn”,x,y);   
  8.     swap2(x,y);   
  9.     printf(“%d %dn”,x,y);   
  10. }   
  11.   
  12. int  swap2( int  a,  int  b)   
  13. {   
  14.     int  temp;   
  15.     temp=a;   
  16.     b=a;   
  17.     a=temp;   
  18.     return  0;   
  19. }   

Answer: 10, 5

10, 5

 

What will be printed as the result of the operation below:

 

  1. main()   
  2. {   
  3.     char  *ptr = ”Cisco Systems”;   
  4.     *ptr++;   
  5.     printf(“%sn”,ptr);   
  6.     ptr++;   
  7.     printf(“%sn”,ptr);   
  8. }   

Answer:Cisco Systems

isco systems

 

What will be printed as the result of the operation below:

  1. main()   
  2. {   
  3.     char  s1[]=“Cisco”;   
  4.     char  s2[]= “systems”;   
  5.     printf(“%s”,s1);   
  6.   
  7. }   

Answer: Cisco

 

What will be printed as the result of the operation below:

  1. main()   
  2. {   
  3.     char  *p1;   
  4.     char  *p2;   
  5.     p1=(char  *)malloc(25);   
  6.     p2=(char  *)malloc(25);   
  7.     strcpy(p1,”Cisco”);   
  8.     strcpy(p2,“systems”);   
  9.     strcat(p1,p2);   
  10.     printf(“%s”,p1);   
  11. }   

Answer: Ciscosystems

 

The following variable is available in file1.c, who can access it?:

  1. static   int  average;   

Answer: all the functions in the file1.c can access the variable.

 

What will be the result of the following code?

  1. #define TRUE 0 // some code    
  2.   
  3. while (TRUE)   
  4. {   
  5.   
  6.    // some code    
  7.   
  8. }   

Answer: This will not go into the loop as TRUE is defined as 0.

What will be printed as the result of the operation below:

 

  1. int  x;   
  2. int  modifyvalue()   
  3. {   
  4.     return (x+=10);   
  5. }   
  6.   
  7. int  changevalue( int  x)   
  8. {   
  9.     return (x+=1);   
  10. }   
  11.   
  12. void  main()   
  13. {   
  14.     int  x=10;   
  15.     x++;   
  16.     changevalue(x);   
  17.     x++;   
  18.     modifyvalue();   
  19.     printf("First output:%dn" ,x);   
  20.     x++;   
  21.     changevalue(x);   
  22.     printf("Second output:%dn" ,x);   
  23.     modifyvalue();   
  24.     printf("Third output:%dn" ,x);   
  25. }   
  

  Answer: 12 , 13 , 13

  What will be printed as the result of the operation below:

 

  1. main()   
  2. {   
  3.     int  x=10, y=15;   
  4.     x = x++;   
  5.     y = ++y;   
  6.     printf(“%d %dn”,x,y);   
  7. }   
  8.   
  9.     

Answer: 11, 16

What will be printed as the result of the operation below:

 

  1. main()   
  2. {   
  3.     int  a=0;   
  4.     if (a==0)   
  5.     printf(“Cisco Systemsn”);   
  6.     printf(“Cisco Systemsn”);   
  7. }   

  Answer: Two lines with “Cisco Systems” will be printed.

====

Quotes :

http://dev.fyicenter.com/Interview-Questions/CPP-1/What_is_polymorphism_Explain_with_an_example_.html

  • preprocessing (to expand macros)
  • compilation (from source code to assembly language)
  • assembly (from assembly language to machine code)
  • linking (to create the final executable)  

A difference between a destructor (of course also the constructor) and other member functions is that, if a regular member function has a body at the derived class, only the version at Derived class gets executed. Whereas in case of destructors, both derived as well as base class versions get executed.


22.    How would you test your own program? 
23.    Tell me the first three of your most familiar technical skills. 
24.    What’s the persentage of back-in, middle tier, and front-in development? 
25.    What do you think a good code is?

26.    How do you keep you with the latest technologies? 
27.    distance vector routing protocol 
It's one of the two major classes of  routing protocols   used in  packet-switched networks   for  computer communications , the other major class being the  link-state protocol . A distance-vector routing protocol uses the  Bellman-Ford algorithm   to calculate paths.

Ignoring speed of development for a moment, is the  best code   software that:

  • can adapt to change
  • performs well
  • is error free
  • is as simple as possible
  • does what my customer needs

28.    c and c++ difference 
OOP concepts

29.    Explain constructor and destructor 
constructor is a member function with the same name as its class. Constructors are used to create, and can initialize, objects of their class type.

转载自:http://blog.csdn.net/Lixinag/archive/2010/04/01/5441417.aspx

阅读(1504) | 评论(0) | 转发(0) |
0

上一篇:sizeof()用法汇总

下一篇:C++面试题大全1

给主人留下些什么吧!~~