Bird
Raised Fist0
C++programming~20 mins

Compilation and execution process in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Compilation and Execution
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a simple C++ program with compilation steps
What is the output of this C++ program when compiled and run?
C++
#include <iostream>
int main() {
    int x = 5;
    int y = 10;
    std::cout << "Sum: " << (x + y) << std::endl;
    return 0;
}
ASum: 510
BSum: 15
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Remember that + operator adds integers, not concatenates strings.
🧠 Conceptual
intermediate
1:30remaining
Identify the compilation stage that converts source code to machine code
Which stage in the C++ compilation process converts the source code into machine code?
APreprocessing
BLinking
CCompilation
DExecution
Attempts:
2 left
💡 Hint
Think about when the code is translated into instructions the computer understands.
❓ Predict Output
advanced
2:30remaining
Output of a program with multiple source files and linking
Given two source files compiled and linked, what is the output when running the program?
C++
// file1.cpp
#include <iostream>
void greet();
int main() {
    greet();
    return 0;
}

// file2.cpp
#include <iostream>
void greet() {
    std::cout << "Hello from linked file!" << std::endl;
}
AHello from linked file!
BCompilation error: greet undefined
CRuntime error: undefined reference
DNo output
Attempts:
2 left
💡 Hint
Both files must be compiled and linked together to find greet.
❓ Predict Output
advanced
2:00remaining
What error occurs if a function is declared but not defined?
What error will the compiler or linker produce for this code?
C++
#include <iostream>
void foo();
int main() {
    foo();
    return 0;
}
ALinker error: undefined reference to foo
BProgram runs with no output
CRuntime error: segmentation fault
DCompilation error: foo not declared
Attempts:
2 left
💡 Hint
Declaration exists but no function body is provided.
🧠 Conceptual
expert
2:30remaining
Order the stages of C++ program compilation and execution
Select the correct order of stages when compiling and running a C++ program.
A1,3,2,4
B2,1,3,4
C3,2,1,4
D1,2,3,4
Attempts:
2 left
💡 Hint
Think about the order from source code to running program.

Practice

(1/5)
1. What is the main purpose of the compilation step in C++ programming?
easy
A. To convert human-readable code into machine code
B. To run the program and show output
C. To write the source code
D. To debug the program automatically

Solution

  1. Step 1: Understand what compilation means

    Compilation changes the C++ source code into machine code that the computer can understand.
  2. Step 2: Differentiate compilation from execution

    Execution runs the compiled machine code, but compilation itself only converts code.
  3. Final Answer:

    To convert human-readable code into machine code -> Option A
  4. Quick Check:

    Compilation = convert code [OK]
Hint: Compilation means converting code, not running it [OK]
Common Mistakes:
  • Confusing compilation with execution
  • Thinking compilation runs the program
  • Believing compilation writes code
2. Which of the following is the correct command to compile a C++ file named main.cpp using g++?
easy
A. compile main.cpp
B. run main.cpp
C. g++ main.cpp -o main
D. execute main.cpp

Solution

  1. Step 1: Recall the g++ compile command syntax

    The g++ compiler uses the command g++ filename.cpp -o outputname to compile.
  2. Step 2: Match the command to the options

    g++ main.cpp -o main matches the correct syntax to compile main.cpp into an executable named main.
  3. Final Answer:

    g++ main.cpp -o main -> Option C
  4. Quick Check:

    g++ compile command = g++ main.cpp -o main [OK]
Hint: Use g++ filename.cpp -o outputname to compile [OK]
Common Mistakes:
  • Using 'run' or 'execute' instead of g++
  • Omitting the -o option
  • Confusing compile and execute commands
3. Consider this C++ program:
#include <iostream>
int main() {
  std::cout << "Hello" << std::endl;
  return 0;
}

What will happen if you try to execute this program without compiling it first?
medium
A. The program will print "Hello"
B. You will get an error saying the program is not found
C. The program will compile automatically and then run
D. The program will run but show no output

Solution

  1. Step 1: Understand execution requires compiled program

    Execution runs the compiled machine code. Without compiling, no executable exists.
  2. Step 2: Predict what happens if you run without compiling

    Trying to run without compiling causes an error because the executable file is missing.
  3. Final Answer:

    You will get an error saying the program is not found -> Option B
  4. Quick Check:

    Execution needs compiled file = error if missing [OK]
Hint: You must compile before running, or get error [OK]
Common Mistakes:
  • Assuming execution auto-compiles
  • Expecting output without compiling
  • Thinking program runs but shows no output
4. You wrote this code in test.cpp:
#include <iostream>
int main() {
  std::cout << "Hi" << std::endl
  return 0;
}

When compiling, you get an error. What is the problem?
medium
A. Wrong return type for main
B. Missing #include <string>
C. Using std::cout incorrectly
D. Missing semicolon after std::endl

Solution

  1. Step 1: Check syntax line by line

    The line with std::cout << "Hi" << std::endl is missing a semicolon at the end.
  2. Step 2: Identify compilation error cause

    Missing semicolon causes a syntax error during compilation, stopping the process.
  3. Final Answer:

    Missing semicolon after std::endl -> Option D
  4. Quick Check:

    Semicolon missing = syntax error [OK]
Hint: Check for missing semicolons after statements [OK]
Common Mistakes:
  • Ignoring missing semicolon errors
  • Thinking #include <string> is needed here
  • Assuming return type is wrong
5. You have two C++ files: main.cpp and helper.cpp. You want to compile both and run the program. Which command correctly compiles and links both files into one executable named app?
hard
A. g++ main.cpp helper.cpp -o app
B. g++ main.cpp -o app helper.cpp
C. g++ -o app main.cpp; g++ helper.cpp
D. compile main.cpp helper.cpp app

Solution

  1. Step 1: Understand compiling multiple files

    To compile multiple source files together, list them all in one g++ command.
  2. Step 2: Check command syntax for linking

    g++ main.cpp helper.cpp -o app correctly lists both files and uses -o to name the executable.
  3. Final Answer:

    g++ main.cpp helper.cpp -o app -> Option A
  4. Quick Check:

    Multiple files compile = list all in one g++ command [OK]
Hint: List all source files in one g++ command to compile [OK]
Common Mistakes:
  • Trying to compile files separately without linking
  • Wrong order or syntax in command
  • Using unknown 'compile' command
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard