What is the difference between a string and a normal array of characters? |
||
A. |
A string cannot be changed |
|
B. |
An array of characters is longer (takes more memory) than the equivalent string |
|
C. |
A string has a terminating "null" (binary-0) character |
|
D. |
A string is stored with its length value |
|
E. |
No difference - an array of characters is always a string |
|
Answer : C |
Which of the following are TRUE?
I. An int can be a value parameter of a function so that the function cannot modify the original integer. II. An array can be a value paramter of a function so that the function cannot modify the contents of the original array. III. A struct can be a value parameter of a function so that the function cannot modify the contents of the original struct. |
||
A. |
I only |
|
B. |
II only |
|
C. |
I and II only |
|
D. |
I and III only |
|
E. |
I, II, and III |
|
Answer : D |
What will the following code print? #include <stdio.h> int main(void) } |
||
A. |
0 |
|
B. |
3 |
|
C. |
6 |
|
D. |
12 |
|
E. |
48 |
|
Answer : D |
As discussed in class, a precondition for the Binary Search algorithm to operate correctly is that... |
||
A. |
the first element of the array must be 0. |
|
B. |
none of the array element values can be duplicates. |
|
C. |
the midpoint element must be larger than both the first element and last element of the array. |
|
D. |
the elements to be searched must be in sorted order in the array. |
|
E. |
the number of elements in the array must be an even number (otherwise the midpoint will not be found properly). |
|
Answer : D |
Suppose you have an array list of integers, and there are n integers in your list. Based on the code below, the function call sel_sort(list, n) will correctly sort the list in increasing order (from smallest to largest). What change will cause the function call sel_sort(list, n) to correctly sort the list in decreasing order (from largest to smallest). void swap (int *first, int *second) } int swap_location (int a[ ], int k, int n) } void sel_sort (int a[ ], int n) } |
||
A. |
Change line A to: pos = n; |
|
B. |
Change line B to: for ( j = n - 1; j >= k; j = j - 1) |
|
C. |
Change line C to: if (a[j] > a[pos]) |
|
D. |
Change line D to: m = swap_location(a,n,k); |
|
E. |
Change line E to: swap(&a[m], &a[n]); |
|
Answer : C |
Suppose we use binary search to look for a particular value in an array of 1000 numbers. How many elements of the array will the binary search algorithm examine looking for the value? |
||
A. |
2 |
|
B. |
10 |
|
C. |
100 |
|
D. |
500 |
|
E. |
1000 |
|
Answer : B |
What will the following program output? #include <stdio.h> #define MAX 10 void out (int *p, int q) } int main(void) } |
||
A. |
7 8 7 8 14 |
|
B. |
8 7 9 8 14 |
|
C. |
8 7 8 9 13 |
|
D. |
7 8 8 9 13 |
|
E. |
7 8 9 8 13 |
|
Answer : D |
Assuming that it is called with the second argument, n, greater than or equal to zero, what does the following function do? double f(double x, int n) } |
||
A. |
Returns the sum of the first n natural numbers. |
|
B. |
It returns xn. |
|
C. |
Returns the product of the first n odd natural numbers. |
|
D. |
It returns nx. |
|
E. |
None of the above. |
|
Answer : B |
Which of the following assignments will make the expression ((a || !c) && (!b || c)) true? |
||
A. |
I only |
|
B. |
II only |
|
C. |
III only |
|
D. |
I and II only |
|
E. |
I, II, and III |
|
Answer : E |
What value will be returned by the function call mystery(10, 30)? int mystery( int n1, int n2 ) |
||
A. |
0 |
|
B. |
3 |
|
C. |
10 |
|
D. |
1 |
|
E. |
5 |
|
Answer : C |
#include <stdio.h> void main(void) }
What is the output when this program is executed? |
||
A. |
alien space |
|
B. |
space alien |
|
C. |
space space |
|
D. |
alien alien |
|
E. |
alien alien alien |
|
Answer : D |
#define NUM_STUDENT 10 typedef struct { } CSE_142; CSE_142 students[NUM_STUDENT]; Suppose that the above declarations are appropriatly initialized and the array named students is ordered according to decreasing student_id numbers (ie. the student with the highest student_id number is first in the array). Choose the code that would change the grade of the 2nd assignment of the student with the 5th highest student_id to a 4.0.
|
||
A. |
students[4].grades[1] = 4.0; |
|
B. |
students[4]->grades[1] = 4.0; |
|
C. |
students[5].grades[2] = 4.0; |
|
D. |
students[5]->grades[2] = 4.0; |
|
E. |
students[4].grades[2] = 4.0; |
|
Answer : A |
Which of the following will be the certain result of failing properly to fill in your name, student ID, section number, and exam version on your scantron form? |
||
A. |
A score of 0 will be recorded for the multiple choice portion of the final exam, regardless of how many questions you answer correctly. |
|
B. |
Your grade in the course will be lower than it might otherwise be since a 0 will be recorded for the multiple choice portion of the final exam. |
|
C. |
You will earn the gratitude of your classmates by helping to lower the curve, since a 0 will be recorded for the multiple choice portion of the final exam. |
|
D. |
You will need to do exceptionally well on the programming portion of this exam to help offset the 0 that you will earn for the multiple choice portion. |
|
E. |
All of the above |
|
Answer : E |
typedef struct { } point; /*set the x and y coordinates of the point parameter to 0 */ void main(void) What statements should be in the body of the function origin so that the x and y values of corner[0] are set to 0? |
||
A. |
p.x = 0; p.y = 0; |
|
B. |
p[0].x = 0; p[0].y = 0; |
|
C. |
*(p.x) = 0; *(p.y) = 0; |
|
D. |
p->x = 0; p->y = 0; |
|
E. |
&p->x = 0; &p->y = 0; |
|
Answer : D |
Given the following: char baseball (double * player, int number) { } What is the best choice as a prototype for mariners? |
||
A. |
char mariners (int * n); |
|
B. |
void mariners (int &number); |
|
C. |
double mariners (char ch); |
|
D. |
char mariners (int p); |
|
E. |
void mariners (int * number); |
|
Answer : A |
typedef struct { } Student; void find_max(int grades[], int * largest) { } Suppose we have the following variable declaration in main(): Student s; Which of the following would be a correct function call in main? |
||
A. |
find_max(s.scores, &(s.best)); |
|
B. |
find_max(s->scores, s.best); |
|
C. |
find_max(&(s.scores), &s.best); |
|
D. |
find_max(s.scores[ ], s.best); |
|
E. |
find_max(&(s->scores), &(s.best)); |
|
Answer : A |
Which of the following is NOT true of structs? |
||
A. |
can be used to group together values of different types |
|
B. |
can be used as elements of arrays |
|
C. |
can be compared using operators >, <, >=, <=, and == |
|
D. |
can be used as function parameters |
|
E. |
can be returned from functions |
|
Answer : C |
Suppose we have the following line of code in a program: cake[4].ingredient = 17; Which statement best describes cake? |
||
A. |
cake is an integer |
|
B. |
cake is a struct |
|
C. |
cake is an array of integers |
|
D. |
cake is an array of structs |
|
E. |
cake is a 2-dimensional array |
|
Answer : D |
Part II: Programming Questions (30 points)
Problem 1 (10 points) Here's a data set consisting of inches of rainfall on consecutive days: 2 0 1 1 0 0 0 3 1 0 0 0 0 2 0 0 -1 Notice that the longest stretch without any rain was 4 days. Write a program that accepts an arbitrarily large such set (terminated with a negative number to indicate the end of data, as usual) and outputs the longest stretch without rain. For example, if the user entered the numbers 5 0 0 8 0 4 -1 Then the program should output that the longest stretch without rain was 2 days. You should write a complete program here. The program should prompt the user for input, correctly read the input, determine the longest number of days in a row without rain, and output the result. |
#include <stdio.h>
void main (void)
{
}
Problem 2 (10 points) Suppose we have the following data structure definitions for a collection of aliens: typedef struct { } space_alien; int num_aliens; /* number of aliens */ space_alien aliens[100]; /* alien descriptions are stored in */ Fill in the body of the following function so it works as specified. Execution of the assignment statement k = num_above(aliens, 17, 100); should store in k the number of aliens whose y coordinate is strictly greater than 17.
/* yield number of aliens in array a whose y coordinate is */ } |
Problem 3 (10 points) The object of this problem is to write a function that rearranges the contents of an integer array so that all the non-zero elements of the array appear at the beginning of the array (they do not have to be in the same order as in the original array). For example, if array b initially contains the 12 values 3 12 0 -5 16 0 -12 0 6 6 3 4 then, after executing move_zeroes(b,12), array b should contain the same numbers arranged so all the zeroes are at the end of the array: 3 12 -5 16 -12 6 6 3 4 0 0 0 In the example above, the non-zero elements are in the same order as in the original array, but this does not need to be the case in the function you write. Hint: Think about what the array looks like when some, but not all, of the elements have been moved to their final positions. /* Rearrange the contents of array b so that all the non-zero */ /* Everything before cursor is nonzero, everything after zero_pntris zero. We start at beginning of array and step through array one element at a time. If array element is non-zero we move to the next element. If array element is zero we swap it with the last element in the array before the known "zero" list. */ } |