🌍 C++ Programming · subject

C++ Programming Basic Syntax Syllabus

The Basic Syntax syllabus for C++ Programming in full: 3 chapters and 12 topics, every one listed below, plus 50 Basic Syntax flashcards with answers.

3Chapters
12Topics
50Flashcards

Basic Syntax syllabus: all 3 chapters

Expand any chapter to see its topics and sub-topics.

  1. Variables and Data Types

    3 topics
    • Primitive Data Types
    • User-defined Data Types
    • Type Modifiers
  2. Operators

    6 topics
    • Arithmetic Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Assignment Operators
    • Miscellaneous Operators
  3. Control Structures

    3 topics
    • If-Else Statements
    • Switch Case
    • Loops (For, While, Do-While)

C++ Programming Basic Syntax flashcards — 50 questions with answers

  1. What are the fundamental primitive data types in C++?

    The built-in primitives are: `int` (integer), `char` (character), `bool` (boolean), `float` (single-precision floating point), `double` (double-precision floating point), `void` (valueless), and `wchar_t` (wide character).

  2. What is the typical size (in bytes) and value range of a standard `int` on most modern systems?

    A standard `int` is typically 4 bytes (32 bits), giving a signed range of $-2^{31}$ to $2^{31}-1$, i.e. $-2{,}147{,}483{,}648$ to $2{,}147{,}483{,}647$.

  3. How many bytes does a `char` occupy in C++, and what is guaranteed about its size?

    A `char` is always exactly 1 byte by definition ($\texttt{sizeof(char)} = 1$). The C++ standard guarantees a byte holds at least 8 bits.

  4. What are the only two values a `bool` can hold, and what integer values do they convert to?

    A `bool` holds `true` or `false`. When converted to an integer, `true` becomes $1$ and `false` becomes $0$; any nonzero value converts back to `true`.

  5. What is the difference between `float` and `double` in C++?

    `float` is single-precision (typically 4 bytes, ~7 significant decimal digits), while `double` is double-precision (typically 8 bytes, ~15-16 significant decimal digits). `double` has greater range and precision.

  6. What is the purpose of the `void` type in C++?

    `void` represents the absence of a type/value. It is used as the return type of functions that return nothing, for generic pointers (`void*`), and to indicate a function takes no parameters.

  7. List the four categories of user-defined data types in C++.

    The main user-defined types are: `struct` (structure), `class`, `union`, and `enum` (enumeration). Type aliases via `typedef`/`using` are also user-defined.

  8. What is the key default access-level difference between a `struct` and a `class` in C++?

    In a `struct`, members are `public` by default; in a `class`, members are `private` by default. Otherwise they are functionally equivalent.

  9. What is a `union` in C++ and what is its defining memory characteristic?

    A `union` is a user-defined type whose members all share the same memory location, so only one member holds a valid value at a time. Its size equals the size of its largest member.

  10. What does an `enum` (enumeration) do, and what value does the first enumerator get by default?

    An `enum` defines a type consisting of named integer constants. By default the first enumerator is $0$, and each subsequent one increments by $1$ unless explicitly assigned.

  11. What is a scoped enumeration (`enum class`) and how does it differ from a plain `enum`?

    An `enum class` (scoped enum) does not implicitly convert to `int` and its enumerators must be accessed with scope resolution (e.g. `Color::Red`), preventing name clashes and unsafe conversions that plain `enum` allows.

  12. What is the difference between `typedef` and the `using` alias declaration in C++?

    Both create type aliases. `typedef OldType NewName;` is the classic form; `using NewName = OldType;` is the modern (C++11) form that reads left-to-right and also supports alias templates.

  13. Name the four type modifiers used to alter the meaning of base data types in C++.

    The four modifiers are: `signed`, `unsigned`, `short`, and `long`. They can be combined (e.g. `long long int`).

  14. What is the difference between a `signed` and an `unsigned` integer type?

    `signed` types can hold negative and positive values; `unsigned` types hold only non-negative values but double the positive range. For an $n$-bit type, unsigned range is $0$ to $2^{n}-1$.

  15. What is the minimum size guarantee of `long long int`, and its typical signed range?

    `long long int` is at least 8 bytes (64 bits), giving a signed range of $-2^{63}$ to $2^{63}-1$.

  16. What does the `const` type qualifier do in C++?

    `const` makes an object immutable after initialization; any attempt to modify it is a compile-time error. It is a type qualifier (technically a cv-qualifier) rather than a size modifier.

  17. List the five basic arithmetic operators in C++.

    They are: addition `+`, subtraction `-`, multiplication `*`, division `/`, and modulus `%`.

  18. What does the modulus operator `%` compute, and what type restriction does it have in C++?

    `%` returns the remainder of integer division (e.g. $17 \% 5 = 2$). It works only on integer operands; applying it to `float`/`double` is a compile-time error.

  19. In C++, what is the result of `7 / 2` versus `7.0 / 2`, and why?

    `7 / 2` yields $3$ (integer division truncates toward zero), while `7.0 / 2` yields $3.5$ because at least one operand is floating point, so floating-point division is performed.

  20. What is the difference between the pre-increment `++x` and post-increment `x++` operators?

    `++x` increments `x` then returns the new value; `x++` returns the original value then increments. In an expression, if $x=5$, `++x` gives $6$ while `x++` gives $5$ (with `x` becoming $6$ afterward).

Show the other 30 Basic Syntax flashcards
  1. List the six relational (comparison) operators in C++ and the type of value they produce.

    They are `==` (equal), `!=` (not equal), `<` (less than), `>` (greater than), `<=` (less than or equal), and `>=` (greater than or equal). Each yields a `bool` (`true`/`false`).

  2. What is a common bug caused by confusing `=` and `==` in a condition?

    `=` is assignment while `==` is equality comparison. Writing `if (x = 5)` assigns $5$ to `x` and evaluates to `true` (nonzero), instead of testing whether `x` equals $5$ as `if (x == 5)` intends.

  3. List the three logical operators in C++ and the meaning of each.

    `&&` is logical AND (true only if both operands are true), `||` is logical OR (true if at least one is true), and `!` is logical NOT (inverts the truth value).

  4. What is short-circuit evaluation of `&&` and `||` in C++?

    For `A && B`, if `A` is false, `B` is not evaluated (result is already false). For `A || B`, if `A` is true, `B` is not evaluated. This avoids unnecessary computation and enables guard patterns like `p && p->x`.

  5. List the six bitwise operators in C++.

    They are: AND `&`, OR `|`, XOR `^`, NOT (one's complement) `~`, left shift `<<`, and right shift `>>`.

  6. What does the bitwise XOR operator `^` produce, and what is $5 \char`\^ 3$ in binary?

    XOR yields $1$ in each bit position where the two operands differ. $5 = 101_2$ and $3 = 011_2$, so $5 \oplus 3 = 110_2 = 6$.

  7. What is the arithmetic effect of the left shift `x << n` on a non-negative integer?

    Left-shifting by $n$ bits multiplies the value by $2^{n}$ (assuming no overflow). For example, $3 << 2 = 3 \times 2^{2} = 12$.

  8. What is the arithmetic effect of the right shift `x >> n` on a non-negative integer?

    Right-shifting by $n$ bits performs integer division by $2^{n}$, discarding the low bits. For example, $20 >> 2 = \lfloor 20 / 2^{2} \rfloor = 5$.

  9. What does the bitwise NOT operator `~` do, and what is `~5` for a signed 8-bit representation?

    `~` flips every bit (one's complement). For a signed value, $\texttt{\char`\~}x = -x - 1$, so `~5` $= -6$.

  10. What is the difference between bitwise AND `&` and logical AND `&&`?

    `&` operates bit-by-bit on the binary representations of its operands (e.g. $6 \,\&\, 3 = 2$), while `&&` treats operands as booleans and returns a single `true`/`false` with short-circuiting.

  11. What is the simple assignment operator, and name three compound assignment operators.

    The simple assignment is `=`. Compound assignments combine an operation with assignment, e.g. `+=`, `-=`, `*=`, `/=`, `%=`, `&=`, `|=`, `^=`, `<<=`, `>>=`. For example `x += 3` means `x = x + 3`.

  12. What does the compound assignment `x <<= 2` do?

    It left-shifts `x` by 2 bits and stores the result back in `x`, equivalent to `x = x << 2`, i.e. multiplying `x` by $2^{2} = 4$.

  13. What is the associativity of the assignment operator `=` in C++, and what does `a = b = c` do?

    Assignment is right-associative, so `a = b = c` is evaluated as `a = (b = c)`: `c` is assigned to `b`, and that result is then assigned to `a`.

  14. What does the `sizeof` operator return, and is it evaluated at compile time or run time?

    `sizeof` returns the size in bytes of a type or object as a `size_t`. It is a compile-time operator (its operand is not evaluated), e.g. `sizeof(int)` is typically $4$.

  15. Describe the ternary conditional operator `?:` and its general syntax.

    It is the only ternary operator in C++: `condition ? expr1 : expr2`. If `condition` is true it evaluates to `expr1`, otherwise to `expr2`. Example: `int max = (a > b) ? a : b;`.

  16. What do the address-of `&` and dereference `*` operators do when used as unary operators?

    Unary `&x` yields the memory address (a pointer) of `x`; unary `*p` dereferences a pointer `p` to access the object it points to. They are inverse operations.

  17. What is the purpose of the scope resolution operator `::` in C++?

    `::` accesses a name within a specific scope: a namespace member (`std::cout`), a class's static/member definition (`Class::member`), or the global scope (`::x` when a local hides it).

  18. What is the general syntax and behavior of an `if-else` statement?

    Syntax: `if (condition) { /*A*/ } else { /*B*/ }`. If `condition` evaluates to `true`, block A runs; otherwise block B runs. The `else` clause is optional.

  19. How does an `else if` ladder work, and how many of its blocks execute?

    An `else if` ladder chains conditions: `if(c1){} else if(c2){} else {}`. Conditions are tested top to bottom; the block of the first true condition runs and the rest are skipped, so at most one block executes.

  20. In C++, which rule resolves the 'dangling else' ambiguity?

    An `else` always binds to the nearest preceding unmatched `if` within the same block. Braces `{}` should be used to force a different association.

  21. What is the required syntax of a `switch` statement in C++?

    `switch(expr){ case const1: statements; break; case const2: statements; break; default: statements; }`. The `expr` must be an integral or enum type, and each `case` label must be a compile-time constant.

  22. What is 'fall-through' in a C++ `switch` statement?

    If a `case` block lacks a `break`, execution continues into the next `case`'s statements (fall-through). This is sometimes intentional for grouping cases but is a common source of bugs when accidental.

  23. What is the role of the `default` label in a `switch`, and is it required?

    `default` runs when no `case` label matches the switch expression. It is optional; if omitted and no case matches, the `switch` does nothing. It need not be placed last.

  24. Why can't a `switch` statement branch on a `float`, `double`, or `string` in C++?

    A `switch` requires an integral or enumeration expression and case labels that are integral constant expressions. Floating-point and `std::string` values are not integral constant expressions, so they are disallowed.

  25. What are the four parts of a C++ `for` loop and their execution order?

    Syntax: `for(init; condition; update) { body }`. Order: `init` runs once; then `condition` is tested — if true, `body` runs, then `update` runs, and the condition is re-tested; the loop ends when `condition` is false.

  26. How does a `while` loop differ from a `do-while` loop in C++?

    A `while` loop tests its condition before the body, so the body may run zero times. A `do-while` loop tests the condition after the body, guaranteeing the body runs at least once.

  27. What is the required syntax of a `do-while` loop, including punctuation?

    `do { body } while (condition);` — note the trailing semicolon after the `while(condition)`, which is mandatory and distinguishes it syntactically from a plain `while` loop.

  28. What is the difference between the `break` and `continue` statements inside loops?

    `break` immediately terminates the entire loop and transfers control after it. `continue` skips the rest of the current iteration and jumps to the loop's next condition test (and update step, in a `for` loop).

  29. What is a range-based `for` loop in C++11, and give its syntax.

    It iterates directly over elements of a container/array: `for (auto element : container) { ... }`. Using `auto&` binds by reference to allow modification and avoid copies.

  30. How do you write an intentional infinite loop in C++ using `for` and using `while`?

    Using `for`: `for(;;) { ... }` (all three parts omitted). Using `while`: `while(true) { ... }`. Both loop forever until an internal `break`, `return`, or exception exits them.

C++ Programming Basic Syntax FAQ

What is in the C++ Programming Basic Syntax syllabus?

C++ Programming Basic Syntax is split into 3 chapters: Variables and Data Types, Operators and Control Structures. Together they contain 12 topics.

How many chapters are in C++ Programming Basic Syntax?

3 chapters, containing 12 topics. The largest chapter is Operators, with 6 topics.

Are there flashcards for C++ Programming Basic Syntax?

Yes — a 50-card Basic Syntax deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.