CS 241 Exam 1 Review

Joseph Haugh

University of New Mexico

Question 1: Syntax

Which of these statements would result in an error?

  1. int a = 40 * 2*(1 + 3);

  2. int b = (10 * 10 * 10) + 2

  3. int c = (2 + 3) * (2 + 3);

  4. int d = 1/2 + 1/3 + 1/4 + 1/5 + 1/6;

  5. int e = 1/2 - 1/4 + 1/8 - 1/16;

Question 1: Syntax

Which of these statements would result in an error?

  1. int b = (10 * 10 * 10) + 2

Question 2: = symbol

In the C programming language, the = symbol is most accurately read:

  1. Equals
  2. Assign the value of the expression on the right side to the variable on the left
  3. Is equivalent to
  4. A mathematical symbol used to indicate equality
  5. A conditional symbol used to indicate equality

Question 2: = symbol

In the C programming language, the = symbol is most accurately read:

  1. Assign the value of the expression on the right side to the variable on the left

Question 3: Alphabet char char c = getchar();

Which is true if and only if c is a letter in the standard English alphabet?

  1. ((c>=‘a’ && c<=‘z’) && (c>=‘A’ && c<=‘Z’))
  2. ((c>=‘a’ && c<=‘z’) || (c>=‘A’ && c<=‘Z’))
  3. ((c>=‘a’ || c<=‘z’) || (c>=‘A’ || c<=‘Z’))
  4. ((c>=‘a’ || c<=‘z’) && (c>=‘A’ || c<=‘Z’))
  5. (c>=‘a’ && c<=‘z’ && c>=‘A’ && c<=‘Z’)

Question 3: Alphabet char char c = getchar();

Which is true if and only if c is a letter in the standard English alphabet?

  1. ((c>=‘a’ && c<=‘z’) || (c>=‘A’ && c<=‘Z’))

Question 4: Call by value

In the C Programming Language, call by value means:

  1. When two functions have the same name.
  2. The called function is given the address of its arguments so that the function can both read and set the argument’s values.
  3. Each called function is assigned a value that is used by the operating system to determine the function’s priority.
  4. The called function is given the values of its arguments which are copied into temporary variables.

Question 4: Call by value

In the C Programming Language, call by value means:

  1. The called function is given the values of its arguments which are copied into temporary variables.

Question 5: Automatic variable

In the C Programming Language, an automatic variable:

  1. Comes into existence at the time the function is called, and disappears when the function is exited.
  2. Is automatically initialized.
  3. Is a global variable that is automatically available to all functions within the source file.
  4. Is a global variable that is available to all functions within any source file that declare the variable as extern.
  5. Is automatically defined by the compiler such as PI, E, and HBAR.

Question 5: Automatic variable

In the C Programming Language, an automatic variable is:

  1. Comes into existence at the time the function is called, and disappears when the function is exited.

Question 6: if, else if, else

int main(void)
{
  int x = 4;

  if (x == 1) {
    printf("x is 1\n");
  }
  else if (x == 2) {
    printf("x is 2\n");
  }
  else x = 3; {
    printf("x is %d\n", x);
  }
}

What is the output of this code?

  1. x is 1
  2. x is 2
  3. x is 3
  4. x is 4
  5. Nothing is printed.

Question 6: if, else if, else

int main(void) {
  int x = 4;

  if (x == 1) {
    printf("x is 1\n");
  }
  else if (x == 2) {
    printf("x is 2\n");
  }
  else x = 3; {
    printf("x is %d\n", x);
  }
}

What is the output of this code?

  1. x is 3

Question 7: Flag

int main(void) {
  int i, n;
  int flag;
  for (n = 10; n > 1; n--) {
    flag = 0;
    for (i = 2; i < n; i++) {
      if(n % i == 0) flag = 1;
    }
    if (flag == 0)
      printf("%d ", n);
  }
  printf("\n");
}

What is the output of this code?

  1. 10 9 8 7 6 5 4 3 2
  2. 9 8 7 6 5 4 3
  3. 9 7 5 3
  4. 7 5 3 2

Question 7: Flag

int main(void) {
  int i, n;
  int flag;
  for (n = 10; n > 1; n--) {
    flag = 0;
    for (i = 2; i < n; i++) {
      if(n % i == 0) flag = 1;
    }
    if (flag == 0) 
      printf("%d ", n);
  }
  printf("\n");
}

What is the output of this code?

  1. 7 5 3 2

Aside: Flag

  • In computer programming, flag often refers to a variable or bit used to indicate a particular property is “on” or “off”.
  • Generally a good idea to use a more meaningful name than “flag”, though.

Aside: More efficient printer of primes

int main(void) {
  int i, n;
  int flag;
  for (n = 10; n > 1; n--) {
    flag = 0;
    for (i = 2; i < n; i++) {
      if(n % i == 0) {
        flag = 1;
        break;
      }
    }
    if (flag == 0) 
      printf("%d ", n);
  }
  printf("\n");
}

Once a factor of n is found, n cannot be prime, so break out of the inner loop.

Question 8: Functions

This code will not compile because:

int foo(float x);

void main(void) { 
  int n=5;
  printf("%d\n", foo(n));
}

int foo(int n) {
  return 2*n;
}
  1. The prototype of foo accepts a float, but returns an int.
  2. The prototype of foo has no body.
  3. The variable n is declared in two different places.
  4. The prototype of foo does not agree with the definition.

Question 8: Functions

This code will not compile because:

int foo(float x);

void main(void) { 
  int n=5;
  printf("%d\n", foo(n));
}

int foo(int n) {
  return 2*n;
}
  1. The prototype of foo does not agree with the definition.