View Single Post
Old 05-30-2009, 09:22 AM   #4
UndueUndo
Junior Member

Activity Longevity
0/20 14/20
Today Posts
0/11 ssssssss2
Default

Alright, I guess I should be honest and not look any of this up...

* What is the difference between between a const global variable and a #define? Where would you use one over the other?

A const global variable is an actual variable in memory at runtime, while a #define sees the compiler run through and switch out each instance of the define with a literal. Other than stylistic choice... well globals can be accessed via namespace instead of having to include a header. Not really sure when you should use either, I always use defines because inline numbers are faster.

* Difference between:
const int * const foo
const int * foo
int * const foo

const int * const foo is a non-changeable reference to a non-changeable integer, const int * foo is a changeable reference to a non-changeable integer, and int * const foo is a non-changeable reference to a changeable integer.

* What is the difference between a recursive function and loops? Give an example of each.

A recursive function calls itself until it reaches a condition, a loop is a block of code that is run until a condition is met.

int FibRec(int n)
{
if (n <= 1)
return n;

return (FibRec(n-1) + FibRec(n-2));
}

int FibLoop(int n)
{
if (n <= 1)
return n;

int j = 0, k = 1, temp, retval = 0;
for (int i = 2; i <= n; i++)
{
temp = k;
k = j + k;
j = temp;
retval = k;
}
return retval;
}

I had to pull out scratch paper and make sure I had this right, if it matters.

* Write a bubble sort function
void BubbleSort (int * IntArray, const unsigned int ArraySize)
{
...
}

void BubbleSort (int * IntArray, const unsigned int ArraySize)
{
for (int i = 0; i < ArraySize; i++)
{
for (int j = ArraySize-1; j > i; j--)
{
if (IntArray[j] < IntArray[j-1])
swap(IntArray[j], IntArray[j-1]);
}
}
}

void Swap (int * i, int * j)
{
int temp;
temp = *i;
*i = *j;
*j = temp;
}

* What errors would occur in the following code and why?

class Blah
{
private:
int Foo;

public:
Blah() { Foo = 10; }
~Blah() {}
MyFunction ( int &Mobor)
{
Mobor += 10;
}
};

int main( int argc, char **argv)
{
Blah * ptr = 0;
ptr->MyFunction(10);
return 0;
}

When you call MyFunction, you're not going to change the passed integer as it's passed by value. MyFunction gets it's own copy of the integer in memory, that gets incremented, and then is lost when the function returns.

The first line of main creates a pointer to a Blah object addressed at 0. It doesn't create a Blah object there, and you'll get a runtime error when trying to access it.

* What does a static variable do in a function?

A static variable maintains its state between function calls. So if you wanted to know how many times a function has been called, for instance, you'd make a static integer initialized to 0 and increment it once in the function.

* What is a ring buffer and how does a it differ from a standard queue?

A ring buffer has no beginning and end, when it grows past the end of the buffer, new values are written at the beginning. A standard queue doesn't loop back on itself like that, it's a list. Ring buffers should be used with arrays because a standard queue implemented with an array means after each delete operation the remaining items need to be shifted one position. Standard queues are fine with linked lists.

* What does "in place" mean (regarding algorithms)? Give an example.

An algorithm does all the work on top of the variables instead of copying them around. I can't think of a non-trivial example right now...

Square (x)
x : x * x.

* What are the differences between an Octree, Quadtree and a BSP? What can they be used for?

An Octree is a tree structure where each node has either 8 branches or is a leaf itself. A Quadtree is the same except it uses 4 or no branches per node. An octree is used to represent a three-dimensional space, while a Quadtree represents a 2d space. With a binary image, a quadtree can be used so that a node can be either filled with no children (region is black), empty with no children (that region is white), or can have 4 children (reconstruct region from children). You need a key to go along with such a tree so you know which nodes go to which quadrants, usually they're numbered counter-/clockwise. With a 3d space, the octree can represent which areas are empty and which contain obstacles. Not sure what BSP is.

* Optimise this function:
const int Sum (const int * LotsOfNumbers, const unsigned int Size )
{
int Sum = 0;

for(int i = 0; i < Size; ++i)
{
Sum += LotsOfNumbers[i];
}

return Sum;
}

I'm not sure if bitwise addition would really make a difference here but it's all I can think of.

const int Sum (const int * LotsOfNumbers, const unsigned int Size )
{
int Sum = 0;

for(int i = 0; i < Size; ++i)
{
Sum ^= LotsOfNumbers[i];
}

return Sum;
}

* The difference between ++i and i++;

i++ returns the value of i, then increments it in memory. ++i increments the value of i in memory then returns that value.

* Describe a use of a function pointer and suggest other methods to produce the same effect.

Function pointers are one of the things I haven't used much. Yeah I would need to look that up. I know you'd use them when you want to call functions based on conditions to be determined at runtime. A switch statement with standard function calls can do the same thing.

* What is a template class? What an example that stores an object with a function that squares itself returning the object as a const. Give an example why this would not work with all objects.

Templates I'm weak on too. I know it has to do with generic programming, that it would preform operations based on the data types fed to it. The syntax I'm not even sure of.

* What is the difference between an Interface class and an Abstract class?

An abstract class cannot be instantiated itself, it must be used as a base class. The class that inherits it must define each of the abstract functions.

An interface class defines method signatures that any class which supports it must implement.

The difference is that abstract classes can be used to create polymorphism among classes belonging to the same hierarchy, while interface classes can create polymorphism among otherwise unrelated classes and it can create a further polymorphic set among a subset of classes in the same hierarchy.

* What is wrong with the following piece of code and how would you fix it? What would be the result of the error and why?

class A
{
private:
int *AInt;
public:
A() { AInt = new int(0); }
~A() { delete AInt; }
};

class B : public A
{
private:
int *BInt;
public:
B() { BInt = new int(0); }
~B() { delete BInt; }
};

Okay, I know one of the destructors won't get called in some scenario... I'd have to Google inherited destructors. Off the top of my head I'd call A's destructor explicitly in B's destructor. Probably not the right syntax.

class B : public A
{
private:
int *BInt;
public:
B() { BInt = new int(0); }
~B()
{
~A();
delete BInt;
}
};

Quote:
I take offense to this.
Uhh sorry Gshonk... but I've read tons of articles about how schools like that don't really carry any weight with employers. That's all I meant, I'm sure you do learn practical skills there.
UndueUndo is offline   Reply With Quote