Chapter 1 Beginner 55 Questions

Practice Questions — Introduction to C++

← Back to Notes
12 Easy
11 Medium
9 Hard

Topic-Specific Questions

Question 1
Easy
What is the output of the following code?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello, World!";
    return 0;
}
cout << sends text to the standard output.
Hello, World!
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(char) << endl;
    return 0;
}
The size of char is defined by the C++ standard.
1
Question 3
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(int) << endl;
    return 0;
}
On most modern 64-bit systems, int is 4 bytes.
4
Question 4
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Line 1" << endl;
    cout << "Line 2" << endl;
    return 0;
}
endl inserts a newline and flushes the buffer.
Line 1
Line 2
Question 5
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 10 + 20 << endl;
    return 0;
}
The expression is evaluated first, then the result is printed.
30
Question 6
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Value: " << 5 << " + " << 3 << " = " << 5 + 3 << endl;
    return 0;
}
The << operator chains multiple outputs. Expressions are evaluated when reached.
Value: 5 + 3 = 8
Question 7
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(double) << endl;
    cout << sizeof(long long) << endl;
    return 0;
}
double and long long are both 8 bytes on most modern systems.
8
8
Question 8
Easy
Who created C++ and when? What was it originally called?
Think Bell Labs, late 1970s.
C++ was created by Bjarne Stroustrup in 1979 at Bell Labs. It was originally called "C with Classes" and renamed to C++ in 1983.
Question 9
Easy
Name any four real-world applications or domains where C++ is heavily used.
Think about software that needs to be very fast.
C++ is heavily used in: (1) Game engines (Unreal Engine), (2) Operating systems (Windows, parts of Linux), (3) Web browsers (Chrome, Firefox), and (4) Competitive programming (Codeforces, ICPC). Other domains include databases (MySQL), finance (HFT), and embedded systems.
Question 10
Medium
Explain the four stages of the C++ compilation process.
Source code goes through four transformations before becoming an executable.
The four stages are: (1) Preprocessing -- expands #include directives, macros, and conditional compilation. (2) Compilation -- converts preprocessed source to assembly language. (3) Assembly -- converts assembly to machine code (object file, .o). (4) Linking -- combines object files and library references into the final executable.
Question 11
Medium
What are three key differences between C++ and Java?
Think about memory management, compilation, and language features.
(1) Memory management: C++ uses manual memory management (new/delete, smart pointers) while Java has automatic garbage collection. (2) Compilation: C++ compiles to native machine code, while Java compiles to bytecode that runs on the JVM. (3) Multiple inheritance: C++ supports multiple inheritance of classes, while Java only allows multiple interface implementation.
Question 12
Medium
What was the significance of C++11? Name at least four features it introduced.
C++11 was called 'Modern C++' because of how many features it added.
C++11 was the biggest update to C++ since its original standardization. Key features: (1) auto keyword for type inference, (2) Range-based for loops, (3) Lambda expressions, (4) Smart pointers (unique_ptr, shared_ptr), (5) Move semantics and rvalue references, (6) nullptr replacing NULL, (7) constexpr for compile-time computation, (8) Threading support.
Question 13
Medium
What happens when you compile and run this code?
#include <iostream>
int main() {
    std::cout << "No using namespace" << std::endl;
    return 0;
}
Without 'using namespace std', you must prefix std:: before cout and endl.
It compiles and runs successfully. Output: No using namespace
Question 14
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(sizeof(int)) << endl;
    return 0;
}
sizeof returns a value of type size_t. What is the size of size_t?
8 (on a 64-bit system) or 4 (on a 32-bit system)
Question 15
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello";
    cout << " World";
    cout << endl;
    return 0;
}
Multiple cout statements without endl do not insert newlines between them.
Hello World
Question 16
Hard
What is the difference between a compiler error and a linker error in C++? Give an example of each.
Compiler errors happen during the compilation stage. Linker errors happen when combining object files.
A compiler error occurs when the source code has syntax errors or type mismatches. Example: using a variable without declaring it (x = 5; without int x;). A linker error occurs when the compiler succeeds but the linker cannot find a function or variable definition. Example: declaring void myFunc(); and calling it, but never providing the function body -- the linker cannot resolve the reference.
Question 17
Hard
Why is C++ preferred over Python in competitive programming despite Python being easier to write?
Think about execution speed and time limits.
C++ is preferred in competitive programming for three reasons: (1) Speed: C++ is 10-100x faster than Python, and CP problems have strict time limits (usually 1-2 seconds). A solution that passes in C++ may get TLE (Time Limit Exceeded) in Python. (2) STL: The Standard Template Library provides optimized data structures (set, map, priority_queue) and algorithms (sort, lower_bound) out of the box. (3) Deterministic performance: No garbage collector pauses or interpreter overhead.
Question 18
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "C" << "+" << "+" << endl;
    return 0;
}
Each << outputs its operand sequentially.
C++
Question 19
Medium
What does the preprocessor directive #include do? What is the difference between #include and #include "myfile.h"?
Think about where the compiler looks for the file.
#include tells the preprocessor to copy the entire contents of the specified file into the current file before compilation. #include <iostream> (angle brackets) searches in the system/standard library directories. #include "myfile.h" (double quotes) searches in the current directory first, then falls back to system directories.
Question 20
Hard
Does this program compile? If yes, what is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof('A') << endl;
    return 0;
}
In C, sizeof('A') is 4 because character literals are ints in C. What about C++?
1

Mixed & Application Questions

Question 1
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 100 << endl;
    cout << 200 << endl;
    return 0;
}
Each cout with endl prints on a new line.
100
200
Question 2
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << 3 + 4 * 2 << endl;
    return 0;
}
C++ follows standard mathematical operator precedence.
11
Question 3
Easy
Write a C++ program that prints your name, age, and college on three separate lines using three separate cout statements.
Use endl or '\n' after each cout to move to the next line.
#include <iostream>
using namespace std;
int main() {
    cout << "Arjun" << endl;
    cout << 20 << endl;
    cout << "IIT Delhi" << endl;
    return 0;
}
Output: Arjun 20 IIT Delhi
Question 4
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "A" << "B" << endl << "C" << "D" << endl;
    return 0;
}
endl can appear in the middle of a chain, not just at the end.
AB
CD
Question 5
Medium
What is the difference between endl and '\n' in C++?
Both produce a newline, but one does something extra.
Both endl and '\n' insert a newline character. However, endl also flushes the output buffer, forcing all buffered output to be written to the screen immediately. '\n' just adds a newline without flushing. In competitive programming, using '\n' is faster because flushing the buffer takes time.
Question 6
Medium
What is the output?
#include <iostream>
using namespace std;
int main() {
    int a = 10;
    cout << "a = " << a << endl;
    a = 20;
    cout << "a = " << a << endl;
    return 0;
}
The variable is reassigned between the two print statements.
a = 10
a = 20
Question 7
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << sizeof(bool) << endl;
    cout << sizeof(short) << endl;
    cout << sizeof(float) << endl;
    return 0;
}
bool is 1 byte, short is 2 bytes, float is 4 bytes on most systems.
1
2
4
Question 8
Medium
Write a C++ program that declares two integer variables, assigns them values 15 and 27, and prints their sum, difference, product, and quotient.
Remember that integer division truncates the decimal part.
#include <iostream>
using namespace std;
int main() {
    int a = 15, b = 27;
    cout << "Sum: " << a + b << endl;
    cout << "Difference: " << a - b << endl;
    cout << "Product: " << a * b << endl;
    cout << "Quotient: " << a / b << endl;
    return 0;
}
Output: Sum: 42 Difference: -12 Product: 405 Quotient: 0
Question 9
Hard
Does this code compile? If yes, what is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Size: " << sizeof("Hello") << endl;
    return 0;
}
String literals in C++ are arrays of char, and they have a null terminator.
Size: 6
Question 10
Hard
Explain what 'using namespace std;' does and why some programmers avoid it in large projects.
Think about what happens when two libraries define a function with the same name.
The using namespace std; directive imports all names from the std namespace into the current scope. This lets you write cout instead of std::cout. In large projects, this is avoided because it can cause name collisions -- if you define your own function called count or distance, it conflicts with std::count or std::distance from the standard library, leading to ambiguous calls.
Question 11
Easy
What is the output?
#include <iostream>
using namespace std;
int main() {
    cout << "Hello " << 2026 << "!" << endl;
    return 0;
}
You can chain strings and numbers together with <<.
Hello 2026!
Question 12
Hard
What is the output?
#include <iostream>
using namespace std;
int main() {
    int x = 5;
    cout << sizeof(x) << " " << sizeof(x + 1.0) << endl;
    return 0;
}
When int and double are added, the result is promoted to double.
4 8

Multiple Choice Questions

MCQ 1
Who created C++?
  • A. Dennis Ritchie
  • B. Bjarne Stroustrup
  • C. James Gosling
  • D. Guido van Rossum
Answer: B
B is correct. Bjarne Stroustrup created C++ in 1979 at Bell Labs. Dennis Ritchie (A) created C. James Gosling (C) created Java. Guido van Rossum (D) created Python.
MCQ 2
What was C++ originally called?
  • A. C Plus
  • B. Advanced C
  • C. C with Classes
  • D. New C
Answer: C
C is correct. Stroustrup originally named the language 'C with Classes' because his goal was to add class-based object-oriented programming to C. It was renamed to C++ in 1983.
MCQ 3
What does the ++ in C++ represent?
  • A. Plus plus, meaning additional features
  • B. The increment operator from C, meaning one step beyond C
  • C. Two plus signs for double the power
  • D. It is just a brand name with no meaning
Answer: B
B is correct. In C, the ++ operator increments a value by 1. So C++ literally means 'C incremented by 1' -- one step beyond C. This clever naming reflects that C++ is an evolution of C.
MCQ 4
Which of the following is NOT a feature of C++?
  • A. Object-oriented programming
  • B. Automatic garbage collection
  • C. Low-level memory access via pointers
  • D. Compiled to native machine code
Answer: B
B is correct. C++ does NOT have automatic garbage collection like Java or Python. In C++, the programmer is responsible for managing memory using new and delete (or smart pointers in modern C++). All other options are genuine C++ features.
MCQ 5
Which header file is needed to use cout and cin?
  • A. <stdio.h>
  • B. <conio.h>
  • C. <iostream>
  • D. <string>
Answer: C
C is correct. <iostream> provides cout, cin, cerr, clog, and endl. <stdio.h> (A) is the C header for printf/scanf. <conio.h> (B) is a non-standard header. <string> (D) is for the string class.
MCQ 6
What is the correct return type of the main() function in standard C++?
  • A. void
  • B. int
  • C. float
  • D. double
Answer: B
B is correct. The C++ standard requires main() to return int. return 0; indicates successful execution. While some older compilers accept void main(), it is not standard-compliant and should be avoided.
MCQ 7
What is the output of sizeof(char) in C++?
  • A. 0
  • B. 1
  • C. 2
  • D. Depends on the platform
Answer: B
B is correct. The C++ standard guarantees that sizeof(char) is always 1 byte on every platform and every compiler. This is the only type with a guaranteed fixed size.
MCQ 8
Which of the following is the correct compilation command for a C++ file using g++?
  • A. gcc file.cpp -o output
  • B. g++ file.cpp -o output
  • C. cpp file.cpp -o output
  • D. compile file.cpp -o output
Answer: B
B is correct. g++ is the GNU C++ compiler. The -o output flag specifies the name of the output executable. While gcc (A) can compile C++ with the -lstdc++ flag, g++ is the standard approach. cpp (C) is the preprocessor, not the compiler.
MCQ 9
Which stage of the C++ compilation process resolves #include directives?
  • A. Compilation
  • B. Assembly
  • C. Preprocessing
  • D. Linking
Answer: C
C is correct. The preprocessor runs before the compiler and handles all directives starting with #: #include, #define, #ifdef, etc. It replaces #include <iostream> with the actual file contents.
MCQ 10
What does 'return 0;' in main() signify?
  • A. The program output is 0
  • B. The program has zero variables
  • C. The program terminated successfully
  • D. The program runs zero more times
Answer: C
C is correct. return 0; sends an exit code of 0 to the operating system, which conventionally means the program executed successfully. A non-zero return value (like 1) indicates an error.
MCQ 11
In C++, sizeof('A') evaluates to what value?
  • A. 1
  • B. 2
  • C. 4
  • D. 8
Answer: A
A is correct. In C++, character literals like 'A' have type char, which is 1 byte. This is different from C, where 'A' has type int (4 bytes). This difference is a classic C/C++ interview question.
MCQ 12
Which C++ standard introduced smart pointers, lambda expressions, and the auto keyword?
  • A. C++98
  • B. C++03
  • C. C++11
  • D. C++17
Answer: C
C is correct. C++11 was the landmark update that introduced auto, lambda expressions, smart pointers (unique_ptr, shared_ptr), range-based for loops, move semantics, nullptr, and much more. It is often called 'Modern C++.'
MCQ 13
What is the output of sizeof("Hi") in C++?
  • A. 2
  • B. 3
  • C. 4
  • D. 8
Answer: B
B is correct. The string literal "Hi" is stored as a char array {'H', 'i', '\0'}. The null terminator adds 1 extra byte. So sizeof("Hi") = 2 characters + 1 null terminator = 3 bytes.
MCQ 14
Which of the following is NOT a valid C++ file extension?
  • A. .cpp
  • B. .cc
  • C. .cxx
  • D. .c++
Answer: D
D is correct. While .cpp, .cc, and .cxx are all valid and commonly recognized C++ file extensions, .c++ is problematic because the + character is not universally supported in filenames across all operating systems and build tools.
MCQ 15
What does the linker do in the C++ compilation process?
  • A. Converts source code to assembly language
  • B. Expands #include and #define directives
  • C. Converts assembly to machine code
  • D. Combines object files and resolves external references to produce the executable
Answer: D
D is correct. The linker takes one or more object files (.o files), resolves references between them (e.g., function calls across files), links in the standard library, and produces the final executable. Option A is the compiler, B is the preprocessor, C is the assembler.
MCQ 16
Which of the following is true about C++ compared to Java?
  • A. C++ runs on a virtual machine like Java's JVM
  • B. C++ has automatic garbage collection
  • C. C++ supports multiple inheritance of classes
  • D. C++ does not support operator overloading
Answer: C
C is correct. C++ supports multiple inheritance (a class can inherit from multiple base classes), while Java only supports single class inheritance (multiple interface implementation is allowed). C++ compiles to native code, not a VM (A is wrong). C++ has no GC (B is wrong). C++ fully supports operator overloading (D is wrong).
MCQ 17
Which operator is used for output in C++?
  • A. >>
  • B. <<
  • C. ->
  • D. ::
Answer: B
B is correct. The << operator (insertion operator) is used with cout for output. The >> operator (extraction operator) is used with cin for input. -> is for accessing members via pointers. :: is the scope resolution operator.
MCQ 18
What is the value of __cplusplus macro when compiling with C++17 standard?
  • A. 201103L
  • B. 201402L
  • C. 201703L
  • D. 202002L
Answer: C
C is correct. The __cplusplus macro is set to 201703L for C++17. The values correspond to the year and month of the standard's publication: 201103L (C++11), 201402L (C++14), 201703L (C++17), 202002L (C++20).
MCQ 19
Which of these correctly describes C++?
  • A. Interpreted, dynamically typed
  • B. Compiled, dynamically typed
  • C. Interpreted, statically typed
  • D. Compiled, statically typed
Answer: D
D is correct. C++ is a compiled language (source code is compiled to native machine code) and statically typed (variable types are determined at compile time and cannot change). Python is interpreted and dynamically typed. JavaScript is interpreted and dynamically typed.
MCQ 20
Which C++ feature is used to write generic, type-independent code?
  • A. Inheritance
  • B. Polymorphism
  • C. Templates
  • D. Encapsulation
Answer: C
C is correct. Templates allow you to write code that works with any data type. For example, template<typename T> T add(T a, T b) { return a + b; } works with int, float, double, etc. The STL (vector, map, sort) is built entirely on templates. Inheritance (A) and Polymorphism (B) are OOP concepts, not generics.

Coding Challenges

Challenge 1: Print a Formatted ID Card

Easy
Write a C++ program that prints a formatted student ID card on the console. Print the name 'Sneha', roll number 'CS2024035', branch 'Computer Science', and year '2nd Year', each on a separate line with labels.
Sample Input
(No input required - use hardcoded values)
Sample Output
Name: Sneha Roll No: CS2024035 Branch: Computer Science Year: 2nd Year
Use cout for output. Each field must be on its own line.
#include <iostream>
using namespace std;
int main() {
    cout << "Name: Sneha" << endl;
    cout << "Roll No: CS2024035" << endl;
    cout << "Branch: Computer Science" << endl;
    cout << "Year: 2nd Year" << endl;
    return 0;
}

Challenge 2: Data Type Size Reporter

Easy
Write a C++ program that prints the size (in bytes) of all fundamental data types: char, short, int, long, long long, float, double, and bool. Print each on a separate line with the type name.
Sample Input
(No input required)
Sample Output
char: 1 bytes short: 2 bytes int: 4 bytes long: 4 bytes long long: 8 bytes float: 4 bytes double: 8 bytes bool: 1 bytes
Use the sizeof operator for each type.
#include <iostream>
using namespace std;
int main() {
    cout << "char: " << sizeof(char) << " bytes" << endl;
    cout << "short: " << sizeof(short) << " bytes" << endl;
    cout << "int: " << sizeof(int) << " bytes" << endl;
    cout << "long: " << sizeof(long) << " bytes" << endl;
    cout << "long long: " << sizeof(long long) << " bytes" << endl;
    cout << "float: " << sizeof(float) << " bytes" << endl;
    cout << "double: " << sizeof(double) << " bytes" << endl;
    cout << "bool: " << sizeof(bool) << " bytes" << endl;
    return 0;
}

Challenge 3: String Literal Size Puzzle

Medium
Write a program that prints the sizeof the following string literals and explains the output using comments: "Hello", "A", "", and "C++". Remember that string literals include a null terminator.
Sample Input
(No input required)
Sample Output
sizeof("Hello") = 6 sizeof("A") = 2 sizeof("") = 1 sizeof("C++") = 4
Use sizeof on each string literal. Add comments explaining why the size is one more than the visible characters.
#include <iostream>
using namespace std;
int main() {
    // Each string literal has a hidden '\0' null terminator
    cout << "sizeof(\"Hello\") = " << sizeof("Hello") << endl;  // 5 chars + 1 null = 6
    cout << "sizeof(\"A\") = " << sizeof("A") << endl;          // 1 char + 1 null = 2
    cout << "sizeof(\"\") = " << sizeof("") << endl;             // 0 chars + 1 null = 1
    cout << "sizeof(\"C++\") = " << sizeof("C++") << endl;      // 3 chars + 1 null = 4
    return 0;
}

Challenge 4: Compilation Flags Explorer

Medium
Write a C++ program that uses the __cplusplus macro and preprocessor conditionals (#if, #elif) to print which C++ standard is being used. Test compiling it with different flags: g++ -std=c++11, g++ -std=c++17, g++ -std=c++20.
Sample Input
(No input required)
Sample Output
C++ version: 201703 You are compiling with C++17
Use #if, #elif, #else preprocessor directives. Handle C++11, C++14, C++17, and C++20.
#include <iostream>
using namespace std;
int main() {
    cout << "C++ version: " << __cplusplus << endl;
    #if __cplusplus >= 202002L
        cout << "You are compiling with C++20" << endl;
    #elif __cplusplus >= 201703L
        cout << "You are compiling with C++17" << endl;
    #elif __cplusplus >= 201402L
        cout << "You are compiling with C++14" << endl;
    #elif __cplusplus >= 201103L
        cout << "You are compiling with C++11" << endl;
    #else
        cout << "You are compiling with C++98/03" << endl;
    #endif
    return 0;
}

Challenge 5: Expression vs String Output

Hard
Write a program that demonstrates the difference between printing an arithmetic expression and printing the same expression as a string. Print '10 + 20 = ' followed by the computed result 30, all on one line. Then print the string '10 + 20' literally (without computing it).
Sample Input
(No input required)
Sample Output
Computed: 10 + 20 = 30 Literal: 10 + 20
Use a single cout chain for each line. Do NOT use any variables.
#include <iostream>
using namespace std;
int main() {
    cout << "Computed: 10 + 20 = " << 10 + 20 << endl;
    cout << "Literal: 10 + 20" << endl;
    return 0;
}

Challenge 6: Multi-line Art with cout

Easy
Write a C++ program that prints the following pattern using only cout and endl (or '\n'). Use separate cout statements for each line. * *** ***** *** *
Sample Input
(No input required)
Sample Output
* *** ***** *** *
Use only cout and string literals. No loops.
#include <iostream>
using namespace std;
int main() {
    cout << "  *" << endl;
    cout << " ***" << endl;
    cout << "*****" << endl;
    cout << " ***" << endl;
    cout << "  *" << endl;
    return 0;
}

Need to Review the Concepts?

Go back to the detailed notes for this chapter.

Read Chapter Notes

Want to learn C++ with a live mentor?

Explore our C++ Masterclass