CMSC201 Midterm Exam Review
Fall 2008
Picture ID required for the MidTerm
These questions are sample questions.
The questions below may be modified by being reworded (i.e. true statements
may be made false), by changing variable names, constants, etc. if included
on the midterm. Functions you are asked to write or analyze may not be
exactly the same as those listed here.
True/False Questions
Circle TRUE or FALSE for each of the following questions.
- TRUE/FALSE
In Unix, the command rm can be used to delete files.
- TRUE/FALSE
In Unix, the mv command is used to move to a different directory.
- TRUE/FALSE
In Unix, the command to make a copy of a file is cp.
- TRUE/FALSE
The following command given at the Unix prompt a.out < input.dat
is called redirection and causes the contents of the input.dat file to be
used as input to the program.
- TRUE/FALSE
Once a text file has been created with emacs or xemacs, it may only
be editted with emacs or xemacs.
- TRUE/FALSE
When statements (e.g., while statements) are nested inside other
statements, the compiler uses the indentation to determine the scope
of each statement.
- TRUE/FALSE
A function that returns no result and is executed only for its effect
is called a procedure.
- TRUE/FALSE
A function that returns no result and is executed only for its effect
is called a predicate function.
- TRUE/FALSE
If n is an integer variable, the value of the expression n % y is 0
whenever n is a not a multiple of y.
- TRUE/FALSE
If n is an integer variable, the value of the expression n % 10 is
always less than 10.
- TRUE/FALSE
The prototype of a function is used mainly as a comment to the reader,
but is not useful to the compiler.
- TRUE/FALSE
A function prototype gives the compiler the value returned by the function.
- TRUE/FALSE
A function prototype tells the compiler the type of the value that
is returned by the function.
- TRUE/FALSE
All functions must return a value.
- TRUE/FALSE
When a function is called its local variables initially contain the
value 0.
- TRUE/FALSE
A function with return type void always returns the value 0.
- TRUE/FALSE
When a function is called the number of arguments passed should match
the number of formal parameters given in the function's prototype.
- TRUE/FALSE
After a function has returned, the values stored in its local variables
are copied into variables in the main function with the same name.
- TRUE/FALSE
The values stored in the local variables of a function are not saved
when the function exits.
- 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
A header file typically contains constant definitions, function prototypes
and function definitions.
- TRUE/FALSE
In UNIX, the file foo.o usually contains the the machine language
translation of the source code file foo.c.
- TRUE/FALSE
In order to compile a program, the source code for every function used
by the program must be placed in a single file.
- 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
If the integer variables a and b are holding the values
0 and 1, respectively, what is the value of the following expression :
(a || b ) ?
- TRUE/FALSE
If the integer variables a and b are holding the values
0 and 1, respectively, what is the value of the following expression :
(a && b ) ?
- TRUE/FALSE
What is the value of the C expression 3 * 5 - 12 ?
- TRUE/FALSE
According to DeMorgan's law, !(x && y) is equivalent
to (!x) && (!y)
- TRUE/FALSE
According to DeMorgan's law, (!x) || (!y) is equivalent to !(!x
&& !y)
- TRUE/FALSE
If the integer variables a and b are holding the values
0 and 6, respectively,
after the statement a += b++ the new values of a and
b are both 7.
- 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 double, after executing the statement
c = a / b ;, the value of c is 2.5
- TRUE/FALSE
A procedure returns a 0 or a 1.
- TRUE/FALSE
A procedure has no return value.
- TRUE/FALSE
A predicate function returns a 0 or a 1.
- TRUE/FALSE
In C, "false" is always equivalent to 0.
- TRUE/FALSE
In C, "true" is always equivalent to 1.
- TRUE/FALSE
A call to the srand() function returns a pseudo-random
integer.
- TRUE/FALSE
A call to the rand() function returns a pseudo-random
integer.
- TRUE/FALSE
A call to the srand() function initializes the pseudo-random
number generator.
- TRUE/FALSE
The value of the expression rand() % 5 is an integer between
1 and 5 inclusive.
- TRUE/FALSE
The value of the expression rand() % 7 + 1 is an integer between
1 and 7 inclusive.
- TRUE/FALSE
In C, all arrays passed to functions are passed by reference.
- TRUE/FALSE
When an array is passed to a function, only the address of the array
is passed to the function.
- TRUE/FALSE
When an array is passed to a function, a copy of the elements is made
and passed to the function to manipulate.
- TRUE/FALSE
The preprocessor directive #define RING 1 tells the C preprocessor
to replace each occurrence of the constant RING with 1.
- TRUE/FALSE
Some compilers assume that any function without a prototype returns
an int.
- 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.
- TRUE/FALSE
The default case is required in the switch selection
structure.
- TRUE/FALSE
The cases in a switch statement must be listed in ascending order.
- TRUE/FALSE
The default case must be the last case in the switch
selection structure.
- TRUE/ FALSE
An array can store many types of values simultaneously.
- TRUE/ FALSE
Array subscripts must always start at 0.
- TRUE/ FALSE
The fourth element of an array named foo is foo[4]
- TRUE/FALSE
All arrays with 10 elements require the same amount of memory.
- TRUE/FALSE
typedef is used to give a new name to a known data type.
- TRUE/FALSE
In C, a parameter is passed by VALUE when a copy of the value of
a variable is passed to the function.
- TRUE/FALSE
Structures in C must have members which are of the same type.
- TRUE/FALSE
Structures in C may have members which are of different types.
- TRUE/FALSE
The members of different structures used within the same program
must have unique names.
- TRUE/FALSE
Local variables within main() may not have the same name as
the member of any structure used within main().
- TRUE/FALSE
A structure cannot have two members of the same type.
- TRUE/FALSE
A member in a structure can itself be a structure.
- TRUE/FALSE
A structure cannot have two members with the same name.
- TRUE/FALSE
If rec is a structure variable and number is a member
of the structure, then to access the number member of rec
use the syntax rec.number
- TRUE/FALSE
The string "Bilbo Baggins", may be stored in an array of 13 characters.
Multiple Choice Questions
- Which of the following items belong in a header file?
[a.] function definitions
[b.] #define statements
[c.] function prototypes
[d.] b and c
[e.] All of the above
- A C library contains
[a.] Built-in commands of the C language
[b.] the source code for many useful functions
[c.] the object code for many useful functions
[d.] All of the above
- The on-line manual on the UNIX system provides the following information
about a function you may want to use, except
[a.] The function prototype of the function.
[b.] The location of the source code of the function
[c.] The name of the header file to include if you want to use the
function
[d.] An English description of what the function does
- Suppose that the header file foo.h contains the function prototype
of the function UseMe. You should include foo.h in the file
mine.c
[a.] if the function UseMe is called somewhere in mine.c
[b.] if a function in mine.c calls a function that calls UseMe
[c.] if the function UseMe is defined in the function main of mine.c
[d.] if the function UseMe is defined in mine.c
[e.] a and d
- What is the value of the expression 12 - 10 - 8 * 6 % 4 + 2 % 1
[a.] -2
[b.] 0
[c.] -7
[d.] 2
- What is the output of the following code fragment?
int a = 3, b = 17 ;
a = b % a ;
b = ++a + 5 ;
printf("a = %d, b = %d\n", a, b) ;
[a.] a = 3, b = 8
[b.] a = 2, b = 7
[c.] a = 3, b = 7
[d.] a = 2, b = 8
[e.] None of the above.
- 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 loop to be executed next.
[d.] None of the above.
- Determine the output of the code fragment.
int a = 4, b = 3, c = 1 ;
if ( a < b + 1 )
{
printf("Stop and smell the roses.\n") ;
}
else if ( a > b + c )
{
printf("Type A personality.\n") ;
}
else if ( a % b >= c )
{
printf("What doesn't kill me, makes me stronger.\n") ;
}
else
{
printf("I sense a lot of anxiety in this room.\n") ;
}
[a.] Stop and smell the roses.
[b.] Type A personality.
[c.] What doesn't kill me, makes me stronger.
[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.
- The following Boolean expression is equivalent to which of the given
expressions?
!( a == 12 || n > 15 )
[a.] a != 12 && n <= 15
[b.] a == 12 && n > 15
[c.] a != 12 || n <= 15
[d.] a != 12 && n < 15
- Which of the following is the function prototype for a function
AlphaBeta that takes two double parameters and
does not return any values.
[a.] AlphaBeta(double, double);
[b.] void AlphaBeta(double, double);
[c.] void AlphaBeta(double x, y);
[d.] double AlphaBeta(double, double);
- Which of the following is the function prototype for a function
Foo that takes one double parameter and returns
an integer value.
[a.] void Foo (int, double);
[b.] void Foo (double, int);
[c.] int Foo (double);
[d.] int Foo (int, double);
- Which of the following is the function prototype for a function
Bar that takes two integer parameters and
returns a double value
[a.] Bar (int, int);
[b.] double Bar (int, int);
[c.] void Bar (double, int, int);
[d.] double Bar (int * 2);
- In order to include a header file which is in your current working
directory, which of the following would be used ?
[a.] #include <filename>
[b.] #include "filename"
[c.] #include filename
[d.] Any of the above could be used
[e.] None of the above
- When an array parameter is passed to a function
[a.] the elements of the actual array parameter are copied into the
elements of the formal array parameter.
[b.] the elements of the formal array parameter are copied into the
elements of the actual array parameter.
[c.] the formal parameter is a pointer that holds the address of the
actual array parameter.
[d.] the programmer must write code which allocates enough space for
the function to store the array.
- Let arr be an array of integers, then in the expression
arr[i++]
[a.] adds 1 to the address of arr
[b.] adds 1 to the array element arr[i]
[c.] adds 1 to the index i
[d.] none of the above
- A single C statement that assigns the sum of x and y to z
and then increments the value of x by 1 after the calculation.
[a.] z = y + (x + 1);
[b.] z = ++x + y;
[c.] z = y + x++;
[d.] none of the above
- A single C statement that decrements the variable x by one then
subtracts it from the variable total is
[a.] total = total - x--;
[b.] total -= --x;
[c.] total -= (x - 1);
[d.] none of the above
- A single C statement that adds the variable x to the variable
total and then decrements x by one is
[a.] total = total + x--;
[b.] total += --x;
[c.] total += (x - 1);
[d.] none of the above
- Assuming that the variables x and product each have the value
5, then the new values of x and product after the statement
product *= x++; are
[a.] product = 25 and x = 5
[b.] product = 25 and x = 6
[c.] product = 30 and x = 5
[d.] product = 30 and x = 6
- Assuming that the variables high and low contain the values
3 and 5, respectively, then the new values of high and low
after the statement high *= --low + 4 are
[a.] high = 24 and low = 4
[b.] high = 24 and low = 8
[c.] high = 19 and low = 4
[d.] high = 27 and low = 4
- The main difference between a struct and an array is:
[a.] Structs are fixed size, while arrays can vary in size.
[b.] Structs can have members of different types, but array elements
must be of the same type.
[c.] Structs can contain chars, but arrays cannot.
[d.] There is no real difference; whether a struct or an array is used
is a matter of programming style.
- Which of the following can NOT be done with a structure variable ?
[a.] assign from one to another
[b.] pass as an argument to a function
[c.] return as the value of a function
[d.] all of the above can be done
- The expression sizeof(int) refers to
[a.] the largest number that can be stored in an int variable
[b.] the number of int variables declared in the current function
[c.] the number of bytes needed to store an int variable
[d.] the largest number of characters allowed in the name of an int
variable
-
The expression sizeof(struct foo) refers to
[a.] the number of member variables in struct foo
[b.] the size of the largest member variable in struct foo
[c.] the total size of struct foo in bytes
[d.] the size of the smallest member variable in struct foo
- A segmentation fault occurs when
[a.] a program using structures does not compile
[b.] a program using arrays does not compile
[c.] a program tries to access a memory location forbidden by the operating
system
[d.] a program calls a function in another file
- The typedef facility is used to
[a.] declare a member of a structure.
[b.] declare struct variables.
[c.] give a new name to a type.
[d.] All of the above.
- The third element in the 2nd row of a 7 - by - 5 integer array named
maze is
[a.] maze[2][3]
[b.] maze[3][2]
[c.] maze[2][1]
[d.] maze[1][2]
- The statement printf ("%-30s", "Dan");
[a]. causes "Dan" to be right justified in a field 30 characters wide
[b]. causes "Dan" to be left justified in a field 30 characters wide
[c]. causes "Dan" to be centered in a field 30 characters wide
[d]. causes a compiler error
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.
-
What is the Unix command to list all the files in the current directory?
-
What is the Unix command to change the current working directory?
-
What is the Unix command to display the name of the current working
directory?
-
Assuming a file named foo.c exists in the current directory, what
is the Unix command to rename foo.c to bar.c?
-
What is the Unix command to create a new directory named cmsc201?
-
What is the Unix command to delete the file named circle.c?
-
What is the Unix redirection command that will place the output from an
executable, a.out, into a file called output.txt ?
-
Write a function called ArrayMin that returns the minimum value
in an array of n integers, where array and n are parameters
passed to the function.
-
Give an example of how ArrayMin would be called from main.
-
Write a function called SumArray that returns the sum of the first
n elements of an array of floats, where array and n are
parameters which are passed to it.
-
Write a C code fragment using a for loop that prints the numbers
1 to 5 side-by-side on the same line with 4 spaces between each number
-
Write a C code fragment using a while loop that prints the even
numbers between 1 and 9 (inclusive)side-by-side on the same line with
5 spaces between each number
-
Write a C code fragment using a for loop that calculates and and
prints the sum of the even integers from 2 to 30, inclusive.
-
When passing arguments to a function, what's the difference between
"passing by value" and "passing by reference" ?
-
Given the structure definition/declaration below, write a printf()
statement which prints today in the form month/day/year.
struct date
{
int year;
int month;
int day;
} today ;
-
Give a function prototype for a function called Hypotenuse that
takes two double precision floating point arguments, side1 and
side2 and returns a double precision floating point result.
DO NOT write the code.
-
Give a function prototype for a function called Smallest
that takes three integer arguments, x, y and
z and returns an integer. DO NOT write the code.
-
Write a single C statement that will print a random number from the set
2, 4, 6, 8, 10
-
How are the elements of an array stored in memory ?
-
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);
}
}
-
What is the output of the following program? Don't wory about the exact
number of spaces to appear before or after any numeric output. If the
program goes into an infinite loop, you need show only the first five
lines of output. Assume that the program as shown compiles correctly.
#include
int Mickey(int, int) ;
int Mouse(int) ;
int main()
{
int a = 2, b = 3, c = 3 ;
a = Mickey (b + 5, c) ;
b = Mouse (c) ;
printf ("main: a = %d, b = %d, c= %d\n", a, b, c) ;
return 0 ;
}
int Mickey (int x, int y)
{
int z ;
z = x - y ;
printf ("Mickey: x = %d, y = %d, z = %d\n", x, y, z) ;
return (z) ;
}
int Mouse (int c)
{
int a, b ;
a = 1 ;
b = c * 2 + a ;
a = b + 5 ;
c = Mickey (a, b) ;
printf ("Mouse: a = %d, b = %d, c = %d\n", a, b, c) ;
return (b) ;
}
-
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++)
{
for (j = 0; j <= 4; j++)
{
if ( (i + 1 == j) || (j + i == 4) )
{
printf ("+");
}
else
{
printf ("o");
}
}
printf ("\n");
}
-
In the nested for loop code above, how many times is the
condition of the if clause evaluated ?
-
Write a simple loop which prints out the following sequence of numbers:
18 23 28 33 38 43 48
-
What's the difference between a function's prototype and its
header ?
-
Write a program that inputs 2 floating point values from the keyboard
and displays their product.
-
Write a program that gets 10 integer values that are input from the
keyboard, then displays their sum.
-
Which of C's data types can be used in an array ?
-
In a one-dimensional array with n elements, what is the index of the
last element ?
-
Write the statement(s) that would declare a 10-element integer array
and initialize all of its elements to 1.
-
Write a declaration for a 3 - by - 6 integer array named array.
-
Write a function called Average that takes three integer
parameters n1, n2, and n3 and returns the value of the average of
the three as a double.
-
Write a function called Harmonic that takes an integer parameter
n, and returns the following sum:
1/1 + 1/2 + 1/3 + . . . + 1/n
This is called a partial Harmonic series. The various fractions,
and the returned value should be of type double.
- Given the structure definitions below
typedef struct
{
int x;
int y;
} POINT;
typedef struct
{
POINT start;
POINT end;
} LINE;
Write a function called MakeLine() that takes two parameters of
type POINT and returns a structure of type LINE representing the line
segment that connects the two points.
- Given the definitions of POINT and LINE above, write a function
called LineType() that takes a structure of type LINE and
returns a character (v, h or o) where v means vertical, h means
horizontal and o means oblique. A vertical line is a line whose
x-coordinates are the same. A horizontal line in a line whose
y-coordinates are the same. Any other line is oblique.
- Given the structure definition below, write a scanf()
statement which reads in the quantity member of p.
struct part
{
int partNum;
char description[30];
int quantity;
char type;
};
struct part p;
-
Write a function called NumDigits that has a single formal
parameter of type int and returns the number of digits in that integer.
-
Write a predicate function called IsEven that takes an integer,
n, as its single argument and returns "true" if n is even and "false"
if n is not even.
-
Fill in the blanks below. The function FindNet computes a worker's
weekly net pay given hours (an integer), rate
(a double), and exempt (an int used as a boolean) as
input parameters. The net pay for the worker should be returned as a
double. If the worker turns in more than forty hours and the value of
exempt is FALSE, then pay 1 1/2 times the normal rate ("time and a
half") for any hours worked over forty. If gross pay exceeds $800,
subtract 33% for taxes. Otherwise, subtract 25% for taxes. Include a
statement to print the worker's hours and net pay for the week. (The
net pay will be in dollars and cents, so choose the output format
accordingly. The number of underscore characters shown is not
necessarily an indication of the length of the correct response.)
________ FindNet ( int hours, ________ ______ , int _______ )
{
double ________ , __________ ;
int ___________ ;
if ( _______ > 40 _____ (!exempt ) )
{
extraHours = hours - 40;
grossPay = (40 * _______ ) + extraHours * (rate * _____);
}
else
{
grossPay = hours * _______ ;
}
if (grossPay > ________ )
{
netPay = grossPay - grossPay * _______ ;
}
_________
{
netPay = grossPay - grossPay * _______ ;
}
________ ("Hours = %d and Net Pay = _______ \n", hours, netPay);
________ ( __________ );
}
-
Fill in the blanks below. The function CalculateLabScore calculates
the total lab score by dropping the two lowest lab scores and summing
the remaining scores. The number of underscore characters shown is not
necessarily an indication of the length of the correct response.
/*******************************************************
* CalculateLabScore()
* calculates the total lab score by dropping the two lowest
* lab scores and summing the remaining scores.
*
* Input: the array of scores and its size
* Output: the total of the scores after the two lowest
* scores are dropped.
*******************************************************/
_________ CalculateLabScore(int scores________, int ____________)
{
int i, temp, totalScore = _________;
int lowest, nextLowest, lowestIndex, nextLowestIndex;
/* find lowest value */
lowest = scores[_____];
lowestIndex = _____;
for(i = 1; i < size; i++)
{
if(scores[i] < _____________)
{
lowest = scores[i];
__________________ = i;
}
}
/* move the lowest value to the end of the */
/* array by swapping it with the last element */
______________ = scores[lowestIndex];
scores[lowestIndex] = scores[size-1];
scores[size-1] = ______________;
/* find the next to the lowest value */
/* don't examine last element since */
/* it is holding the lowest score */
_________________ = scores[0];
nextLowestIndex = 0;
_______(i = 1; i < __________________; i++)
{
if(scores[i] < __________________)
{
nextLowest = scores[i];
nextLowestIndex = i;
}
}
/* move the next to the lowest value to the */
/* next to the last position in the array by */
/* by swapping it with the next to last element */
temp = scores[nextLowestIndex];
scores[nextLowestIndex] = scores[________________];
scores[________________] = temp;
/* sum all but the 2 lowest scores */
for(i = 0; i < _____________; _______)
{
totalScore ________ scores[i];
}
return _____________;
}
-
Fill in the blanks in the function below. This function calculates a
gymnast's score by removing the highest and lowest score from an array
of 10 floating point values, (ranging from 0.0 to 10.0) then averaging
the other 8.
#define NR_SCORES __________
______ Gymnastics (____________ scores[NR_SCORES])
{
int i;
float sum = _______;
float lowest = 11.0;
float __________ = -1.0;
for (i = 0; i < ________; i++)
{
sum += _______________;
if (_________________________)
{
lowest = scores[i];
}
if (_________________________)
{
highest = scores[i];
}
}
sum -= highest;
sum -= ______________;
return ( _______________ );
}
-
Fill in the blanks below. The function PrintRandomPhoneNumber
generates and prints a random seven-digit phone number that adheres to
the rules for phone numbers in the United States. Those rules are:
- There is a dash between the first 3 digits and the last 4 digits.
- Neither the first or second digit can be a 0 or a 1.
The function takes the seed for the pseudo-random number generator as
its single parameter of type int. Choose the output format to guarantee
proper printing of the phone number. (The number of underscore
characters shown is not necessarily an indication of the length of the
correct response.)
________ PrintRandomPhoneNumber ( ________ seed )
{
int digit1 , ________, digit3, firstThree, lastFour;
/* Seed the random number generator */
_____________(seed);
/* Generate the first two digits individually */
digit1 = rand( ) ______ 8 + 2;
________ = rand ( ) % ________ + ________ ;
/* Generate the third digit */
________ = rand ( ) % ________ ;
/* Calculate the 3-digit integer called firstThree */
firstThree = digit1 * ________ + ________ * _______ + digit3;
/* Generate a four-digit integer */
lastFour = ________ ( ) % ________ ;
/* Print the random phone number */
___________ ("_______-_______\n", ____________, ______________ );
}
-
Write a function called RollDice( ) that takes no arguments and
returns the result of rolling two six-sided dice. You may assume that
the pseudo-random number generator has already been seeded in main()
or some other function before RollDice( ) is called.
-
Write the function CountVowels( ) that examines an array of n
characters and returns the total number of vowels (a, e, i , o , u).
-
Write the function Stars() whose inputs are an array of characters
(a), the size of the array (n), and a character (c). This function
should replace all occurrences of 'c' in the array, a, with a star (*)
and return the number of times the replacement was made.
-
Write the function VowelStars() whose inputs are an array of
characters (a), and the size of the array (n). This function should
replace all vowels ('a', 'e', 'i', 'o', 'u') with a star (*) and return
the number of times the replacement was made.
-
Describe what the function below does. The parameter a is 2-dimensional
array of positive floating point numbers.
float Bob (float a[10][5])
{
float x = 0.0;
int i, j;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 5; j++)
{
if (a[i][j] > x)
{
x = a[i][j];
}
}
}
return x;
}
-
Under what conditions will this code fragment print "water" ?
if(temp < 32) {
printf("ice\n");
}else if (temp < 212) {
printf("water\n");
}else {
printf("steam\n");
}
-
What's wrong with this code fragment ?
int a[5];
for(i = 1; i <= 5; i++)
{
a[i] = 0;
}
-
Describe top-down design.
-
A "test harness", also known as a "driver" - What is it and when is it
used ?
-
Draw a "design diagram" for the following project:
Project description: This project allows the user to compute the volume
and surface area of boxes, when s/he inputs the length, width and height
of a box. The user can continue to enter the dimensions of new boxes
until s/he is done.
-
Give the prototypes for at least two of the functions you would use
in the project described in the previous question (other than
PrintGreeting()) and describe their purpose.
-
Write a function called Add3Even() that examines an array of n
integers and returns the sum of the first 3 even values it finds. You
may assume that there will be at least 3 even integers in the array.
-
Write the function LargestOdd that examines the contents of an
integer array with n elements and returns the largest odd integer in
the array. If there are no odd integers in the array, return 0.
-
Write a function called Doubler that takes a single integer
parameter, n, and returns (as an integer) the number of times n must be
doubled to get to or exceed 1,000,000.
- Write a function called Scan(), which takes a string and a
character as its two parameters and returns an integer which is the number
of times the given character appears in the string.
- Write a function CountVowels() that examines a string and returns the
total number of vowels in that string (a, e, i, o, u).
- Write a function Stars() whose inputs are an array of characters (a),
the size of the array (n), and a character (ch). This function should
replace all occurrences of ch in the array, a, with a star (*) and
return the number of times the replacement was made.
- Write a function VowelStars() whose inputs are an array of characters (a),
and the size of the array (n). This function should replace all vowels
('a', 'e', 'i', 'o', 'u') with a star (*) and return the number of times
the replacement was made.
- Write a definition for a struct named book which contains
three members:
- a string title of at most 50 characters
- a string author of at most 50 characters
- an integer variable checkedOut.
- What is the empty string ? How big should the array be that stores the
empty string ? What would the function strlen() return when
it is passed the empty string ?
-
Print 1.234 in a 9-digit field with leading zeros.
-
What happens if your program tries to access an array element with an
out-of-range subscript ?
-
Write a set of one or more if statements that will print
out one of the brief messages given in the table below, depending on a
person's grade point average. Assume that the grade point average has
already been read into a variable called gpa. Include code to print a
warning message if the value of gpa is not in the range 0.0 to 4.0.
gpa range |
student rating |
3.50 - 4.0 |
Dean's list |
2.0 - 3.49 |
Satisfactory |
1.0 - 1.99 |
Probation |
0.0 - 0.99 |
Suspended |
-
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);
-
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
_______ (4) (b < c) && (c == 7)
_______ (5) (c + 1 - b == 0) || (a = 5)
Determine the output generated by the following code segments, assuming
the surrounding program code (whatever it is) compiles correctly.
-
int a = 2, b = 3;
b *= a;
a += b++;
printf ("%d %d\n", a, b);
-
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);
-
#include
#define SIZE 10
int Mystery (int a[], int n);
int main ( )
{
int foo[SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
printf ("Mystery returns %d\n", Mystery (foo, SIZE));
return 0 ;
}
int Mystery (int a[], int n)
{
int x = 0;
int i;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
x += a[i];
}
}
return x;
}
-
Briefly describe in English what the function "Mystery" used above does.
Describe the large task it accomplishes, not the individual steps.
-
void F (int x);
void G (int x);
int main ()
{
G (4);
F (5);
G (6);
return 0;
}
void F (int x)
{
printf ("%d ", x);
}
void G (int x)
{
F (x + 1);
printf ("%d ", x);
}
-
#include
int X (int m);
int Y (int n);
int main ()
{
int r, s;
r = X (12);
s = Y (7);
printf ("main: %d %d\n", r, s);
return 0;
}
int X (int m)
{
printf ("X: %d\n", m);
return (m + 2);
}
int Y (int n)
{
printf ("Y: %d %d\n", n, X (n));
return (n * 3);
}
-
int F (int x);
int main()
{
printf ("%d %d", F (5), F (F (7)) );
return 0;
}
int F (int x)
{
return 2 * x + 1;
}
-
int a = 2, b = 11;
b += a;
a -= ++b;
printf ("%d %d\n", a, b);
a = 13;
b = 5;
printf ("%d %d\n", a / b, b / a);
printf ("%d %d\n", a % b, b % a);
-
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 n = 1234;
while (n > 0)
{
n /= 10;
printf ("%d\n", n);
}
-
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 m;
for (m = 1; m <= 5; m++)
{
switch (m)
{
case 1:
printf ("one");
break;
case 2:
printf ("two");
break;
case 3:
printf ("three");
break;
default:
printf ("Default case");
}
printf ("\n");
}
-
int F1 (int a, int b);
int F2 (int b, int c);
int main()
{
int a = 2, b = 5, c = 7;
c = F1 (a, b);
printf ("main: %d %d %d\n", a, b, c);
b = F2 (a, c);
printf ("main: %d %d %d\n", a, b, c);
return 0;
}
int F1 (int a, int b)
{
printf ("F1: %d %d\n", a, b);
return a;
}
int F2 (int b, int c)
{
printf ("F2: %d %d\n", b, c);
return c;
}
-
int F1 (int a, int b);
int F2 (int a, int b);
int main()
{
int a = 2, b = 4, c = 6;
c = F1 (a, b);
printf ("%d %d %d\n", a, b, c);
b = F2 (a, c);
printf ("%d %d %d\n", a, b, c);
a = F2 ( F1 (a, c), b);
printf ("%d %d %d\n", a, b, c);
b = F2 (a, c) / F1 (b, c);
printf ("%d %d %d\n", a, b, F1 (a, b) );
return 0;
}
int F1 (int a, int b)
{
return (a + b + 3);
}
int F2 (int a, int b)
{
return ( (a + b) % 3 );
}