1.Consider the following function called duplicate and the main function that calls it. What are the values for x, y and z that are displayed?
void duplicate (int& a, int& b, int c)
{
a*=2;
b*=2;
c*=2;
}
int main ()
{
int x=1, y=3, z=7;
duplicate (z, y, x);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}
A. x = 1, y=6, z=14
B. x = 3, y=6, z=7
C. x = 1, y=3, z=7
D. x = 3, y=6, z=14
2.
The >> operator can be used to write a line to a file.
True
False
3.
An array is a collection of data elements of one or more data types.
True
False
4.
The __________ function will return true if the file you wish to use is ready for input/output operations.
A. open()
B. is_good()
C. good()
D. ready()
5.
Explain what this code does:
int jimmy [3][5];
A. declares a single dimensional array with a size of 35.
B. declares a multi-dimensional array with 3 rows and 5 columns
C. declares a single dimensional array with 3 rows and 5 columns
D. declares a multi-dimensional array with the value 3 in the first index and the value 5 in the second index.
6.
Consider this for loop to walk through all elements of the array of integers called b. Complete the code for the loop so that the number 0 is placed in each element of the array.
int b[ 5 ];
for ( i=0 ; i<5 ; i++ )
{
//place a 0 in each element of the array
}
A. 0 = b[i];
B. b[0] = i;
C. b[i] = 0;
D. int b[] = {0};
7.多选
Explain two things that are wrong with this code and how to fix them:
int sum(x, y)
{
char result;
result = x + y;
return result;
}
A. return value must be int data type, not char
B. You cannot use the + operator with a char data type.
C. The prototype should be:
void sum(x, y);
D. data types are required for x and y
8.
Consider the following code. What is the output?
foo.h:
1
2
3
4
5
int DoSomething(int nX, int nY)
{
return nX + nY;
}
goo.h:
1
2
3
4
5
int DoSomething(int nX, int nY)
{
return nX - nY;
}
main.cpp:
1
2
3
4
5
6
7
8
9
10 #include "foo.h"
#include "goo.h"
#include <iostream> //include iostream
using namespace std;
int main()
{
cout << DoSomething(4, 3);
return 0;
}
A.1
B.7
C.Error occurs due to naming collision
D.Error occurs due to missing class definition
9.
By default, all variables and functions are defined in the global namespace.
True
False
10.
Although the using keyword can be used outside of a function to help resolve every identifier in the file, this is not recommended. Why?
A. it increase the chance of identifiers from multiple namespaces conflicting
B. it makes it extremely difficult to use identifiers from other namespaces, such as the Standard namespace.
C. it decreases the chance of identifiers from multiple namespaces conflicting
D. it is considered bad style because it makes your code sloppier and more difficult to read.
11.
What is the purpose of a constructor?
A. Constructors can be very useful for setting initial values for certain member variables.
B. Constructors can be very useful for controlling access to private member variables by non-member functions.
C. Constructors can be very useful for defining and initializing private member variables.
D. Constructors can be very useful for handling all of the c++ code required in a program.
12.
Consider the following main function. What can you guess about the Line class?
#include "line.h"
int main( )
{
Line line(10.0);
}
A. It has a constructor that has a parameter
B. It has a constructor that has one or more parameters
C. It has a member variable with the same name as the class that can store the value 10.0
D. None of the above
13.
You have a main method with the following statements. Each statement creates an object of the Platypus class, but one sends an argument in parentheses and the other does not. In which circumstances would this be allowed?
Platypus p1("digger");
Platypus p1;
A. When the Platypus class has a single constructor that has a String parameter. When an object is created with a String argument, this constructor is called. When an object is created without a constructor argument, the Java default constructor is called.
B. This will always produce an error. Only one constructor is allowed. Either a constructor with arguments or without arguments, but not both for the same class.
C. When the Platypus class has an overloaded constructor. One constructor of this class would have no parameters. When an object is created without an argument, this constructor is called. The class also has another constructor that has a string parameter. When an object is created with a String argument, this constructor is called.
14.
Assume that myList is a previously declared an initialized array. Assume that the variable length holds the total number of elements in the array. What does the following code block do?
for (int i = 0; i < length; i++) {
cout << myList[i] << " ";
}
A. Displays the total number of elements in the array called myList followed by a space.
B. Displays the data stored before the first element of the array called myList up to the data element stored in the second to last element in the array, separated by a space.
C. Displays each index number of the array called myList, followed by a space, up to the last index number in the array's length.
D. Displays each data element stored in the array called myList, separated by a space.
15.
Which of the following would be a correct way to call (invoke) this method?
void printArray(int array[]) {
for (int i = 0; i < length; i++) {
cout << array[i] << " ";
}
}
A. int result[]={3, 1, 2, 6, 4, 2};
printArray(result);
B. int result[]={3, 1, 2, 6, 4, 2};
printArray({3, 1, 2, 6, 4, 2});
C. int[]{3, 1, 2, 6, 4, 2};
printArray(int[]);
D. int result[];
printArray(int result[]);
16.
Which search algorithm can be described as follows:
Given a collection you try every element in the collection until you have found the element or until you reach the end of the collection.
A. Sequential Search
B. Iterative Binary Search
C. Recursive Binary Search
D. None of the above
17.
You have the following struct defined in the private area of your class:
struct database {
int id_number;
int age;
float salary;
};
database employee;
How would you assign a value to each of the data members in a function belonging to the same class?
A. age = 22;
id_number = 1;
salary = 12000.21;
B. employee->age = 22;
employee->id_number = 1;
employee->salary = 12000.21;
C. employee::age = 22;
employee::id_number = 1;
employee::salary = 12000.21;
D. employee.age = 22;
employee.id_number = 1;
employee.salary = 12000.21;
18.多选
Which of the following are functions of the vector class?
A. isempty
B. push_back
C. push_front
D. size
19.
What is the value of s4 as a result of the following statements:
string s = "yellow";
s4 = s.substr(1);
A. llow
B. ellow
C. yello
D. yellow
20.
Consider the following incomplete main function.
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
//TODO: complete this block of code
}
else cout << "Unable to open file";
return 0;
}
Which statements might be used to complete the if block?
A. myfile >> "This is a line.\n";
myfile >> "This is another line.\n";
myfile.close();
B. myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
C. while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
myfile.close();
}
D. while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
21.
Which of the following classes handles reading from a file?
A. iostream
B. ifstream
C. ofstream
D. inputstream
22. 多选
Passing by reference is also an effective way to allow a function to return more than one value.
True
False
23.
Consider the following program:
int operate (int a, int b)
{
return (a*b);
}
float operate (float a, float b)
{
return (a/b);
}
int main ()
{
int x=5,y=2;
float n=5.0,m=2.0;
cout << operate (x,y);
cout << "\n";
cout << operate (n,m);
cout << "\n";
return 0;
}
Which line(s) of code could be used to call this function (select all that apply):
float operate (float a, float b)
A. int n=5,m=2;
cout << operate (n,m);
B. int n=5,m=2;
cout << operate (n,n);
C. float n=5.0,m=2.0;
cout << operate (n,m);
D. float n=5.0,m=2.0;
cout << operate (n,n);
24.
Consider the following prototype:
void duplicate (int& a, int& b, int& c)
What do the ampersands (&) signify?
A. The ampersands signify that their corresponding arguments are to be passed by reference instead of by value.
B. The ampersands signify that their corresponding arguments are to be passed by value instead of by reference.
C. The ampersands signify that their corresponding arguments are to be passed as a constant (read-only) value.
D. None of these. Ampersands are not allowed in a function prototype.
25.
What is missing from this function?
int divide (int a, int b=2)
{
int r;
r=a/b;
}
A. &r;
B. cout << r;
C. Nothing is missing
D. return r;
26.
Consider the following:
int BinarySearch(int A[], int key, int low, int high)
{
while (low <= high)
{
int m = (low + high) / 2;
if (key < A[m])
{
high = m - 1;
}
else if (key > A[m])
{
low = m + 1;
}
else
{
return m;
}
}
return -1;
}
What is the purpose of iteration in the above code?
A. To test if the original array has a length greater than zero before attempting any other statements.
B. To search between the low and high index numbers as long as there are index numbers between low and high to search.
C. To walk through each element of the array from the first element until the end of the array is reached.
D. To set the middle index point so the search area can be divided in two.
27.多选
Consider the following recursive binary search function:
int search::rBinarySearch(int sortedArray[], int first, int last, int key)
{
if (first <= last) {
int mid = (first + last) / 2; // compute mid point.
if (key == sortedArray[mid])
return mid; // found it.
else if (key < sortedArray[mid])
// Calls itself for the lower part of the array
return rBinarySearch(sortedArray, first, mid-1, key);
else
// Calls itself for the upper part of the array
return rBinarySearch(sortedArray, mid+1, last, key);
}
return -1;
}
Which of the following might be described as the base case(s) for this method to end the recursion?
A. when there are no elements in the specified range of indices (when this statement is false)
if (first <= last)
B. When the midpoint value is less than the target
C. when the value is found in the middle of the range (when this statement is true)
if (key == sortedArray[mid])
D. When the midpoint value is greater than the target (when this statement is true)
else if (key > sortedArray[mid])
28.
Consider the following two approaches to the binary search algorithm using the STL vector:
Approach A
int BinarySearch(vector<int> A, int key)
{
int l = 0;
int r = A.size()-1;
while (l <= r)
{
int m = (l + r) / 2;
if (key < A[m])
{
r = m - 1;
}
else if (key > A[m])
{
l = m + 1;
}
else
{
return count;
}
}
return -1;
}
Approach B
int search::BinarySearch(vector<int> sortedArray, int first, int last, int key)
{
if (first <= last) {
int mid = (first + last) / 2;
if (key == sortedArray[mid])
return mid;
else if (key < sortedArray[mid])
return BinarySearch(sortedArray, first, mid-1, key);
else
return BinarySearch(sortedArray, mid+1, last, key);
}
return -1;
}
Which approach uses recursion? Explain why Approach B has more parameters than Approach A.
A. Approach A uses recursion. Approach B requires the first and last parameters because they are needed in the process of iteration to reduce the search area, either by increasing the value of first or decreasing the value of last in the argument list when the iteration is repeated.
B. Approach B uses recursion. Approach B has first and last parameters because they are not defined locally in the method itself. Either defining these values in the parameters or locally in the method is a matter of preference and has no bearing on the outcome. These values are needed to define the search area, either by increasing the value of first or decreasing the value of last.
C. Both Approach A and Approach B use recursion. The additional parameters are not necessary and should be removed to make the method implementation more readable.
D. Approach B uses recursion. Approach B includes the additional first and last parameters because they are required in the process of recursion to reduce the search area, either by increasing the value of first or decreasing the value of last when a recursive call is made.
29.
Consider the following code. Assume that the program includes iostream and uses namespace std.
Why would you get identifier not found errors?
int main ()
{
int i;
do {
cout << "Type a number (0 to exit): ";
cin >> i;
odd (i);
} while (i!=0);
return 0;
}
void odd (int a)
{
if ((a%2)!=0) cout << "Number is odd.\n";
else even (a);
}
void even (int a)
{
if ((a%2)==0) cout << "Number is even.\n";
else odd (a);
}
A. Because the variable i is not initialized to a valid integer value before it is used.
B. Because the function calls do not match the function definitions.
C. Because variable a is not defined in the body of the even and odd functions.
D. Because the functions even and odd are not declared prior to the function calls. Their prototypes are needed above main:
void odd (int a);
void even (int a);
30.
You have an array of structs that is defined as follows.
struct pairT {
std::string key;
int val;
};
Which of the following could be used to add a new struct to the array called entries in position 0?
A. pairT p;
entries[0] = {key = "Bob"; val = 12345};
B. pairT p;
p.key = "Bob" && p.val = 12345;
entries.0 =p;
C. pairT p;
p = "Bob";
p = 12345;
entries[0] =p;
D. pairT p;
p.key = "Bob";
p.val = 12345;
entries[0] =p;
万分感谢!