CMSC104, Spring 2007
Exam 2 Sample Questions
True/False Questions
Circle TRUE or FALSE for each of the following questions.- TRUE/FALSE
int is a keyword in C. - TRUE/FALSE
main is a legal identifier in C. - TRUE/FALSE
If n is an integer variable, the value of the expression n % 5 is 0 whenever n is a multiple of 5. - 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. - TRUE/FALSE
If n is an integer variable, the value of the expression n % 6 is always less than 6. - TRUE/FALSE
In the C programming language, variable names can have at most 8 characters. - TRUE/FALSE
The preprocessor directive#include instructs the preprocessor to place the contents of the file foo.h into the code at that point. - TRUE/FALSE
The #include preprocessor directive is used to insert one file into another. - TRUE/FALSE
What is the value of the C expression 2 * 6 - 5 + 8 % 5 - TRUE/FALSE
What is the value of the C expression 3 * 5 - 19 + 7 % 4 - TRUE/FALSE
What is the value of the C expression 5 * 5 - 19 - 6 % 7 - TRUE/FALSE
What is the value of the C expression 3 * 5 - 12 ? - 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 - 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 - 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 ; - TRUE/FALSE
Any code that can be written as a for loop can also be written as a while loop. - TRUE/FALSE
All variables must be declared and given a type before they are used - TRUE/FALSE
Variable declarations can appear anywhere in the body of a function. - TRUE/FALSE
Each call to printf causes printing to begin at the next line. - TRUE/FALSE
You must use the newline ('\n') character to start printing at the beginning of the next line. - TRUE/FALSE
A C program that prints three lines of output must contain three printf statements.
Multiple Choice Questions
a.out
is:-
[a.] a source code file
[b.] a text file
[c.] an executable file
[d.] a C code file
- The C language comment delimiters are
-
[a.] { and }
[b.] /* and */
[c.] //
[d.] ( and )
- What is the value of the expression 12 - 10 - 8 * 6 % 4 + 2 % 1
-
[a.] -2
[b.] 0
[c.] -7
[d.] 2
- Execution of a break statement in the body of a while loop
-
[a.] causes the program to terminate.
[b.] causes the current iteration of the loop to terminate and the next iteration to begin.
[c.] causes the loop to terminate and the statement after the body of the while loop to be executed next.
[d.] None of the above.
- Execution of a continue statement in the body of a for loop
-
[a.] causes the program to terminate.
[b.] causes the current iteration of the loop to terminate and the next iteration to begin.
[c.] causes the loop to terminate and the statement after the body of the while loop to be executed next.
[d.] None of the above.
- 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") ; } -
[a.] Tea, Earl Grey, Hot!
[b.] Ahead warp factor 9. Engage!
[c.] Warp core breach in 20 seconds!
[d.] I sense a lot of anxiety in this room.
- 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"); -
[a.] It prints out the integers from 3 to 15.
[b.] It prints out the multiples of 3 from 3 to 15.
[c.] It prints out the sum of the integers from 3 to 15.
[d.] It prints out the sum of the multiples of 3 from 3 to 15.
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.
- 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
- 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
- Write a C code segment using a for loop that prints a
7 x 7 square out of asterisks.
- Write a C code segment using a for loop that prints the
following sequence of numbers: 3, 8, 13, 18, 23
- 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.
- Write a C code segment using a for loop that calculates
and prints the product of the odd integers from 1 to 15, inclusive.
- All programs can be written in terms of three control structures:
_____________________, ______________________ and ___________________.
- 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); } } - 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 ? - This question tests your knowledge of nested for loops. Please put
newlines 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"); } - In the nested for loop code above,
how many times is the if statement executed?
- Write a simple loop which prints out the following sequence of numbers:
18 23 28 33 38 43 48
- Write a program that reads 2 floating point values from the keyboard
and displays their product.
- Write a program that reads 10 integer values from the
keyboard, then displays their sum.
- 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$ - 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); - 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] - 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.
printf ("We sell "); printf ("C shells\n"); printf ("by the\nsea shore\n"); -
int m = 5, n = 8; printf ("%d %d\n", m % n, n % m); -
int x = 12, y = 28; printf ("%d %d\n", x / y, y / x); -
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"); -
int j, k; for (j = 1; j <= 4; j = j + 1) { for (k = 1; k <= j; k = k + 1) { printf ("*"); } printf ("\n"); } -
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"); }