CMSC104, Spring 2005
Exam 2 Sample Questions
Exam2- Nov 18th Room ACIV 008
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
num$ is a legal identifier in C.
- TRUE/FALSE
AREA 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 <foo.h>
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.
More True/False Questions
-
int a = 1, b =2, c =
3, d =4, e =0
Expression
|
Numeric Value
|
True/False |
a + b |
|
T or F |
d - 2 * b |
|
T or F |
d - b – 2 |
|
T or F |
d - 2 * a
|
|
T or F |
c * d - 6 * b |
|
T or F |
a*b =< b*c - d |
T or
F |
c + b >= d + a |
T or
F |
c + b > a |
T or
F |
c + b != d - c |
T or
F |
c+2*d =< b*4 |
T or
F |
b && e |
T or
F |
a && b |
T or
F |
a || e |
T or
F |
a || b |
T or
F |
!c |
T or
F |
!!c |
T or
F |
a && !e |
T or
F |
a < b && b < c |
T or
F |
a > b && b < c |
T or
F |
a >= b || b < c |
T or
F |
Replace all
+,
-,
/ , *
and
=
with equivalent statements using only
++,
- -,
and +=, *=,
/= %
a = a + 2 ;
a = a - 3 ;
a = a * 2 ;
a = a / 4 ;
a = a % 2 ;
b = b + ( c + 2 ) ;
d = d * ( e - 5 ) ;
Multiple Choice Questions
a.out is:
- Which of the following is NOT part of the process to create an
executable program?
- The preprocessor strips out all of the following EXCEPT
- The C language comment delimiters are
- What is the value of the expression 12 - 10 - 8 * 6 % 4 + 2 % 1
- 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++)
{
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++)
{
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
new lines in the proper places. What is the output of the following
code?
int i, j;
for (i = 0; i <= 3; i++)
{
for (j = 0; j <= 4; j++)
{
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 identifiers
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++)
{
for (k = 1; k <= j; k++)
{
printf ("*");
}
printf ("\n");
}
-
int g, h;
for (g = 1; g <= 3; g++)
{
for (h = 0; h < 3; h++)
{
if (g == h)
{
printf ("O");
}
else
{
printf ("X");
}
}
printf ("\n");
}
-
int amount, count;
count = 3;
amount = 2*count++;
printf ("count=%d, amount=%d\n", count, amount);
-
int amount, count;
count = 3;
amount = 2*++count;
printf ("count=%d, amount=%d\n", count, amount);
-
int amount, count;
count = 3;
amount = 2*count--;
printf ("count=%d, amount=%d\n", count, amount);
-
int amount, count;
count = 3;
amount = 2*--count;
printf ("count=%d, amount=%d\n", count, amount);
|