Comparaison des versions

Légende

  • Ces lignes ont été ajoutées. Ce mot a été ajouté.
  • Ces lignes ont été supprimées. Ce mot a été supprimé.
  • La mise en forme a été modifiée.

...

  1. What C++ references are linked in the NavBar on myCourses? Why are they useful?

  2. What is C++11?

    1. C++11 (formerly known as C++0x) is the most recent version of the standard of the C++ programming language.
  3. VS2012 doesn’t implement all of C++11. Give a link that summarizes the features.
    http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx

     

  4. What is a declarator?
    http://msdn.microsoft.com/en-us/library/tb971bed.aspx

     

  5. Explain the difference between declaration and definition. Given an example using a function.

  6. What is a translation unit?

  7. What is an object file?
    http://en.wikipedia.org/wiki/Object_file

     

  8. When you compile a C++ program, what are the basic steps the C++ compiler follows?

    http://faculty.cs.niu.edu/~mcmahon/CS241/Notes/compile.html

     

  9. What is a pre-processor macro?
    Fragment of code which is given a name, wherever the name is used, it is replaced by the contents of the macro //This is not correct according to this link: http://www.cplusplus.com/doc/tutorial/preprocessor/
    // 
    this isn't wrong, its just another way of explaining a pre-processor macro. The cplusplus link even says the same thing in the last sentence: http://gcc.gnu.org/onlinedocs/cpp/Macros.html

    A pre-processor macro is expression, statement, a block of code, or anything that is defined by a name and the #define preprocessor directive.  Here is the format of a pre-processor macro: #define identifier replacement.  Anywhere in the program where the pre-processor macro name is used, it is replaced by its replacement before compile time.

    Example:

    #define NUMBER_OF_ELELMENTS 2

    int array[] = new array[NUMBER_OF_ELEMENTS];

    Before compile time:

    int array[] = new array[2];

     

  10. Demonstrate how to write an include-guard without using “once.”

    #ifndef WITHOUT_ONCE
    #define WITHOUT_ONCE

    //code here
    #endif

  11. What is a pointer?

    1. In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.
  12. What is a reference?

    1. References allow us to address a variable/method in a manner similar to how we use pointers, but with some fundamental differences. For example when you "pass by reference" into a function, you don't create a new object with the same data as that which was passed in; you instead "point" to the existing variable and pull the data from there. This allows us to act upon the stored data indirectly - we can even go so far as to destroy our reference without destroying the original object. To further passing by reference, if we modify the "reference" we actually are updating the variable it refers to.
    2. References can't be null, unlike pointers - they always refer to data/an object, and after assigned they can't be "reseated" or reassigned, unlike pointers.
    3. Source: http://en.wikipedia.org/wiki/Reference_(C%2B%2B)
  13. Explain the difference between an L-value and an R-value. Demonstrate your response via an example with pointers on either side of an assignment.

  14. Explain the difference between a pointer and a reference.

  15. Demonstrate how to pass by reference via pointers.

    1. int x = 5,*p = &x, &r = (*p); //This is not right.  Pass by reference refers to passing in parameters of a function by reference.

    2.  

      void print(int* x) //print(int* &x) <- this doesn't make any sense. Pass by reference != pass "as" a reference. Just use a pointer. http://stackoverflow.com/questions/1257507/what-does-this-mean-const-int-var

      It actually does make sense.  If you google pass by reference, you will get results passing in arguments of a function by reference.  http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr233.htm

       //Nothing on that page says to do (int * &x), writing this means a reference to a pointer to an int. You can do this in C++, but all that happens when you use it is the Reference gives you the pointer which then you dereference anyway, to get the value you want.

      {

       

                    cout << "x = " << *x << "\n";

       

      }

       

      int x = 5;

       

             int* y = &x;

       

      print(y);

  16. What operator gives you a short-cut for code like (*p).x?

    "->"

  17. Explain the difference between NULL and nullptr. Why should you switch to nullptr?

    Null is nothing/garbage data, nullptr is a pointer to nothing. Use nullptr because its type safe, because null is an int under the hood. (http://www.devx.com/cplus/10MinuteSolution/35167)

     

  18. Should you pass by value or reference (or even pointer) when passing an array (or other “large” object)? Give a brief of example how bad memory gets and how to fix the issue.

    Passing large arrays by value copies the entire array contents into a second object. Sending by reference only sends the address of the array and doesn’t copy the data.
    Ex: array of a million items, copied makes two million items. 

     

  19. Demonstrate how to allocate an array via a pointer. In the same example, demonstrate how to access and store values in the array, also using pointers. No [] notation allowed!

    http://www.ics.uci.edu/~dan/class/165/notes/memory.html 

    *b = 1;

    *(b + 1) = 2;

    *(b + 2) = 3;

    int x = *(b + 2);

    cout << x // 3


  20. Refer to the above question and give the relationship between pointers and arrays.

  21. Demonstrate via an example how to create a 2-D array of 3 rows and 2 columns.

    Int n[3][2] = {{1,2},{3,4},{5,6}}; //this is one way of doing it with an initializer, see http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new

  22. Demonstrate via an example how to create a 2-D array of a user-input number of rows and columns.

    http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new

  23. Now that you’ve used arrays and pointers, are pointers and arrays the same?

    No.

  24. Read http://c-faq.com/aryptr/aryptr2.html. Now answer the above question again. In your own words, explain the difference(s).

    An array is a named location of items while a pointer points to the starting location of a series of items. Because the array is a named set of items it cannot get items from other areas, but a pointer can change where it is pointing to and get new information.

     

  25. Demonstrate how to store the address of a variable in another variable.
    int otherInt = 4;
    int* x = &otherInt; // I think he means below 

    int v =100,  address = (int)&v; //You do not have to cast to an int, memory locations are int by default, i'll simplify above

  26. Demonstrate how to print the address of a variable.

    Printf(“%i”, &x);

     

  27. What is const correctness and why should you maintain it?

    http://www.cprogramming.com/tutorial/const_correctness.html

     

  28. Demonstrate the difference between a pointer to a constant, a constant pointer, and a constant pointer to a constant. Under what circumstances can you change values for each?

    1. Int Const* x : can change the pointer but not the value of whats being pointed at
    2. Int* const y; : can change the value of whats being pointed at, but cannot change what to point to
    3. Int const * const z; cannot change either the value of whats being pointed at or change what to point to

     

  29. Are references automatically constant? Justify your response with examples.
    Yes, because a reference does the following:

    • It is not possible to refer directly to a reference object after it is defined; any occurrence of its name refers directly to the object it references.
    • Once a reference is created, it cannot be later made to reference another object; it cannot be reseated. This is often done with pointers.

    This means that a reference once created cannot be changed - if this occurs, then we are dealing with a const.

    Example: 

    int x = 3;

    int &y = x;

    y++;

    x==4; //should be true

    int z = 5;

    y=z;//should - if it doesn't fail - assign x to be 5. what we are SAYING though is make y refer to z, which we cannot do as it is assigned to x.

  30. Explain the difference between a C-string and C++ string.
    A C string is a char array terminated by '\0' whereas a C++ string is an actual class/data type.
    http://cs.stmarys.ca/~porter/csc/ref/c_cpp_strings.html

     

  31. Does char s[] = {'h','e','l','l','o','\0'}; create a C-string? Why or why not?

  32. Demonstrate how to convert a user-input string into its numerical equivalent, i.e., what is C++’s way of doing int.Parse()? Better yet, include exception handling.

  33. Define the function for the declaration int length(char* c), which returns the length of an input string. You must not use array notion ([]) anywhere in the solution. Hint: you must use pointers.

    int length(char* c) {

    int length = 0;

    while ( *c != NULL ) {

     c++;

      length++;

    }

    return length;

    }

  34. Demonstrate how to allocate and free memory via malloc/free.
    http://computer.howstuffworks.com/c29.htm

     

  35. Demonstrate how to allocate and free memory via new/delete.

  36. Can (or even should) you “mix” malloc/delete or new/free? Why or why not?
    http://www.codeproject.com/Articles/6555/To-new-is-C-To-malloc-is-C-To-mix-them-is-sin

  37. What is a memory leak, and why is it bad?

  38. What operator do you call to prevent memory leaks?

  39. What tools/functions could you use to detect memory leaks?

  40. What is a smart pointer, and how does it help with memory leaks?

  41. Which smart pointers does VS2012 support?

    Many types:

    unique, shared, weak

    http://msdn.microsoft.com/en-us/library/vstudio/hh279674.aspx

  42. What is an initializer? Give some examples of different kinds of initializers.

  43. Demonstrate how to create and initialize an array.

  44. Explain the differences between expressions, statements, and expression-statements. Given C++ examples of each.

  45. Does C++11 support a native for-each statement? Does VS2012 support it?

  46. Explain instance variables, data members, and fields with respect to classes. Do the concepts to which these terms refer differ in any great fashion?

  47. Explain the differences and similarities of plain-old data types and structs.

  48. Explain the differences and similarities of structs and classes.

    In c++ they are pretty much the same except for syntax and structs’ methods and fields are by default public while classes are by default private.

    http://stackoverflow.com/questions/2750270/c-c-struct-vs-class

  49. Give an example of a struct that might help graphics programming.

    struct point3d {

    float x;

    float y;

    float z; 

    };


  50. You probably never covered unions in your prior course, but they sometimes get asked at an interview. What are they?

    union is a user-defined data or class type that, at any given time, contains only one object from its list of members
    http://msdn.microsoft.com/en-us/library/5dxy4b7b.aspx

  51. Explain the difference between the stack and the heap with respect to OOP.

    Stack is "scratch-space" memory for the program while it is executing. Heap stores dynamically allocated memory and it not allocated per each new scope like the stack.
    http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap

  52. Actually, what’s wrong with the above question with respect to C++? Hint: who invented C++? What did he call “the heap?”

    Bjarne Stroustrup. He called it the “Free-Store”.

    http://stackoverflow.com/questions/1350819/c-free-store-vs-heap 

  53. Demonstrate how to create a stack-allocated object from a class.

  54. Demonstrate how to create a heap-allocated object from a class.

  55. Why are memory alignment and the heap a “big deal” with respect to game engines?

  56. Under what circumstances do object fields get zero-initialized in object creation? Does it matter if the object is stack or heap allocated?

  57. C++ supports static. Demonstrate how to assign and use static data members, global values, and static functions.

  58. Think about const and how it relates (if at all) with static. Since const means “not changeable” and static seems to mean something similar, why did the creators of C++, Java, C#, … actually use the keyword static given the possible confusion. Hint: What’s the antonym of “static?”

    Static exists only in one spot, i.e. it belongs to a class or a function, but it can be changed. When it is changed, it is changed for any and all instances of that class or for anyone who calls that function.

    Const means that it cannot be changed. Multiple objects may have different const variables, but their individual copies of it cannot be changed.

    The two keywords can be used together also.

    http://stackoverflow.com/questions/2216239/what-is-the-difference-between-a-static-and-const-variable

  59. Explain the meaning of “special member function” and give examples using the “Big-Three” of C++.

  60. If you do not provide special member functions for a class, which ones does C++ provide by default?

  61. When should you provide special member functions?

  62. Explain the difference between a copy constructor and copy assignment operator overload.

    A copy constructor is used to initialize a previously uninitialized object from some other object's data.

    An assignment operator is used to replace the data of a previously initialized object with some other object's data.

  63. Speaking of overloading, is it the same principle as overriding? (Schwartz asks this question every year, and students continue to get it wrong. Hint: look up inheritance.)

    Overriding is writing a new behavior onto a new function. Overloading is creating a function with the same name and more parameters.

    Thus if you overload you create a new function, that has the same name as another, but is totally separate and identified from the other via the parameters.


  64. What is a destructor? Give an example of when you need to define one.

  65. Demonstrate a conversion constructor.

  66. Demonstrate a conversion function.

  67. Demonstrate a function pointer.

    Destructors deallocate memory and do other cleanup tasks for an object when it is destroyed.

    If you want your object to print a message to the console when it is being destroyed then you need to define your own destructor.

    http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Fcplr380.htm


  68. Demonstrate a conversion constructor.

    class Point
    {
    public:
       Point(); // constructor
       Point( int ); // conversion constructor
       //...
    };

    http://msdn.microsoft.com/en-us/library/s2ff0fz8.aspx 


  69. Demonstrate a conversion function.

    struct Money {
      operator double() { return _amount; } //conversion function

    private:
       double _amount;
    };

    int main() {
      Money Account;
      double CashOnHand = Account;
    }

    http://msdn.microsoft.com/en-us/library/5s5sk305.aspx 


  70. Demonstrate a function pointer.

    void *(*foo)(int *); 

    http://www.cprogramming.com/tutorial/function-pointers.html


  71. You’ll notice size_t quite a bit in C++ reference pages, e.g., http://msdn.microsoft.com/en-us/library/y1w9bk0b.aspx and many others. What is it?

  72. What is structure padding and how should you avoid it?

  73. Explain the difference between an unsafe and static cast. Which should you generally use when not worrying about inheritance or polymorphism?

  74. How do you “do” a “this” in C++? Give a few examples.

    "this" is actually a pointer, as described here:
    http://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm

     

  75. How do you a “ToString” in C++? Give an example. Demonstrate your response via code that would work in VS2012.

  76. What is purpose of friend? Demonstrate how to use friends in classes.
    http://en.wikipedia.org/wiki/Friend_class

    http://www.cprogramming.com/tutorial/friends.html

     

  77. Why do you need both #include <iostream> and using namespace std;? What happens if you leave one out? What does each line do? Demonstrate how to avoid writing using namespace std;.

  78. Why do you need to split your code into header and CPP files?

  79. What is a circular class definition, how do you resolve it?
    Circular class definitions occur when two or more classes are dependent on one another in order to be initialized. This can be resolved either by changing the code to follow a different design pattern (ex. MVC, so that only one thing can modify the other, while a third thing displays the changes) or by using forward declaration when creating the objects.
    http://en.wikipedia.org/wiki/Forward_declaration

     

  80. What happens if you attempt to write a class (or struct) called Point in VS2012. Does the compiler accept the name? If not, what should you do to fix the problem?

    (Zachary Sherwin) Works. Made an empty console application in C++ in VS2012 and no errors. Anyone else?

  81. What is an abstract data type, and why does that term arise when discussing the design and implementation of data structures?

  82. What kind of list should you use to limit memory allocation?

  83. Does C++ have a class Object/object?

    No. You can sort of fake them with templates (and technically all instantiated classes are "objects" conceptually, but are only of that class type, not an object).

    Source: http://www.cplusplus.com/forum/beginner/25721/

    Source: http://stackoverflow.com/questions/11747439/c-object-class

     

  84. A Thing is a creature with a single letter (A-Z) for its name. When they’re born they get a random letter for their name. Things can have the same name. Write a list class, node class, and a test class in which you generate a list of four random Things. Use header and C++ files. You can skip using inheritance for now.

  85. Explain the difference(s) between a stack and queue.

    Both are linear data structures; the only difference is really in HOW they add/remove/access data within the structure. Simply put, stacks are LIFO (last in first out) and queue are FIFO (first in first out) in how they access the "first" object in the data structure (i.e. the top of the stack or the front of the queue).
    http://www.cs.cmu.edu/~adamchik/15-121/lectures/Stacks%20and%20Queues/Stacks%20and%20Queues.html

     

  86. Explain the difference between an array-based queue and a list-based queue. Under what circumstances should you use an array-based queue, and what is its name?

  87. If you were asked to write a binary search tree with parent pointers on a white board for a technical interview, could you do it? What should you do in the meantime?

  88. What is asymptotic complexity? What does it mean if you refer to space or time complexity of an algorithm?

  89. Explain the difference between Big-O and Big-Omega complexity classes. What are the Big-O and Big-Omega of “get” and “search” in a priority queue.

  90. By the way, how does a priority queue differ from a queue?

  91. Draw an example of a directed acyclic graph (DAG).

  92. How does knowing how to explain a DAG help you to understand C++? See http://msdn.microsoft.com/en-us/library/84eaw35x.aspx.

  93. Explain the difference between breadth-first vs. depth first for graph search.

  94. Can you drag a file into a C++ VS2012 project and make it “stick?” For example, see if you can drag a file in from elsewhere, zip up the project, and submit it.

  95. Without doing any “cleaning,” look at the size of a VS2012 project from one of the problems in this assignment. How can you reduce the project size such that the zip file is manageable?

  96. Give one review question we should ask next time not already covered in this assignment.

...