Quiz: C++ Functions Recursion and OOP — 57 questions

Detailed questions and answers

1. Which description best defines a data structure?

A sequence of instructions for solving a problem through computation
A translation process that converts source code into executable machine code
A collection of programming rules for controlling program execution
A logical model for organizing data so operations can be performed effectively

A logical model for organizing data so operations can be performed effectively

Explanation

A data structure models how data is collected and organized to support effective operations. An algorithm, in contrast, provides instructions for solving a problem rather than organizing the data itself.

2. A programmer writes ordered instructions that calculate a solution to a problem; what has the programmer created?

An algorithm that specifies how to solve the problem
A compiler that translates the program into machine language
A programming paradigm that defines the program's overall style
A data structure that determines how values are stored

An algorithm that specifies how to solve the problem

Explanation

An algorithm is a set of instructions or rules for solving a problem or accomplishing a task. A data structure would describe the organization of the values involved, not the problem-solving instructions.

3. Why is C++ considered suitable for system software such as operating systems?

It combines object-oriented programming with procedural C in a multiparadigm language
It restricts programs to object-oriented techniques for consistent system design
It focuses on web-page scripting rather than low-level system implementation
It replaces procedural programming with a single mathematical programming model

It combines object-oriented programming with procedural C in a multiparadigm language

Explanation

C++ combines object-oriented programming with procedural C, making it a multiparadigm language appropriate for system software. The language is not limited to one programming paradigm or primarily designed for web-page scripting.

4. What roles do a text editor and a compiler play in a C++ development environment?

The editor writes source code, while the compiler translates it for the computer
The editor runs executable files, while the compiler diagnoses hardware failures
The editor translates source code, while the compiler writes and formats it
The editor manages program memory, while the compiler designs user interfaces

The editor writes source code, while the compiler translates it for the computer

Explanation

A text editor is used to write C++ source code, and a compiler translates that code into a language the computer can understand. Compilation is not the role of the editor, and editing does not involve hardware diagnosis.

5. Which command sequence correctly compiles and runs a Linux C++ program named hello.cpp?

compilehello.cppcompile hello.cpp followed by executehello.cppexecute hello.cpp
g++−ohellohello.cppg++ -o hello hello.cpp followed by ./hello./hello
g++hello.cppg++ hello.cpp followed by run−ohellorun -o hello
./hello./hello followed by g++−ohellohello.cppg++ -o hello hello.cpp

$$g++ -o hello hello.cpp$$ followed by $$./hello$$

Explanation

The workflow first compiles the source with g++−ohellohello.cppg++ -o hello hello.cpp and then runs the resulting executable with ./hello./hello. Running must follow compilation, and the other commands do not represent the specified workflow.

6. Which structural feature is required in a C++ program?

At least one function named main containing executable statements
A loop statement placed before every function declaration
A user-defined helper function for every executable statement
A separate class definition containing all program instructions

At least one function named main containing executable statements

Explanation

A C++ program requires a function named main, whose body contains executable statements. Helper functions may be added, but they are optional rather than a mandatory part of every program.

7. How does a C++ program use cout to display text or values?

It uses cin with the stream insertion operator <<
It uses cout with the stream insertion operator <<
It uses cin with the stream extraction operator >>
It uses cout with the stream extraction operator >>

It uses cout with the stream insertion operator <<

Explanation

cout is the standard output object and is combined with << to send text or values to the output stream. The operator >> is associated with reading input through cin, not displaying output.

8. Which expression reads keyboard input into a variable in C++?

cout << value
cin << value
cin >> value
cout >> value

cin >> value

Explanation

cin is the standard input object, and the extraction operator >> reads keyboard data into a variable. cout is used for output, typically with the insertion operator <<.

9. Which statement correctly distinguishes primitive and complex C++ data types?

Primitive types are programmer-defined, while complex types are built into the language.
Primitive types require pointers, while complex types cannot contain pointers.
Primitive types represent basic values, while complex types organize or combine data.
Primitive types store text, while complex types represent numerical values.

Primitive types represent basic values, while complex types organize or combine data.

Explanation

Primitive types such as int and char represent basic values, whereas complex types such as arrays and structures organize or combine data. The distractor claiming that primitive types store text reverses the distinction between basic and compound data.

10. What does the expression cin >> word do when reading text containing a space?

It reads the complete line and preserves every space.
It reads the first character and discards the remaining input.
It reads the first word and stops when it reaches whitespace.
It reads the text until a punctuation mark appears.

It reads the first word and stops when it reaches whitespace.

Explanation

The extraction operator reads one word because whitespace terminates formatted string input. Reading a complete line requires getline rather than treating punctuation as the stopping condition.

11. After using formatted input, which sequence correctly reads the next complete line?

getline(cin, variable); followed by cin.ignore();
cin.ignore(); followed by getline(cin, variable);
cin >> getline(cin, variable); followed by cin.ignore();
cin.clear(); followed by cin >> variable;

cin.ignore(); followed by getline(cin, variable);

Explanation

cin.ignore() removes the leftover newline from formatted input, allowing getline to read the intended complete line. Calling getline first can cause it to consume that leftover newline and produce an empty result.

12. Which identifier is valid in C++?

2total
total-value
total_2
class

total_2

Explanation

total_2 begins with a letter and contains only permitted letters, digits, and an underscore. The other choices either begin with a digit, contain a special character, or use a reserved word.

13. What is the primary purpose of a namespace in C++?

To give identifiers a distinct declarative scope
To convert functions into symbolic constants
To determine the numeric base of output
To prevent variables from being initialized

To give identifiers a distinct declarative scope

Explanation

A namespace creates a declarative region that gives names such as functions, variables, and types a distinct scope. It does not control initialization, constant conversion, or output formatting.

14. How can code outside namespace namesp1 access its function named value?

By writing ::namesp1.value()
By writing namesp1::value()
By writing namespace.value()
By writing namesp1->value()

By writing namesp1::value()

Explanation

The scope resolution operator :: qualifies the namespace name, so namesp1::value() accesses the function within that namespace. A period and an arrow are not the operators used for namespace qualification in this form.

15. What does setw(8) affect in a C++ output statement?

The precision of every floating-point value
The number of lines printed by the statement
The field width for the next insertion operation
The numeric base used by subsequent integers

The field width for the next insertion operation

Explanation

The setw() manipulator from <iomanip> sets the field width for the next insertion operation. It does not set floating-point precision, select an integer base, or control line count.

16. Which description best defines a function in C++?

A complete subprogram designed to perform a well-defined task
A storage location reserved for one returned value
A command that transfers control without containing program logic
A library header that declares related variables

A complete subprogram designed to perform a well-defined task

Explanation

A function is a complete subprogram that performs a defined task and may include input, processing, and output logic. A function call transfers control to a function, but it is not the function itself.

17. What distinguishes a library function from a user-defined function?

A library function returns values, whereas a user-defined function performs procedures.
A library function is supplied by the standard library, whereas a user-defined function is written by the programmer.
A library function has parameters, whereas a user-defined function cannot receive arguments.
A library function requires a prototype, whereas a user-defined function cannot have one.

A library function is supplied by the standard library, whereas a user-defined function is written by the programmer.

Explanation

Library functions are predefined and supplied through the standard library, while user-defined functions are created by the programmer. Both categories may have parameters, return values, and prototypes.

18. What does a C++ function prototype provide before the function is implemented?

Its complete processing logic, output statements, and required header
Its local variables, return expression, and executable control flow
Its return type, name, parameter list, and a terminating semicolon
Its call sites, input values, and results during program execution

Its return type, name, parameter list, and a terminating semicolon

Explanation

A prototype declares the function's return type, name, and parameter list, followed by a semicolon, so implementation can occur later. It does not contain the function's executable processing logic.

19. Which header provides character classification and conversion functions such as `isdigit`, `isalpha`, and `tolower`?

<cctype>
<iostream>
<string>
<cstring>

<cctype>

Explanation

The `<cctype>` header supplies functions for testing and converting individual characters, including `isdigit`, `isalpha`, and `tolower`. The `<cstring>` header instead focuses on operations involving C-strings.

20. What distinguishes a C-string from an ordinary character array?

A C-string stores characters in a string object.
A C-string can contain only alphabetic characters.
A C-string ends with a null character.
A C-string requires a length value before its characters.

A C-string ends with a null character.

Explanation

A C-string is a character array whose contents end with a null character, marking the string boundary. A string object is instead an instance of the standard C++ string class.

21. Which operation correctly matches the function or method used for it?

`push_back` copies one character array to another.
`strlen` returns the length of a C-string.
`strcmp` appends one character array to another.
`strcat` inserts a character into a string object.

`strlen` returns the length of a C-string.

Explanation

`strlen` measures the length of a null-terminated C-string. `strcat` appends character arrays, `strcpy` copies a character array, and `push_back` adds a character to a string object.

22. What happens when a function call is executed?

Execution transfers to the called function until it returns or ends.
The function's parameter list is rewritten before execution continues.
The program skips the function body and continues with the next declaration.
The compiler records the function name without executing its statements.

Execution transfers to the called function until it returns or ends.

Explanation

A function call transfers control to the called function, which runs until a return statement or the end of its body. A declaration specifies a function but does not execute it.

23. Which statement correctly describes how a function may be used in a program?

It must receive literal arguments whenever it is called.
It can be called once because its definition is written once.
It can be called only as a separate statement, not within an expression.
It can be called repeatedly with literals, variables, or expressions.

It can be called repeatedly with literals, variables, or expressions.

Explanation

A function can be invoked multiple times, and its arguments may be literals, variables, or parts of larger expressions. Writing a function definition once does not limit how often the function can be called.

24. Which pair of functions can be overloaded in C++?

Functions with different names and identical parameter lists
Functions with the same name but different parameter types
Functions with identical names, parameters, and return types
Functions with the same name and parameters but different return types

Functions with the same name but different parameter types

Explanation

C++ distinguishes overloads by differences in the number or types of parameters. A return type by itself does not provide enough information to distinguish overloaded functions.

25. What is the role of a function template in C++?

It creates a function with fixed types before the template is written.
It executes a function without requiring any particular argument types.
It replaces every function call with a manually written function body.
It defines a generic function whose types are chosen during instantiation.

It defines a generic function whose types are chosen during instantiation.

Explanation

A function template provides a generic function definition, with its concrete types determined when the template is instantiated. An instantiated function uses particular types rather than remaining a generic definition.

26. How is a function template commonly instantiated explicitly?

By placing function values in angle brackets before the template name
By declaring a second function with the same template parameters
By placing template arguments in angle brackets before the function arguments
By adding the desired return type after the function call

By placing template arguments in angle brackets before the function arguments

Explanation

Explicit instantiation uses template arguments in angle brackets after the function name, followed by ordinary function arguments, as in `sum<int>(x, y)`. The template arguments specify types, while the function arguments provide values.

27. Which call instantiates a generic sum template for two `double` values?

`double sum(n, m)`
`sum(n, m, double)`
`sum<int>(n, m)`
`sum<double>(n, m)`

`sum<double>(n, m)`

Explanation

The call `sum<double>(n, m)` selects `double` as the template type for the two parameters and returns their sum. The form `sum<int>(n, m)` instead requests an instantiation using `int`.

28. What makes a function call recursive rather than iterative?

It stores every intermediate result in a table
It divides the input into unrelated subproblems
It repeats a loop while updating a control variable
It calls itself on a smaller problem of the same kind

It calls itself on a smaller problem of the same kind

Explanation

Recursion occurs when a function calls itself to solve a smaller problem of the same nature. Iteration repeats a loop without requiring the function to call itself.

29. Which statement correctly describes mathematical induction?

Assume P(1), prove P(k), then identify a base case
Prove a base case, assume P(k), then prove P(k+1)
Prove P(k+1), assume P(k+1), then prove the base case
Check several values, infer a pattern, then state the result

Prove a base case, assume P(k), then prove P(k+1)

Explanation

Induction establishes a base case, assumes the statement for an arbitrary positive integer k, and uses that assumption to prove it for k+1. The induction hypothesis is the assumption about P(k), not the conclusion about P(k+1).

30. Which formula gives the sum of the first n positive integers?

∑i=1ni=n(n−1)2\sum_{i=1}^{n}i=\frac{n(n-1)}{2}
∑i=1ni=n2+n+1\sum_{i=1}^{n}i=n^2+n+1
∑i=1ni=n(n+1)2\sum_{i=1}^{n}i=\frac{n(n+1)}{2}
∑i=1ni=n22+1\sum_{i=1}^{n}i=\frac{n^2}{2}+1

$$\sum_{i=1}^{n}i=\frac{n(n+1)}{2}$$

Explanation

The induction formula for the sum from 1 through n is n(n+1)2\frac{n(n+1)}{2}. The other expressions do not produce the correct sum for general positive integers.

31. Which recursive definition correctly computes factorial?

fact(n)=n×fact(n+1),fact(1)=1fact(n)=n\times fact(n+1),\quad fact(1)=1
fact(n)=fact(n−1)+n,fact(0)=0fact(n)=fact(n-1)+n,\quad fact(0)=0
fact(n)=fact(n−2)+n,fact(0)=1fact(n)=fact(n-2)+n,\quad fact(0)=1
fact(n)=n×fact(n−1),fact(0)=1fact(n)=n\times fact(n-1),\quad fact(0)=1

$$fact(n)=n\times fact(n-1),\quad fact(0)=1$$

Explanation

Factorial reduces n by one at each recursive call and stops with fact(0)=1fact(0)=1. The recursive case reduces the argument, whereas the base case terminates the computation.

32. Which recurrence and base cases define the Fibonacci sequence in the stated form?

f(n)=f(n−2)−f(n−1),f(0)=1, f(1)=0f(n)=f(n-2)-f(n-1),\quad f(0)=1,\ f(1)=0
f(n)=f(n−1)+n,f(0)=0, f(1)=1f(n)=f(n-1)+n,\quad f(0)=0,\ f(1)=1
f(n)=n×f(n−1),f(0)=1, f(1)=1f(n)=n\times f(n-1),\quad f(0)=1,\ f(1)=1
f(n)=f(n−1)+f(n−2),f(0)=0, f(1)=1f(n)=f(n-1)+f(n-2),\quad f(0)=0,\ f(1)=1

$$f(n)=f(n-1)+f(n-2),\quad f(0)=0,\ f(1)=1$$

Explanation

Fibonacci combines the two preceding values and begins with 0 and 1. This distinguishes it from factorial, which uses one preceding value multiplied by the current argument.

33. How does a recursive digit-sum algorithm reduce a positive integer n?

It sums the digits of ⌊n/10⌋\lfloor n/10\rfloor and adds n mod 10n\bmod 10
It sums the digits of 10n10n and subtracts n mod 10n\bmod 10
It subtracts the last digit from n and adds the original number
It divides n by 10 repeatedly and multiplies the remainders

It sums the digits of $$\lfloor n/10\rfloor$$ and adds $$n\bmod 10$$

Explanation

The recurrence is sum(n)=sum(⌊n/10⌋)+(n mod 10)sum(n)=sum(\lfloor n/10\rfloor)+(n\bmod 10), with sum(0)=0sum(0)=0. The quotient removes the final digit, while the remainder supplies that digit.

34. What should a recursive sorted-array check return for an array with one element?

True only if the element equals the array index
False, because sorting requires at least two elements
True, because an array with zero or one element is sorted
False until a recursive call finds an adjacent pair

True, because an array with zero or one element is sorted

Explanation

The check returns true for arrays containing zero or one element because no inverted adjacent pair can exist. A false result is required when an adjacent pair is out of order.

35. Which recurrence correctly computes the sum of an array with n elements?

sumOfArray(a,n)=a[1]+sumOfArray(a−1,n−1),sumOfArray(a,0)=nsumOfArray(a,n)=a[1]+sumOfArray(a-1,n-1),\quad sumOfArray(a,0)=n
sumOfArray(a,n)=a[0]×sumOfArray(a+1,n−1),sumOfArray(a,0)=0sumOfArray(a,n)=a[0]\times sumOfArray(a+1,n-1),\quad sumOfArray(a,0)=0
sumOfArray(a,n)=a[0]+sumOfArray(a+1,n−1),sumOfArray(a,0)=0sumOfArray(a,n)=a[0]+sumOfArray(a+1,n-1),\quad sumOfArray(a,0)=0
sumOfArray(a,n)=a[n]+sumOfArray(a,n+1),sumOfArray(a,0)=1sumOfArray(a,n)=a[n]+sumOfArray(a,n+1),\quad sumOfArray(a,0)=1

$$sumOfArray(a,n)=a[0]+sumOfArray(a+1,n-1),\quad sumOfArray(a,0)=0$$

Explanation

The recurrence adds the current first element to the sum of the remaining smaller array, and the empty array contributes zero. The recursive call processes the remainder, while a[0]a[0] contributes the current element.

36. How does recursive conversion of a digit string into an integer process its final digit?

It converts the final digit, multiplies it by 10, and adds the shorter prefix
It converts the shorter prefix, multiplies that result by 10, and adds the final digit
It adds the digits in the shorter prefix without shifting their place values
It multiplies the shorter prefix by the final digit before adding the prefix

It converts the shorter prefix, multiplies that result by 10, and adds the final digit

Explanation

The algorithm preserves place value by converting the shorter prefix, multiplying it by 10, and then adding the final digit. Treating the final digit as the part multiplied by 10 would place it incorrectly.

37. What does a recursive palindrome check do after the first and last elements match?

It compares the first element with the next element instead
It recursively checks the sequence between those two matching elements
It reverses the sequence before comparing the next pair
It declares the entire sequence a palindrome without further comparison

It recursively checks the sequence between those two matching elements

Explanation

After matching the first and last elements, the algorithm recursively examines the inner sequence. A mismatch at either end produces false, while matching outer elements require checking the remaining interior.

38. What is the relationship between a class and an object in C++?

A class is memory allocated for one specific instance.
A class is a stored object created from another object.
A class is a blueprint or type used to create objects.
A class is a function that initializes an existing object.

A class is a blueprint or type used to create objects.

Explanation

A class defines the data and functions that objects can have, while an object is an instance created from that class. Treating the class itself as allocated storage confuses the blueprint with the resulting object.

39. What happens to memory when an object is instantiated from a C++ class?

Memory is allocated only when a member function is called.
Memory is allocated for the class but not for its instances.
Memory is allocated for that identifiable object instance.
Memory is reserved for every possible object during class definition.

Memory is allocated for that identifiable object instance.

Explanation

Instantiating an object creates a distinct instance with its own characteristics and allocated memory. Defining the class describes a type but does not create an object instance.

40. Which C++ access level allows a member to be used within the class and by derived classes, but not generally from outside?

Private
Public
Static
Protected

Protected

Explanation

Protected members are accessible within their class and within derived classes. Private members restrict access to the class, while public members can be accessed from outside when the object is visible.

41. A class stores a private width value that outside code must read or change safely; which design best preserves encapsulation?

Replace the width with a protected free function.
Provide public getter and setter member functions.
Declare the width as a public data member.
Copy the width into a global variable.

Provide public getter and setter member functions.

Explanation

A getter can read the private value and a setter can update it through controlled public functions. Making the data member public bypasses the encapsulation that getters and setters are intended to preserve.

42. Which description correctly identifies a C++ constructor?

A class-named member function with no return type that initializes a new object.
A class-named function returning void that is called after initialization.
A global function with the same name as a variable that allocates memory.
A public function with an integer return type that updates an existing object.

A class-named member function with no return type that initializes a new object.

Explanation

A constructor has the class name, has no return type, and runs to initialize an object during creation. Declaring it with void would incorrectly treat it like an ordinary member function.

43. When does a C++ constructor normally execute?

Automatically during creation of a new object
After an explicit call from another member function
Whenever a public data member is read
At the end of each program execution

Automatically during creation of a new object

Explanation

A constructor executes automatically as the object is created and normally runs once for that creation. An ordinary member function, by contrast, runs when code explicitly calls it.

44. What is a default constructor in C++?

A constructor that takes no parameters when an object is declared without arguments
A constructor that accepts one parameter with a compiler-chosen value
A constructor that returns a default object through a function call
A constructor that runs after an object receives initialization arguments

A constructor that takes no parameters when an object is declared without arguments

Explanation

A default constructor takes no parameters and is selected when an object is declared without initialization arguments. A constructor requiring arguments represents a different initialization form.

45. Which statement distinguishes a constructor from an ordinary member function?

A constructor initializes global data and receives a return value from the compiler.
A constructor initializes a newly created object and has no return value, including void.
A constructor returns void after an object is created and can be called like any method.
A constructor modifies an existing object and must declare the class as its return type.

A constructor initializes a newly created object and has no return value, including void.

Explanation

Constructors initialize objects during creation and do not declare any return type, not even void. Ordinary member functions may be called explicitly and must have a declared return type.

46. What does the declaration `Rectangle rectc();` mean in C++?

It creates a Rectangle object named rectc with the default constructor.
It creates a function object that stores a default Rectangle value.
It declares a Rectangle reference named rectc without initialization.
It declares a function named rectc that returns a Rectangle.

It declares a function named rectc that returns a Rectangle.

Explanation

An empty pair of parentheses after the name is interpreted as a function declaration, so rectc is a function returning Rectangle. It does not create an object or invoke the default constructor in this form.

47. Which set lists valid C++ forms for invoking a constructor during object initialization?

A return statement, a cast operator, a namespace, and a reference
Functional parentheses, assignment with an equal sign, braces, and equal-sign braces
Square brackets, a colon, a comma, and a pointer dereference
A function call, a loop header, a conditional expression, and a macro

Functional parentheses, assignment with an equal sign, braces, and equal-sign braces

Explanation

Constructor initialization can use functional form, assignment initialization, uniform initialization with braces, or assignment-style uniform initialization with an equal sign and braces. The other forms are not the stated initialization syntaxes.

48. When a constructor uses `: width(x), height(y)` before its body, when are `width` and `height` initialized?

Before the constructor body begins
When the object is first accessed
After the constructor body has finished
During the program's linking stage

Before the constructor body begins

Explanation

A member initialization list constructs or initializes data members before control enters the constructor body. Assigning values inside the body happens later and is not equivalent to member initialization.

49. What happens to a member object that is omitted from a constructor's member initialization list?

It is created after the constructor body has completed
It remains uninitialized until a method accesses it
It is default-constructed before the constructor body runs
It is constructed with the constructor's first argument

It is default-constructed before the constructor body runs

Explanation

An omitted member object is default-constructed before the constructor body executes. Supplying it in the initialization list instead allows construction with specific arguments.

50. What does a declaration such as `Rectangle* prect` represent?

A Rectangle object that stores a pointer as one member
An array containing several Rectangle objects
A pointer that stores the address of a Rectangle object
A function that constructs a Rectangle object dynamically

A pointer that stores the address of a Rectangle object

Explanation

`Rectangle* prect` declares a pointer whose value can be the address of a Rectangle object. It does not itself declare a Rectangle object or an array.

51. If `prect` is a pointer to a Rectangle object, how should its `width` member be accessed?

Use `prect->width` to access the member through the pointer
Use `width->prect` to reverse the member access direction
Use `prect::width` to access the member through the pointer
Use `prect.width` to access the member through the pointer

Use `prect->width` to access the member through the pointer

Explanation

The arrow operator accesses a member through an object pointer, so `prect->width` is appropriate. The dot operator is used when the object itself, rather than a pointer, is available.

52. What is the defining feature of inheritance in object-oriented programming?

A class stores objects but cannot add members of its own
A new class copies unrelated functions without sharing a class relationship
A base class loses its members when a second class is created
A new class reuses and extends another class's fields and methods

A new class reuses and extends another class's fields and methods

Explanation

Inheritance allows a derived class to obtain features from a base class and add its own members. An unrelated class has no inherited members from that base class.

53. Which declaration correctly defines `ChildClass` as publicly derived from `ParentClass`?

`class ChildClass : public ParentClass { };`
`class ParentClass : public ChildClass { };`
`class ChildClass inherits ParentClass { };`
`class ChildClass -> public ParentClass { };`

`class ChildClass : public ParentClass { };`

Explanation

Public inheritance uses the form `class ChildClass : public ParentClass { };`. The child is the derived class, while the parent is the base class whose features are inherited.

54. If `Dog` and `Cat` inherit from `Animal` and define their own sound methods, what behavior does the example produce?

Dog produces “Dog barks” and Cat produces “Cat meows”
Dog produces “Animal sounds” and Cat produces “Animal sounds”
Dog and Cat both use a shared method that produces “Animal sounds”
Dog produces “Cat meows” and Cat produces “Dog barks”

Dog produces “Dog barks” and Cat produces “Cat meows”

Explanation

The example gives each derived class its own sound method, so Dog produces “Dog barks” and Cat produces “Cat meows.” This illustrates derived classes extending a common Animal base class with distinct behavior.

55. How can a C++ program be organized across multiple source files?

By compiling one source file and treating other files as comments
By placing every definition in one header without compilation
By linking headers directly while omitting source-file compilation
By including headers or compiling and linking separate source files

By including headers or compiling and linking separate source files

Explanation

Multiple-file C++ programs may expose declarations through included headers and may also be built by compiling and linking separate source files. Including a header and linking translation units serve related but distinct roles.

56. Which organization is considered good practice for a class spread across files?

Place declarations in several source files without a shared interface
Place the interface in a `.h` file and the implementation in a `.cpp` file
Place the interface in a `.cpp` file and implementations in a separate header
Place prototypes and method implementations together in one header file

Place the interface in a `.h` file and the implementation in a `.cpp` file

Explanation

Separating the class interface in a header from its implementation in a source file is identified as good practice. Mixing method prototypes and implementations in one header is identified as bad practice.

57. What is the standard build sequence for a program containing multiple C++ source files?

Convert every source file into a header before creating object files
Compile only the file with `main`, then include the other files at runtime
Compile each source file into an object file, then link the object files
Link each source file first, then compile the resulting executable

Compile each source file into an object file, then link the object files

Explanation

Each source file is compiled into an object file, and the object files are then linked to create the executable. Linking does not replace the compilation of the individual source files.

Review with flashcards

Memorize the answers with 85 flashcards on C++ Functions Recursion and OOP.

What is a data structure in computing?

A logical or mathematical model for collecting and organizing data.

What defines an algorithm in computer science?

A set of instructions or rules for solving a problem or task.

What programming paradigms does C++ combine?

Object-oriented programming with procedural C.

See flashcards →

Read the study sheet

Read the complete study sheet on C++ Functions Recursion and OOP.

See study sheet →

Similar courses

Create your own quizzes

Import your course and AI generates quizzes with corrections in 30 seconds.

Quiz generator