Chapter 1 CSC 132

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/37

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:00 AM on 7/28/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

38 Terms

1
New cards

Describe the basic structure of a C++ program.

A basic C++ program consists of: 1) #include directives, 2) using namespace std;, 3) int main(), 4) variable declarations, 5) executable statements, 6) return 0;, and 7) the closing brace }. Execution begins inside main() and ends when return 0 executes.

2
New cards

What is the purpose of #include ?

The #include directive tells the compiler to include a library before compilation. The iostream library provides the definitions for cin, cout, endl, and other standard input/output features.

3
New cards

Why is using namespace std; commonly used?

It tells the compiler to use names from the standard namespace without requiring std:: before every object. Without it you would write std::cout, std::cin, and std::endl.

4
New cards

What is the main() function?

main() is the entry point of every C++ program. Program execution always begins inside main(). It is declared as int main() because it returns an integer value to the operating system.

5
New cards

Why is return 0; used at the end of main()?

return 0; tells the operating system that the program finished successfully. Although modern C++ allows omitting it, many instructors and compilers still expect it.

6
New cards

What is a variable?

A variable is a named memory location that stores a value which may change while a program executes. Variables must be declared before they are used.

7
New cards

What are the rules for naming variables?

A variable must begin with a letter or underscore, may contain letters, numbers, and underscores, cannot contain spaces or special symbols, and cannot use C++ keywords such as int, return, or if.

8
New cards

Compare the common C++ data types.

int stores whole numbers, double stores decimal numbers, char stores a single character inside single quotes, bool stores true or false, and string stores multiple characters inside double quotes.

9
New cards

What is the difference between declaring and initializing a variable?

Declaring creates the variable. Initializing gives it its first value. Example: int age; declares a variable, while int age = 18; both declares and initializes it.

10
New cards

Why should variables always be initialized?

Using an uninitialized variable produces garbage values and results in a logic error that can be difficult to locate because the compiler usually does not detect it.

11
New cards

What is an assignment statement?

An assignment statement stores a value into a variable using the = operator. Example: total = price * quantity; calculates the expression first and stores the result into total.

12
New cards

Explain the difference between = and ==.

The = operator assigns a value to a variable. The == operator compares two values for equality and returns either true or false. Accidentally using = inside an if statement is a common programming mistake.

13
New cards

What are keywords in C++?

Keywords are reserved words that already have a meaning in the language, such as int, if, else, while, return, double, and switch. They cannot be used as variable names.

14
New cards

Explain how cin works.

cin is the standard input stream. It uses the extraction operator (>>) to read values from the keyboard and store them into variables. Example: cin >> age;

15
New cards

Explain how cout works.

cout is the standard output stream. It uses the insertion operator (<<) to display text, variables, expressions, and manipulators to the screen.

16
New cards

What do the << and >> operators mean?

The << operator inserts data into an output stream, while the >> operator extracts data from an input stream. Think of << as sending information out and >> as bringing information in.

17
New cards

What is the purpose of endl and \n?

Both move output to a new line. \n inserts a newline character directly into the output, while endl also flushes the output buffer. For most programs, \n is slightly faster.

18
New cards

What are escape sequences?

Escape sequences begin with a backslash and represent special characters. Common ones include \n for a new line, \t for a tab, \ for a backslash, and \" for a quotation mark.

19
New cards

Explain how multiple values can be read with cin.

Multiple values can be read using a single cin statement. Example: cin >> x >> y >> z; The user enters the values separated by spaces or by pressing Enter.

20
New cards

Explain cascading output with cout.

Several pieces of output can be combined into one statement using multiple << operators. Example: cout << "Area = " << area << "\n"; This improves readability.

21
New cards

What is the difference between integer and floating-point division?

If both operands are integers, the decimal portion is discarded. Example: 5 / 3 equals 1. If at least one operand is a double, the result contains decimals. Example: 5.0 / 3 equals 1.66667.

22
New cards

What determines the type of an arithmetic expression?

If all operands are integers, the result is an integer. If any operand is a double, the entire expression is evaluated as a double.

23
New cards

What is the modulus operator (%)?

The modulus operator returns the remainder after integer division. Example: 17 % 5 equals 2. It only works with integer operands.

24
New cards

List the arithmetic operators in C++.

The arithmetic operators are + for addition, - for subtraction, * for multiplication, / for division, and % for remainder.

25
New cards

Explain operator precedence in arithmetic expressions.

C++ follows PEMDAS. Parentheses are evaluated first, then multiplication, division, and modulus, followed by addition and subtraction. Operators of equal precedence are evaluated from left to right.

26
New cards

What is type casting?

Type casting converts one data type into another. It is commonly used to force integer values to behave as doubles before division.

27
New cards

Why is static_cast() preferred?

static_cast() is the modern, safe C++ method for converting data types. Example: average = static_cast(sum) / count;

28
New cards

What happens if integer division is assigned to a double?

The integer division occurs first, losing the decimal portion, and then the result is converted to a double. Example: double x = 5 / 2; stores 2.0 instead of 2.5.

29
New cards

What are the three major categories of programming errors?

Syntax errors violate C++ grammar and are detected by the compiler. Run-time errors occur while the program executes. Logic errors allow the program to run but produce incorrect results.

30
New cards

What is a syntax error?

A syntax error is a violation of C++ grammar, such as a missing semicolon, unmatched braces, misspelled keyword, or incorrect punctuation.

31
New cards

What is a run-time error?

A run-time error occurs while the program is executing. Examples include dividing by zero, opening a nonexistent file, or using invalid input.

32
New cards

What is a logic error?

A logic error occurs when the program compiles and runs but produces incorrect results because the algorithm is wrong.

33
New cards

Explain the roles of the compiler and linker.

The compiler translates source code into object code. The linker combines object code with precompiled libraries to create the executable program.

34
New cards

What is source code?

Source code is the original C++ program written by the programmer before compilation.

35
New cards

What is object code?

Object code is the machine-language version produced by the compiler before the linker creates the executable.

36
New cards

Why is indentation important in C++?

The compiler ignores most whitespace, but proper indentation greatly improves readability, debugging, and maintenance of programs.

37
New cards

Why should variable declarations appear near the beginning of a function?

Declaring variables near the beginning makes programs easier to read, easier to maintain, and helps prevent accidentally using undeclared variables.

38
New cards

What are good programming practices taught in early C++?

Use meaningful variable names, initialize variables, indent code consistently, use comments when appropriate, avoid magic numbers, separate problem solving from coding, and test programs thoroughly using multiple inputs.