0
0
C++programming~20 mins

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

Choose your learning style9 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.