Bird
Raised Fist0
C++programming~10 mins

Structure of a C++ program - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to include the standard input-output library.

C++
#include [1]
Drag options to blanks, or click blank then click option'
A<vector>
B<stdio.h>
C<iostream>
D<string>
Attempts:
3 left
💡 Hint
Common Mistakes
Using which is a C library, not standard for C++ input/output.
Forgetting the angle brackets around the library name.
2fill in blank
medium

Complete the code to start the main function.

C++
int [1]() {
    return 0;
}
Drag options to blanks, or click blank then click option'
Amain
Bstart
Cbegin
Dexecute
Attempts:
3 left
💡 Hint
Common Mistakes
Using other function names like start or begin which are not recognized as entry points.
Omitting the parentheses after the function name.
3fill in blank
hard

Fix the error in the code to print "Hello, World!".

C++
#include <iostream>

int main() {
    std::cout [1] "Hello, World!" << std::endl;
    return 0;
}
Drag options to blanks, or click blank then click option'
A>>
B<<
C+
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using the extraction operator >> which is for input.
Using assignment or addition operators which are incorrect here.
4fill in blank
hard

Fill both blanks to declare and initialize an integer variable named count with value 10.

C++
[1] [2] = 10;
Drag options to blanks, or click blank then click option'
Aint
Bcount
Cfloat
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Using float instead of int for an integer variable.
Using invalid variable names or reserved words.
5fill in blank
hard

Fill all three blanks to create a simple for loop that prints numbers 1 to 5.

C++
for (int [1] = 1; [2] <= 5; [3]++) {
    std::cout << i << std::endl;
}
Drag options to blanks, or click blank then click option'
Ai
Dj
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in different parts of the loop.
Using a variable name not declared in the loop.

Practice

(1/5)
1. Which function does a C++ program start executing from?
easy
A. init()
B. start()
C. begin()
D. main()

Solution

  1. Step 1: Understand program entry point

    In C++, the program always starts running from the main() function.
  2. Step 2: Identify correct function name

    Other function names like start(), init(), or begin() are not recognized as entry points.
  3. Final Answer:

    main() -> Option D
  4. Quick Check:

    Program starts at main() [OK]
Hint: Remember: C++ always starts at main() function [OK]
Common Mistakes:
  • Thinking start() or init() is the entry point
  • Confusing main with other function names
  • Assuming program runs from top to bottom without main
2. Which of the following is the correct way to include the input/output library in a C++ program?
easy
A. include <iostream>
B. #include <iostream>
C. #include iostream
D. iostream

Solution

  1. Step 1: Recognize correct include syntax

    The correct syntax to include a library is #include <library> with a hash and angle brackets.
  2. Step 2: Check each option

    #include <iostream> uses #include <iostream> which is correct. Others miss the hash or angle brackets or use invalid tags.
  3. Final Answer:

    #include <iostream> -> Option B
  4. Quick Check:

    #include <iostream> is correct [OK]
Hint: Use #include with angle brackets for libraries [OK]
Common Mistakes:
  • Omitting the # symbol
  • Not using angle brackets around library name
  • Using incorrect tags like <include>
3. What will be the output of this C++ program?
#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}
medium
A. Hello, World!
B. cout << Hello, World!
C. Hello World
D. Error: missing semicolon

Solution

  1. Step 1: Analyze the output statement

    The program uses cout to print the string "Hello, World!" followed by a newline.
  2. Step 2: Confirm syntax correctness

    All syntax is correct, so the output will be exactly the string printed.
  3. Final Answer:

    Hello, World! -> Option A
  4. Quick Check:

    cout prints "Hello, World!" [OK]
Hint: cout prints exactly what is inside quotes [OK]
Common Mistakes:
  • Ignoring the comma and exclamation mark
  • Confusing cout syntax with printf
  • Assuming missing semicolon error
4. Identify the error in this C++ program:
#include <iostream>
using namespace std;

int main() 
    cout << "Hello" << endl;
    return 0;
}
medium
A. Missing semicolon after main()
B. Missing #include directive
C. Missing curly braces { } for main function
D. Incorrect return type for main

Solution

  1. Step 1: Check main function syntax

    The main function must have curly braces { } to group its code block.
  2. Step 2: Identify missing braces

    The code lacks opening { after main() and closing } at the end, causing syntax error.
  3. Final Answer:

    Missing curly braces { } for main function -> Option C
  4. Quick Check:

    Main needs { } to group code [OK]
Hint: Always use { } to group main function code [OK]
Common Mistakes:
  • Thinking semicolon needed after main()
  • Ignoring missing braces error
  • Assuming #include is missing
5. You want to write a C++ program that prints "Welcome!" and then ends. Which of these is the correct minimal structure?
hard
A. #include <iostream> using namespace std; int main() { cout << "Welcome!" << endl; return 0; }
B. int main() { cout << "Welcome!" << endl; }
C. #include <iostream> int main() { print("Welcome!"); return 0; }
D. #include <iostream> using namespace std; void main() { cout << "Welcome!" << endl; }

Solution

  1. Step 1: Check for required includes and namespace

    To use cout, #include <iostream> and using namespace std; are needed.
  2. Step 2: Verify main function signature and body

    Main must return int and have curly braces. #include <iostream> using namespace std; int main() { cout << "Welcome!" << endl; return 0; } meets all requirements.
  3. Final Answer:

    Option A is the correct minimal structure -> Option A
  4. Quick Check:

    Include iostream + main() with return int [OK]
Hint: Include iostream and use int main() with braces [OK]
Common Mistakes:
  • Using void main() instead of int main()
  • Omitting #include <iostream>
  • Using print() instead of cout
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