Final Exam COP
All preprocessor directives begin with
#
Define macro CUBE_VOLUME that computes the volume of a cube. The macro takes one argument.
#define CUBE_VOLUME( x ) ( ( x ) * ( x ) * ( x ) )
Write a preprocessor directive to define symbolic constant YES to have the value 1.
#define YES 1
Write a preprocessor directive to accomplish the following: If symbolic constant TRUE is defined, undefine it and redefine it as 1. Do not use #ifdef.
#if defined( TRUE ) #undef TRUE #define TRUE 1 #endif
Directives #ifdef and #ifndef are provided as shorthand for:
#if defined(name) and #if !defined(name)
Write a preprocessor directive to Include the header common.h. The header is found in the same directory as the file being compiled.
#include "common.h"
Print the elements of an array called values using pointer offset notation
*(values+i)
The output of printf( "%+09d\n", 452 ); is
+00000452
The output of printf( "%+d\n%+d\n", 786, -786); is
+786 -786
structure pointer operator
->
structure member operator
.
The output of printf("%05d\n%+07d\n%07.1f", 1234,-1234,12.34); is:
01234 -001234 00012.3
How many tokens the following code will create? #include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.ucf.ed - website"; const char s[4] = ".-"; char *token; token = strtok(str, s); while( token != NULL) { printf( " %s\n", token ); token = strtok(NULL, s); } return(0); }
5
if dest="12345" and src="67890", the value of dest after the following code will be what? memmove(memmove(dest, src, 2), memset(dest, '1', 3), 2);
67145
if s1="12345" and s2=" 98765", then what is the output of the following printf function? printf("%s", strncat(s2,strncat(s2, s1,5),1));
9876512345
The character handling function are stored in:
<ctype.h>
size_t strcspn( const char *s1, const char *s2 );
Determines length of s1 only consisting of characters NOT contained in string s2.
size_t strspn( const char *s1, const char *s2 );
Determines the length of s1 consisting of ONLY characters contained in s2.
If enum months { JAN = 1, FEB, MAR, APR, MAY = 4, JUN, JUL, AUG, SEP, OCT, NOV, DEC }, then (JUN+FEB ==6) returns true.
False
Only white-space characters may appear before a preprocessor directive on a line.
False
The #define preprocessor directive is used to create just symbolic constants.
False
What, if anything, prints when the following C statement is performed? Assume the following variable definitions: char s1[ 50 ] = "jack", s2[ 50 ] = " jill", s3[ 50 ], *sptr; printf( "%c%s", toupper( s1[ 0 ] ), &s1[ 2 ] );
Jck
char *strchr( const char *s, int c );
Locates the first occurrence of c in string s.
char *strrchr( const char *s, int c );
Locates the last occurrence of c in string s.
Do structure definitions reserve space in memory?
NO
Can cast expressions, sizeof expressions, and enumeration constants be evaluated in preprocessor directives?
No
What does sscanf do?
Reads input from array in first argument rather than from keyboard.
What does int isprint( int c ) do?
Returns a true value if c is a printing character INCLUDING A SPACE ('') and returns 0 otherwise.
What does int isgraph( int c ) do?
Returns a true value if c is a printing character OTHER THAN A SPACE ('') and returns 0 otherwise.
the third argument of fseek can have one of three values:
SEEK_SET, SEEK_CUR, or SEEK_END
What does sprintf do?
Stores output into an array in its first argument instead of printing it on screen.
What does sscanf return?
The number of items successfully read by the function or EOF if error occurs
A symbolic constant is a name for a constant.
True
All pointer types can be assigned a void * pointer, and a void * pointer can be assigned a pointer of any type.
True
Function fread transfers a specified number of bytes from the location in the file specified by the file position pointer to an area in memory beginning with a specified address.
True
Function memchr searches for the first occurrence of a byte, represented as unsigned char, in the specified number of bytes of an object. If the byte is found, a pointer to the byte is returned; otherwise, a NULL pointer is returned.
True
Pointers can be compared using equality and relational operators
True
Pointers can be defined to point to objects of any type.
True
The compiler does not differentiate between a function that receives a pointer and a function that receives a single-subscripted array.
True
Two pointers to elements of the same array may be subtracted from one another to determine the number of elements between them.
True
When the compiler encounters a function parameter for a single-subscripted array of the form int b[], the compiler converts the parameter to the pointer notation int *b.
True
What does the structure pointer operator do?
accesses a structure member via a pointer to the structure
the only 4 valid operations that may be performed on a structure:
assigning structure variables to variables of the same type, taking the address (&) of a structure variable, accessing the members of a structure variable and using the sizeof operator to determine the size of a structure variable.
In mode "a+", all writing is done where?
at the end of the file
char *strtok (char *s1, const char *s2)
breaks string s1 into token separated by characters contained in string s2.
copies string s2 into array s1. The value of s1 is returned.
char *strcpy( char *s1, const char *s2 )
The conditional preprocessor directives only evaluate what kinds of expressions?
constant integer expressions
double atof( const char *nPtr )
converts the string nPtr to double
double strtod (const char *nPtr, char **endPtr);
converts the string nPtr to double
int atoi( const char *nPtr )
converts the string nPtr to int
long strtol( const char *nPtr, char **endPtr, int base )
converts the string nPtr to long
long atol( const char *nPtr )
converts the string nPtr to long int
Mode "w+"
creates a file for reading and writing; if file exists already, it's contents are discarded
Given the following structure and variable definitions, struct customer { char lastName[ 15 ]; char firstName[ 15 ]; int customerNumber; } customerRecord, *customerPtr; customerPtr = &customerRecord; write an expression that can be used to access the Member lastName of structure customerRecord. a) (*customerRecord)->.lastName Correct Answer b) customerRecord.lastName c) *customerRecord..lastName d) customerRecord ->lastName
customerRecord.lastName
if dest="oldstring" and src="newstring", what will be the the values of dest and src after the following code: memmove(dest, memset(dest, 'a', 2), 1);
dest = aadstring, src = newstring
if dest="oldstring" and src="newstring", what will be the the values of dest and src after the following code: memmove(dest, memmove(dest, src, 1), 1);
dest = nldstring, src = newstring
What does mode "w+" do if a file already exists?
discards its contents
Write a statement that reads a record from the file "oldmast.dat". The record consists of integer accountNum, string name and floating-point currentBalance. a) fscanf( ofPtr, , &accountNum, name, ¤tBalance ); b) fscanf( ofPtr, "%d%s%f", accountNum, name, currentBalance ); c) fscanf( ofPtr, "%d%s%f", &accountNum, name, ¤tBalance ); d) fread( ofPtr, "%d%s%f", &accountNum, name, ¤tBalance );
fscanf( ofPtr, "%d%s%f", &accountNum, name, ¤tBalance );
string converstion functions are stored in what library?
general utilities <stdlib.h>
The number of bytes used to store a union must be at least enough to:
hold the largest member
int toupper (int c);
if c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper returns the argument unchanged.
int tolower (int c);
if c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged.
referencing a value through a pointer is called:
indirection
What is the output of the following code? #include <string.h> #include <stdio.h> int main () { char str[80] = "This is - www.ucf.ed - website"; const char s[4] = "."; char *token; token = strtok(str, s); while( token != NULL) { printf( " %s\n", token ); token = strtok(str, s); } return(0); }
infinite loop
The scope of a symbolic constant or macro is from its definition until:
it is undefined with #undef or until the end of the file
char *strpbrk (const char *s1, const char *s2)
locates first occurrence in string 1 of any character in string 2.
char *strstr (const char *s1, const char *s2)
locates first occurrence of string s1 of string s2.
When the address of a variable is passed to a function, the indirection operator * can be used to do what to the value it points to?
modify it at the location in the caller's memory
Write a statement that opens the file "oldmast.dat" for reading and assigns the returned file pointer to ofPtr.
ofPtr = fopen( "oldmast.dat", "r" );
How many members of a union can be referenced at a time?
one
Mode "r+"
opens a file for reading and writing
Mode "a+"
opens a file for reading and writing; all writing is done at end of file. If file does not exist, it's created.
Fix the error in the following snippet: printf( "%s\n", *cPtr->face ); Assume that struct card has been defined containing two pointers to type char, namely face and suit. Also, the variable c has been defined to be of type struct card and the variable cPtr has been defined to be of type pointer to struct card. Variable cPtr has been assigned the address of c.
printf( "%s\n", (*cPtr)->face );
fgets (reads/writes) one character from a file.
reads
int iscntrl (int c);
returns a true value if c is a control character and 0 otherwise.
int isxdigit (int c);
returns a true value if c is a hexadecimal digit character and 0 otherwise.
int ispunct (int c);
returns a true value if c is a printing character other than a space, digit, or a letter and returns 0 otherwise.
int isspace (int c);
returns a true value if c is a white space character (new line \n, space ' ', form feed \f, carriage return \r, horizontal tab \t, or vertical tab \v) and 0 otherwise.
int isupper (int c);
returns a true value if c is an uppercase letter and 0 otherwise.
int islower (int c);
returns true value if c is a lowercase letter and 0 otherwise.
void mystery( const char * const sPtr ) { if ( sPtr[ 0 ] == '\0' ) { return; } else { mystery (&sPtr[1]); putchar(sPtr[0]); } }
reverse string sPtr
If the field width is larger than the object being printed, the object is ______ justified by default.
right
To print a space before a positive value not printed with the + flag we can use the following flag:
space
Which three files and their associated streams are automatically opened when program execution begins?
standard input, standard output, standard error
Given the following structure and variable definitions, struct customer { char lastName[ 15 ]; char firstName[ 15 ]; int customerNumber; struct { char phoneNumber[ 11 ]; char address[ 50 ]; char city[ 15 ]; char state[ 3 ]; char zipCode[ 6 ]; } personal; } customerRecord, *customerPtr; customerPtr = &customerRecord; write an expression that can be used to copy "Flower" to the Member lastName of structure customerRecord. a) customerRecord.personal-> lastName b) customerRecord->personal-> lastName c) strcpy( customerRecord.lastName, "Flower"); d) customerRecord->personal. lastName
strcpy( customerRecord.lastName, "Flower");
Given the following structure and variable definitions, struct customer { char lastName[ 15 ]; char firstName[ 15 ]; int customerNumber; struct { char phoneNumber[ 11 ]; char address[ 50 ]; char city[ 15 ]; char state[ 3 ]; char zipCode[ 6 ]; } personal; } customerRecord, *customerPtr; customerPtr = &customerRecord; write an expression that can be used to copy "Orlando" to the Member city of member of personal of structure customerRecord. a) strcpy( customerRecord->personal->city, "Orlando"); b) customerRecord.personal.city= "Orlando"; c) strcpy( customerRecord.personal.city, "Orlando"); d) customerRecord->personal->city= "Orlando";
strcpy( customerRecord.personal.city, "Orlando");
Define a structure called part containing int variable partNumber and char array partName with values that may be as long as 25 characters (including the terminating null character).
struct part { int partNumber; char partName[26]; };
A union is declared with keyword union in the same format as a:
structure
NULL is known as a:
symbolic constant
What does macro assert do?
tests the value of an expression and prints an error message if it is 0 (false)
Symbolic constants and macros can be discarded by using:
the #undef preprocessor directive
Function feof receives a pointer to a FILE and returns a nonzero (true) value when:
the end-of-file indicator has been set
What does sprintf return?
the number of characters written to the array in its first argument or EOF if error occurs
When using strcat, the first character of the second string overwrites what?
the terminating null character of the first string
What does strcpy return?
the value of the first string
The indirection operator * returns what?
the value of the object to which its operand points
What does directive #undef do?
undefines the symbolic constant or macro name
A variable name (directly/indirectly) references a value, and a pointer (directly/indirectly) references a value.
variable name directly references, pointer indirectly references
Assume We have defined three functions—function1, function2 and function3—that each take an integer argument and return nothing. Define an array of pointers to these three functions called "f".
void (*f[ 3 ])( int ) = { function1, function2, function3 };
fputs (reads/writes) one character to a file.
writes