C++ Final

studied byStudied by 0 people
0.0(0)
learn
LearnA personalized and smart learning plan
exam
Practice TestTake a test on your terms and definitions
spaced repetition
Spaced RepetitionScientifically backed study method
heart puzzle
Matching GameHow quick can you match all your cards?
flashcards
FlashcardsStudy terms and definitions

1 / 41

encourage image

There's no tags or description

Looks like no one added any tags here yet for you.

42 Terms

1

What does it mean to increment something?

To increment something is to increase its value by 1.

New cards
2

What operator is used in C++ to increment something?

The operator in C++ is the ++ (plus-plus) operator.

New cards
3

What does it mean to decrement something?

To decrement something is to decrease its value by 1.

New cards
4

What operator is used in C++ to decrement?

the -- (minus-minus) operator.

New cards
5

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.

New cards
6

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++

New cards
7

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

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

New cards
8

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

The data type will be an integer

New cards
9

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.

New cards
10

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

New cards
11

Name the three different types of loops.

while loop, do-while loop, for loop

New cards
12

while loop

pretest, which means it will evaluate its expression before the loop executes once.

New cards
13

do-while loop

posttest, which means it will evaluate its expression after the loop executes once

New cards
14

for loop

pretest, which means it will evaluate its expression before the loop executes once.

New cards
15

What are the three parts of a for loop definition called

The three parts of a for loop definition are initialization, test, and update.

New cards
16

Initialization

the counter variable is initialized to a value. It executes once, the first time the loop

New cards
17

test

the expression that evaluates to true or false, which controls the execution of the loop

New cards
18

update

the expression that updates the counter variable. It executes at the end of each loop.

New cards
19

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...

New cards
20

What do the following statements evaluate to, given the defined variables?

--------------------------------------------------------------------------

*/

string name1 = "John", name2 = "Jane", name3 = "Devin", name4 = "John ";

name1 < name2; //false

name2 > "Janet"; //false

name3 != "devin"; //true

name3 < "DEVIN"; //false

name1 == name4; //false

New cards
21

What is a reference variable?

A reference variable is a variable that, used as a function parameter, can be changed within the function.

New cards
22

How do reference variables from normal parameters?

This is unlike normal variables, which any changes in a function to the variable will not remain outside the function.

New cards
23

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.

New cards
24

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 x = 10, y = 2;

refVarFunc(x, y);

return 0;

}

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

New cards
25

Global Variables

-variables defined outside of any function
-can be accessed and changed in any function

-can be accessed across the whole program after it is created

New cards
26

Local variables

-defined within a function

-UNLESS passed as parameter, can only be changed and accessed within that function

-only accessible within the function that created it after it is created

New cards
27

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.

Then, write the function prototype for this function in the space above the main function.

*/

//Function header:

//double overtimePay(string name, int hours, double payrate)

/*

New cards
28

What is a function signature?

A function signature is the name of the function and the data types of the function's parameters in proper

New cards
29

What does it mean to overload a function?

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).

New cards
30

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 firstVal = 4;

double secondVal = 3.14;

doSomething(firstVal, secondVal);

The code will display:

….

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

New cards
31

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];

New cards
32

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' };

New cards
33

What is a range-based for loop?

A range-based for loop is a special for loop that is used on "lists" - in our case, arrays.

New cards
34

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

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.

New cards
35

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;

}

New cards
36

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'?
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.

New cards
37

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.

New cards
38

What are the two algorithms used to sort an array?

The bubble sort algorithm and the selection sort algorithm.

New cards
39

Bubble Sort works by…?

starting with the first element, it 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.

New cards
40

Selection Sort works by..?

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.

New cards
41

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}

New cards
42

Given the following array 'jumbled2', what will the array be after the third swap of a bubble sort algorithm?

(Assume the algorithm sorts in ascending order).

int jumbled2[] = { 5, 9, 50, 6, 1, 70, 0 };

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

return 0;

Here are the function definitions for this program

------------------------------------------------*/

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;

}

//******************************************************************************

// The function arrayFunc should display all the elements in the passed array. *

// Then, the function should add 5 to each element in the array. *

//******************************************************************************

void arrayFunc(int array1[], int size)

{

for (int i = 0; i < size; i++)

{

cout << array1[i] << endl;

array1[i] += 5;

}

}

New cards
robot