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

101Final

The document is a final exam for a Computing I course from Fall 2007, consisting of two parts: Quick Hitters and Short Answer, Templates, and Code Fragments. It includes questions on binary numbers, ASCII values, function behavior, memory allocation, and C programming concepts. Students are required to demonstrate their understanding of programming principles and memory management through various coding tasks and theoretical questions.

Uploaded by

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

101Final

The document is a final exam for a Computing I course from Fall 2007, consisting of two parts: Quick Hitters and Short Answer, Templates, and Code Fragments. It includes questions on binary numbers, ASCII values, function behavior, memory allocation, and C programming concepts. Students are required to demonstrate their understanding of programming principles and memory management through various coding tasks and theoretical questions.

Uploaded by

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

Name: _________________________________ 91.

101 Computing I Final


Fall 2007 Part I – Quick Hitters

I have enjoyed getting to know you a bit this semester.


1) Here is a positive binary number. Please write this number using the IEEE 754 single precision floating point
format.

00011101.11100001

2) Here is a decimal number that has a decimal point. Please write this number as a binary number using a
binary point. (I do not want the IEEE version.) Your answer should have exactly 8 bits to the left of the binary
point and 8 bits to the right of the binary point.
29.341

3) Suppose you are given 10 bit positions. How many different numbers can you represent if you only have two
symbols ( 0 and a 1 ) to fill each position?
____________________

4) What is the decimal equivalent of the blank character in ascii? ______________

5) A call to the scanf function is an expression. It reports out a value. Please tell me what is returned by a call to
the scanf function. Please tell me both typical possibilities. I will get you started.

The scanf function returns either ….

6) What is the value of the following expression. A decription of the value will do.

“I left my heart in San Francisco.”

7) The following is an expression. It reports out a value. That value has type. What is the type of the value
reported out by the following expression?

“I left my heart in San Franciso.”


8) When calling the fopen function is it required that both arguments be surrounded by double quotes? Explain.
For example,
fin = fopen ( “income.dat”, “r” ) ;

9) I give you 9 bits. What is the decimal equivalent of the smallest number that can be represented using a twos
complement strategy?

10) Assume that a struct is passed as an argument to a function foo. Is a copy of the whole struct made and
pushed onto the run-time stack or is a copy of the address of the struct passed?

For example,

struct node { int p ; char h ; struct node *next; } ;


struct node x ;

foo ( x ) ;

11) I have taught you a specific way to phrase the type of an object when it is presented in a declarator. I call
the technique the Right Left Walk Method. Use the right left walk method precisely to tell me X’s type in the
declarator given below:
double * (*X) ( int a ) ;

12) Does the declarator in question 11 above reserve space? Yes or No? ________________

13) Types convey two fundamental pieces of information. What are they?

_______________________ and _________________________

14) How many arguments are being passed to the function foo below?

foo ( (1, 2, 3, 4, 5 ) );

Your answer is: ________________


15) Assume that integers are stored in 4 bytes. Assume that an integer array X has been defined and it begins at
memory location 4000. So, X[0] is located at memory location 4000, X[1] is located at memory location 4004,
X[2] is located at memory location 4008.

What is the value of the following expression X + 1? _____________________

16) The following declarator does three fundamental things. What are they?

int x ;

a) ______________________________________________________________________

b) _______________________________________________________________________

c) _______________________________________________________________________

17) The following call to malloc will do two fundamental things. What are they?

malloc ( 10 ) ;

a) ________________________________________________________________________

b) ________________________________________________________________________

18) In particular, the following call will NOT do two fundamental things. What are they?

malloc ( 10 ) ;

a) ________________________________________________________________________

b) ________________________________________________________________________

19) The purpose of the atoi function is to convert a string number into its twos complement integer
representation. What function would you use to do the opposite? That is, to convert a two’s complement
number into its string represenation? Say for example, you had the decimal value 143 stored in a variable
named x. You also had a definition of a character array S. How would you convert the integer 143 into a string
representation? DO NOT USE a call to the non-standard itoa . Do not write your own function.

int x = 143 ;
char S[100] ;

Write the one line code here:

________________________________________________________
20) Are you ok with this? Is it ok? Or is it wrong? If wrong, please explain.

char s[50] ;

s = “Buffalo gal won’t you come out tonight” ;

21) Assume there are no compile errors in the following function definition. Does the function look ok to you?
If ok, say ok, otherwise tell me what trap the programmer could be falling into?

int *foo ( int sum ) {

int i ;

int A[5] = { 1,2,3,4,5 } ;

for (i = 0 ; i <= 4 ; i++ ) { A[i] = A[i] + sum } ;

return ( A ) ;
}

22) How would Jim read the following expression? Be precise.

x -> next ;

23) Assume the following declarator is given.

int A[3][4][5] ;

Further assume a program contains the following expression.

A[1][3]

What is A[1][3]’s type? ___________________________________________________


24) Assume that a machine uses 2 bytes to represent an integer and 4 bytes to represent a pointer. How many
bytes of space are reserved by the following declarator?

int *A[10] ; ____________________________

25) Assume that a machine uses 2 bytes to represent an integer and 4 bytes to represent a pointer. How many
bytes of space are reserved by the following declarator?

int (*A)[10] ; ___________________________

26) Assume that a machine uses 2 bytes to represent an integer and 4 bytes to represent a pointer. How many
bytes of space are reserved by the following declarator?

int *(*A)[10] ; __________________________

27) Is this ok? Or is this wrong? If wrong, please explain. You may assume that strcpy has been declared.

char *s = “hello” ;
char *t ;

strcpy( t, s );

28) Is this ok? Or is this wrong? If wrong, please explain.

int A[3] = { 1, 2, 3 } ;
int B[3] ;

B=A;

29) What is argv[0]’s type?

30) Suppose you want to pass a two-dimensional array x to a function named foo. Assume the declarator for x is
int x[5][10]. Is the following call to function foo ok? Explain.

foo ( x[][10] )

31) Assume that a machine uses 1 byte to represent a character and 4 bytes to represent a pointer. How many
bytes of space are reserved by the following declarator?

char **c ; ____________________________


32) Please give me the full first name and the full last name of the person who is credited with the creation of
the C programming language.

___________________________________________________________________________________

33) The definition of the C programming language does not include the type referred to as a string. To us, what
is the definition of a string in C. I will help get you started.

A string is ….

34) The function getc , short for get character, is a standard c function. As found in appendix D, the declaration
only for getc is
int getc ( FILE *file_pointer) ;

If the purpose of the function is to get a character why isn’t the declaration written as

char getc ( FILE *file_pointer ) ;

35) It is important that you understand how objects in memory are represented. Here is a definition declarator
with an initializer. The machine the program will run on uses two bytes to store a twos complement integer.

int x = - 43 ;

Show me x’s internal representation. That it, show me the 1s and 0s.
36) In C, what is the purpose of the . operator?

37) In C what is the exponentiation operator? ___________________________________

38) When writing a C program the * symbol can be used three different ways. One of those ways is within a
declarator. When you see the * symbol in a declarator what phrase should come to you mind?

____________________________________________________________

39) When writing a C program the * symbol, used within an executable statement, has two different meanings.
One of the meanings is “multiplication operator”. For example, 5 * 8 . What is the other meaning? Explain.

40) Here is a program fragment. Does it look good to you? Or does it look bad to you? If it looks like that there
could be trouble, please explain.

char *s ;

s[0] = ‘B’ ;
s[1] = ‘I’ ;
s[2] = ‘L’ ;
s[3] = ‘L’ ;
s[4] = ‘\0’ ;
Name: _________________________________ 91.101 Computing I Final
Fall 2007 Part II – Short Answer, Templates, and Code Fragments

Preparation is the key.

Take the next 5 weeks to improve your scholarship, to increase your confidence, and to prepare for the
academic challenges that lie ahead of you. Your current career path is to become a terrific scholar. You can.

Read, push the pencil, and write code.


1) Fill in the following code fragment by completing the Boolean expression. The purpose of the while loop is
to read in an unknown number of integer values from a file until EOF is reached and it then sums them up.

int n, sum = 0 ;

while ( ){

sum = sum + n ;

2) Show me that you know how to define a file pointer and that you know how to open a file whose name has
been given on the command line as argv[1]. You should show this to me by completing the code below. You
should include anything you need to. You should not include anything you do not need to include. The program
should read in one integer value from the file and then print this value out. You should close the file.

int main ( int argc , char *argv[] ) {

return 0 ;
}
3) Please complete the following function called JOIN. The purpose of the function join is to return an array
that contains all of the integers in the array X followed by the integers in array Y.

int *JOIN ( int X[ ] , int Y[ ] , int sizeX , int sizeY ) {

4) Complete the main function that calls the function JOIN.

int join ( ) ; /* finish this line */

int main ( int argc, char *argv[] ) {

int A[4] = { 1, 2 , 3, 4 } ;

int B[8] = { 5, 6, 7, 8, 9, 10, 11, 12 } ;

/* ------ insert a definition for the variable C on this line */

C = join ( ) /* finish this line */

return 0 ;

}
5) Can you call and write a function called swap? Show me by completing this code. I do not want to see any
arrays or usage of the array index operator.

swap ( ) ;

int main ( int argc , char *argv [ ] ) {

int x = 6 , y = 4 ;

swap ( ) ;

return 0 ;

swap ( ) {

}
6) A program’s command line contains a number of integer values. For example, when it is launched it could
look like: % a.out 5 8 12 44 6 4 511 23 . Or it could be, for example: % a.out 34 5 277 .
Complete the following program that will read in these command line arguments and output their sum.

#include <stdio.h>
int main ( int argc, char *argv[] ) {

return 0 ;

}
7) Here are three declarators. Study them and then doodle a clear picture of what space being reserved looks
like. Use pointer arrows where obvious. This is a terrific problem. If you can do this you’ve got it. Have fun.

If you cannot do this – do not waste too much time.

char *a[] = { “ENTER” , “NEW”, “POINT”, “FIRST” } ;

char **b[] = { a + 3 , a + 2 , a + 1 , a } ;

char ***c = b

8) Using the declarators in problem 7 above, what is printed by the following. Again, do not waste time if you
do not get it. I wanted to give a question that could challenge some of the stronger students.

printf( “%s”, * * ++c ) ; In case you forgot:


printf( “%s”, * -- * ++c + 3) ;
printf( “%s”, *c[-2] + 3 ) ; [] has highest precedence and it associates from the left
printf( “%s”, c[-1][-1]+1 ) ;
* -- ++ have equal precedence, but they associate from the right.

+ has lower precedence than the other operators.


9) Found below is a small program. The program does not do anything useful. On the next page is a blank
memory template for this problem. Fill out the memory template using the conventions we used in class up to
the point of the arrow. Be clear. Be complete. Show me that you know how memory is changed and updated. If
I have made a typo, then fix the problem and then complete the problem. Do not bail on the problem if a typo
exists.

int size = 23 ;

int roger ( int *A, int n ) {

int a = 4, b = 6, c = 14 ;
int i ;

for ( i = 0 ; i <= (n-1) ; i++ ) { A[i] = (int *) malloc ( 3 * sizeof( int ) ) ; }

A[2][1] = 511 ;

return 23 ;

int main ( int argc, char * argv[] ) {

int B[4] ;

int n = 32 ;

n = n + roger ( B , 4 )

return 0 ;

}
Problem 9
Name Addr Content

4032

Code Area
4036

4040

4044

4048
Global and Static Data Area

4052

4056

4060

4064

4068

4072

4076

4080
Heap
4084

4088

4092
4000
4096
4004
4100
4008
4104
4012
4108
4016
4112
4020
4116
4024
4120
4028
Run Time Stack
10) Found below is a small program. The program does not do anything useful. On the next page is a blank
memory template for this problem. Fill out the memory template using the conventions we used in class up to
the point of the arrow. Be clear. Be complete. Show me that you know how memory is changed and updated. If
I have made a typo, then fix the problem and then complete the problem. Do not bail on the problem if a typo
exists.

int mystery ( int n ) {

int i = 4 ;

static int count = 0 ;

count = count + 1 ;

if ( n >= 15 ) return ( 5 ) ;

return ( mystery ( n * 2 ) + 7 ) ;

int main ( int argc, char *argv[] ) {

int i = 1 ;

int a ;

a = mystery ( i ) ;

return 0 ;

}
Problem 10

Name Addr Content

4032

Code Area 4036

4040

4044

Global and Static Data Area 4048

4052

4056

4060

4064

4068

4072

4076

4080
Heap
4084

4088
4000
4092
4004
4096
4008
4100
4012
4104
4016
4108
4020
4112
4024
4116
4028
4120

Run Time Stack


Name: _________________________________ 91.101 Computing I Final
Fall 2007 Part III - Coding

Exams are good.

Do not get discouraged.

Do not give up.

Only a few more questions.

There have been some easy questions and there have been some hard ones too.

Do your best.
1) Write a complete C program that will prompt the user to enter an integer value, say n, between 1 and 50.
Your program should read in the value. Then your program will print out a single line containing n asterisks.
The asterisks is the * symbol.

2) Twin primes are two prime numbers that differ by 2. For example, 3 and 5 are twin primes, so are 101 and
103. Write a program that prints all twin primes less than 3000.

You do not need to write the definition of the IsPrime function. You may assume its declaration is found in
Isprime.h. The declaration is

int IsPrime ( int x ) ;

and the function will return a 1 if the number x is prime and 0 if the number x is not prime. You may call this
function from your main routine. DO NOT WRITE the IsPrime function. You may call it.
3) Write a complete C program that will compute the inner product of two vectors. Each vector contains exactly
8 floating point values. Your program will enter these values in from the standard input. Once these values have
been read in, your program should call a function that will compute the inner product of these two vectors. The
function should return the value back to main. The inner product of vector U = (5, 1, 6, 2 ) and vector V = ( 1,
2, 3, 4 ) is 33 because

5*1=5
1*2=2
6 * 3 = 18
2*4=8

and 5 + 2 + 18 + 8 is 33.
4) This is not a coding question, but more or less a check on a coding question that I asked you to do. What
is the persistence of each of the following numbers:

417 _____________

1000 ___________

6 _____________

715 ____________

5) The Fibonacci sequence of ORDER 3 is 1, 1, 1, 3, 5, 9, 17, 31, …

That is, the first three terms are each 1 and then each subsequent term is the sum of the previous three terms.

Write a complete C program that inputs an integer value from the command line as argv[1], say n. Then it will
print out the nth term in the ORDER 3 fibonacci sequence. Your program must call a RECURSIVE function to
do the job. Your program should print out the value of the nth term. The sequence starts with the first term (1).
The sequence does not start with the zeroth (0). The first term is 1. The second term is 1.

(I wish I could give you more coding problems to do, but it is already too much grading.)

You might also like