Questions and exercises for Review

 

Note. Questions preceded by green numbers apply to the midterm

 

1. What does the copyright law protect?


2. What is a software trademark?

 

3. What is a bit, a byte, a word?

 

4. How many bytes are in 12kB, 3GB, 128MB?


5. What is included in an Interactive Development Environment (IDE)?


6. Describe the stages needed to produce an executable program from sources.


7. Describe the stages of the compilation process.


8. Draw a diagram showing how one cand get from source code to executable code.


9. Explain the terms: program, programming


10. What are the basic requirements for a well-posed problem?

 

11. What are the results of the analysis stage?


12. What is program design?


13. Explain the benefits of modularity.


14. Briefly describe the top-down programming technique for program design

 

15. Briefly describe the bottom-up programming technique for program design

 

16. What are the benefits of top-down program-design?

17. Explain the stages of program implementation.

 

18. What  is the purpose and the contents of program documentation?


19. What must be done during program maintenance?

 

20. What is an algorithm and what properties should it have?

21. Explain the general requirements for an algorithm.

 

22. Explain the term flowchart.


23.
Explain the term pseudocode.


24.
Explain the term hierarchy chart.

25. What are the types of control structures used in describing algorithms?

 

26. Describe the selection control structure.

26. Describe the while loop control structure.


27. Describe the repeat-until loop control structure.


28. Describe the for loop control structure.


29. Explain the in, inout, out formal parameters to procedures.


30. What is programming style and what does it characterize?


31. Provide a set of recommendations for good programming style.


32. Describe the basic/typical form of a C program.

 

33. List the primitive integral types and their sizes in bits for PCs.

 

34. What are the primitive types in C?


35. Describe the while loop control structure.


36. Provide examples of decimal, octal, and hexadecimal integer constants in C.


37. Write in C the equivalent of the following string (text starts at My and ends one space after the second dot): My name is "Sun". I'm very shiny.


38. List and explain the common C escape sequences.


39. How can one achieve conditional compilation in C?

 

40. What can one do using the #define directive in C?

 

41. Explain the differences between functions and macrodefinitions.


42. Show how can you use the #ifndef directive to avoid multiple inclusions of header files in C.

 

43. What is a condition?

 

44. Why may an algorithm be finite and yet impractical to use?


45. Describe the three "files" automatically attached to a standard terminal at runtime in a C program.


46. Provide examples of declarations of constants of various types in C.

 

47. When the program is running it produces a number that is too large to fit into the space allocated for it in memory. What type of error is this?

 

48. What would be output by the following segment of C?

int fact, i;

fact = 1;

i = 2;

while (i <= 6)

   fact *= i;

   i++;

printf(“%d\n”, fact);

If the purpose of this segment of C was to output the value of 1*2*3*4*5*6 how should it be changed?

 

49. Consider the following piece of C code

int i;

while (i < 10)

{

  printf("%d\n", i);

  i++;

}

To what should i be initialized so that the loop would be traversed 10 times? In this case what would be printed out? How would you change the body of the loop so that with the same initialization the numbers 1 to 10 would be printed? If the body of the loop was kept as it is above how should the initialization and the condition be changed so that the numbers 1 to 10 are printed out?

 

50. What would be output by the following section of C?

int A[5] = {1 , 2, 3, 4};

int i;

for (i=0; i<5; i++)

 {

  A[i] = 2*A[i];

  printf("%d ", A[i]);

 }

 

51. What is wrong with the following section of program?

int A[10], i;

for (i=1; i<=10; i++)

   scanf("%d", &A[i]);

52. What is the purpose of gets and why it is dangerous to use it?

 

53. What can be an operand to an expression?


54. What is taken into account when evaluating an expression?

 

55. What are the relational operators?

 

56. Write a relational expression which would evaluate to true if the sum of variables x and y was equal to the value of a variable z.

 

57. Bracket the following logical expressions to show the order of evaluation of the operators. Hence if a is 5, b is 10, c is 15 and d is 0 what are the truth values of the expressions?

c == a+b

a != 7

b <= a

a > 5

a+d >= c-b

d/a < c*b

 

58. Bracket the following logical expressions to show the order of evaluation of the operators. Hence if a is 5, b is 10, c is 15 and d is 0 what are the truth values of the expressions?

c == a+b || c == d

a != 7 && c >= 6 || a+c <= 20

!(b <= 12) && a % 2 == 0

!(a >5) || c < a+b

 

59. Write a logical expression which returns true if a float variable x lies between -10.0 and 10.0.

 

60. To what do the following expressions evaluate?

     17/3  17%3  1/2  1/2*(x+y)

 

61. Given the declarations:

float x;

int k, i = 5, j = 2;

To what would the variables x and k be set as a result of the assignments

k = i/j;

x = i/j;

k = i%j;

x = 5.0/j;

 

62. If x has the value 3.5 when the following statement is executed what value would be assigned to y?

if (x + 1 <= 3.6)

   y = 1.0;

else

   y = 2.0;

 

63. In words what do you think the effect of the following statement is intended to be? Why is the statement syntactically incorrect? How could it be changed to be syntactically correct? Has the change that you have made ensured that the statement actually carries out the logical effect you stated at the beginning?

if (x >= y)

   sum += x;

   printf("x is bigger\n");

else

   sum += y;

   printf("y is bigger\n");

 

64. Write an if-else statement which would add a variable x to a variable possum if x is positive and would add x to negsum if the variable x was negative.

 

65. Expand the solution to the previous question so that if x is positive then a variable poscount is incremented and if x is negative a variable negcount is incremented. If this was part of a program what would be sensible initializations to carry out?

 

66. It is decided to base the fine for speeding in a built up area as follows - 50 pounds if speed is between 31 and 40 mph, 75 pounds if the speed is between 41 and 50 mph and 100 pounds if he speed is above 50 mph. A programmer writing a program to automate the assessment of fines produces the following statement:

if (speed > 30)

   fine = 50;

else if (speed > 40)

   fine = 75;

else if (speed > 50)

   fine = 100;

Is this correct? What fine would it assign to a speed of 60 mph? If incorrect how should it be written?

 

67. Write a nested if-else statement that will assign a character grade to a percentage mark as follows - 70 or over: A, 60-69: B, 50-59: C, 40-49: D, 30-39: E, less than 30 :F.

 

68. What is the major difference between a while statement and a do-while statement?

 

69. What would be output by the following segment of C?

int i;

i = -12;

do

  {

    printf("%d\n", i);

    i = i - 1;

  }

while (i > 0)

 

70. What would be output by the following segment of C?

int i;

for ( i = 1; i <= 12; i *= 2 )

    printf("%d\n", i);

 

71. What is printed by the following segment of C?

int i;

for (i = 1; i < 20; i = i + 3)

    printf("%d\n", i);

 

72. What would happen if the i+3 in the update expression were changed to i-3?

 

73. Write a segment of C using a for statement which accepts n real values entered by a user and outputs their average value. Assume n will be greater than zero.


74. What type(s) is(are) allowed for the expression of a switch statement?


75. What is the #define directive used for?


76. Given the following code snippet

#define plus(a, b) a + b


int a = 3;
int b = 5;
int z = -1;

z *= plus(a, b)

 

What is the value of z after the execution of the above sequence?


77. How can one guard against multiple inclusions of header file content?


78. What is the #define directive used for?

 

79. Write a statement using scanf which reads an octal integer, followed by a number of spaces, then by a hexadecimal number, a comma, then a double, and assigns the values to variables octal, hex, and dbl. Include proper declarations for those variables.


80. If a = 1, b = -2, c = 0 and d = 7, determine the value assigned to y.


y =((c=(a<0)? –a: a), (d=(b<0)? -b: b), (c>d)? c: d);

 

81. If x = 1, y = -2, z = 11 and w = 7, determine the value assigned to alpha.

alpha = ((a1=x+y+z), (a2=x+y+w), (a1*a2+10));


82. What is a comma expression typically used for?

 

83. Write a set of statements to read from the standard input three variables a, b and c, where the variables have the following types:

The input values are separated by one to three spaces. The second variable is given as a hexadecimal number.

The admissible values for a and b are as follows:

Suitable error messages should be given if the values are not within the allowed range or in the wrong format. 

Sample input:

-.000001  0xfff Is this a good one?

-1.e-3  0xEE04F Maybe this one...

213e+36 0xEE04E    Or this one. Could this string be too long to put into allocated area?


84. Write a set of statements to read from the standard input three variables a, b and c, where the variables have the following types:

The input values are separated by one comma. The first variable is given as an octal number.

The admissible values for a and b are as follows:

Suitable error messages should be given if the values are not within the allowed range or in the wrong format.

Sample input:

12745,-1.e-3,Is this a good one?

0xEE04F,21e75,Maybe this one...

70073,21e+6,Or this one. Could this string be too long to put into allocated area?


85. Write a set of statements to read from the standard input three variables a, b and c, where the variables have the following types:

The input values are separated by one semicolon. The first variable is given as an hexadecimal number.

The admissible values for a and c are as follows:

Suitable error messages should be given if the values are not within the allowed range or in the wrong format.

Sample input:

43310000; Is this a good one;-1.e-3

0xEE04F, Maybe this one...;21e75

700x73;Or this one. Could this string be too long to put into allocated area?;21e+6

 

86. Write a set of statements to write to the standard output a sequence of variables, a, b, c and d, of the following types:

The output fields have the following specifications:

 All fields are separated by a vertical bar (i.e. '|').

 

87. Write a set of statements to read from the standard input the output produced by exercise 86.

 

88. Write a set of statements to write to the standard output a sequence of variables, a, b, c and d, of the following types:

The output fields have the following specifications:

 All fields are separated by a semicolon (i.e. ';').

 

89. Write a set of statements to read from the standard input the output produced by exercise 88.

 

90. What C statements cause a program to become unstructured?

 

91. For what purpose can one use the function exit?

 

92. How can one achieve forced program termination?

 

93. Write a sequence of C code to calculate the geometric mean of n variables stored in array a, where 1 <=n <= 200.

The elements of the array are real numbers in the range -1055..10+55.

 

94. Write a sequence of C code to draw diamonds of vowels or consonants, according to the model shown in examples below:

Input data

Output

Input data

Output

v,4

   *

  *a*

 *eee*

*iiiii*

 *ooo*

  *u*

   *

V,6

     *

    *A*

   *EEE*

  *IIIII*

 *OOOOOOO*

  *UUUUU*

   *AAA*

*E*

 *

c,4

   *

  *b*

 *ccc*

*ddddd*

 *fff*

  *g*

   *

C,7

      *

     *B*

    *CCC*

   *DDDDD*

  *FFFFFFF*

 *GGGGGGGGG*

*HHHHHHHHHHH*

 *JJJJJJJJJ*

  *KKKKKKK*

   *LLLLL*

    *MMM*

 *N*

  *

That is, the first letter of input specifies the filler for the diamond: v for vowel, c for consonant.

The second element is the number of stars ('*') on each side of the diamond.

 


Questions/Exercises for the final exam

  1. What types of error can occur in computing and at what stages of the processes of producing a program do they occur?
  2. A program runs without any errors being reported but outputs results that are wrong, what type of error is likely to have caused this?
  3. What is the purpose of the open function of a stream?
  4. Write a declaration for an input stream and connect it to a file called "datain.txt". How would you read a real value from this stream into a float variable?
  5. Write a declaration for an output stream and connect it to a file called "results". Assuming that there existed float variables x and y how would you write the values of x and y to the file results together with a message "The values of x and y are "?
  6. Why are functions used?
  7. What role do the parameters of a function play?
  8. How is information supplied as input to a function? How can information be conveyed back to the calling program?
  9. What would the following function do?

void example(int n)

{

    int i;

    for (i=0; i<n; i++)

       printf("%c", '*');

    printf("\n");

}

  1. How would you call this function in a program? How would you use this function in producing the following output on the screen?

*

**

***

****

  1. What would be the output from the following programs?

 

a)

void change(void)

 {

   int x;

   x = 1;

 }

void main()

 {

   int x;

   x = 0;

   change();

   printf("%d\n", x);

 }

 

b)

void change(int x)

  {

    x = 1;

  }

void main()

  {

    int x;

    x = 0;

    change(x);

    printf("%d\n", x);

  }

  1. Write a function declaration for a function that takes two parameters of type float and returns true (1) if the first parameter is greater than the second and otherwise returns false (0).
  2. Write a function declaration for a function that takes two parameters of type int and returns true if these two integers are a valid value for a sum of money in pounds and pence. If not valid then false should be returned.
  3. A function named ex1 has a local variable named i and another function ex2 has a local variable named i. These two functions are used together with a main program which has a variable named i. Assuming that there are no other errors in the program will this program compile correctly? Will it execute correctly without any run-time errors?
  4. How is information supplied as input to a function? How can information be conveyed back to the calling program?
  5. What would be the output from the following program?

void change(int& y)

  {

    y = 1;

  }

void main()

  {

    int x;

    x = 0;

    change(x);

    printf("%d\n", x);

  }

  1. Write a function declaration for a function that takes two parameters of type int and returns true if these two integers are a valid value for a sum of money in pounds and pence. If they are valid then the value of the sum of money should also be returned in pence (without affecting the input value of pence). If not valid then false should be returned.
  2. If an array has a 100 elements what is the allowable range of subscripts?
  3. What is the difference between the expressions a4 and a[4]?
  4. Write a declaration for a 100 element array of floats. Include an initialization of the first four elements to 1.0, 2.0, 3.0 and 4.0.
  5. An array day is declared as follows:

int day[] = {mon, tue, wed, thu, fri};

How many elements has the array day? If the declaration is changed to

int day[7] = {mon, tue, wed, thu, fri};

how many elements does day have?

  1. Write a function heading for a function which will double the first n elements of an array. If the function was amended so that it would return false if n was larger than the size of the array how should the function heading be written? If the function was to be changed so that a new array was produced each of whose elements were double those of the input array how would the heading be written?
  2. What is dynamical memory allocation, and what functions support it in C?
  3. What is a pointer in C, and where can it be stored?
  4. Explain the difference between const int *p; and  int * const p;
  5. What does the code snippet following do?

int (*pfi)(int, int);

  1. Given the code snippet:

float a[10], *p;
p=a;

what is the difference between p and a?

  1. What is the difference between a struct and a union? Give a relevant example.
  2. What is the difference between the following two  statements:

char *(*myfunc1)(char *, int) and
char *myfunc2(char *, int)?

  1. Write a code snippet to allocate space for all elements of a 2D matrix of chars where only the elements under the main diagonal need to be remembered. There will be n rows and m columns in the matrix.
  2. What does the following code snippet do?

typedef struct cell{
    int a[5];
    char *name[41];
    struct cell* next;
}

  1. Write a definition for a new type of a node in a doubly linked list to hold a wage  (a double value) and a person name of at most 40 useful characters.
  2. What does the following code snippet define?

union marks // A
{
    float perc; // B
    char grade; // C
}

  1. What does the following code snippet define?

struct address // A
{
    char state[30];
    char street[30];
    char city[30];
}
struct student // B
{
    char name[30];
    float marks;
    struct address adr; // C
}

  1. Write code to declare two pointers to the structures define above and fill the name field with the string "George Popescu" and print the contents of its address, assuming that it is already filled with valid data.

 

  1. What will this program print?

 

      #include <stdio.h>

 

      void f(int d);

 

      int a = 1, b = 2, c = 3, d = 4;

 

      int main(){

        int a = 5, c = 6;

        f(a);

        f(b);

        f(c);

        printf("%d %d %d %d\n",a,b,c,d);

        return 0;

      }

 

      void f(int d){

        static int a = 0;

        a = a + 7;

        b = a + d;

        c++;

        d--;

        printf("%d %d %d %d\n",a,b,c,d);

      }

 

  1. What, if anything, is wrong with this function?

 

      int q_dequeue(q_queue_t * from_queue){

        q_node_t *node;

        node = q_remove_front(from_queue);

        if (node == NULL) {

          return 0;

        } else {

          return node->data;

        }

      }

 

  1. What, if anything, is wrong with the following code?

 

     char a[6] = "abcde";

     char b[6] = "fgh";

 

     strcpy( a, b );

 

  1. If function prototypes allow the compiler to do type checking and conversions of actual parameters into the data type of the matching formal parameters then why does the following occur?

 

      float x = 3.67;

      printf("%d\n",x);

 

      1074617385

  1. Does the following code compile cleanly?  If so, what does it do?

 

     #define N 4

     int a[N];

     int &pa = a[0];

     int i;

 

     for( i = 0; i < N; i++ ){

       pa++ == 0;

     }

 

  1. Does the following code compile cleanly?  If so, what does it do?

 

     #define N 4

     int a[N];

     int *start = a;

     int *end = a + N;

     int *ptr;

 

     for( ptr = start; ptr != end; *ptr++ = 0 );

 

  1. What does the following prototype declare?

 

     int f( int ( * g )( int ), int h );

 

  1. Are the two following functions equivalent?  If not, why not?

 

     s1( char dest[], char src[] ){

       int i = 0;

       while( dest[i++] = src[i++] ) ;

     }

 

     s2( char * dest, char * src ){

       while( *dest++ = *src++ ) ;

     }

 

  1. What, if anything, is wrong with this program?

 

     #include <stdio.h>

 

     int main(){

       char line[256];

       int i = 0;

 

       while( gets( line ) != EOF ){

         printf( "line %d is \"%s\"\n", i, line );

         i++;

       }

 

       return 0;

     }

 

  1. What, if anything, is wrong with this program?

 

     #include <stdio.h>

 

     int main(){

       FILE *fp1;

       char line[256];

 

       if( (fp1 = fopen( "my_data", "r" )) == NULL ){

         perror( "my_data" );

         exit( -1 );

       }

 

       fseek( fp1, (long) 256, SEEK_SET );

       fgets( line, 256, fp1 );

 

       fprintf( stdout, "line is %s\n", line );

 

       fclose( fp1 );

     }

  1. What's the format of a struct declaration? Give a relevant example.
  2. What's the format of a union declaration? Give a relevant example.
  3. When should one use a struct and when a union for holding data?
  4. To be continued