Questions:
1) Let's look at the following code:
int (**val1)[2];
int *(*val2)[2];
int (**const val3)() = 0;
int *(*val4)();
Explain what does each value mean.
2) What's wrong with the following code?
int scrollXOffSet;
if( isScroll ) {
scrollXOffSet = 2;
} else {
scrollXOffSet = -2;
}
Assuming isScroll returning a true value when there is a request to offset for an x amount in scrolling.
So, what's wrong with the code snippet above? If you can't find anything wrong, then why? If you can find something wrong, what is it?
3) For the following code:
int *val1 = new int;
int *val2 = new int[6];
delete [] val1;
delete val2;
val1 = new int[2];
delete val1;
Can you spot the delete error in the above code? If so, how would you correct them?
4) Let's say we have the following code:
Code:
/**************************************************
*
* Name: CMain.cpp
*
* Description:
* Linker: no dependencies needed
*
* Change history:
*
* Author:
*
**************************************************/
//-----------------------------------------------------------------------------
// Specific Includes
//-----------------------------------------------------------------------------
#include <iostream>
//-----------------------------------------------------------------------------
// Function Declaration
//-----------------------------------------------------------------------------
int checkDigit(const int&, const int&);
//-----------------------------------------------------------------------------
// Namespaces define
//-----------------------------------------------------------------------------
using namespace std;
//-----------------------------------------------------------------------------
// Name : main()
// Desc : main function
//-----------------------------------------------------------------------------
int main()
{
//stuff here
return(0);
}
//-----------------------------------------------------------------------------
// Name : checkDigit
// Desc : computing checkDigit
//-----------------------------------------------------------------------------
int checkDigit(const int& numb, const int& index) {
//implementation stuff here
return result;
}
Code:
//----------------------------------------------------------------------------- // Function Declaration //----------------------------------------------------------------------------- int checkDigit(const int&, const int&);
Code:
//-----------------------------------------------------------------------------
// Name : checkDigit
// Desc : computing checkDigit
//-----------------------------------------------------------------------------
int checkDigit(const int& numb, const int& index) {
//implementation stuff here
return result;
}
int checkDigit(const int&, const int&);
and there is in the implementation part
int checkDigit(const int& numb, const int& index)
Is this the way we do it? Is it safe or any potential error might occur later? or should we just define in the first place? Like:
Code:
//----------------------------------------------------------------------------- // Function Declaration //----------------------------------------------------------------------------- int checkDigit(const int& numb, const int& index);
Code:
//-----------------------------------------------------------------------------
// Name : checkDigit
// Desc : computing checkDigit
//-----------------------------------------------------------------------------
int checkDigit(const int& numb, const int& index) {
//implementation stuff here
return result;
}
5) So, we have the following:
int * p1;
const int * p2;
int * const p3;
const int * const p4;
mutable int *p6;
Explain what each does.
Which one can you modify the pointer?
which one can you modify the data?
which one you can modify for both?
which you can't do anything?
6) So, if we have,
i++;
it translate to
i = i +1;
1) If we have *++p and *(++)p, what does it translate to? (translate to means how does it look in address processing, and how does it look when dereferencing it?)
2) What are about *p++ and *p(++)? what does it translate to? (translate to means how does it look in address processing, and how does it look when dereferencing it?)
Which one of the above applies for the following?
A) address processing applies before dereferencing, is it *++p or *(++)p or *p++ or *p(++)?
B) dereferencing applies before address processing, is it *++p or *(++)p or *p++ or *p(++)?
7) What's wrong with this line?
const int FOURTY = 40;
Discuss the do's and don't.
8) Let's say we have the following structures:
struct _myStruct1
{
char a;
char b;
char c;
} Struct1;
and you get the total memory size of this Struct1 by calling the function sizeof()
For example:
sizeof(Struct1);
The answer is 3 bytes;
Now, you have another structure
struct _myStruct2
{
int x;
int a;
} Struct2;
and you get the total memory size of this Struct2 by calling the function sizeof()
For example:
sizeof(Struct2);
The answer is 8 bytes;
Why? Because each data gives us:
char - 1 byte
bool - 1 byte
int - 4 bytes
float - 4 bytes
pointer - 4 p p
double - 8 bytes
Now, the question is how many bytes do I get for the following? and why?
struct _myStruct3
{
int x;
char a;
} Struct3;
how many bytes does Struct3 have ? why? how can you optimize this code by reducing more bytes?
9) Let's say somewhere in the class implementation file, you see the portion of the code
int CAlgoirthm::AABBCal()
{
.......
.......
calcBox = 1;
distanceBox = 2;
......
......
{
By looking just at those two variables, how can you tell which variable belongs to class data members and which belongs to local?


