Midterm Exam Key

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CSE2421 MIDTERM Spring 2013

NAME __________________________________________

SCORE __________________/ 100

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:

Spring 2013 Midterm#1 1|Page


TRUE/FALSE put a T or F in each blank (10 pts; 1 pt each)

________ main is a valid variable name. (T)

_______ 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.

________ The correct order of compilation of a C program is

i) Compile source code to object code A. i ii iii iv


ii) Link object code and libraries B. i iv ii iii
iii) Create binary or executable file C. iv ii i iii
iv) Process Preprocessor directives D. iv i ii iii
E.

________ 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); }

Spring 2013 Midterm#1 2|Page


________ What will be output if you compile and execute the following c code?

#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; }

________ What is meaning of following declaration? int(*ptr[5])();


A. ptr is pointer to function
B. ptr is array of pointer to function
C. ptr is pointer to such function which return type is array
D. ptr is pointer to array of function

________ What will be output of following c code?

#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"); }

________ ptr->next is the same as


A. ptr.next B. *ptr.next C. (*ptr).next D. none of the above

Spring 2013 Midterm#1 3|Page


_______ Determines when the variable is created and destroyed and how long it will retain its value.
A. linkage B. scope C. storage class D. none of the above

_______ Which one is not a valid scope type for the C programming language we are using?
A. file B. block C. prototype D. external

_________ On combining the following statements, you will get

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

_________ Why can typecasting be dangerous?


A. Some conversions are not defined, such as char to int.
B. You might permanently change the value of the variable.
C. You might temporarily lose part of the data - such as truncating a float when typecasting to an int.
D. There are no dangers.

SHORT ANSWER (8 questions; 24 pts - 2,4,2,2,4,4,2,4). Put your answer in the blank provided.

sizeof(x) returns a value of: ___________ (2)

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; }

a = _________ b = _________ ANS a=10 and b=5

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

Spring 2013 Midterm#1 4|Page


Determine the output of the following c code:
#include<stdio.h>
int main(){
int a=10; Designate output here: //Visibility within main block.
{ 30 15
a+=5; // not really a space //Accessing outer local variable a.
int a=20; // just there to help read it //Visibility within inner block.
a+=10; //Accessing inner local variable a.
printf(%d,a); //Accessing inner local variable a.
}
printf(%d,a); //Accessing outer local variable a.
return 0;
}

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; }

Determine the output of the following c code:

#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

In the following program how would you print 50 using p?

#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));

Spring 2013 Midterm#1 5|Page


CODE EVALUATION#1 (10 pts). Put your answers in the blanks provided.

Succinctly comment the 4 lines as designated and determine the output.


int * function(); // _________________________________________________________
int main() {
auto int *x;
int *(*ptr)(); // _________________________________________________________
ptr=&function; // _________________________________________________________
x=(*ptr)(); // _________________________________________________________
printf("%d",*x); } // Output is: _________ 10
int * function() {
static int a=10;
return &a; }

// 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

Spring 2013 Midterm#1 6|Page


CODE EVALUATION #2 (16 pts; 2 pts per blank) FILL IN THE BLANKS

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++;
}
}

Spring 2013 Midterm#1 7|Page


CODE EVALUATION #3 (16 pts; 2 pts per blank) FILL IN THE BLANKS
The goal of this code is to reverse the order of a linked list by literally adjusting the links such that the beginning of
the list becomes the end of the list; and the end of the list becomes the beginning of the list.

#include <stdio.h>

typedef struct Node { // designate the look of a node structure


char data;
struct Node* next;
} Node;

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;
}

Spring 2013 Midterm#1 8|Page

You might also like