Exam 1 Practice Questions

Tue Sep 16 2025
Updated: Wed Sep 17 2025
Facebook share linkTwitter/X share linkLinkedIn share linkReddit share linkReddit share link

Definitions

  1. Match the term on the left to the definition on the right (Programming Basics).
___Algorithma.
The rules that tell which statements (instructions) are legal, or accepted by the programming language, and which are not.
___High-Level Languageb.
Code written in machine language (0's and 1's).
___Machine Codec.
The starting point of program execution, defined as main().
___Syntaxd.
A clear and ordered set of steps that a computer can follow to solve a problem.
___Program Entry Pointe.
A programming language which abstracts away the intricate details of how the computer handles bits and machine code.
  1. Match the term on the left to the definition on the right (Input/Output/Assignment).
___Output Streama.
The operator used to insert data into an output stream. It places the provided data into cout for display on the terminal.
___Stream Insertion Operatorb.
A mechanism that allows data to be sent from a C++ program to an external destination, usually the terminal (console).
___Assignment Statementc.
An operator used with cin to extract input from the input stream and store it in a variable.
___Input Streamd.
A statement that assigns a value to a variable using the = operator
___Stream Extraction Operatore.
A data flow mechanism that sends information from an external source (e.g., keyboard) into a program.
  1. Match the term on the left to the definition on the right (Data Types and Variables).
___Integral Dataa.
The process of reserving memory space for data or instructions during a program’s execution.
___Floating-Point Datab.
The process of defining a variable by specifying its data type and identifier.
___ASCII (American Standard Code for Information Interchange)c.
Any data type that stores decimal numbers.
___Variabled.
A user-defined name assigned to a variable, function, or other entities in C++.
___Memory Allocatione.
A character encoding standard where each character is mapped to a numerical value (0-255).
___Identifierf.
Any data type that stores whole numbers (without decimals).
___Variable Declarationg.
A name (identifier) given to an address (byte) or grouping of addresses in memory. When a variable is created, memory is allocated.
  1. Match the term on the left to the definition on the right (Arithmetic and Type Conversion).
___Integer Divisiona.
Division where both operands are integers, resulting in an integer with the decimal part discarded.
___Floating-Point Divisionb.
An arithmetic expression that combines integer and floating-point values.
___Mixed Expressionc.
Forcibly converting a value from one data type to another through the use of a cast operator.
___Implicit Typecastd.
Division where at least one operand is a floating-point value, resulting in a decimal value.
___Explicit Typecaste.
The automatic conversion of one data type to another by the compiler, such as converting an integer to a floating-point value in a mixed expression.
  1. Match the term on the left to the definition on the right (<cmath> and <iomanip>).
___pow(x, y)a.
Square Root
___sqrt(x)b.
How many floating-point digits to show
___abs(x)c.
Absolute Value
___floor(x)d.
Sets how many spaces the next piece of data to be output will take at minimum
___ceil(x)e.
Exponent
___setprecision(int)f.
Displays output floating-point numbers in fixed-point notation
___fixedg.
Round Up
___setw(int)h.
Sets the character to fill the blank spaces of `setw()` with
___setfill(char)i.
Round Down
  1. Match the term on the left to the definition on the right (Selection).
___Relational Operatora.
An operator that combines or modifies boolean expressions.
___Logical Operatorb.
A control structure that allows a program to choose between more than 2 possible execution paths.
___One-Way Selectionc.
An operator that allows relational comparisons to be made in a program.
___Two-Way Selectiond.
A control structure that chooses one case from many possible cases based on the value of an integral expression.
___N-Way (Multiple) Selectione.
A single block of code that executes only if a condition is true.
___Switch Statementf.
A control structure that chooses between two possible paths of execution, depending on whether a condition is true or false.

Short Answer

  1. Write the command to compile a program main.cpp.
  1. What is the syntax for the entry point to a program that every C++ program must have?
  1. What would happen if a C++ program did not include a main() function?
  1. What do libraries do?
  1. What is the purpose of the <iostream> library?
  1. What is the purpose of the <cmath> library?
  1. What is the purpose of the <iomanip> library?
  1. For each of the following, mark if the identifier is valid or invalid. If the identifier is invalid say what character makes it invalid.
Mariovalid/invalid______________
1uigivalid/invalid______________
Pe@chvalid/invalid______________
D4isyvalid/invalid______________
Mickey Mousevalid/invalid______________
  1. Name 5 different data types and what type of data can they hold.
  1. What will the following snippet output:
    int a = 2, b = 5; double c = -5.5; cout << static_cast<int>(pow(b / a, a) + abs(c));
  1. What will the following snippet output:

    double a = 8.63216; int b = 18, c = 4; cout << static_cast<int>(a + static_cast<double>(b) / c)`;
  1. What will the following snippet output:
    double a = 10.0, b = 4.0; double c = a / b; cout << setprecision(2) << c << endl;
  1. In the previous question, how can the cout statement be modified to make the setprecision() output the value showing 2 decimal places in fixed-point notation?
  1. Explain the purpose of setw() and how the left and right operators effect it.
  1. What is the difference between one-way, two-way, and n-way selection?
  1. Explain what input failure is and why it might occur.
  1. What data types might input failure occur on?
  1. What boolean value can be checked to see if input failure occurred?
  1. What data types can switch statement expressions be?
  1. What happens if a break statement is left out of a case in a switch statement?
  1. What is the order of operations including parenthesis, arithmetic operators, relational operators, logical operators, and the assignment operator? (Hint: there are 9 levels)
  1. Determine if the following boolean expression results in a true or false result:
    int a = 20.0, b = 3.0; double c = 20.0, d = 3.0; a / b > 6 && c / d > 6
  1. Using the following snippet of code:
    int a = 10, b = 5; cout << "Enter a value: "; cin >> a; if (a % b == 1) { cout << "If" << endl; } else if (a % b == 3) { cout << "Else if" << endl; } else { cout << "Else" << endl; }
    What prints if the user enters: (a) 11, (b) 23, and (c) 17?

Error Code

  1. In the following program fix all the errors (write the fixes directly on the code)

    # include <iostream> using namespace std; int main() { int a = 5 b = a + 10; cout << b << " + 10 = " << b << endl; return 0; }
  2. In the following program fix all the errors (write the fixes directly on the code)

    # include <iostream> using namespace std; int main() { int bottles = 0; double ounces = 5.5; double total = bottles * ounces; cout >> "How many bottles do you have: "; cin cout >> bottles >> " bottles of " >> ounces >> "oz is " >> total >> "oz total\n"; return 0; }
  3. In the following program fix all the errors (write the fixes directly on the code)

    # include <iostream> using namespace std; int main() { double radius = 0.0; const double pi = 3.14159265359; cout << "Radius: "; cin >> radius; double area = pi * pow(radius, 2); cout << setprecision(2) << pi << " * " << radius << "^2 = " << area << endl; return 0; }

Programming Questions

  1. Write a program that reads an integer length in inches, converts the read in length from integer inches to floating-point feet, then from floating-point feet to floating-point yards. Then, output the calculated values using the following output manipulation:

    • The label column (x -> y) should be left justified with a width of 20.
    • The second column (calculated value) should be right justified with a width of 10.
    • Output numbers should always have a precision of 3.

    Note: There are 12 inches in a foot, and 3 feet in a yard.

    Note: No error checking needs to be done.

    Example Program Interaction

    Inches: 275 Inches -> Feet 22.917 Feet -> Yards 7.639
  2. Write a program that reads an integer length in centimeters, converts the read in length from integer centimeters to floating-point meters, then from floating-point meters to floating-point kilometers. Then, output the calculated values using the following output manipulation:

    • The label column (x -> y) should be left justified with a width of 25.
    • The second column (calculated value) should be right justified with a width of 15.
    • Output numbers should always have a precision of 1.
    • The fill of unused width spaces should be a -.

    Note: There are 100 centimeters in a meter, and 1000 meters in a kilometer.

    Note: No error checking needs to be done.

    Example Program Interaction

    Centimeters: 287541 Centimeter -> Meter--------------2875.41 Meter -> Kilometer------------------2.88
  3. Write a program that reads a floating-point side length of a decagon, then error checks the value entered for input failure and that it is greater than 0. If input failure occurs or the input is less than 0, display an error and quit the program. If a valid value is read in the user should then select ’a’ or ’A’ to calculate the area or ’p’ or ’P’ to calculate the perimeter by the program reading in a character. A switch statement should then be used to calculate only the area or perimeter and output the respective result, or output an invalid selection message based on the character the user entered.

    Formulas:
    perimeter=10sperimeter=10s
    area=52a25+25area=\frac{5}{2}a^2\sqrt{5+2\sqrt{5}}

    Example Program Interactions

    Decagon Side Length: 6.8 (A/a)rea -or- (P/p)erimeter: a Area: 355.78
    Decagon Side Length: 6.8 (A/a)rea -or- (P/p)erimeter: P Perimeter: 68.00
    Decagon Side Length: 6.8 (A/a)rea -or- (P/p)erimeter: e Invalid selection
  4. Write a program that reads a floating-point side length of a hexagon, then error checks the value entered for input failure and that it is greater than 0. If input failure occurs or the input is less than 0, display an error and quit the program. If a valid value is read in the user should then select ’a’ or ’A’ to calculate the area or ’p’ or ’P’ to calculate the perimeter by the program reading in a character. A switch statement should then be used to calculate only the area or perimeter and output the respective result, or output an invalid selection message based on the character the user entered.

    Formulas:
    perimeter=6sperimeter=6s
    area=332a2area=\frac{3\sqrt{3}}{2}a^2

    Example Program Interaction

    Decagon Side Length: 7.2 (A/a)rea -or- (P/p)erimeter: a Area: 134.68
    Decagon Side Length: 6.8 (A/a)rea -or- (P/p)erimeter: P Perimeter: 43.20
    Decagon Side Length: 6.8 (A/a)rea -or- (P/p)erimeter: e Invalid selection