Week 11 - Gemini Flashcards

(38 cards)

1
Q

What is the main purpose of ‘output formatting’ in C++?

A

To control how data (numbers, strings, etc.) is displayed on an output stream (like the console or a file), ensuring it’s readable, aligned, and presented in a specific style (e.g., number of decimal places, base).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which C++ header file must typically be included to use output manipulators that take arguments (e.g., setw, setprecision)?

A

<iomanip>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference between using endl and '\n' for newlines with cout? What additional action does endl perform?

A

Both endl and '\n' insert a newline character. However, endl also flushes the output buffer, ensuring the output is immediately displayed. '\n' typically does not force a flush on its own.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the purpose of the flush manipulator (e.g., cout << flush;)? In what scenario might it be particularly useful?

A

flush forces the contents of the output buffer to be written to the output device immediately. It can be useful in debugging (to ensure print statements appear before a potential crash) or when immediate output is critical, though cerr is often preferred for error messages as it’s unbuffered.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the cerr output stream, and how does it differ from cout in terms of buffering?

A

cerr is the standard error stream, typically directed to the console. Unlike cout, output to cerr is usually unbuffered or flushed automatically after each insertion, making it suitable for error messages that should appear immediately.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the setw(n) manipulator. Does its effect persist for subsequent output operations?

A

setw(n) sets the minimum field width for the next item to be printed to n characters. If the item is shorter, it’s padded (usually with spaces). If longer, the field expands. Its effect is not persistent; it only applies to the immediately following output.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the default justification for output within a field set by setw(n)? Which manipulators can change this?

A

The default justification is usually right-justified. std::left changes it to left-justified. std::right explicitly sets it back to right-justified. These justification manipulators are persistent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the showpos and noshowpos manipulators?

A

showpos forces a plus sign (+) to be displayed before positive numerical values. noshowpos (the default) omits the plus sign for positive values (negative values always show a minus sign).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which manipulators are used to change the numerical base for printing integer values? What are the common bases supported?

A

std::dec (decimal - base 10, default), std::oct (octal - base 8), and std::hex (hexadecimal - base 16). These only affect integer output, not floating-point numbers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Are floating-point values affected when std::oct or std::hex manipulators are active?

A

No, floating-point values are always printed in decimal format, regardless of the active integer base manipulator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Describe the three floating-point output formats in C++ and the manipulators to select them.

A
  1. General (default): Mix of fixed and scientific; uses fixed for small numbers, scientific for large. 2. Fixed (std::fixed): Always displays a number, decimal point, and fractional part (e.g., 123.450). 3. Scientific (std::scientific): Always displays in scientific notation (e.g., 1.234500e+02).
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you revert the floating-point output format to ‘general’ after setting it to fixed or scientific?

A

Using cout.unsetf(std::ios::fixed | std::ios::scientific); (requires using namespace std; or qualifying ios). There isn’t a single ‘general’ manipulator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the setprecision(n) manipulator control for floating-point numbers in (a) general format and (b) fixed or scientific format?

A

(a) General format: n is the maximum number of significant digits displayed. (b) Fixed/Scientific format: n is the number of digits displayed after the decimal point.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the default output precision for floating-point numbers if not set by setprecision?

A

The default precision is typically 6.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of the showpoint and noshowpoint manipulators, especially in ‘general’ floating-point format?

A

showpoint forces the decimal point and any trailing zeros (up to the current precision) to be displayed for floating-point numbers, even if they are whole numbers (e.g., 4.00000). noshowpoint (default) omits unnecessary trailing zeros and the decimal point for whole numbers (e.g., 4).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a ‘namespace’ in C++, and what is its primary purpose?

A

A namespace provides a scope for identifiers (names of types, functions, variables, etc.). Its primary purpose is to prevent name collisions that can occur when different libraries or parts of a large project use the same identifier for different things.

17
Q

How do you define a namespace MyLibrary containing a function void func();?

A

namespace MyLibrary { void func(); /* other declarations/definitions */ }

18
Q

How can you access func() from the MyLibrary namespace? (Show two ways)

A
  1. Using the scope resolution operator: MyLibrary::func(); 2. With a using directive: using namespace MyLibrary; func(); (the latter brings all names from MyLibrary into the current scope).
19
Q

Why is it generally discouraged to use using namespace std; (or any other namespace) in header files?

A

Because header files can be included in many translation units. A using namespace directive in a header file pollutes the global namespace for all files that include it, increasing the risk of name collisions and making it harder to see where names originate.

20
Q

What are the two primary ways C++ handles strings, as mentioned in ‘Last_week.docx’?

A
  1. The C-style character string (a null-terminated array of characters). 2. The std::string class type (part of the C++ Standard Library).
21
Q

What is the key characteristic of a C-style character string in terms of its storage in memory?

A

It is a one-dimensional array of characters that is terminated by a null character (\0).

22
Q

How does the std::string class (from <string> header) generally improve upon C-style strings?

A

std::string objects manage their own memory, automatically handle resizing, provide many useful member functions (e.g., .size(), .length(), .append(), .find()), and support intuitive operations like concatenation with + and assignment with = without the risks of buffer overflows common with C-style string functions.

23
Q

How do you declare a std::string variable message and initialize it with “Hello, C++”? How do you get its length?

A

#include <string> std::string message = "Hello, C++"; int len = message.size(); (or message.length();)

24
Q

How do you concatenate two std::string objects, s1 and s2, into a new string s3?

A

std::string s3 = s1 + s2;

25
What is a `struct` (structure) in C++? What is its main purpose?
A `struct` is a user-defined data type that groups together variables of potentially different data types under a single name. Its main purpose is to represent a record or a collection of related data fields as a single logical unit.
26
Provide the basic syntax for defining a structure named `StudentRecord` with members for name (string) and ID (int).
`struct StudentRecord { std::string name; int studentID; };`
27
If `student1` is a variable of type `StudentRecord`, how do you access its `name` member?
`student1.name` (using the dot `.` operator).
28
If `studentPtr` is a pointer to a `StudentRecord` object (`StudentRecord* studentPtr = &student1;`), how do you access its `name` member using the pointer?
`studentPtr->name` (using the arrow `->` operator).
29
Can structures be passed as arguments to functions? Can they be returned from functions?
Yes, structures can be passed to functions by value or by reference, and functions can return structure types.
30
What is the `typedef` keyword often used for in conjunction with `struct` declarations (especially in C-style C++)?
`typedef` can be used to create an alias for the structure type, avoiding the need to write `struct StructName` every time a variable of that type is declared. E.g., `typedef struct { ... } MyStructType; MyStructType var;` In C++, `struct StructName var;` is already valid without `typedef`.
31
What is the purpose of the `printf()` function in C/C++, and which header is typically required?
`printf()` is used for formatted output to the standard output stream (usually the console). It allows printing characters, strings, integers, floating-point numbers, etc., according to specified format specifiers. Header: `` (C++) or `` (C).
32
Name three common format specifiers used with `printf()` and the data types they correspond to.
`%d` or `%i` for signed integers. `%f` for floating-point numbers (doubles are often promoted). `%s` for C-style strings (null-terminated char arrays). `%c` for single characters. `%lf` for `double` (though `%f` often works for `double` arguments to `printf`).
33
What is the purpose of the `scanf()` function in C/C++, and which header is typically required?
`scanf()` is used for formatted input from the standard input stream (usually the keyboard). It reads data according to specified format specifiers and stores it into variables. Header: `` (C++) or `` (C).
34
When using `scanf()` to read into a variable (e.g., an `int`), why is the address-of operator (`&`) typically used before the variable name (e.g., `scanf("%d", &myInt);`)?
`scanf()` needs the memory address of the variable where it should store the input value. The `&` operator provides this address. (Not needed for C-style string character arrays, as the array name itself decays to a pointer).
35
What is a key difference in type safety between C++ iostreams (`cin`/`cout`) and C-style `scanf`/`printf`?
C++ iostreams are generally more type-safe because the type of the data is known from the variable itself, and operations are often overloaded. With `scanf`/`printf`, the programmer is responsible for ensuring the format specifiers match the actual types of the arguments; mismatches can lead to runtime errors or undefined behavior.
36
What is a 'stream' in the context of C++ file handling?
A stream is an abstraction that represents a flow of data to or from a device (like a file, console, or even memory). It allows programmers to perform I/O operations in a consistent way regardless of the actual device.
37
What is the main difference between `ios::out` and `ios::app` when opening a file for writing with `ofstream`?
`ios::out`: If the file exists, its contents are typically truncated (deleted). If it doesn't exist, it's created. Writing starts at the beginning. `ios::app` (append): If the file exists, new data is written to the end of the file, preserving existing content. If it doesn't exist, it's created.
38
How can you represent a C-style string in memory using an array of characters? What is crucial about its termination?
A C-style string is represented as an array of `char` elements. It is crucial that the sequence of characters is terminated by a null character (`\0`) to mark the end of the string.