<<

CS101 Spring 2014 Midterm II (120 minutes) Thursday, April 24th READ and complete the following: • Bubble your Scantron only with a No. 2 pencil. • On your Scantron (shown in the figure below), bubble :

1. Your Name 2. Your NetID 3. Form letter "A" 4. Bubble the corresponding 3-digit code (shown below) for your lab section on your Scantron. Monday Tuesday Wednesday Thursday 9:00-10:50 101 104 108 111 11:00-12:50 102 105 109 112 1:00-2:50 106 113 3:00-4:50 103 107 110 114

Five points will be deducted if a student does not correctly record their netid on the bubble sheet.

• No electronic devices, books, notes, or cheat sheets are allowed while taking this exam. • Please fill in the correct answer on the provided Scantron sheet. • We will not answer any questions during the exam. • Each question has only ONE correct answer. • You must stop writing when time is called by the proctors. No extra time will be given after the exam ends to fill in bubble sheets with answers. • Hand in both these exam pages and the Scantron. • DO NOT turn this page UNTIL the proctor instructs you to. Given the following directory , answer the next 3 questions.

1. The jsmith directory is your home directory. After entering the Unix prompt, you get the following output:

/home/jsmith/labs/lab2

Which of the following commands, when entered at the Unix prompt, will change your current working directory to the mp1 directory?

(a) ../../mp1 (b) cd ~/jsmith/mp1 (c) cd ../mp1 (d) ./..mp1

2. If your current working directory is changed to the jsmith directory, of the following commands, when entered at the Unix prompt, will copy every file with .c suffix from the mp1 directory to the handin directory?

(a) *.c mp1 handin (b) cp mp1/.c handin (c) cp .c mp1 handin (d) cp mp1/*.c handin

3. If your current working directory is the jsmith directory, which of the following commands, when entered at the Unix prompt, will delete the labs directory and everything in the labs directory?

(a) del -d labs (b) labs (c) rm -r labs (d) rm -f labs 4. Suppose there is a sub-directory named student in your current working directory. Which of the follow- ing commands, when entered at the Unix prompt, will rename the student directory as assignment? (a) rn student assignment (b) mv student assignment (c) mv assignment student (d) cp assignment student

5. Suppose you have a C programming language file named .c in your current directory. The file test.c contains a complete C program that has no errors. Which of the following commands will successfully compile and run the C program found in the file test.c ? (a) ./test.c (b) gcc test.c ./test (c) gcc test.c -o test test.c (d) gcc test.c ./a.out

6. Which of the C programs shown below will not compile successfully?

(a) #include void main(void) { int a = 7; scanf("%i", &a); printf("%i is my favorite number\n",a); } (b) #include void main(void) { int a = 7; printf("%d is my favorite number\n",a); } (c) #include void main(void) { int 5+2 = a; printf("%i is my favorite number\n",a); } (d) #include void main(void) { int a = 5, b = 2; printf("%i is my favorite number",a+b); } 7. Which of the C programs shown below will not produce the following result when compiled and then run at the Unix terminal?

1 is my favorite number!

(a) #include

void main(void) { int a = 5/3; printf("%i is my favorite number!\n", a); } (b) #include #define M 1

void main(void) { printf("%i is my favorite number!\n", M); } (c) #include

void main(void) { int a = 1; printf("%a is my favorite number!\n", a); } (d) #include

void main(void) { int a = 5; printf("%i is my favorite number!\n", a%2); } 8. The following C program compiles and runs without errors.

#include

void main(void) { int a = 3; int i = 0; char L = ’c’;

/* Hint: Look very carefully at the "for" statement below. */ for( i = 0; i < a; i++); printf("W"); while(i<5) { printf("O"); i++; } printf("%c",L); }

What is the output the program produces?

(a) WOOc (b) WWWOOL (c) WWWOOc (d) WOOOOOL 9. The following C program compiles and runs without errors.

#include

void main(void) { int hours;

printf("How many hours a day do you spend on homework? "); scanf("%i", &hours);

if(hours >= 5) printf("Heavy workload."); else { if((hours < 5) && (hours >= 0)) { if(hours >= 3) printf("Normal workload."); if(hours > 0) printf("Light workload."); else printf("No workload."); } } }

What is the output if the user types an input of integer value 3 where prompted with "How many hours a day do you spend on homework? " ?

(a) Normal workload.No workload. (b) Normal workload.Light workload. (c) Heavy workload. (d) Normal workload. 10. The following C program compiles and runs without errors.

#include

void main(void) { int num; int remain;

printf("Please enter an integer than 10 but greater than zero: "); scanf("%i", &num);

if((num < 10) && (num > 0)) { switch(num % 4) { case 0: remain = 0; printf("Integer value is %i with remainder %i.", num, remain); break; case 1: remain = 1; printf("Integer value is %i with remainder %i.", num, remain); case 2: remain = 2; printf("Integer value is %i with remainder %i.", num, remain); break; default: remain = 3; printf("Integer value is %i with remainder %i.", num, remain); } } else printf("Error: Please rerun the program with a valid input."); }

What is the output if the user types an input of integer value 5 where prompted with "Please enter an integer less than 10 but greater than zero: " ?

(a) Integer value is 5 with remainder 1. (b) Integer value is 5 with remainder 1.Integer value is 5 with remainder 2. (c) Error: Please rerun the program with a valid input. (d) Integer value is 5 with remainder 1.Integer value is 5 with remainder 1. 11. The following C program compiles and runs without errors.

#include #include

int x = 1;

int function1 (int);

void main(void) { int result = function1(x+1); printf(" %i %i\n", result, x); }

int function1 (int x) { int y = 4; return x * y; }

What is the output the program produces?

(a) 8 1 (b) 8 2 (c) 4 1 (d) 4 2 12. The following C program compiles and runs without errors.

#include

void main(void) { int i; int j; int count; count = 0;

for(i = 0; i < 2; i++) { for(j = 2; j < 4; j++) { printf("(%i %i) ", i, j); count = count + 1; } j = 1; printf("(%i %i) \n", i, j); } printf("Count is: %i", count); }

What is the output the program produces?

(a) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) Count is: 4 (b) (0 2) (0 3) (0 4) (0 1) (1 2) (1 3) (1 4) (1 1) Count is: 6 (c) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) (2 2) (2 3) (2 1) Count is: 6 (d) (0 2) (0 3) (0 4) (0 1) (1 2) (1 3) (1 4) (1 1) (2 2) (2 3) (2 4) (2 1) Count is: 9 13. The following C program compiles and runs without errors.

#include

void main(void) { int i; int j; int count;

count = 0;

for(i = 0; i < 2; i++) { j = 2; while(j < 4) { printf("(%i %i) ", i, j); j++; } j = 1; count = count + 1; printf("(%i %i) \n", i, j); } printf("Count is: %i", count); }

What is the output the program produces?

(a) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) Count is: 4 (b) (0 2) (0 3) (0 1) (1 2) (1 3) (1 1) Count is: 2 (c) (0 2) (0 3) (0 4) (0 1) (1 2) (1 3) (1 4) (1 1) (2 2) (2 3) (2 4) (2 1) Count is: 3 (d) (0 2) is displayed on the computer screen infinitely many times. 14. Which of the following programs does NOT produce the following output?

0.0, 7.5, 15.0, 22.5,

(a) #include void main(void) { double i = 2.0; int j;

i = (15 * 2.0) / 4; for(j = 0; j < 4; j++) printf("%5.1lf, ", j * i); } (b) #include void main(void) { double i = 2.0; int j = 0;

i = (15 * 2.0) / 4; while(j < 4) printf("%5.1lf, ", j * i); } (c) #include void main(void) { double i = 2.0; int j = 0;

i = (15 * 2.0) / 4; do { printf("%5.1lf, ", j * i); j++; }while(j < 4); } (d) #include void main(void) { double i = 2.0; int j = 0;

i = (15 * 2.0) / 4; while(j < 4) { printf("%5.1lf, ", j * i); j++; } } 15. The following C program compiles and runs without errors.

#include #include

void main(void) { int i; char mystr[] = {’d’,’o’,’g’,’!’,’\n’,’\0’,’c’,’a’,’t’,’?’,’\n’};

for (i=0; i < strlen(mystr); i++) printf("%c",mystr[i]); }

What is the output the program produces?

(a) dog! ? (b) dog! cat? (c) dog! (d) dog! \0cat? 16. The following program compiles and runs without errors. What is the output this program produces?

#include

void swap2(int* x) { int temp = x[0];

x[0] = x[1]; x[1] = temp; }

void swap1(int x, int *y) { int temp = x;

x = *y; *y = temp; }

void main(void) { int a[2] = {1, 2}; int b[3] = {4,5,6};

swap1(a[0], a); printf("%i %i \n", a[0], a[1]); swap2(&b[1]); printf("%i %i %i\n", b[0], b[1], b[2]); }

(a) 2 1 5 4 6 (b) 1 2 5 4 6 (c) 2 1 4 6 5 (d) 1 2 4 6 5 17. The following C program compiles and runs without errors.

#include

void func1(int x, int z[]) { x = x + 1; z[0] = x; }

void main(void) { int i; int x = 1; int y[2] = {0, 2};

func1(x, y);

printf("%i ", x); for (i = 0; i < 2; i++) printf("%i ", y[i]); }

What is the output the program produces?

(a) 2 0 2 (b) 1 0 2 (c) 2 2 2 (d) 1 2 2 18. You are given the incomplete C program below.

#include #include

void main(void) { ______(replace this blank with your code)______= "red"; color[1] = ’u’; printf("%s \n", color); }

Which of the following choices, when replacing the blank in the program above, would produce the following result when compiled and run? rud

(a) char color (b) char * color (c) char color[16] (d) All of the above 19. Which of the following C code fragments dynamically allocates an array of data double with 25 elements?

(a) double ptr; ptr = calloc (25, sizeof(double)); (b) double ptr; ptr = malloc (25, sizeof(double)); (c) double * ptr; ptr = calloc (25 * sizeof(double)); (d) double * ptr; ptr = malloc (25 * sizeof(double));

20. Which one of the answer choices, when replacing the blank below, outputs the values shown below? The correct answer choice will complete the program that calls qsort and sorts the array named num in ascending order. The output from the program is as follows: 1 2 3 4 5

#include #include

int cmp_int1(int * a, int * b) { return *a - *b; }

int cmp_int2(int * a, int * b) { return *b - *a; }

void main(void) { int i; int num[5] = {3,2,1,5,4};

______(replace this blank with your code)______

for(i = 0; i < 5 ; ++i) printf("%i ", num[i]); }

(a) qsort(num, sizeof(int), 5, cmp_int1); (b) qsort(num, sizeof(int), 5, cmp_int2); (c) qsort(num, 5, sizeof(num[0]), cmp_int1); (d) qsort(num, 5, sizeof(num[0]), cmp_int2); 21. Which answer choice contains the correct code for the compare function named cmp_fruits and the correct code to call the qsort function so that when replacing the corresponding blanks in the program shown below, the program would compile and run without errors and would output the following values? strawberry peach orange banana apple

#include #include #include typedef char string[100];

______(code for cmp_fruits compare function goes here)______

void main(void) { int i; string fruits[5] = {"peach", "apple", "strawberry", "banana", "orange"};

______(call to qsort goes here)______

for(i = 0; i < 5; ++i) printf("%s ", fruits[i]); }

(a) int cmp_fruits(string* ptr1, string* ptr2) { return -1 * strcmp(*ptr1, *ptr2); }

qsort(fruits, 5, sizeof(string), cmp_fruits); (b) int cmp_fruits(string* ptr1, string* ptr2) { return *ptr2 - *ptr1; }

qsort(fruits, 5, sizeof(char), cmp_fruits); (c) int cmp_fruits(string* ptr1, string* ptr2) { return *ptr1 - *ptr2; }

qsort(fruits, sizeof(char), 5, cmp_fruits); (d) int cmp_fruits(char* ptr1, char* ptr2) { return strcmp(ptr2, ptr1); }

qsort(fruits, sizeof(string), 5, cmp_fruits); Given the following code, answer the next 4 questions.

#include #include #include

typedef struct { char title[50]; char genre[50]; int year; }Book;

typedef struct { char name[50]; int count; /* actual count of the number of books */ Book books[1000]; } Author;

Author authors[5];

22. What is the correct way to assign the value "John Smith" to the name field of the fifth element of the array named authors?

(a) authors[5].name = "John Smith"; (b) strcpy(authors[4].name, "John Smith"); (c) strcpy(authors[5].name, "John Smith"); (d) authors[4].name = "John Smith";

23. Which of the following correctly declares a variable named book of data type Book and assigns the value "Hamlet" to the title field, "drama" to the genre field and 1600 to the year field?

(a) Book book = {"Hamlet","drama",1600}; (b) Book book = ["Hamlet","drama",1600]; (c) Book book; book.title = "Hamlet"; book.genre = "drama"; book.year = 1600; (d) Book book; strstr(book.title,"Hamlet"); strstr(book.genre,"drama"); book.year = 1600; 24. Replace the blank in the following comparison function named compCountAsc with the correct code to the array named authors of data type Author in ascending order by the value in the count field. qsort will call the compCountAsc function.

int compCountAsc(Author * ptrL, Author * ptrR) {

______(replace this blank with your code ) ______}

(a) return strcmp( *ptrL.count, *ptrR.count ) ; (b) return *(ptrR.count) - *(ptrL.count) ; (c) return (ptrL -> count) - (ptrR -> count) ; (d) return ((*ptrR) -> count) - ((*ptrL) -> count) ;

25. Complete the code for the function shown below by replacing the blank with the correct code that prints the book title. You may assume that author is a pointer that points to a variable of data type Author. You may also assume that booknum is the index of the books array of the book whose title we want to print. Note that the books array is a field of the variable of data type Author.

void display(Author *author, int booknum) { printf("%s", ____(replace this blank with your code)___ ); }

(a) (*author).books[booknum] (b) author->books[booknum] (c) (&author).(books[booknum].title) (d) (author->books[booknum]).title Extra Credit

Answering the questions below correctly will add points to your exam total. Answering incorrectly or not answering will not add points to your exam total.

26. The following C program compiles and runs without any errors.

#include

void main(void) { int i = 1; int a = 6; int b = 3;

do { if (a > b) { b += b; printf("%i ", a++/++i); } if (b > a) { break; } if (b == a) { printf("%i ", a); printf("%i ", i--); return; } --a; i++; } while (i < 4);

}

What is the output the program produces?

(a) 3 (b) 3 6 3 (c) 3 6 2 (d) 7 6 2 27. Replace the blanks in the code below with the correct code to complete the binary_Search function. Binary Search is an algorithm that searches a sorted array of integers to find a match given a user search value. It is assumed that the array is sorted in ascending order. The main idea is as follows: If the search value is greater than the middle value in the array, then search the right half of the array, else if the search value is less than the middle value in the array, then search the left half of the array, else the search value equals the middle value in the array, return the index of the middle value. If the search value is not found return the value -1.

/* array is the name of the array to be searched */ /* size is the number of elements in the array */ /* search_value is the value being searched for */

int binary_Search(int array[], int size, int search_value) { int midpt, index = -1; /* index = -1 means not found */

/* first is the left-most index of the sub-array being searched */ /* last is the right-most index of the sub-array being searched */ int first = 0, last = size - 1;

while ((first <= last) && (index == -1)) { midpt = (first+last)/2; /* integer division!! */

if (search_value > array[midpt]) ______(i)______else if (search_value < array[midpt]) ______(ii)______else ______(iii)______}/* end of while */

return index ; }

(a) (i) last = midpt - 1; (ii) first = midpt + 1; (iii) index = midpt; (b) (i) first = midpt - 1; (ii) last = midpt + 1; (iii) index = -1; (c) (i) first = midpt + 1; (ii) last = midpt - 1; (iii) index = -1; (d) (i) first = midpt + 1; (ii) last = midpt - 1; (iii) index = midpt;