CMSC104, Summer 2002

Exam 2 Sample Questions


True/False Questions

Circle TRUE or FALSE for each of the following questions.

  1. TRUE/FALSE
    int is a keyword in C.

  2. TRUE/FALSE
    main is a legal identifier in C.

  3. TRUE/FALSE
    If n is an integer variable, the value of the expression n % 5 is 0 whenever n is a multiple of 5.

  4. TRUE/FALSE
    If n is an integer variable, the value of the expression n % 7 is 0 whenever n is a not a multiple of 7.

  5. TRUE/FALSE
    If n is an integer variable, the value of the expression n % 6 is always less than 6.

  6. TRUE/FALSE
    In the C programming language, variable names can have at most 8 characters.

  7. TRUE/FALSE
    The preprocessor directive #include <foo.h> instructs the preprocessor to place the contents of the file foo.h into the code at that point.

  8. TRUE/FALSE
    The #include preprocessor directive is used to insert one file into another.

  9. TRUE/FALSE
    What is the value of the C expression 2 * 6 - 5 + 8 % 5

  10. TRUE/FALSE
    What is the value of the C expression 3 * 5 - 19 + 7 % 4

  11. TRUE/FALSE
    What is the value of the C expression 5 * 5 - 19 - 6 % 7

  12. TRUE/FALSE
    What is the value of the C expression 3 * 5 - 12 ?

  13. TRUE/FALSE
    If the integer variables a and b are holding the values 5 and 2, respectively, the expression
    a / b produces the result 2

  14. TRUE/FALSE
    If the integer variables a and b are holding the values 5 and 2, respectively, the expression
    a / b produces the result 2.5

  15. TRUE/FALSE
    If the integer variables a and b are holding the values 5 and 2, respectively, and c is a float, after executing the following statement, the value of c is 2.5
    c = a / b ;

  16. TRUE/FALSE
    Any code that can be written as a for loop can also be written as a while loop.

  17. TRUE/FALSE
    All variables must be declared and given a type before they are used

  18. TRUE/FALSE
    Variable declarations can appear anywhere in the body of a function.

  19. TRUE/FALSE
    Each call to printf causes printing to begin at the next line.

  20. TRUE/FALSE
    You must use the newline ('\n') character to start printing at the beginning of the next line.

  21. TRUE/FALSE
    A C program that prints three lines of output must contain three printf statements.


Multiple Choice Questions

  1. a.out is:

  2. Which of the following is NOT part of the process to create an executable program?

  3. The preprocessor strips out all of the following EXCEPT

  4. The C language comment delimiters are

  5. What is the value of the expression 12 - 10 - 8 * 6 % 4 + 2 % 1

  6. Execution of a break statement in the body of a while loop

  7. Execution of a continue statement in the body of a for loop

  8. Determine the output of the code fragment. int a = 5, b = 4, c = 1 ; if ( a < b + 1 ) { printf("Tea, Earl Grey, Hot!\n") ; } else if ( a > b + c ) { printf("Ahead warp factor 9. Engage!\n") ; } else if ( a % b >= c ) { printf("Warp core breach in 20 seconds!\n") ; } else { printf("I sense a lot of anxiety in this room.\n") ; }

  9. What is the effect of the following code? int i, total ; for (i = 1 ; i <= 15 ; i = i + 1) { if ( i % 3 == 0) { printf("%d ", i) ; } } printf("\n\n");


Short Answers

In the following questions, no syntax errors have put in deliberately. (So, "there is a syntax error" is NOT the right answer and will receive no credit.) Please mark your final answer clearly. In order to receive partial credit, you must show your work.

  1. Write a C code segment using a for loop that prints the numbers 1 to 10, inclusive, side-by-side on the same line with 3 spaces between each number

  2. Write a C code segment using a while loop that prints the even numbers between 1 and 10, inclusive, side-by-side on the same line with 3 spaces between each number

  3. Write a C code segment using a for loop that prints a 7 x 7 square out of asterisks.

  4. Write a C code segment using a for loop that prints the following sequence of numbers: 3, 8, 13, 18, 23

  5. Write a C code segment using a for loop that calculates and and prints the sum of the even integers from 2 to 30, inclusive.

  6. Write a C code segment using a for loop that calculates and prints the product of the odd integers from 1 to 15, inclusive.

  7. All programs can be written in terms of three control structures: _____________________, ______________________ and ___________________.

  8. Rewrite the following code segment that uses a for loop as an equivalent code segment that uses a while loop. for (i = LOWER; i <= UPPER; i = i + 1) { if (i % 5 == 0) { printf("%d is a multiple of 5\n", i); } else { printf("%d is not\n", i); } }

  9. Given the following declarations and initializations: int m = 5, n = 8; float x = 9.0; What is the value and type of the expression m + n * x ?

  10. This question tests your knowledge of nested for loops. Please put carriage returns in the proper places. What is the output of the following code? int i, j; for (i = 0; i <= 3; i = i + 1) { for (j = 0; j <= 4; j = j + 1) { if ( (i + 1 == j) ) { printf ("+"); } else { printf ("o"); } } printf ("\n"); }
  11. In the nested for loop code above, how many times is the if statement executed?

  12. Write a simple loop which prints out the following sequence of numbers: 18 23 28 33 38 43 48

  13. Write a program that reads 2 floating point values from the keyboard and displays their product.

  14. Write a program that reads 10 integer values from the keyboard, then displays their sum.

  15. In the list below, circle the items that are NOT legal identfiers in C. 3d XyYzZx ABC123 __yes star*it m me_to-2 main money$

  16. Determine the output of the following program segment: int a = 17, b; b = a; printf ("b = %d\n", b); b = b + 1; printf ("b = %d\n", b); b = a / b; printf ("b = %d\n\n\n", b);
  17. Rewrite the following program, correcting all errors: include (stdio.h) main{} / Program execution begins here / ( people, legs integer; print ("How many people are there?/n); scanf ("%d", people); legs = people * 2 print ("There are %f legs."); [end main]
  18. Given:
    int a = 5, b = 7, c = 15;

    Evaluate each expression below as TRUE or FALSE.

    _______ (1) c / b == 2 _______ (2) c % b <= a % b _______ (3) b + c / a != c - a


    Determine the output generated by the following code segments, assuming the surrounding program code (whatever it is) compiles correctly.


  19. printf ("We sell "); printf ("C shells\n"); printf ("by the\nsea shore\n");


  20. int m = 5, n = 8; printf ("%d %d\n", m % n, n % m);


  21. int x = 12, y = 28; printf ("%d %d\n", x / y, y / x);


  22. int x = 1; if (x > 3) { if (x > 4) { printf ("A"); } else { printf ("B"); } } else if (x < 2) { if (x != 0) { printf ("C"); } } printf ("D");


  23. int j, k; for (j = 1; j <= 4; j = j + 1) { for (k = 1; k <= j; k = k + 1) { printf ("*"); } printf ("\n"); }


  24. int g, h; for (g = 1; g <= 3; g = g + 1) { for (h = 0; h < 3; h = h + 1) { if (g == h) { printf ("O"); } else { printf ("X"); } } printf ("\n"); }