C++ 325 Quiz 5 Review, C++ 325 Quiz 4 Review, C++ 325 Quiz 3 Review, C++ 325 Quiz 2 Review, C++ 325 Quiz 1 Review, cecs 325 midterm
Consider this code segment: int x = 10; int y = 20; int z = mystery(x, y); cout << z << "/" << x + y; Also consider this function: int mystery(int a, int &b) { a += 5; b += 5; return a + b; } What prints to the screen?
40/35 When running the code it goes through the mystery function and because y is stored as "int &b" as a reference it affects the integer y even after the mystery function is done.
The function pthread_create( ) has how many parameters?
4
Consider the following C++ code segment: char word[10] = "matrix"; cout << strlen(word); What will be printed to the screen?
6 It gets the length of a string.
Suppose I have a file numbers.dat with the number 1 through 1000 sorted, one number per line. What will be printed on the screen if I type this command sequence: % more numbers.dat | tail -100 | head -20 | tail -1
920. It's telling us to take the the last 100 digits from the tail(end). So we're left with 900-1000. Then we look at the first(head) 20 digits from that. So we're left with 900-920. Then we look at the last(tail) digit from that which is 920.
In which c++ library is strlen( ) found?
<cstring>
/word
How do you search forward for a specific word in VIM?
False
In the C++ language when building a class, header files (.h) must be separate from implementation files (.cpp)
<cstring>
In which c++ library is strlen( ) found?
What statement is true about the C++ language? - It was invented in 1968 - It is interpreted - None of these answers are correct - It is compiled
It is compiled
What is the name of the linux root directory? - ./ - root - / - ../
/ It is the top level directory
Which sorting algorithm is the fastest of those listed: - Bubble Sort - Insertion Sort - Selection Sort
Although Insertion sort is technically faster. The listed algorithms are all about the same because they all are n-squared sort
sptr->age = 49;
Assume the following C++ code segment: struct student { string name; int age; char grade; }; student s1, *sptr; sptr = &s1; s1.name = "MasterGold"; What code would you use to set Master Gold's age to 49?
The correct answer is not listed
Assume the following C++ code segment: struct student { string name; int age; char grade; }; student s1, *sptr; sptr = &s1; s1.name = "MasterGold"; What code would you use to set Master Gold's grade to A?
What is another name for "null terminated character array"
C-string This is because cstring is a null-terminated character array. In other words, a cstring is just a character array that's terminated with the integer value zero, because a null character has an integer value of zero.
You can't assign "cowboy" to name
Consider the following C++ code segment: char name[15] = "PonyExpress"; char * cptr = name; name = "cowboy"; cout << name; Which of the following is true:
6
Consider the following C++ code segment: char word[10] = "matrix"; cout << strlen(word); What will be printed to the screen?
The memory address of a
Consider the following C++ code: int a = 10; int *ptr = &a; cout << ptr; What is printed out?
cout << ptr[5];
Consider the following code segment in C++:: int A[ ] = {0, 1, 2, 3, 4, 5}; int *ptr = A; How would I print 5 to the screen?
15
Consider the following program. What will be the output? #include <iostream> #include <vector> using namespace std; int main() { vector<int> v; for (auto i=0; i<10; i++) v.push_back(i * 5); vector<int>::iterator it = v.begin(); cout << *(it+3); return 0; }
the correct answer is not listed
Consider the following segment of code in C++: int nums[10] = {1,2,3,4,5,6,7,8,9,10}; printArray(nums); Also consider the this function: void printArray(int n[ ]) { for(int i=0, i < n.size(), i++) cout << n[i]; } How many numbers will the printArray( ) function actually print out?
pass by value
Consider the following valid C++ function: void func(char * ptr) { cout << ptr; } What is the passing mode used in the function to pass the parameter ptr?
The code will compile but there is something wrong with this code
Consider this code segment: int nums[10]; nums[10] = 999; Which of the following statements is true?
40/35
Consider this code segment: int x = 10; int y = 20; int z = mystery(x, y); cout << z << "/" << x + y; Also consider this function: int mystery(int a, int &b) { a += 5; b += 5; return a + b; } What prints to the screen?
What is the output of the following program: #include <iostream> using namespace std; void swap(int &x, int y){ int temp = x; x = y; y = temp; } int main() { int a = 10, b = 20; swap (a, b); cout << a << ',' << b<< endl; return 0; } Options: A. 20, 20 B. 20, 10 C. 10, 10 D. 10, 20
The answer is 20, 20. This is because in the function swap we assign a to &x which means that any change we make to x also changes a.
In the C++ language when building a class, header files (.h) must be separate from the implementation files (.cpp). True or False
False
This program will compile and run just fine: #include <iostream> int main() { cout << "Hello World\n"; } True or False
False
When building a class in C++, it is good practice to make the data members public so that other classes can quickly access them. This is what makes C++ a fast language. True or False
False By making data members public, other classes can access and modify them directly, without going through the interface provided by the class, which can lead to unexpected changes
0
How can you move the cursor to the beginning of the current line?
:wq
How can you save changes to a file and exit Vim?
int nums[20];
How do you create an array of 20 integers called nums in C++?
Consider the following program. What is printed? #include <iostream> using namespace std; int x = 10; void print(int y){ cout << y << ":" << x << endl; } int main() { int x = 5; print(x); return 0; } Option: A. 10:10 B. 5:10 C. 10:5 D. 5:5
The answer is 5:10. Since in main we are creating int x and assigning it to 5, it is passed in the parameter as print(x). Int y becomes the 5 from the main and x in the function is 10 since it's a global variable.
What kind of code do computers understand?
Machine Code
#include <iostream> using namespace std; int x = 10; void print(int y){ cout << y << ":" << x << endl; } int main() { x = 5; print(x); return 0; } Option: A. 5:10 B. 5:5 C. 10:5 D. 10:10
The answer is 5:5. This is because x is reassigned to 5 and not made to be a new int variable. Therefore, when x is referred to in the function it is equal to 5.
920
Suppose I have a file numbers.dat with the numbers 1 through 1000 sorted, one number per line. What will be printed on the screen if I type this command sequence: $ more numbers.dat | tail -100 | head -20 | tail -1
deck.push_back(c1);
Suppose there is a class called Card which is a playing card and suppose I have a vector of cards called deck. Consider the following code segment: vector<Card> deck; // create a vector of cards Card c1('A','S'); // create a card ????? // add the card to the deck What is the statement that will add the card to the deck?
What is true about the Collatz Conjecture - also known as the 3n+1 problem? Options: A. It does not work with negative numbers. B. It has been proven that it will converge to 1 for all integers. C. It has been proven that it will converge to 1 for all positive integers. D. The biggest number is INT_MAX
The answer does not work with negative numbers.
If itr is a vector iterator and q is of the type stored in the vector values, then which of the following is a valid expression? Option: A. *itr = q; B. itr = &q C. itr = *q
The answer is *itr = q.
The following program will compile and run without errors. What is the output of the following program? #include <thread> #include <iostream> int mystery(int a, int b, int c, int *d){ *d = a + b + c; return *d / 2; } int main(){ int sum = 0; int *ptr = ∑ std::thread t1(mystery,1,2,3,ptr); t1.join(); std::cout<<sum; return 0; } Options: A. 3 B. 6 C. 0 D. 1
The answer is 6. This is because you are reassigning the reference ptr as *d in the function, and since *d is the same as ptr then any change you make to *d affects ptr in main. int *ptr = ∑ so the sum is he same as *d which is 6.
Which of the following member functions is valid syntax for overloading the operator%( ) in C++? Example: BigInt X, Y(20), Z(3); X = Y % Z; // <--- using the % operator Options: A. void BigInt::operator % (BigInt a, BigInt b) B. void BigInt::operator % (BigInt a) C. BigInt BigInt::operator % (BigInt a) D. BigInt BigInt::operator % (BigInt a, BigInt b)
The answer is BigInt BigInt::operator % (BigInt a).
The 'this' pointer is used in the increment operator. What is the purpose of the 'this' pointer in C++ operator overloading? Options: A. It refers to the object being modified B. It refers to the object being compared C. It refers to the current object D. It refers to the object being assigned
The answer is it refers to the current object. This is because this is this.
Think carefully... What action do you take when calculating the Collatz conjecture for the following code? if (n%2) Options: A. None of these answers are correct B. return 1 C. next n = 3 * n + 1 D. next n = n / 2
The answer is next n = 3 * n + 1. This is because the if statement returns either true or false depending on the input. If the number is odd n%2 will be 1 which is the same as true.
Think carefully... What action do you take when calculating the Collatz conjecture for the following code? if (n%2 == 0) Options: A. next n = 3 * n + 1 B. throw overflow exception C. next n = n / 2 D. return 1
The answer is next n = n / 2. This is because an even number mod 2 is equal to 0 which is true and therefore the function will run.
Think carefully... What action do you take when calculating the Collatz conjecture for the following code? if (n > INT_MAX) Options: A. throw overflow exception B. return 1 C. next n = 3 * n + 1 D. the correct answer is not listed
The answer is not listed.
What is printed to the screen when this program is run: #include <iostream> using namespace std; void toss( ){ throw 42; cout << "hello" << endl; } int main() { try { toss(); } catch ( const char * s){ cout << "world"<< endl; } cout << "Awesome"; return 0; } Options: A. the correct answer is not listed B. awesome C. world awesome D. hello awesome
The answer is not listed. This is because in the toss() function we are throwing an int while trying to catch a const char.
What is printed to the screen when this program is run: #include <iostream> using namespace std; void toss( ){ throw 42; cout << "hello" << endl; } int main() { try { toss(); } catch ( int ){ cout << "world"<< endl; } cout << "awesome"; return 0; } Options: A. the correct answer is not listed B. awesome C. world awesome D. hello awesome
The answer is world awesome. This is because when you try toss(), it throws and integer and so an integer is caught which causes "world" to print. Then after the catch takes place "awesome" is printed.
The following program will print all of the numbers in the vector. Choose the correct statement to replace the yellow comment: #include <iostream> #include <vector> using namespace std; int main() { vector<int> v = {1,2,3,4,5}; for (auto i = v.begin(); i!= v.end(); i++) // choose the correct statement to place here return 0; } Options: A. cout << i << endl; B. cout << *i << endl; C. None of these answers are correct D. cout << &i << endl;
The answer is, cout << *i << endl.
What is operator overloading in C++? Options: A. Overriding existing operators in C++ B. Creating new operators in C++ C. Overriding virtual functions D. Overloading functions with different names
The answer is, overriding existing operators in C++.
Consider this code segment: int nums[10]; nums[10] = 999; Which of the following statements is true? - The code will compile but there is something wrong with this code. - There is nothing wrong with the code - The code will not compile
The code will compile but there is something wrong with this code. 10 is outside the range of the array
What can be understood from the following Linux command and result: $ time sort numbers.dat > sorted.out & real 0m1.593s user 0m5.128s sys 0m0.184s
The command took about 1.6 seconds to run. (Look at real)
Which keyword is used to define an overloaded operator function in C++? Options: A. func B. overload C. class D. operator
The correct answer is 'operator.'
Consider the following working program. What function is being called based on the yellow highlight? #include <iostream> #include "BigInt.h" using namespace std; int main() { BigInt X(10); BigInt Y = X++; // what function is called? cout << X; } Options: A. BigInt BigInt::operator++(int); B. BigInt operator++(int); C. BigInt BigInt::operator++( ); D. BigInt operator++( );
The correct answer is BigInt BigInt::operator++(int).
When using friend functions for operator overloading, which of the following statements is true? Options: A. Friend functions can only access public members of a class B. Friend functions are member functions of a class C. Friend functions can access any data member of a class. D. Friend functions cannot be used for operator overloading
The correct answer is Friend functions can access any data member of a class.
Which one of the following operator cannot be overloaded in C++? Options: A. operator& B. operator[ ] C. operator:: D. operator+
The correct answer is operator::.
cout << *(A + index);
The following C++ function receives an array of integers and a particular index in that array. Index is within the bounds of the passed array. The cout statement will correctly print the desired element. Which of the answers will print the same correct output that is printed by this function? int showElement(int A[ ], int index) { cout << A[index]; }
True
The function pthread_create( ) allows a program to have multiple threads running concurrently.
4
The function pthread_create( ) has how many parameters?
What is the linux command to show the contents of a file? - show - more - cat - There are 2 correct answers listed
There are 2 correct answers listed. cat and more
True
This program will compile and run just fine: #include <iostream> using namespace std; int main() { std::cout << "Hello World\n"; return 0; }
The function pthread_create( ) allows a program to have multiple threads running concurrently. True or False
True
man wc
Using linux commands, how would I find out more about the wc command?
The command took about 1.6 seconds to run
What can be understood from the following Linux command and result: $ time sort numbers.dat > sorted.out & real 0m1.593s user 0m5.128s sys 0m0.184s
Displays a help message
What does the :help command do?
Exits without saving changes
What does the command :q! do?
Undoes the last change
What does the u key do in normal mode?
C-string
What is another name for "null terminated character array"
more, cat
What is the linux command to show the contents of a file?
/
What is the name of the linux root directory?
Line 3 is wrong
What is wrong with the following C++ program: #include using namespace std; int main(){ int x=3, y=5 // line 1 int * ptr; // line 2 int *ptr2 = &y // line 3 y = x; // line 4 ptr = & x; // line 5 cout << x << y << ptr; return 0; }
i
What key is used to enter insert mode in Vim?
Machine Code
What kind of code do computers understand?
echo Hello World
What linux command is used to print "Hello World" on the terminal
It is compiled
What statement is true about the C++ language?
False
When building a class in C++, it is good practice to make the data members public so that other classes can quickly access them. This is what makes C++ a fast language.
yy
Which command is used to copy the current line in normal mode?
x
Which command is used to delete the character under the cursor?
Visual mode
Which mode allows you to navigate and manipulate highlighted text?
Consider the following C++ code segment: char name[15] = "PonyExpress"; char * cptr = name; name = "cowboy"; cout << name; Will the code run and execute properly?
You can't assign "cowboy" to name.
What Linux command is used to generate information about memory usage?
free It outputs a summary of RAM usage, including total, used, free, shared, and available memory and swap space
The following C++ function receives an array of integers and a particular index in that array. Index is within the bounds of the passed array. The cout statement will correctly print the desired element. Which of the answers will print the same correct output? int showElement(int A[ ], int index) { cout << A[index]; }
cout << *(A + index);
Consider the following code segment in C++: int A[ ] = {0, 1, 2, 3, 4, 5}; int *ptr = A; How would I print 5 to the screen?
cout << ptr[5];
What linux command is used to print "Hello World" on the terminal
echo Hello World echo is a command tool that displays lines of text or string which are passed as arguments on the command line
How do you create an array of 20 integers called nums in C++? - int nums = new int[20]; - int nums[20]; - integer nums[20]; - nums[20] <int>
int nums[20];
What is wrong with the following C++ program: #include <iostream> using namespace std; int main(){ int x = 3; // line 1 int * ptr; // line 2 int & y; // line 3 y = x; // line 4 ptr = & x; // line 5 cout << x << y << ptr; return 0; }
line 3 is wrong. This is because it is referencing nothing since y is declared without being initialized.
Using linux commands, how would I find out more about the wc command?
man wc. Man refers to manual
Consider the following valid C++ function: void func(char * ptr) { cout << ptr; } What is the passing mode used in the function to pass the parameter ptr?
pass by value
Assume the following C++ code segment: struct student { string name; int age; char grade; }; student s1, *sptr; sptr = &s1; s1.name = "MasterGold"; What code would you use to set Master Gold's grade to A?
s1.grade = 'A'; or sptr->grade = 'A';
Assume the following C++ code segment: struct student { string name; int age; char grade; }; student s1, *sptr; sptr = &s1; s1.name = "MasterGold"; What code would you use to set Master Gold's age to 49?
sptr -> age = 49; Which means access the age member variable of the student object pointed to by sptr.
Which of the following non-member functions is valid syntax for overloading the addition operator (+) in C++? Example: BigInt X, Y(20), Z(10); X = Y + Z; // <--- using the addition operator Options: A. void operator + (BigInt a) B. BigInt operator + (BigInt a) C. BigInt operator + (BigInt a, BigInt b) D. void operator + (BigInt a, BigInt b)
the correct answer is BigInt operator + (BigInt a, BigInt b).
Consider the following segment of code in C++: int nums[10] = {1,2,3,4,5,6,7,8,9,10}; printArray(nums); Also consider the this function: void printArray(int n[ ]) { for(int i=0, i<n.size; i++) cout << n[i]; } How many numbers will the printArray( ) function actually print out?
the correct answer is not listed. This is because there is no .size function for arrays.
Consider the following C++ code: int a = 10; int *ptr = &a; cout << ptr; What is printed out?
the memory address of a. This is because *ptr is equal to the address of &a