CP2003 - Tutorial 1

Values and Types

The aim for this tutorial is to get students to understand why we are studying programming language principles, types, structuring concepts and expressions.


1. Basic, Cobol and Fortran are archaic, but still among the most commonly used programming languages in the world. Why do you think this is?

2. Why do you think that object-oriented languages such as Smalltalk, C++ and Java are becoming the most popular new languages?

3. In the first lecture, it was stated that a programming language should be universal, i.e. it should be able to compute anything than can be computed. Do you think this requirement is too strict?

4. Why are we concentrating on semantics in this course, rather than language syntax?

5. Why use structuring concepts such as Cartesian product and powersets to define a languages composite types?

6. Explain the relationship between types and sets.

7. Consider the two sets Suit = {club, diamond, heart, spade} and Rank = {2,3,4,5,6,7,8,9, 10,11,12,13}. Show the result of applying the first three structuring concepts discussed in lectures to this set.

8. Analyse the types and expressions in C++ and answer the following:

  (a) What is the set of values of each of the primitive types and
what is its cardinality?
  (b) Can recursive types be defined?
  If so, how?

9. Consider the relationship between S - Truth-value and p(S). In what ways are they similar? Hint: think of S as the set of the three primary colours and think of their use in pixels.

10. Examine the following program. Assume static type checking. At which lines in the program will a type mismatch error be generated assuming type checking is by name? by structure?

line#
       typedef int numericType;
       typedef struct {
         int dummy;
         } intStructType;

       int intVariable;
       numericType numericVariable;
       intStructType intStructVariable;

       numericType foo(numericType variable) {
  1      variable = intVariable;
  2      if (variable < numericVariable) 
  3        variable = intStructVariable;
         else
  4        variable = numericVariable;
         return variable;
       }

       void main() {
  5      int tempVariable;
         
  6      intVariable = 3;
  7      tempVariable = foo(3);
       }

11. Examine the following program. Assume dynamic type checking. At which lines in the program will a type mismatch error be generated (assume execution begins in main and if a type error occurs the resulting test fails, that is evaluates to false, but execution continues)?

line#
       foo() {
  1      if (variable < 10) 
  2        variable = 4;
         else
  3        variable = "harry";
         return variable;
       }

       main() {
  4      variable = "joe";
  5      if (foo() < 23)) print "success!";
       }

12. What is the conditional expression in C? What is the conditional command in C? Are two conditionals really needed?


Joarder Kamruzzaman, James Cook University..