Midterm Exam Key
Midterm Exam Key
Midterm Exam Key
NAME __________________________________________
FYI: There are no compiler errors in the code presented on this exam. If you think there is, please bring it to
my attention.
NOTE: If there is not an answer in the blank provided, the answer is wrong.
Warning: This test is for your eyes only. The information on this exam is NOT to be shared in any way. This
exam is 100% your own work. If I see you trying to page through your exam to compare questions with your
neighbor, I will move you. If I see you looking at your neighbors paper, I will move you. If I suspect that you
are talking with your neighbor, I will move you. The time and attention this takes away from you taking
your exam is not available for makeup; that is, you get no extra time for not paying strict attention to your
own paper.
YOU MUST TURN IN EVERY PAGE OF THIS EXAM including the ref card page
Scratch area:
_______ Makefiles contain UNIX commands and will run them in a specified sequence. (T)
_______ continue, break and goto work in basically the same way and can be used interchangeably. (F)
________ The gdb command invokes a debugger environment. (T)
________ A typedef and a #define can be used interchangeably. (F)
________ Every C program will contain at least one preprocessor directive. (F)
________ reeves@osu is a valid variable name. (F)
________ C does not support objects (per a typical object-oriented programming definition). (T)
________ A switch statement can be used to switch on string values. (F)
________ A pointer is not a basic type in C, but instead, is called a derived type. (T)
MULTIPLE CHOICE (12 questions; 2 points each). Put your answer in the blank provided.
________ What will be output if you will compile and execute the following c code?
#include <stdio.h>
#define x 5+2 A. 343
void main() { B. 27
int i; C. 133
i=x*x*x; D. None of the above
printf("%d",i); }
________ What will be output if you will compile and execute the following c code?
#include <stdio.h> A. 0 1 2
void main() { B. 0 1 2 3
int i=0; C. 1 2 3
for(;i<=2;) D. Infinite loop
printf(" %d",++i); }
#include<stdio.h>
int main() {
A. 10
int i=10;
B. 20
int *ptr=&i;
C. The address of i
*ptr=(int *)20;
D. None of the above
printf("%d",i);
return 0; }
#include<stdio.h>
void main() {
struct field {
int a; A. A
char b; B. 5
} bit; C. 45
struct field bit1 = {5,'A'}; D. None of the above
char *p=&bit1;
*p=45;
printf("\n%d",bit1.a); }
________ What will be output if you will compile and execute the following c code?
#include<stdio.h>
void main() {
A. 1
int i=0;
B. 3
if(i==0)
C. 5
{
D. equal
i=((5,(i=3)),i=1);
printf("%d",i);
}
else
printf("equal"); }
_______ Which one is not a valid scope type for the C programming language we are using?
A. file B. block C. prototype D. external
char*p;
p=malloc(100);
A. char *p= malloc(100) B. p= (char*)malloc(100) C. All of the above D. None of the above
SHORT ANSWER (8 questions; 24 pts - 2,4,2,2,4,4,2,4). Put your answer in the blank provided.
What will be output if you compile and execute the following c code?
#include<stdio.h>
int main(){
int a=5,b=10;
a=a+b-(b=a);
printf("\na= %d b= %d",a,b);
return 0; }
What is the value of the file pointer when there is an error opening the file? ______________________ (NULL)
Given:
int arr[3][4]
int * ptr = &arr
What value is added to ptr to reach the array location arr[1][2]? __________________ 6
[0][0], [0][1], [0][2], [0][3], [1][0], [1][1], [1][2]
arr +1 +2 +3 +4 +5 +6
Determine the output of following c code which is stored in the file arg.c and the following command is given:
% arg c question bank
Designate output here:
#include<stdio.h> arg
int main(int argc, char *argv[]) { c
int i; question
for(i=0; i<argc; i++) bank
printf("\n%s",argv[i]);
return 0; }
#include <stdio.h>
void main() {
char *pwest = "west",*pnorth = "north", *peast="east", *psouth = "south";
enum location { east=1, west=2, south=3, north=4};
enum location direction;
direction = east;
if( direction == east )
printf("Cannot go %s\n", peast); } // Output: __________________________________
Cannot go east
#include<stdio.h>
int main(){
int a[]={10, 20, 30, 40, 50};
char *p;
p= (char*) a;
} // _________________________________________________________
printf("\n%d",*((int*)p+4)); also printf(%d\n, *(p + sizeof(int)*4));
// function prototype/declaration
// declare ptr to be a function pointer (no params and return int to a pointer)
// assign the address/location of the function
// call the function
A string (example "I am writing an email") is entered through the keyboard. Write a program in C to get the words
output in reverse order in a column as output. The example output, for the given example input, is:
email
an
writing
am
I
Assume that the user entered string will fit into the array size given in the program str[200]
#include <stdio.h>
void main()
{
char str[200]; *ptr!=0 also // declare identifier and initialize where appropriate
char *ptr = str, *temp; *ptr!=\0
int i=0, j; also
*ptr!=NULL
scanf("%[^\n]", str); (warning) // read user string/input; reads all characters until enter key
while (*ptr) also str[i] // loops until the nul character is found in the string array
{
i++; ptr = ptr + 1 // the identifier i is the number of characters in the string
ptr++; also
} ptr=&str[i] // ptr now pointing to the end of the string
for (j=0; j<=i; j++) also
{ ptr=str+i
if(*ptr==' ') // comparing to a blank
{
temp=ptr;
ptr--;
temp++;
while ((*temp!=' ') && (*temp!='\0') )
{
printf("%c",*temp); // printing a character
temp++;
}
printf("\n"); // end of outputting a word so go to a new line
}
else // not at the end of a word
{
ptr--;
}
}
while(*ptr != ' ') // printing last output word (which is first input word)
{
printf("%c",*ptr);
ptr++;
}
}
#include <stdio.h>
void print_list(Node* root) // output the char data from each node
{
while (root) // traverse the linked list, printing each piece of data along the way
{
printf("%c ", root->data);
root = root->next;
}
printf("\n");
}
Node* reverse(Node* root) // puts null at the start of the list and root at the end
{ // and moves the links to reverse the list
Node* new_root = 0; // now the end is the start of the list; and the start, the end
while (root) // loop until end of list
{
Node* next = root->next;
root->next = new_root;
new_root = root;
root = next;
}
return new_root;
}
int main()
{
Node d = { 'd', 0 }; // initialize 4 nodes of the linked list
Node c = { 'c', &d };
Node b = { 'b', &c };
Node a = { 'a', &b };
Node* root = &a; // the variable root points to Node a
print_list(root); // prints a b c d
root = reverse(root);
print_list(root); // prints d c b a
root = reverse(root);
print_list(root); // prints a b c d
return 0;
}