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
intermediate2: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; }
Attempts:
2 left
💡 Hint
Look carefully at the exact text inside the quotes and the capitalization.
✗ Incorrect
The program prints exactly "Hello, World!" followed by a newline. The capitalization and punctuation must match exactly.
❓ Predict Output
intermediate2: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; }
Attempts:
2 left
💡 Hint
Remember the first argument is the program name itself.
✗ Incorrect
argc counts the program name plus arguments, so argc is 3. argv[1] is the first argument after the program name, which is "apple".
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Code after return is not executed.
✗ Incorrect
The program prints "Start" then returns 1 immediately. The line printing "End" is never reached.
❓ Predict Output
advanced2: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; }
Attempts:
2 left
💡 Hint
Check the return type of main function in C++.
✗ Incorrect
In standard C++, main must return int. Using void main() causes a compilation error.
🧠 Conceptual
expert3: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; }
Attempts:
2 left
💡 Hint
Look at what happens when an exception is thrown and caught.
✗ Incorrect
The throw 5 causes control to jump to the catch block, setting result to 5. The assignment result=1 is skipped.