Showing posts with label Sample questions for programming classes. Show all posts
Showing posts with label Sample questions for programming classes. Show all posts

Thursday, April 22, 2010

Programming Puzzle



1. Declare a double variable named Velocity and a pointer variable named PtrVelocity, and make PtrVelocity refer (point) to variable Velocity.

double Velocity;
double *PtrVelocity;
PtrVelocity = &Velocity;


2. Given this definition: int X_Array[] = {10, 20, 30, 40, 50, 60, 70, 80}; 
What is the meaning or, if applicable, the value of:

a. X_Array 
The address of the first element of the X_Array[] which holds 10 in our case.


b. X_Array + 2 
The address of the third element of the X_Array[] which holds 30 in our case.


c. *X_Array
Since an array is a pointer in itself, *X_Array gives us the value of the first element of the X_Array[] and the value is 10.



d. *X_Array + 2
Since an array is a pointer in itself, *X_Array + 2 adds the value of the first element of the X_Array[] + 2 Hence, the value is 12.



e. *(X_Array + 2)
Since an array is a pointer in itself, *(X_Array + 2) gives us the value of third element of the X_Array[] which is 30 because it is the value in the position that is two bytes ahead of where X_Array starts.





3. A program contains the following declarations and statements.

char u;
char v = 'A';
char* pu;
char* pv = &v;

*pv = v + 1;

u = *pv + 1;

pu = &u;

If the address of u is 5000 and the address of v is 5001, then (next page):


Columbia College - Aurora Campus
CISS 242 Midterm Exam

(a) [2] What value is stored at &v?        _5001 ________


(b) [2] What value is assigned to pv?      _5001_________


(c) [2] What value is represented by *pv?  _65  _________


(d) [2] What value is assigned to u?       _66___________


(e) [2] What value is stored at &u?        _5000_________


(f) [2] What value is assigned to pu?      _5000_________


(g) [2] What value is represented by *pu?  _66___________



4. Write a C-string function named strrev that uses exclusively pointer notation and reverses the elements of a given string. As is the convention of most C-string functions, have your function return the address of the given string.

int strrev (char*);

int strrev (char* str) {
   char* p = str + strlen (str) – 1;
   while (p >= str) {
      cout << *p;
      --p;
   }
   return &str;
}
  


CISS 242 Midterm Exam


5. Write a C-string function named strsum that uses exclusively pointer notation and calculates and returns the sum of the ASCII values of all characters in a given string.


int strsum(char*);

int strsum(char* str)
{
     int num = 0;
     int sum = 0;
     while (str[num] != '\0') {    
         sum += str[num];      
         ++num;
     }
     return sum;
}



6. Write a C-string function named strsumd that uses exclusively pointer notation and calculates and returns the sum of digits embedded within a given string. For example, if the given string were “ab2c38d4e27fg” then the sum would be 26 since 2 + 3 + 8 + 4 + 2 + 7 = 26. (Note: A more difficult function would be to calculate and return the sum of embedded numerical substrings; i.e., for the example given, the sum would be 71 since 2 + 38 + 4 + 27 = 71. Are there any takers for some extra credit?)


int strsumd (char*);

int strsumd (char* str) {
   int sum = 0;
   char* p = str;
   while (*p) {
      if (isdigit (*p)) {
         sum = sum + *p – ‘0’;
      }
   ++p;
   }
   return sum;


7. A challenging “fill-in-the-blanks” take-home problem (next/last page):
MEMORY LOCATIONS - Byte Addresses Shown: Hex Value (Decimal Value)

// Fill in the bits according to the declarations at the bottom.

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 1 0 1 1 1
0 (0)             1 (1)             2 (2)             3 (3)

1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1   1 0 1 0 1 0 1 1
4 (4)             5 (5)             6 (6)             7 (7)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0
8 (8)             9 (9)             A (10)            B (11)

0 1 0 0 0 0 1 0   1 0 0 0 0 0 1 0   1 1 1 1 0 1 0 1   1 1 0 0 0 0 1 1
C (12)            D (13)            E (14)            F (15)

1 1 0 0 0 0 0 0   0 0 1 1 1 0 1 1   0 1 1 0 0 0 1 1   1 1 0 1 0 1 1 1
10 (16)           11 (17)           12 (18)           13 (19)

0 0 0 0 1 0 1 0   0 0 1 1 1 1 0 1   0 1 1 1 0 0 0 0   1 0 1 0 0 0 1 1
14 (20)           15 (21)           16 (22)           17 (23)

0 0 1 1 1 1 1 1   0 1 1 1 0 0 1 0   0 1 0 1 0 0 1 0   0 1 0 1 0 0 1 0
18 (24)           19 (25)           1A (26)           1B (27)

0 1 1 0 1 1 1 1   0 1 1 0 1 1 1 0   0 0 0 0 0 0 0 0   0 1 0 0 1 0 0 0
1C (28)           1D (29)           1E (30)           1F (31)

0 1 1 0 0 0 0 1   0 1 1 1 0 0 1 0   0 1 1 1 0 1 0 0   0 0 0 0 0 0 0 0
20 (32)           21 (33)           22 (34)           23 (35)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 1 0 0
24 (36)           25 (37)           26 (38)           27 (39)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 1 1 0 0
28 (40)           29 (41)           2A (42)           2B (43)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 1 1 1 1 1
2C (44)           2D (45)           2E (46)           2F (47)

0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0   0 0 1 0 1 1 0 0
30 (48)           31 (49)           32 (50)           33 (51)

_ _ _ _ _ _ _ _   _ _ _ _ _ _ _ _   _ _ _ _ _ _ _ _   _ _ _ _ _ _ _ _
34 (52)           35 (53)           36 (54)           37 (55)

      int Index = 23, Value = -85; // sizeof(int) is 4; 2s-complement for
      long FlightSeconds = 0L;     //                   negative integers.   
      float Velocity = 65.48; // sizeof(float) is 4; this already done.
      double Temp = -27.39; // sizeof(double) is 8; this already done too.
      char Ch = '?', Letter1 = 'r', Letter2 = 'R';
      char FirstName[] = "Ron";   // How many bytes?
      char LastName[]  = "Hart";  // How many bytes?
      int* Valueptr = &Value; // Use 4 bytes for pointer variables.
      float* VelocityPoint = &Velocity;
      char* ByteAddr = LastName;
      char** PointToPoint = &ByteAddr;