Bird
Raised Fist0
C++programming~20 mins

main function and program entry in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of main function and program entry
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this C++ program?
Consider the following C++ program. What will it print when run?
C++
#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
AHello World
Bhello, world!
CCompilation error
DHello, World!
Attempts:
2 left
💡 Hint
Look carefully at the exact text inside the quotes and the capitalization.
❓ Predict Output
intermediate
2:00remaining
What is the output of this program with command line arguments?
What will this C++ program print if run with the command line: ./program apple banana?
C++
#include <iostream>

int main(int argc, char* argv[]) {
    std::cout << argc << std::endl;
    std::cout << argv[1] << std::endl;
    return 0;
}
A
3
apple
B
2
apple
C
2
banana
D
3
banana
Attempts:
2 left
💡 Hint
Remember the first argument is the program name itself.
❓ Predict Output
advanced
2:00remaining
What is the output of this program with multiple return statements?
What will this C++ program print when run?
C++
#include <iostream>

int main() {
    std::cout << "Start" << std::endl;
    return 1;
    std::cout << "End" << std::endl;
}
AEnd
B
Start
End
CStart
DCompilation error
Attempts:
2 left
💡 Hint
Code after return is not executed.
❓ Predict Output
advanced
2:00remaining
What error does this program produce?
What error will this C++ program cause when compiled?
C++
#include <iostream>

int main() {
    std::cout << "Hello" << std::endl;
    return 0;
}
ANo error, prints Hello
BError: main must return int
CError: missing semicolon
DError: std::cout not declared
Attempts:
2 left
💡 Hint
Check the return type of main function in C++.
🧠 Conceptual
expert
3:00remaining
What is the value of 'result' after running this program?
Consider this C++ program. What is the value of the variable 'result' after main finishes?
C++
#include <iostream>

int main() {
    int result = 0;
    try {
        throw 5;
        result = 1;
    } catch (int e) {
        result = e;
    }
    return result;
}
A5
B0
C1
DCompilation error
Attempts:
2 left
💡 Hint
Look at what happens when an exception is thrown and caught.

Practice

(1/5)
1. What is the role of the main function in a C++ program?
easy
A. It stores all the variables used in the program.
B. It is the starting point where the program begins execution.
C. It compiles the program into machine code.
D. It handles input and output operations automatically.

Solution

  1. Step 1: Understand the purpose of main

    The main function is where the program starts running when executed.
  2. Step 2: Compare options with this purpose

    Only It is the starting point where the program begins execution. correctly states that main is the starting point of execution.
  3. Final Answer:

    It is the starting point where the program begins execution. -> Option B
  4. Quick Check:

    Program entry point = main function = A [OK]
Hint: Remember: program always starts at main() [OK]
Common Mistakes:
  • Thinking main stores variables
  • Confusing compilation with execution
  • Assuming main handles I/O automatically
2. Which of the following is the correct syntax for the main function in C++?
easy
A. int main() { return 0; }
B. void main() { return 0; }
C. main() int { return 0; }
D. int main(void) return 0;

Solution

  1. Step 1: Recall standard main function syntax

    The standard main function returns an int and has parentheses for parameters, e.g., int main().
  2. Step 2: Check each option for syntax correctness

    int main() { return 0; } matches correct syntax with braces and return statement. Options A, B, and D have syntax errors or non-standard forms.
  3. Final Answer:

    int main() { return 0; } -> Option A
  4. Quick Check:

    Standard main syntax = int main() { } = C [OK]
Hint: Main must return int and have braces {} [OK]
Common Mistakes:
  • Using void main() which is non-standard
  • Missing braces {} around main body
  • Incorrect parameter syntax
3. What will be the output of this program?
#include <iostream>
int main() {
  std::cout << "Hello, World!" << std::endl;
  return 0;
}
medium
A. Hello World
B. No output
C. Compilation error
D. Hello, World!

Solution

  1. Step 1: Analyze the output statement

    The program prints the exact string "Hello, World!" followed by a newline.
  2. Step 2: Confirm no errors and output correctness

    The code is correct and will output "Hello, World!" exactly as shown.
  3. Final Answer:

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

    cout prints "Hello, World!" = B [OK]
Hint: Look for exact string inside cout << [OK]
Common Mistakes:
  • Ignoring punctuation in output
  • Assuming missing newline means no output
  • Confusing compilation errors
4. Identify the error in this program:
#include <iostream>
int main() {
  std::cout << "Start" << std::endl;
  return;
}
medium
A. Incorrect header file included
B. Missing semicolon after cout statement
C. Missing return value in main's return statement
D. main function missing parentheses

Solution

  1. Step 1: Check the return statement in main

    Main returns int, so return must have a value like 0. Here, return is used without a value.
  2. Step 2: Verify other parts of code

    Semicolon after cout is present, header is correct, and main has parentheses.
  3. Final Answer:

    Missing return value in main's return statement -> Option C
  4. Quick Check:

    int main() must return int value = A [OK]
Hint: int main() must return an int value [OK]
Common Mistakes:
  • Using return without value in int main
  • Assuming void main is valid
  • Ignoring missing return value errors
5. You want your C++ program to return 1 to the operating system when it finishes. Which main function code correctly does this?
hard
A. int main() { return 1; }
B. void main() { return 1; }
C. int main() { return; }
D. int main() { return '1'; }

Solution

  1. Step 1: Understand return type and value

    Main returns int, so return value must be an integer. Returning 1 signals a specific exit code.
  2. Step 2: Check each option for correctness

    int main() { return 1; } returns integer 1 correctly. void main() { return 1; } uses void main which is non-standard. int main() { return; } returns nothing. int main() { return '1'; } returns a char, not int.
  3. Final Answer:

    int main() { return 1; } -> Option A
  4. Quick Check:

    Return integer 1 from main = D [OK]
Hint: Return integer value from int main() [OK]
Common Mistakes:
  • Using void main instead of int main
  • Returning no value or wrong type
  • Returning character instead of integer
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