COP3014 Midterm Study
What will be the result of the following code? int b[ 10] = { 1 }; cout << b[1] << endl; - 1 - random value (undefined) - error - 0
0
What is the output of the following code snippet? Pay attention to scope. int count = 0; int sum = 0; for(int count = 0; count < 3; count++) sum += count; cout << count << " " << sum << endl; - error - 2 6 - 0 3 - 2 3
0 3 *Any modification to the count inside the for loop will not affect the count variable outside.
What is the output of the following main function? int main () { int i = 1; int &r = i; int x = r; r = 2; cout << i << " " << r << " " << x; return 0; } - 2 2 1 - 1 2 1 - 2 1 1 - 1 1 1
2 2 1
What is the expected output of the below code snippet? cout << fixed << setprecision(2); //this line let cout to print two digits after the decimal point cout << (static_cast<int> (9.5)/(4.5) << endl; - 2.00 - 2.11 - Error, the code will neither compile nor execute - 2
2.00
What will happen if the following code snippet is executed? void foo(int num) { return 5 * num; } int main() { cout << foo(2) << endl; return 0; } - Syntax error - 2 - 10 - Run-time error, compiles and fails to run
Syntax error *Returning value in a void function
Given the following function, which of the following statement is true? // Make an array with duplicate values int *makeArray(int value, int size) { int array[size]; for (int i=0; i < size; i++) array[i] = value; return array; } - This function has a wrong return type - The function has no problem - This function may cause memory problem because a local array is returned - This function cannot be compiled because of syntax errors
This function may cause memory problem because a local array is returned
Which of the following is true about memory leaks? - To avoid memory leak, you must write destructors for all classes - To avoid memory leak, you must "delete" anything "new"ed - Memory leak will happen on stack memory region - Memory leak is not important because all memory will be released when the program ends
To avoid memory leak, you must "delete" anything "new"ed
Pass by value is the default way of parameter passing. The value is passed and a copy of the value is stored in the formal parameter. True/False
True
C++ programmers concentrate on creating _______, which contain data members and the member functions that manipulate those data members and provide services to clients. - constructors - objects - classes - functions
classes
What is the correct way to release the memory allocated with this statement: int *myArray = new doubl[10]; - delete [] myArray; - delete myArray; - delete myArray[10]; - delete [10] myArray;
delete [] myArray;
Which of the following is NOT a valid wat to define a double-typed two-dimensional array myArr with the capacity to store two rows and three columns? (either local or dynamic is acceptable) - double myArr[2][3] = {1, 3, 4, 7, 4, 9}; - double myArr[2][] = {1, 3, 4, 7, 4, 9}; - double myArr[2][3]; - double myArr[][3] = {1, 3, 4, 7, 4, 9};
double myArr[2][] = {1, 3, 4, 7, 4, 9};
Why of the below is NOT a fundamental control flow in C++? - None of the choices - branch - loop - flowchart
flowchart
Which one is not a legal identifier? - ___1 - foo-bar - elseif - _class
foo-bar *The hyphen symbol is not allowed!
Choose all correct statements about the delete operator: - is how data files get erased in C++ - can only be called on objects - frees memory allocated by new - should only be used with pointers
frees memory allocated by new & should only be used with pointers
The break statement will ____. - get out of the innermost loop - get out all layers of loops - get out of a switch statement - get out of a function
get out of the innermost loop & get out of a switch statement
When we create variables using new keyword they are created in the ____ memory region. - global - stack - static - heap
heap
Which of the following statement is invalid? - int a[2][] = {1, 2, 3, 4, 5, 6}; - int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; - int a[][3] = {1, 2, 3, 4, 5, 6}; - int a[2][3] = {1, 2, 3, 4, 5, 6};
int a[2][] = {1, 2, 3, 4, 5, 6};
Identify all correct ways to round a double variable a, so if a is 3.6 in the beginning, it should become 4.0 after. - a = static_cast<int>(a) + 0.5; - int b = a + 0.5; a = b; - a = static_cast<int>(a + 0.5); - a = a + 0.5;
int b = a + 0.5; a = b; & a = static_cast<int>(a + 0.5);
What is a correct way to create a local 2D array with 2 rows and 3 columns? - int [][] myArray = new int[2][3] - int myArray[2][3]; - int **myArray; myArray = new int *[2]; for(int i = 0; i < 2; i++) myArray[i] = new int[3]; - int **myArray; myArray = new int[2][3];
int myArray[2][3];
To avoid everything in the header file to be included only once, which of the following is used? (XXX used as a place holder for some text) - #include XXX - #import XXX - #ifndef XXX #define XXX ... #endif - #ifndef XXX #include XXX ... #endif
#ifndef XXX #define XXX ... #endif
Statement: You allocate memory for a two-dimensional dynamic array like this: int **twoDimArr = new int[2][4]; True/False
False
A C string is basically an array and can only handle fixed-sized text. True/False
False *C string use \0 to mark the end and can handle variable length content.
A for loop is the best choice if you do not know how many times the loop will iterate. True/False
False *No, do-while or while loop is
You only use return statements in functions when some data needs to be passed to outside as a returned value. True/False
False *No. You use return statements in void functions also.
Instance variables or methods declared with access specifier private are accessible to other methods of the same class and any other functions in the same program besides main(). True/False
False *Not accessible to any other function in the same program besides main().
We can declare multiple destructors for a same class. True/False
False *Zero or one destructor per class
What is the output of the snippet? int n = 0; cout << endl << "The numbers are;" << endl; do { cout << n << "\t"; n++; } while (n <= 100); cout << endl; - Print 0 to 99 - Print 1 to 99 - Print 0 to 100 - Print 1 to 100
Print 0 to 100 *Focus on the first and last round.
Which of the following statement about arrays is true? - An array can be stored many different types of values. - It is an error if the number of initial values provided in [] to initialized an array is more than the size of the array. - An array index should normally be data type float. - If there are fewer initial values provided in {} than the size of the array, the remaining elements are initialized to the last value in the initial value list.
It is an error if the number of initial values provided in [] to initialized an array is more than the size of the array.
Which of the following statements about an identifier is true? - An identifier must begin with a letter. - It consists of letter, digit, hyphen and underscore character - It is case sensitive. - Any identifier whose name satifies the rule can be used
It is case sensitive.
Which of the following is NOT part of a compilation process in C++? - Linking - Loading - Compilation - Preprocessing
Loading
To create an object using the following statement, which is a matching definition of the constructor called below? MyClass obj1 = 10; - MyClass::MyClass(int value): value(value) {} - void MyClass::MyClass(int value) { this -> value = value; } - MyClass::MyClass() { value = 10; } - MyClass(int value) { this -> value = value; }
MyClass::MyClass(int value): value(value) {}
What is NOT true about the following function with pass by reference parameters? int myFunc1(const string &name, int &score); - The "name" parameter cannot be modified in the function body - The "score" parameter can be used to return a value by modifying it in the function body - You will enhance the performance by passing the "name" by reference - You may call the function like: string name = "abc"; cout << myFunc1(name, 13);
You may call the function like string name = "abc"; cout << myFunc1(name, 13);
The process to generate a single value from a given sequence is known as. - aggregrating/reducing - sorting - mapping - filtering
aggregrating/reducing
After the following snippet is executed, which option will be true? int i = 10; int b; int *a = &b; b = i; b ++; (*a)++; i ++; - b is 12 - i is 12 - b is 13 - None of the other choices is correct
b is 12
Which of the following statements will compile? - bool myArr[] = {1, 0, 1}; - boolean myArr[] = {1, 0, 1}; - None of the other choices is correct - Boolean myArr[3] = = {1, 0, 1}; - boolean myArr[];
bool myArr[] = {1, 0, 1};
Which Linux command should be used if you want to change the current directory? - chdir - cd - mkdir - rm
cd
What are correct ways to declare a function to take a two-dimensional array with 2 rows and 3 columns? Select all correct answers. The function will be called as the following: int arr[2][3]; int result = myFunc(arr, 2, 3); - int myFunc(int array[2][3], int rows, int cols); - int myFunc(int array[][3], int rows, int cols); - int myFunc(int array[][], int rows, int cols); - int myFunc(int array[2][3], int rows, int cols); - int myFunc(int **array, int rows, int cols).;
int myFunc(int array[2][3], int rows, int cols); & int myFunc(int array[][3], int rows, int cols); & int myFunc(int **array, int rows, int cols).;
A pointer is a variable that contains as its value the ______ of another variable. - memory address - dimension - name - declaration
memory address
Declaration of a class(class keyword) ensures that, by default, the access specifier of the methods are: - void - None of the other choices - public - private
private
An array of arrays is also referred to as a(n)______________ array. - composite array - joint array - vector - two dimensional array
two dimensional array
For a sorting function that sort an array in place(modifying the given array), which of the following is correct declaration (prototype) of this function? - void sort(int arr[int size]); - void sort(int arr[]); - int * sort(int arr[], int size); - void sort(int *arr, int size);
void sort(int *arr, int size);
What is the expected output of the below code snippet? double input = 1000 - 1000; switch(input) { case 0: cout << "Case 0" << endl; break; case 'A': cout << "Case 1" << endl; break; default: cout << "Default case" << endl; } - Default case - Case 0 - Case 1 - you should not switch on a double-typed value
you should not switch on a double-typed value