Exam 1 Program

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

COSC 1437 (DL)- Spring 2017

Exam 1- Chapters 1-12 & Notes


Total Points: 80

Due: Saturday, March 4th @ 11:59PM. Look at Syllabus/ICR about late work.

Directions: For Questions 1-23, clearly mark answers on a separate word (or notepad) document. See
sample file/directions provided by your professor and submit to the appropriate location under the
MyTCC (BlackBoard) site.

Assume all variables are properly declared- unless otherwise mentioned.

Multiple Choice. Mark the one best answer for each question. (2 pts. each)

1. From the list below, which is an invalid variable name?


A. @4me C. Dollars_4_me
B. _dollars4me D. All are invalid.
2. Given the following array, what would be the array order after two passes of the bubble sort?

{22 38 57 26 91 10 63}

A. {10 22 57 26 91 38 63} C. {22 38 57 26 91 10 63}


B. {10 22 26 38 57 63 91} D. {10 22 26 38 57 63 91}
3. Given the code:

int number, *ptr_num = &number;

what is wrong with the assignment:

ptr_num = 3;

A. You cant assign an int to a pointer.


B. ptr_num is not defined.
C. The number variable must be assigned an address.
D. Nothing is wrong.

1
4. Given the function definition, which of the following are correct- with arguments 7 and 2.0?

int func(int n, double d)


{
int j = n;
double sum = 0;

while(j >= 0)
{
sum += d;
--j;
}

return sum;
}

A. returns 7 * 2
B. returns 7 + 2
C. returns 7!
D. It compiles but computes none of these.
5. How many times does the statement below execute?

int x = 27;
int y = 10;

do
x = x / 3;
while (x >= y);

A. none C. twice
B. once D. three times
6. Suppose you create a structure that has an integer field called partNum. Next, you create an array of
ten elements of objects of this structure. Which of the following is a correct way to access the first
element of the array?
A. (part.partNum)[0] C. part.[0]partNum
B. part.partNum[0] D. part[0].partNum
7. Passing by reference is used if a parameter's data flow is
A. one-way, into the function. C. one-way, out of the function.
B. two-way, into and out of the function. D. B and C above

2
8. What is output by the following code?

int a = 5;
switch(a)
{
case 5: a+=3;
case 6: a--; break;
case 7: a++; break;
}

cout << a << endl;

A. 5 C. 7
B. 6 D. 8
9. In the following table:

int table[3][4] = {3,7,0,2,4,9,8,1,3,6,5,4};

what is the value of table[2][1]?

A. 4 C. 6
B. 7 D. 1
10. With respect to the loop in the following main function, what is missing?

int main()
{
int loopCount;
while (loopCount <= 8)
{
cout << "Hi";
loopCount++;
}
return 0;
}

A. the initialization of the loop control variable


B. the testing of the loop control variable
C. the incrementation of the loop control variable
D. nothing is missing.
11. Consider the following list:

int list[] = {4, 8, 19, 25, 34, 39, 45, 48, 66, 75, 89, 95};

When performing a binary search, the target is first compared with ____.
A. 4 C. 39
B. 25 D. 95

3
12. An assignment of the value of a conditional expression to a variable (x = y ? z: w;) can always
be replaced by
A. a switch statement with assignment statements for its case statements.
B. one or more ifs with else clauses and assignment statements for its true and false
clauses.
C. one or more nested while loops with assignments for the bodies of the loops.
D. All of the above
13. What is wrong with this function?

void Multiply(int x, int y)


{
int z;
z = x * y;

return z;
}

A. You cant have two input arguments. C. Multiply doesnt know what z is.
B. The return type is void. D. There is nothing wrong with it.

14. Consider the statement int list[10][8];. Which of the following about list is true?
A. list has 10 rows and 8 columns.
B. list has 8 rows and 10 columns.
C. list has a total of 18 components.
D. list has a total of 108 components.
15. Indicate which of these remarks about function parameters and arguments is correct.
A. The word parameter is sometimes used as shorthand for formal parameter.
B. Arguments are something that is used to fill in a formal parameter.
C. The first argument fills in the first formal parameter, the second argument fills in the
second formal parameter and so on.
D. All of the above
16. What is the output of the following code fragment?

int n = 1;
while (n <= 5)
cout << n << ' ';
n++;

A. 1 2 3 4 5 C. 1 1 1 forever
B. 1 2 3 4 D. 2 3 4 5
17. Which of the following can be used to initialize a pointer variable?
A. 1 C. "0"
B. NULL D. '0'
18. For a program to create a file object, it must contain the ____ directive.
A. #include <fstream> C. #include <ostream>
B. #include <istream> D. #include <iostream>

4
19. The following code fragment invokes a function named InitToZero:

int alpha[10][20];

InitToZero(alpha);

Which of the following is a valid function heading for InitToZero?


A. void InitToZero(int beta[][]) C. void InitToZero(int beta[][20])
B. void InitToZero(int beta[10][20]) D. B and C above
20. Consider the following statements:

string str1 = "ABCDEFGHIJKLM";


string str2;

After the statement str2 = str1.substr(1,4); executes, the value of str2 is "____".
A. ABCD C. BCD
B. BCDE D. CDE

Short Answer. Clearly mark answers as directed. Partial Credit will be given. (10 @ 2 each)

21. Sort the following list in ascending order using the selection sort. Show the list after each pass of the
loop.

7 2 3 8 9 1

Pass 1: ______________________________

Pass 2: ______________________________

Pass 3: ______________________________

Pass 4: ______________________________

Pass 5: ______________________________

5
22. Suppose the following declarations are in effect:

int a[] = {5, 15, 34, 54, 14, 2, 52, 72};


int *p = &a[1]; *q = &a[5];

A. What is the value of: *(p + 3)?

B. What is the value of: *(q - 3)?

C. What is the value of: q - p?

D. Is the condition p < q true or false?

E. Is the condition *p < *q true or false?

23. Write a C++ program to test a function that dynamically allocates an array of integers. The function
should accept an integer argument indicating the number of elements to allocate. The function should
return a pointer to an array. Output should look similar to below.

Sample Run:

Enter an array size: 6


Here are the values in the array:
Element 0 has the value 0
Element 1 has the value 1
Element 2 has the value 2
Element 3 has the value 3
Element 4 has the value 4
Element 5 has the value 5

Fill in the missing parts of the program below to solve the problem as stated above. Do not add any
additional lines of code. (10 @ 2 each)

6
//ArrayAllocate.cpp

#include <iostream>
using namespace std;

// Function Prototype
___________________ //1

int main()
{
int numElements;
int *pointer = nullptr;
int i;

// Get the array size.


cout << "\nEnter an array size: ";
cin >> numElements;

// Allocate the array.


_____________________ //2

// Fill the array with values.


for (i = 0; i < numElements; i++)
________________ //3
// Display the values.
cout << "Here are the values in the array:\n";
for (i = 0; i < numElements; i++)
cout << "Element " << i << " has the value "
<< pointer[i] << endl;

// Deallocate the array.


__________________ //4
pointer = nullptr;

return 0;
}

int* arrayAllocator(int num)


{
int *arrPtr = nullptr;

// Allocate space for the array.


_____________________ //5

// Return the address of the allocated memory.


return arrPtr;
}

Pre-Test- This part has already been taken. Your score will be added to the exam. (10 pts.)

7
Extra Credit: Implement the following program. Follows same program guidelines and graded on the
same scale as program sets. Submit only your .cpp file- no test runs/folder required. Partial credit given.
(10 points)

Write a C++ program using nested loops to print out a square word pattern as shown below. Input the
word from the keyboard size (1-10). Convert string to uppercase on input. The program must use the C++
class string, and nested loops for full credit. Output should look similar to below.

Sample Runs (2):

Enter a string (size 1-10): toad

TOAD
O A
A O
DAOT

Enter a string (size 1-10): SQUARE

SQUARE
Q R
U A
A U
R Q
ERAUQS

Name the program: BoxWordsXX.cpp, where XX are your initials.

You might also like