Quiz 1 — Answers¶
Q1 — B. Preprocessing (handling #include and #define) runs first, then compilation
turns each translation unit into an object file, then linking joins them with the
libraries. Why each order matters: the preprocessor's output is the compiler's input, so
it must run first; the linker needs object files, so it runs last.
Q2 — False. A missing semicolon is a compilation error. The linker only sees object files and never sees your source, so it cannot report a syntax error.
Q3 — C, linking. The declaration satisfies the compiler — it knows the function's name and signature and emits a call to it. The definition is what the linker needs, and it is missing. The clue is the phrasing: a linker says the symbol is undefined (it is referred to but never defined), whereas a compiler says it is undeclared (no declaration was seen at all).
One wrinkle worth knowing before you meet it: this course's toolchain prints only
collect2.exe: error: ld returned 1 exit status and names no symbol, so the phrasing may
not be visible. When that happens, the evidence comes from the object files —
nm -C shows the undefined symbol — which is the technique
Reading Compiler Errors and
Module 16 both demonstrate.
Q4 — 3 then 3.5.
total / 2 divides two ints, so the result is the integer 3 (the fraction is
discarded, not rounded). total / 2.0 has a double operand, so the division happens in
floating point and gives 3.5. The type of the operation is decided by its operands, not by
the type it is later stored in.
Q5 — Class vs object. A class is a type: a description of what data an object has and what operations it offers. An object is one instance of that class, with its own values for the data. Two objects of the same class share the code and own their own state.
Q6 — A, C, D, E. Encapsulation, inheritance, polymorphism and abstraction are the four pillars. Compilation and linking are build stages, not design ideas, so B and F are wrong.
Q7 — A header is copied into every translation unit that includes it. A using
namespace std; in a header therefore injects every name in std into the global
namespace of every file that includes it, silently. That is how count, swap or
distance start colliding with your own functions, and the resulting error appears in a
file that never mentioned std at all. Keep using directives out of headers.
Q8 — A missing semicolon after the std::cout statement. The compiler's message points
one line below the real mistake, at the closing brace, because that is where the statement
became impossible to parse. Fix:
Q9 — It compiles and links; when run, the behaviour is undefined. Dereferencing a null pointer is not a compile-time error — nothing about the pointer's value is known until the program runs. The standard makes no promise about what happens: it might print garbage, crash, or appear to work. "It compiled" and "it worked on my machine" are therefore not evidence that this code is correct.
Q10 — A full-credit answer names two credible types and one defensible exclusion. For
example: types Student (identity and name) and Course (code and title); not a
type a name, because std::string already is one and wrapping it adds a layer with no
behaviour of its own. The exclusion is marked on the reason, not on which thing was chosen
— a design in which everything becomes a type has made no decisions.
Marking. Score one point per question (Q6 is all-or-nothing). Then read Marking Yourself.