Exam 1 Program
Exam 1 Program
Exam 1 Program
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.
Multiple Choice. Mark the one best answer for each question. (2 pts. each)
{22 38 57 26 91 10 63}
ptr_num = 3;
1
4. Given the function definition, which of the following are correct- with arguments 7 and 2.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;
}
A. 5 C. 7
B. 6 D. 8
9. In the following table:
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;
}
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?
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);
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:
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:
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;
return 0;
}
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.
TOAD
O A
A O
DAOT
SQUARE
Q R
U A
A U
R Q
ERAUQS