Examination 1

Closed book. Answer all the questions. The total points for each question or part of a question follows it in parentheses.

1

Which of the following constants have incorrect syntax, and why? (12)

0.2000    'AB'    13.3e2   12,345    "n\nn"


Which of the following names have incorrect syntax, and why? (12)

7UP       UP7    a_longish_name   mid-level-performance a000000000

2

Evaluate the following expressions assuming that the initial values given are assigned before each one is evaluated: (24)

int i = 2; int j = 3; char c = 'Z'; float f = 32.2;
/*Z has ASCII value 90*/


Expr1: 1 <= j < 3


Expr2: c - 20 / 10 % j


Expr3: i - --j


Expr4: i + j + f / 2

Expr5: j = 5 + i

Expr6: j += 5 + c

Expr7: j += c -= 2 * i

Expr8: i - 2 || c || j / 10

3

What does the following program fragment print? A hand simulation will help. (18)

int i, j = 5;

for (i = 5; i <= 10; ++i) {
   printf("%d\n", i == j);
   if (i == j)
      j += i;
   else
      break;
}

4

What does the following program fragment print? A hand simulation will help. (16)

int i = 40;

while (i >= 1) {
   i = i - 12;
   printf("%d\n", i + 2);
}

5

In the following program, draw a line from the uses of the names x, y and z as they occur in any expression, to their corresponding declaration, following the rules of scope. (18)

int x, y, z;

void f1(int x)
{
  {
    int y;

    printf("%d %d\n", x, y);
  }
}
void f2(void)
{
  int y;

  printf("%d %d %d\n", x, y, z);
}

main()
{
  printf("%d\n", z);
  {
    int z;

    printf("%d\n", z);
  }
}