CMSC 140 Final Exam Review

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What will be the result of the expression 7 / 2 ? What will be different compared to the expression 7.0 / 2?

3 - it will be different from the second expression, which is 3.5, since because it is integer division any numbers after the decimal are ignored.

What is a function signature? What does it mean to overload a function?

A function signature is the name of the function and the data types of the function's parameters in proper order. A function is overloaded when another function is created with the same name as the first function but with a different parameter list (thusly, a different function signature).

What is a range-based for loop? How does it differ from a normal for loop?

A range-based for loop is a special for loop that is used on "lists" - in our case, arrays. It differs because there is no update or test statement: only the initialization of a variable which will hold the current element in the array, and the array itself. The loop will go through each element in the array in order, and assign the value of the element to the initialized variable in the for loop.

What is a reference variable? When used as function parameters, how do they differ from normal parameters?

A reference variable is a variable that, used as a function parameter, can be changed within the function. This is unlike normal variables, which any changes in a function to the variable will not remain outside the function.

Write the function header for a function called overtimePay, that returns a double and has a parameter list that takes a string called name, an int named hours, and a double named payrate.

Function header: double overtimePay(string name, int hours, double payrate)

What are global and local variables? What are the differences between the two?

Global variables are variables defined outside of any function, and can be accessed and changed in any function. Local variables are defined within a function, and can only be changed and accessed within that function (unless passed as a parameter) Global variables can be accessed across the whole program after it is created, and a local variable is only accessible within the function that created it after it is created

What is the difference in using a increment/decrement operator in postfix or prefix?

In prefix mode, the value is incremented before used in any expression in the same statement. In postfix mode, the value is incremented after used in any expression in the same statement.

What operator is used in C++ to represent division?

The / operator is used to represent division in C++.

Given the following array 'jumbled', what will the array be after the first swap of a selection sort algorithm? (Assume the algorithm sorts in ascending order). int jumbled[] = { 12, 5, 3, 1, 9, 100, 45 };

The array's value will be: {1, 5, 3, 12, 9, 100, 45}

Given the following array 'jumbled2', what will the array be after the third swap of a bubble sort algorithm? (Assume the algorithm sorts in descending order). int jumbled2[] = { 5, 9, 50, 6, 1, 70, 0 };

The array's value will be: {9, 50, 6, 5, 1, 70, 0}

What are the two algorithms used to sort an array? How do they work?

The bubble sort algorithm and the selection sort algorithm. Bubble Sort: The bubble sort algorithm works by, starting with the first element, checks to see if it should go in a place later in the array than the next element over (i.e. if the array is of ints, and is sorting in ascending order, it checks if the first element is greater than the second element). If so, the elements' positions are swapped. It then goes to the next element over and checks again if the two should be swapped. It goes through the array completely and then goes back through until it does not swap any more elements. Selection Sort: The selection sort algorithm works by going through the array and finding the smallest (or largest, if in descending order) value in the array. It swaps it with the first element (if it isn't there already) to put it at the beginning. It then looks for the second smallest value and swaps it into the position after the smallest element. It then looks for the third smallest value, swaps it after the second smallest, and so on.

What will be displayed on the screen after the execution of the following code? int counter = 0; bool moreThan20 = false; while (!moreThan20) { if (counter != 6) { cout << counter << endl; } if (counter == 14) { cout << "...Getting close..." << endl; } else if (counter == 18) { cout << "...Almost there..." << endl; } if (counter > 20) { cout << "...It's over twenty..." << endl; moreThan20 = true; } counter += 2; }

The code will display the following: 2 4 8 10 12 14 ...Getting close... 16 18 ...Almost there... 20 22 ...It's over twenty...

What will be the data type of the result of division between two integers?

The data type will be an integer.

Create an array named 'letters' of type char and initialize it with the values of 'a', 'b', 'c', 'd', and 'z' in one line.

char letters[] = { 'a', 'b', 'c', 'd', 'z' };

Create an array named 'aBunchOfStuff' of type string and with the size of 45. Use a constant integer, named SIZE, to define the size.

const int SIZE = 45; string aBunchOfStuff[SIZE];

Create the code below to open a file named 'testing123.txt' for writing. Save the values 1, 2, 20, and 37 into the file. Each value should be on its own line inside the file. Then close the file.

ofstream outputFile; outputFile.open("testing123.txt"); outputFile << 1 << endl; outputFile << 2 << endl; outputFile << 20 << endl; outputFile << 37 << endl; outputFile.close();

What will the variables 'someNum', 'anotherNum', 'lastNum', and 'aBool' contain after the execution of the following code? int someNum = 2, anotherNum = 3, lastNum = 1; bool aBool; aBool = !(++someNum > (--anotherNum * lastNum++));

someNum: 3 anotherNum: 2 lastNum: 2 abool: false

Given the function definition below (also found after the main function), call the function and pass the variables 'firstVal' and 'secondVal' into the function, respectively. What will be displayed on the screen after the execution of the code? What will the variables 'firstVal' and 'secondVal' contain? void doSomething(int a, double &b) { for (int i = 0; i < a; i++) { cout << "."; b += 0.01; } cout << endl; a += 5; cout << "A variable a is: " << a << endl; } int main() { int firstVal = 4; double secondVal = 3.14; doSomething(firstVal, secondVal); }

.... A variable a is: 9 The value of firstVal after the code will be 4 The value of secondVal after the code will be 3.18

What do the following statements evaluate to, given the defined variables? tring name1 = "John", name2 = "Jane", name3 = "Devin", name4 = "John "; name1 < name2; name2 > "Janet"; name3 != "devin"; name3 < "DEVIN"; name1 == name4;

1. false 2. false 3. true 4. false 5. false

Name the three different types of loops.

1. while loop 2. do-while loop 3. for loop

For each type of loop, state if the loop is pretest or post test, and explain what that means.

1. while loop: pretest, which means it will evaluate its expression before the loop executes once. 2. do-while loop: post test, which means it will evaluate its expression after the loop executes once 3. for loop: pretest, which means it will evaluate its expression before the loop executes once.

What are two algorithms used to search through arrays for a value? How do they work?

The linear search algorithm, and the binary search algorithm The linear search goes from the beginning of the array to the end and checks each element in order to see if it matches the one being looked for. Once it is found, it is returned and the search ends. The binary search starts in the middle of the array (which must be sorted) and checks if the value is either the element it is on (the middle element), or if not which half the value resides in. If the element is not the middle, it finds the half which the value will be in and goes to the middle of that, and repeats until the element is found. Once the element is found, it is returned and the search ends.

What are the three parts of a for loop definition called? What do each of them do?

The three parts of a for loop definition are initialization, test, and update. initialization - the counter variable is initialized to a value. It executes once, the first time the loop executes test - the expression that evaluates to true or false, which controls the execution of the loop update - the expression that updates the counter variable. It executes at the end of each loop.

What does it mean to decrement something? What operator is used in C++ to do this?

To decrement something is to decrease its value by 1. The operator in C++ is the -- (minus-minus) operator.

What does it mean to find the modulus of something? What operator is used in C++ to do this?

To find the modulus is to find the remainder after the division of two numbers. The operator % is used in C++.

What does it mean to increment something? What operator is used in C++ to do this?

To increment something is to increase its value by 1. The operator in C++ is the ++ (plus-plus) operator.

What must you use in the variable name when you create a reference variable?

You must use an ampersand (&) when creating a reference variable, both before the name in the function header and after the data type declaration in the function prototype.

Write a range-based for loop that will display every element in an array. Use the array 'numbers' defined below.

int numbers[] = { 1, 5, 8, 34, 6, 73, 2 }; for (int element : numbers) { cout << element << endl; }

Complete the function 'arrayFunc' defined below the main function. The function should first display every element in the passed array, and then add 5 each element. Don't forget to create the prototype. Then, call the function using the defined array 'numbers2'. What will the values of the elements of 'numbers2' be after you call the function 'arrayFunc'?

void arrayFunc(int array1[], int size) { for (int i = 0; i < size; i++) { cout << array1[i] << endl; array1[i] += 5; } } int main() { int numbers2[] = { 4, 5, 6, 7, 8 }; arrayFunc(numbers2, 5); } The elements will each be 5 more than their original values. That is, the elements will be 9, 10, 11, 12, 13, respectively.

Given the function definition below, what will the value of 'x' and 'y' be after the code executes below? void refVarFunc(int &num1, int num2) { for (int i = 0; i < 5; i++) { num1++; num2 += 2; } } int main() { int x = 10, y = 2; refVarFunc(x, y); return 0; }

x will be 15 after this code is executed, and y will be 2.


Ensembles d'études connexes

Week 4 prokaryotic and eukaryotic cells

View Set

Multicultural Families Final Exam

View Set

2. Ética de la persona (conciencia moral, libertad y responsabilidad, virtudes, las bienaventuranzas)

View Set

BIOL200 - Introduction to Cell Biology and Genetics

View Set

Chapter 9 Milady Nail Structure and Growth

View Set