C++ Basics Quiz

C++
0 Passed
0% acceptance

40 in-depth questions covering C++ fundamentals, program structure, input/output operations, comments, compilation process, and exit codes — with 10 code examples to solidify understanding.

10 Questions
~20 minutes
1

Question 1

When developing a C++ application that needs to read user input from the console and display formatted output, which header file must be included to access the standard I/O streams?

A
iostream - provides cin, cout, and cerr objects for console input and output operations
B
stdio.h - legacy C-style I/O functions like printf and scanf
C
fstream - handles file-based input and output operations only
D
string - provides string manipulation but not console I/O capabilities
2

Question 2

In a C++ program that processes command-line arguments and returns different exit codes based on success or failure conditions, what is the correct signature for the main function?

A
int main(int argc, char* argv[]) - accepts argument count and array of C-style strings
B
void main() - no return value and no command-line argument support
C
int main() - returns integer but cannot access command-line arguments
D
string main(int args) - incorrect types for both return and parameters
3

Question 3

During the C++ compilation process, what happens when the preprocessor encounters an #include directive for a standard library header like <iostream>?

A
The preprocessor replaces the directive with the actual header file contents, making declarations available for compilation
B
The compiler immediately generates machine code for the included header functions
C
The linker searches for and connects the header file to the final executable
D
The preprocessor ignores standard headers and only processes user-defined includes
4

Question 4

When writing a C++ program that needs to handle both successful execution and error conditions, what are the conventional exit code values that should be returned from main?

A
Return 0 for success, non-zero values (typically 1-255) for different error conditions
B
Return 1 for success, 0 for any error to follow Unix conventions
C
Exit codes are optional and don't affect program behavior
D
Return -1 for success, positive values for errors in Windows systems
5

Question 5

In a cross-platform C++ application that must work on both Windows and Unix-like systems, what namespace qualification is typically used with standard library components?

A
std:: prefix for all standard library names to avoid naming conflicts and ensure portability
B
No namespace qualification needed as standard library names are global
C
using namespace std; directive to bring all standard names into global scope
D
Platform-specific namespaces like windows:: or unix:: for different systems
6

Question 6

Consider this C++ code that attempts to read an integer from console input and display it. What will happen when this program runs and the user enters '42' followed by Enter?

cpp
#include <iostream>

int main() {
    int number;
    std::cin >> number;
    std::cout << "You entered: " << number << std::endl;
    return 0;
}
A
Program successfully reads 42, stores it in number, and prints 'You entered: 42'
B
Compilation fails because iostream header is not included properly
C
Runtime error occurs because cin cannot read from console input
D
Program prints garbage value because number is uninitialized before reading
7

Question 7

When compiling a multi-file C++ project with separate header and implementation files, what is the typical sequence of commands using g++ compiler?

A
First compile implementation files to object files, then link them together: g++ -c file1.cpp file2.cpp && g++ file1.o file2.o -o program
B
Direct compilation in one step: g++ file1.cpp file2.cpp -o program
C
Compile headers first: g++ -c header1.h header2.h && g++ file1.cpp file2.cpp -o program
D
Link object files without compilation: g++ file1.o file2.o header1.h -o program
8

Question 8

In a C++ program that includes both single-line and multi-line comments, what happens to these comments during the compilation process?

A
Comments are completely removed by the preprocessor, having no effect on the final executable size or performance
B
Single-line comments are kept for debugging, multi-line comments are removed
C
Comments are converted to machine code instructions for runtime documentation
D
Comments are stored in a separate debug section of the executable file
9

Question 9

When developing a C++ library that will be used by other programs, what considerations should be made regarding the main function and program structure?

A
Libraries should not contain main functions, only provide reusable functions and classes for other programs to call
B
Every C++ file must have a main function, even in libraries
C
Libraries can have main functions that are conditionally compiled out
D
Main functions in libraries serve as example usage demonstrations
10

Question 10

In a complex C++ build system where multiple source files include the same standard headers, how does the preprocessor prevent duplicate symbol definitions?

A
Header guards (#ifndef/#define/#endif) or #pragma once prevent multiple inclusion of the same header content
B
The compiler automatically detects and removes duplicate includes during compilation
C
Duplicate includes are allowed and resolved by the linker through symbol resolution
D
Standard library headers cannot be included multiple times and will cause compilation errors

QUIZZES IN C++