0% found this document useful (0 votes)
3 views

x

Uploaded by

harikapoor05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

x

Uploaded by

harikapoor05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

UNIVERSITY OF CENTRAL FLORIDA

COP 3223 – Introduction to Programming with C


Dr. Andrew Steinberg

Practice Exam 3 Review

INSTRUCTIONS TO STUDENTS
1. This review contains 12 practice questions. Please note that the num-
ber of questions in this review is NOT always the same to the number
of questions on the exam.
2. The questions on this review should not be interpreted as the precise
topics to be asked on the exams. Students are expected to review all
content that was presented in lecture.
3. Answer all questions to the best of your ability. Partial credit will be
given for attempts on questions. Make sure to write proper C Syntax
for the answers. Any incorrect syntax will cause points to be deducted.
Pay close attention to syntax and write clearly and neatly.
4. You do not need to write out all components of a working C
program (main function, preprocessor directives, etc...) un-
less specified otherwise in the questions.

Last Name:

First Name:

UCF ID:
COP 3223

Question 1

Consider the following lines of code: Consider the following code and partial
sample output.
1 int main ( void )
2 {
3 char phrase [10];
4 char phrase2 [10];
5 printf ( " Enter a phrase : " );
6 scanf ( " % s " , phrase );
7 printf ( " Phrase is % s \ n " , phrase );
8 printf ( " Enter a second phrase : " );
9 scanf ( " % s " , phrase2 );
10 printf ( " Phrase is % s \ n " , phrase2 );
11 return 0;
12 }

Enter a phrase: She-Hulk Attorney at Law


...

• Write the rest of the output exactly how is should be after the user in-
puts the following text from the parital sample output above. In other
words, what will be displayed to the terminal after the first scanf state-
ment when collecting She-Hulk Attorney at Law from the keyboard?

Phrase is She-Hulk
Enter a second phrase: Phrase is Attorney

• What is exactly left in the standard input stream after line 6 executes?

Attorney at Law

• What is exactly left in the standard input stream after line 9 executes?

at Law

2
COP 3223

Question 2

Consider the following code.


char arr [15] = " Intro " ;
char arr2 [15] = " To " ;
char arr3 [15] = " C Programming " ;
strcat ( arr , arr2 );
printf ( " % s \ n " , arr );
printf ( " % s \ n " , arr2 );
printf ( " % s \ n " , arr3 );
strcpy ( arr3 , & arr [2]);
printf ( " % s \ n " , arr );
printf ( " % s \ n " , arr2 );
printf ( " % s \ n " , arr3 );
strncpy (& arr3 [4] , & arr [3] , 2);
printf ( " % s \ n " , arr );
printf ( " % s \ n " , arr2 );
printf ( " % s \ n " , arr3 );

Draw out the arrays after each string function call.


After strcat function call...

arr 'I' 'n' 't' 'r' 'o' 'T' 'o' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
arr2 'T' 'o' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
arr3 'C' ' ' 'P' 'r' 'o' 'g' 'r' 'a' 'm' 'm' 'i' 'n' 'g' '\0' '\0'

After strcpy function call...

arr 'I' 'n' 't' 'r' 'o' 'T' 'o' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
arr2 'T' 'o' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
arr3 't' 'r' 'o' 'T' 'o' '\0' 'r' 'a' 'm' 'm' 'i' 'n' 'g' '\0' '\0'

After strncpy function call...

arr 'I' 'n' 't' 'r' 'o' 'T' 'o' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
arr2 'T' 'o' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0' '\0'
arr3 't' 'r' 'o' 'T' 'r' 'o' 'r' 'a' 'm' 'm' 'i' 'n' 'g' '\0' '\0'

3
COP 3223

Question 3

Complete the following user defined function definition called removeAlphaLetters.


The function takes a string as one of the parameters and removes all alphabet
characters of the string. The function does not return any values. Assume
that the string only contains capital alphabet letters.
void r em ov eA lp ha Le tt er s ( char word [])
{
for ( int x = 0; x < strlen ( word ); ++ x )
if ( word [ x ] >= ’A ’ && word [ x ] <= ’Z ’)
{
for ( int y = x ; y < strlen ( word ); ++ y )
word [ y ] = word [ y + 1];
--x ;
}
}

4
COP 3223

Question 4

Write a user defined function definition called noSpace. The function has one
parameter, a string called s1. The function replaces every space character
(’ ’) in the string parameter with an underscore (’ ’). The function does
not return anything. Assume the character library was already imported.
void noSpace ( char s1 [])
{
for ( int x = 0; x < strlen ( s1 ); ++ x )
if ( s1 [ x ] == ’ ’)
s1 [ x ] = ’_ ’;
}

5
COP 3223

Question 5

Write a user defined function definition called wordsCombo. The function


parameter takes one string called word1 and another string called word2.
The function combines word1 and word2 together into one string. Store
the resulting string in word1 and display it. The function does not return
anything. Assume the string library was already imported.
void wordsCombo ( char word1 [] , char word2 [])
{
strcat ( word1 , word2 );
printf ( " % s " , word1 );
}

6
COP 3223

Question 6

Write the typedef structure declaration of course t. The components of the


structure include:

• code which is type int

• subject which is string with three characters

• instructor which is a dynamic string

• credits which is type int

typedef struct {
int code ;
char subject [4];
char * instructor ;
int credits ;
} course_t ;

7
COP 3223

Question 7

Given the following code.


double * a = malloc (5 * sizeof ( double ));
for ( int x = 0; x < 5; ++ x )
a [ x ] = 1.4;
free ( a );

Draw a visualization of the memory state (both stack and heap space) of
what is happening after lines 4 and 5 are executed.

Figure 1: After line 4.

Figure 2: After line 5.

8
COP 3223

Question 8

Given the following struct definition.


typedef struct {
char * word ;
int x ;
} mystruct_t ;

Write the following statements:

• Allocate a dynamic array of type mystruct t of size 10. Store it in a


variable called myarray.

mystruct t * myarray = malloc(sizeof(mystruct t) * 10);

• Populate the first element of the array with content.

myarray[0].word = malloc(sizeof(char) * 5);


strcpy(myarray[0].word, "the");
myarray[0].x = 3;

• Free the memory being used to prevent memory leaks.

free(myarray[0].word);
free(myarray);

9
COP 3223

Question 9

Write a user defined function definition called palindrome that has one
string parameter called stuff and a second parameter that holds an integer
value called size which represents the number of characters in the string.
The function will determine if the string in stuff is considered a palindrome
or not. If the string is a palindrome, the function returns 1. Otherwise return
0. A string is said to be palindrome if it reads the same backward as forward.
Examples of palindrome strings, level, refer, aabaa, and abccba are a few of
many examples of what is considered a palindrome.
int palindrome ( char stuff [] , int size )
{
for ( int x = 0; x < size / 2; ++ x )
if ( stuff [ x ] != stuff [ size - x - 1])
return 0;

return 1;
}

10
COP 3223

Question 10

Given the following typedef structure definition.


typedef struct {

char name [50]; // static string that contains employee name


int startyear ; // start year of the job
char * jobtitle ; // dynamic string that contains job title

} employee_t ;

Complete the following user defined function updateJobTitle. The function


takes a dynamic array of employee t and updates a specific employee with a
brand new job title. Make sure to properly store the new value. You do not
know how many bytes of memory were stored in the dynamic string compo-
nent previously. The function has three parameters. The first parameter is
a reference to the array of type employee t stored in the heap space. The
second parameter is a string (stored in the stack space) that contains the
new updated job title. The third parameter represents the index location of
the array of the specific employee to update.
void updateJobTitle ( employee_t * records , char title [] , int loc )
{
if ( strlen ( title ) > strlen ( records [ loc ]. jobtitle ))
{
free ( records [ loc ]. jobtitle );
records [ loc ]. jobtitle = malloc ( sizeof ( char ) * strlen ( title ) + 1);
strcpy ( records [ loc ]. jobtitle , title );
}
else
{
strcpy ( records [ loc ]. jobtitle , title );
}
}

11
COP 3223

Question 11 Given the following code. Assume r and c are variables al-
ready declared and initialize to proper values.
int ** a = ( int **) malloc ( r * sizeof ( int *));
for ( int i = 0; i < r ; i ++)
a [ i ] = ( int *) malloc ( c * sizeof ( int ));

int * b = ( int *) malloc ( r * c * sizeof ( int ));

Are the variables a and b exactly the same in terms of implementation and
how it is stored in memory of a 2D dynamic array? If not, explain the dif-
ference. You are welcome to draw a visualization to help your explanation.

They are not. Variable a is a double pointer. It points to a heap


which contains a set of integer pointers. Inside the for loop, each
integer pointer is assigned to an address of another heap which holds
regular primitive integer values. This means that the 2D is not
adjacent in memory location. Variable b is a regular pointer that
points to heap. However in this allocation all of the elements will
be adjacent in the heap. Both variables respectively point to the
same amount of memory however the main difference is that they are
not the same from an adjacent perspective

12
COP 3223

Question 12 Given the following code. Assume r and c are variables al-
ready declared and initialize to proper values.
char ** words = ( char **) malloc ( r * sizeof ( char *));

for ( int i = 0; i < r ; ++ i )


words [ i ] = ( char *) malloc ( c * sizeof ( char ));

for ( int x = 0; x < r ; ++ x )


{
printf ( " Enter a word : " );
scanf ( " % s " , words [ x ]);
}

Write the remainder code that detects duplicate strings. If a duplicate is


found, then the message “Duplicates!” is displayed. If no duplicates exists,
then “No Duplicates!” will be displayed.
int flag = 0;

for ( int x = 0; x < r ; ++ x )


for ( int y = x + 1; y < r ; ++ y )
{
if ( strcmp ( words [ x ] , words [ y ]) == 0)
{
printf ( " Duplicates !\ n " );
flag = 1;
}
}

if ( flag == 0)
printf ( " No Duplicate !\ n " );

13

You might also like