Final Review c++ 17-a

Ace your homework & exams now with Quizwiz!

What will the following code snippet display? int x; for (x = 5; x <= 14; x += 3) cout << x << endl;

5 8 11 14

Review the below code. How many times will "Hello" display? #include <iostream> using namespace std; int main() { int number = 0; while (number < 5) { cout << "Hello\n"; number++; } return 0; }

5 times

What is the first step of writing a program?

Clearly define what the program is to do.

What will the following code snippet display? int count = 10; do { cout << "Hello World\n"; count++; } while (count < 1);

Hello World do while is postfix, so will always execute at least once

In program design, what type of chart begins with the overall task then refines it into smaller subtasks?

Hierarchy chart

What happens when an argument is passed into a parameter?

Only a copy of the argument's value is passed. Changes to the parameter do not affect the original argument.

What is the purpose of a parameter of a scope in a function?

The scope of a parameter is limited to the body of the function that uses it.

How does bubble sort arrange data in the descending order?

The smaller values move towards the end when comparing 2 sets of data.

Describe the function of selection sort.

The smallest value in the array is located and moved to element 0. Then, the next smallest value is located and moved to element 1. This process continues until all of the elements have been placed in their proper order.

Fix the following statement: char letter = "Z";

char letter = 'Z';

In C++, what type of objects causes a program to wait until data is typed at the keyboard and the Enter key is pressed; no other lines in the program will be executed until input is made?

cin

1 // A well-adjusted printing program 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 cout << "The following items were top sellers" << endl; 8 cout << "during the month of June:" << endl; 9 cout << "Computer games" << endl; 10 cout << "Coffee \n"; 11 cout << "Aspirin \n"; 12 return 0; 13 } Identify the lines that contain stream manipulator in the above program.

line 7-9 "endl;"

According to the text-book, what type of function must exist in every C++ program?

main

Convert the following algebraic expressions into C++ expressions: z = 2wy + 5 A = πr^2 y = (z/3)+(9*k);

z=(2*w*y) + 5 A= (pow(r,2.0))* 3.14 z/3 + 9k = y

What symbols are used to enclose a group of statements, such as the contents of a function?

{ }

ch.2 ---- 1 // A simple C++ program 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 cout << "Programming is great fun!"; 8 return 0; 9 } Identify the comment in the above program.

//A simple C++ program

Refer to below details. What is the output? value = 18.397; cout << setw(2) << value;

18.39

Explain how cin.ignore is used in a program.

to skip one or more characters in the keyboard buffer. cin.ignore(n, c); If used, n is an integer and c is a character. They tell cin to skip n number of characters, or until the character c is encountered. If no arguments are used, cin will skip only the very next character.

In C++ 11 library, which function converts a numeric value to a string object?

to_string

Based on C++ operator precedence, write the mathematical expression to calculate the total of $200 payments in 12 months including interests of 3 percent of annual payments.

totalPayments= (200*12)*0.03 + (200*12); totalPayments = $2472;

What type of functions are provided by the C++ library for converting the case of a character? Describe their roles in case conversion.

toupper: Returns the uppercase equivalent of its argument tolower: Returns the lowercase equivalent of its argument

Write a statement containing a variable val1, which is equal to square root of val2.

val1= sqrt (val2);

Refer to the below variables and write a type cast expression to represent value is equal to total as an integer. double total = 3.7; int value;

value = static_cast(total);

Describe the import points about characters and strings.

-- Printable characters are internally represented by numeric codes. Most computers use ASCII codes for this purpose --Characters normally occupy a single byte of memory. --Strings are consecutive sequences of characters that occupy consecutive bytes of memory. --String literals are always stored in memory with a null terminator at the end. This marks the end of the string. --Character literals are enclosed in single quotation marks. --String literals are enclosed in double quotation marks. --Escape sequences such as '\n' are stored internally as a single character.

Ch.7-- How many elements are in this array? int list[10];

10

What will the following code snippet display? int total = 0; for (int x = 5; x <= 14; x += 3) { total += x; } cout << total << endl;

38

ch.5--- What is the output of the below code snippet? num = 7; cout << num++;

7 because postfix.

Look at the following array definition. int numbers[] = { 2, 4, 6, 8, 10 }; What will the following statement display? cout << *(numbers + 3) << endl;

8

What is the E notation for 87500.11?

8.750011E4

What is the output of the below code snippet? num = 10; cout << --num;

9 because prefix

What is the last element of this array? int list[10];

9 because the array subscripts start at 0

Ch.10--- What header file is required in a C++ program that contains functions for character testing?

<cctype>

What type of file header is used in a C++ program that uses rand or srand function?

<cstlib>

What type of file header is used in a C++ program that uses time function?

<ctime>

1 // A simple C++ program 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 cout << "Programming is great fun!"; 8 return 0; 9 } Identify the header file in the above program.

<iostream>

What type of header files must be included in any program in order to integrate cin object?

<iostream>

What is C-string? What is its purpose?

A C-string is a sequence of characters stored in consecutive memory locations, terminated by a null character. In C++, all string literals are stored in memory as C-strings. Recall that a string literal (or string constant) is the literal representation of a string in a program. In C++, string literals are enclosed in double quotation marks.

Under what circumstances can you successfully return a pointer from a function?

A pointer to an item that was passed into the function as an argument A pointer to a dynamically allocated chunk of memory

Define "&&" operator.

AND connects two expressions into one. Both expressions must be true for the overall expression to be true.

1 // A simple C++ program 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 cout << "Programming is great fun!"; 8 return 0; 9 } Explain the purpose of line 3 of the above program.

Declares that the program will be accessing entities whose names are part of the namespace called std, which organize the names of program entities. The reason the program needs access to the std namespace is because every name created by the iostream file is part of that namespace. In order for a program to use the entities in iostream, it must have access to the std namespace. (creates lists of names you create so it can refer back to the list and use it)

1 // This program calculates the user's pay. 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 double hours, rate, pay; 8 9 // Get the number of hours worked. 10 cout << "How many hours did you work? "; 11 cin >> hours; 12 13 // Get the hourly pay rate. 14 cout << "How much do you get paid per hour? "; 15 cin >> rate; 16 17 // Calculate the pay. 18 pay = hours * rate; 19 20 // Display the pay. 21 cout << "You have earned $" << pay << endl; 22 return 0; 23 } 👽Identify the variable definition statement in the above code.

Double hours, rate, pay;

When is the function prototype used in C++ programming? Why?

Eliminate the need to place a function definition before all calls to the function.

What are the three data types that can represent floating-point numbers in C++ programs?

Float: single precision Double: double precision Long double: larger than double precision

1 // This program calculates the user's pay. 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 double hours, rate, pay; 8 9 // Get the number of hours worked. 10 cout << "How many hours did you work? "; 11 cin >> hours; 12 13 // Get the hourly pay rate. 14 cout << "How much do you get paid per hour? "; 15 cin >> rate; 16 17 // Calculate the pay. 18 pay = hours * rate; 19 20 // Display the pay. 21 cout << "You have earned $" << pay << endl; 22 return 0; 23 } 👽Identify programmer's pre-defined identifiers from lines 5 - 7 of the above code.

Hours, rate, pay

1 // This program calculates the user's pay. 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 double hours, rate, pay; 8 9 // Get the number of hours worked. 10 cout << "How many hours did you work? "; 11 cin >> hours; 12 13 // Get the hourly pay rate. 14 cout << "How much do you get paid per hour? "; 15 cin >> rate; 16 17 // Calculate the pay. 18 pay = hours * rate; 19 20 // Display the pay. 21 cout << "You have earned $" << pay << endl; 22 return 0; 23 } 👽Identify the variables in line 5 - 7 of the above code.

Hours, rate, pay

Write an if/else statement that assigns 0.15 to commissionRate unless sales is greater than or equal to 50000.00, in which case it assigns 0.20 to commissionRate.

If (sales>=50000.00) commisionRate = 0.20; else comissionRate = 0.15 *sales;

Write an IF statement that displays the message "Invalid number" only if value is greater than 50.

If (value>50) cout<< "Invalid number\n";

Assuming ptr is a pointer to an int, what happens when you add 4 to ptr?

Increment address by 4.

Review the below code. How many times will "Hello" display? #include <iostream> using namespace std; int main() { int number = 0; while (number < 5) { cout << "Hello\n"; } return 0; }

Infinite times

What are the three primary activities of a program?

Input, processing, output

If a variable needs to hold numbers in the range -240,000 to 840,000, what data type would be best?

Int

1 // This program calculates the user's pay. 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 double hours, rate, pay; 8 9 // Get the number of hours worked. 10 cout << "How many hours did you work? "; 11 cin >> hours; 12 13 // Get the hourly pay rate. 14 cout << "How much do you get paid per hour? "; 15 cin >> rate; 16 17 // Calculate the pay. 18 pay = hours * rate; 19 20 // Display the pay. 21 cout << "You have earned $" << pay << endl; 22 return 0; 23 } 👽Identify the keywords in lines 5 - 7 of the above code.

Int, double. Keywords you cannot use for variable names. They have a specific meaning in C++.

What are the three elements in a count-controlled or a for loop?

It must initialize a counter variable to a starting value. It must text the counter variable by comparing it to a maximum value. When the counter variable reaches its maximum value, the loop terminates. It must update the counter variable during each iteration. This is usually done by incrementing/decrementing the variable.

What is the purpose of const keyword in a parameter declaration?

It prevents a function from making changes to an array argument.

1 // A simple C++ program 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 cout << "Programming is great fun!"; 8 return 0; 9 } Explain the purpose of line 8 of the above program.

It sends the integer value 0 back to the operating system upon the program's completion. The value 0 usually indicates that a program executed successfully.

What happens when an argument is passed into a parameter as a reference?

It shows a function to access the parameter's original argument. changes to the parameter are also made to the argument.

Refer to the below chart and specify which variable name is legal or illegal: dayofWeek 3Danimation _employee_ID 2010_months Recipe#100

Legal Illegal legal Illegal Illegal

1 // A well-adjusted printing program 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 cout << "The following items were top sellers" << endl; 8 cout << "during the month of June:" << endl; 9 cout << "Computer games" << endl; 10 cout << "Coffee \n"; 11 cout << "Aspirin \n"; 12 return 0; 13 } Identify the lines that contain escape sequence in the above program.

Line 10 & 11 "\n"

ch.1 ---- 1 // This program calculates the user's pay. 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 double hours, rate, pay; 8 9 // Get the number of hours worked. 10 cout << "How many hours did you work? "; 11 cin >> hours; 12 13 // Get the hourly pay rate. 14 cout << "How much do you get paid per hour? "; 15 cin >> rate; 16 17 // Calculate the pay. 18 pay = hours * rate; 19 20 // Display the pay. 21 cout << "You have earned $" << pay << endl; 22 return 0; 23 } 👽Identify the processor from the above code

Line 2 #include <iostream> Iostream is header file includes the library to use in program for input and output

ch.3 --- 1 // A well-adjusted printing program 2 #include <iostream> 3 using namespace std; 4 5 int main() 6 { 7 cout << "The following items were top sellers" << endl; 8 cout << "during the month of June:" << endl; 9 cout << "Computer games" << endl; 10 cout << "Coffee \n"; 11 cout << "Aspirin \n"; 12 return 0; 13 } Identify the lines that contain streaming objects in the above program.

Line 7-11 Lines that contain cout statements

Ch.8--- What type of search compares each element with the value being searched for, and stops when either the value is found or the end of the array is encountered.

Linear search or sequential

1 // This program uses variables and literals. 2 #include <iostream> 3 using namespace std; 4 int main() 5 { 6 int little; 7 int big; 8 little = 2; 9 big = 2000; 10 cout << "The little number is " << little << endl; 11 cout << "The big number is " << big << endl; 12 return 0; 13 } Identify the variables and literals in the above program.

Little=2, big=2000 Little, big are variables 2, 2000 are literals

What is the definition of logical errors?

Mistakes that cause the program to produce erroneous (incorrect) results.

Differentiate between procedural and object-oriented programming.

Procedural programming is centered on the procedure, or function. Object-oriented programming (OOP) is centered on the object, programming element that contains data and the procedures that operate on the data.

What must the function prototype have in each parameter, if any?

The function prototype must list the data type of each parameter.

1 // This program uses variables and literals. 2 #include <iostream> 3 using namespace std; 4 int main() 5 { 6 int little; 7 int big; 8 little = 2; 9 big = 2000; 10 cout << "The little number is " << little << endl; 11 cout << "The big number is " << big << endl; 12 return 0; 13 } What will be the output or display of the above program?

The little number is 2 The big number is 2000

How do you pass an array as an argument to a function?

To pass an array as an argument to a function, pass the name of the array

Write the equivalent statements using combined assignment operators for the following expressions: Total = Total * (y + 9) x = x - (a * 2) b = b % (z - 6)

Total *= y + 9; x -= a * 2; b %= z - 6;

How can you compare string objects in a C++ program?

Use strcmp function to compare string objects, or you may use the <,>,<=,>=, ==, and != relational operators

ch.3--- What is the two-step process to gather user input?

Use the cout object to display a prompt on the screen. Us the cin object to read a value from the keyboard.

ch.4--- Assuming x is 9, y is 11, and z is 13, indicate whether each of the following relational expressions is true or false: a.) x == 7 b.) y != 4 c.) z >= 12

a. false b. true c. true

Look at the following array definition: int numbers[5] = { 1, 2, 3 }; a.) What value is stored in numbers[2]? b.) What value is stored in numbers[4]?

a.) 3 b.) ?

Ch.9--- Look at the following code. int x = 4; int *iptr = &x; a.) What will be displayed if you send the expression *iptr to cout? b.) What happens if you send the expression iptr to cout?

a.) 4 b.) Memory address of x

Refer to below expressions and determine the value of x. int x, y = 5; float z = 3.7; x = y * z;

actual result is 18.5, but x is an integer, which will truncate the result to 18.

Which search algorithm is more efficient, linear and binary?

binary

Use the fixed and setprecision manipulators in an expression to display the tenth digit after the decimal point for the result of question 36.

double value = 18.397; cout<< setw(10)<<fixed<<value;

Is the following a function header or a function call? showResults();

function call

Ch.6--- Is the following a function header or a function call? void showResults()

function header

What will the following code display? int funny = 7, serious = 15; funny = serious % 2; if (funny != 1) { funny = 0; serious = 0; } else if (funny == 2) { funny = 10; serious = 10; } else { funny = 1; serious = 1; } cout << funny << "" << serious << endl;

funny=1 serious=1 output: 1 1

In C++, what type of functions reads an entire line, including leading and embedded spaces, and stores it in a string object?

getline

Write an if statement checks the value in x to determine whether it is in the range of 10 through 50. Hint: Use && operator.

if (x>=10 && x<=50) cout << x << " is in the acceptable range. \n"

Explain the result of using cin.get(); in a C++ program.

pause the screen until the key is pressed and does not need to store the character. it reads the newline character, without giving the user a chance to enter any more input.

Which sorting algorithm is more efficient, bubble or selection?

selection sort


Related study sets

Life Insurance Policy Provisions, Options, and Riders

View Set