- TRUE/FALSE
When a function is called its parameters are local to the function.
- TRUE/FALSE
The lifetime of a static variable is the duration of your program's execution.
- TRUE/FALSE
A static global variable may only be referenced in the .c file in which it is defined
- TRUE/FALSE
A static function may only be used in the .c file in which it is defined.
- 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
An array can store many types of values simultaneously.
- TRUE/FALSE
All arrays with 10 elements require the same amount of memory, regardless
of the type of elements stored in the array.
- TRUE/FALSE
If p is a pointer to an int, then the expression p[ i ] is invalid
unless p is the name of an array.
- TRUE/FALSE
typedef is used to give a new name to a known data type.
- TRUE/FALSE
In C, an argument is passed by VALUE when a copy of its value of
is passed to the function's parameter.
- 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.
- TRUE/FALSE
The declaration typedef int *intPtr; creates a variable called
intPtr which can hold the address of (or point to) an integer.
- TRUE/FALSE
The functions malloc ( ) and calloc( ) are used to
dynamically allocate memory.
- TRUE/FALSE
If p is a pointer to structure variable called rec
and number is a member of the structure, then to access
the number member of rec using p, use the
syntax p -> number
- TRUE/FALSE
The preprocessor directives #ifndef, #define,
and #endif are used to guard header files.
- TRUE/FALSE
The #include preprocessor directive is used to insert one file
into another.
- TRUE/FALSE
The function free() is used to release dynamically allocated
memory back to the heap for possible reuse.
- TRUE/FALSE
When an array is passed to a function, a copies of the array elements
are made for the function to manipulate.
- TRUE/FALSE
If p is a pointer then the expression
p = p + 1; adds 1 to the value of p regardless of p's type.
- TRUE/FALSE
If p is a pointer to type int,
then the expression &p is a "pointer to a pointer to an int".
- TRUE/FALSE
The declarations char name[10]; and char *name;
are identical.
- TRUE/FALSE
If p is a pointer to an int, then the expression
&p is invalid and results in a compiler error.
- TRUE/FALSE
If p is a pointer to an int and A
is an array of integers, then the statement
*p = A[0];
assigns the value stored in A[0]
to p.
- TRUE/FALSE
If p is a pointer to integers and A
is an array of integers, then the following statements are equivalent:
p = &A[0];
p = A;
- "gcc&qout; is the C compiler used on the GL servers.
C Programming - Multiple Choice Questions
- Members of a structure
[a.] may be of different types.
[b.] may be structure.
[c.] may be pointers to a struct of the same kind
[d.] all of the above
- A function prototype is
[a.] is used mainly as a comment to the reader.
[b.] tells the compiler the value returned by the function.
[c.] tells the compiler the type of the value that
is returned by the function.
[d.] none of the above
- 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);
- 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
- 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
- When an array argument is passed to a function
[a.] the elements of the array argument are copied into the
elements of the array parameter.
[b.] the elements of the array parameter are copied into the
elements of the array argument.
[c.] the array parameter is a pointer that holds the address of the
array argument.
[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
- If arr is an array of integers, then a[ i ] is equivalent to
[a.] *a + i
[b.] a + i
[c.] *(a + i)
[d.] none of the above
- 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.
- The main difference between a struct and a union is:
[a.] The syntax used to define them
[b.] The syntax used to access their members
[c.] Struct members have separate memory, union members share memory
[d.] Struct and Union are really different names for the same thing
- 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
- The size of a union is
[a.] The sum of the sizes of the union's members
[b.] The size of the largest member of the union
[c.] Is determined at runtime
[d.] Depends on which member of the union is used
- 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 length of a string is determined
[a.] From the declared size of the array which contains it.
[b.] By a length byte stored at the beginning of the string.
[c.] By a null terminator stored at the end of the string.
[d.] By a length byte stored at the end of the string.
- Given the following declaration,
char string[8] = "abcdefg";
what is output by the following statement:
printf ("%s\n", string + 3);
[a.] abcdefg
[b.] abc
[c.] defg
[d.] a compiler error
- The following code
char string[8] = "abcdefg";
*string = '\0';
printf ("%s", string);
[a.] generates a compiler error
[b.] generates a run-time error
[c.] creates no output, but no error is generated
[d.] creates the output bcdefg
- Which of the following declares a pointer variable (ptr)
and initializes it to the address of x?
[a.] int *ptr = *x
[b.] int *ptr = &x
[c.] int &ptr = *x
[d.] int &ptr = &x
- What is the effect of dereferencing a pointer which has the value NULL ?
[a.] Zero is returned.
[b.] The operation is ignored.
[c.] The compiler detects the error and issues an error message.
[d.] A run-time error (seg fault) results.
- What is the advantage of using a pointer to a structure as a parameter to
a function, instead of the structure itself ?
[a.] The code is easier to read.
[b.] It's more efficient because the structure is not copied.
[c.] There is no difference; it is a matter of style which is used.
[d.] Passing a structure as a parameter is not allowed.
- During file processing, a file is referenced via what type of variable ?
[a.] a file handle (type int)
[b.] a file pointer (type FILE*)
[c.] the file name (type char*)
[d.] a pointer to the file name (type char**)
- When using command line arguments, what is the content of argv[0] ?
[a.] The name of the input data file.
[b.] The name of the executable file.
[c.] The name of the output data file.
[d.] None of the above.
- Given the array declaration int array[10]; which of the
following is a pointer to the second element of array.
[a.] &array[2]
[b.] array + 1
[c.] array[2]
[d.] &(array + 1)
C Programming - 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.
- Explain why it is (almost) never correct to #include one .c file inside of another.
- Describe the technique used to "guard" a header (.h) file. Why is guarding
a header necessary?
- In general, what should be included in a header (.h) file?
-
Write a function called ArrayMin that returns the minimum value
in an array of n integers, where array and n (the size of the array) are parameters passed to the function.
-
Give an example of how ArrayMin would be called from main.
-
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 ;
-
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 <stdio.h>
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) ;
}
-
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, ________ ______ , bool _______ )
{
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);
________ ( __________ );
}
-
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;
}
-
What common bug is found in the code fragment below ?
int a[5];
for(i = 1; i <= 5; i++)
{
a[i] = 0;
}
- 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 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 happens if your program tries to access an array element with an
out-of-range subscript ?
Determine the output generated by the following code segments, assuming
the surrounding program code (whatever it is) compiles correctly.
-
int n = 1234;
while (n > 0)
{
n /= 10;
printf ("%d\n", n);
}
- Show two ways to obtain the address of the first element of the array
data[].
- Show two ways to assign the value 100 to the third element of the array
named data[].
- What is the output of the following code ?
int m = 5, n = 6, *p, *q;
q = &n;
p = &m;
*p = *q * 2;
printf ("%d %d %d %d\n", m, n, *p, *q);
- Write a function called GetCoordinates() which prompts the user to
enter three coordinates, x, y, and z. All three coordinates are of type
float. These values are "returned" via the functions parameters using
call by reference of the three values.
- Write a single statement that opens the file "oldmast.dat" for reading and
assigns the returned file pointer to the variable ofPtr.
- Given the following structure definition
typedef struct person
{
char name[50];
int age;
} PERSON;
and given the following code fragment:
struct person Bob; /* line 1 */
scanf ("%s", Bob.name); /* line 2 */
scanf ("%d", &Bob.age); /* line 3 */
explain why the call to scanf ( ) in line 3 requires the 'address of' operator,
but the call to scanf ( ) in line 2 does not. BE SPECIFIC
- Starting with PERSON from above
- Modify PERSON by adding a pointer to another PERSON.
- Declare a pointer named me to be a pointer to a PERSON.
- Allocate dynamic memory to which me points.
- Write code to input my name and age using scanf( )
- Define another PERSON named sister then write code to input my
sister's name and age, and create a link between me and my sister. Be sure to indicate that
we have other siblings.
- Write a program that takes several command line arguments and then
prints those strings to the screen.
- What is a memory leak ?
- What is a dangling pointer ?
Determine the output generated by the following code segments,
assuming the surrounding program code (whatever it is) compiles correctly.
char string[10] = "abcdefgh", *p = string, ch;
while (*p != '\0')
{
ch = *p;
++p;
*p = ch;
p++;
}
printf ("%s\n", string);
-
char string[15] = "This is a test";
strcpy (string + 10, "cat");
printf ("%s\n", string);
- Describe each of the steps taken by gcc to create an executable program from a .c source file
- Define a recursive function and give a short example.
- What is the meaning of the following printf conversions --
%d, %x, %p, %s, %f
- What is the difference between using scanf( "%s", string); and fgets( ) to read
input from the user?
- Explain why it is necessary to use the C string library function strcmp( ) rather than using
the equality operator ==.
- What common programming error is found in the following code?
char name[20];
printf("%s", "Please enter your name\n");
scanf( "%s", name);
- Draw the picture of memory that results from the following code
int i;
char *names[5];
for (i = 0; i < 5; i++)
names[i] = NULL;
names[0] = (char *)malloc(10 * sizeof(char));
names[2] = (char *)malloc(12 * sizeof(char));
names[4] = (char *)malloc(5 * sizeof(char));
strcpy(names[0], "Bob Smith");
strcpy(names[2], "Jimmy Smith");
strcpy(names[4], "Tom");
- Consider the declarations below.
char *boysNames[5];
char **girlsNames;
Explain the difference between these declarations.
Draw the pictures of memory that each represensts.
Write code to store the names "Bobby", "James", "Bill", "Thomas" and "Sam" into boysNames
Write code to store the names "Susan", "Rachel", "Laura", "Michele" and "Liz", "Joanne" and "Samantha"
into girlssNames
- What is the purpose of assert( ). Give a short example of its use
- Describe 3 methods to create 2-dimensional array of doubles. Explain when each method
would be appropriate. Draw a picture of memory for each method.
- Explain how a "pointer to a function" might be used. Give an example of its use.
- Explain how command line arguments are passed to main? What is the function header for
main when command line arguments are used?
- What special steps if any must be taken so that your program may be debugged using gdb?
- Describe the functionality of each of the following gdb commands
- l
- b
- r
- c
- p
- x
- n
- s
- where
- What is the purpose of the "make" utility?
- Describe the following parts of a makefile
- target
- dependency
- build rule
- "variables"