LearnFrenzy
Sign in Take a free test

C++ Programming · Practice by type

C++ Programming Programs for Practice with Output

Practice programs for every C++ Programming topic, each with a full solution, its real output and an explanation. Try writing each one yourself first.

  • 270 questions
  • 27 topics
  • Answers checked on a real compiler

Basic Syntax and Data Types 10 questions

  1. Swap two numbers in C++ (three ways) Easy
  2. Sum and reverse the digits of a number in C++ Easy
  3. Convert seconds to hh:mm:ss in C++ with std::format Easy
  4. Simple interest and loan EMI calculator in C++ Medium
  5. Check leap years in C++ Easy
  6. Print ASCII values of characters in a string in C++ Easy
  7. Print ranges of C++ data types with numeric_limits Medium
  8. Read marks and print total, average and grade in C++ Easy
  9. BMI calculator with input and category in C++ Easy
  10. Simple calculator using switch in C++ Easy

Input/Output with cin and cout 10 questions

  1. Sum, average, minimum and maximum of n numbers in C++ Easy
  2. Print an aligned invoice table with setw and setfill in C++ Medium
  3. Read an ID, full name and address with getline in C++ Easy
  4. Keep asking until a valid age is entered in C++ Medium
  5. Count lines, words and characters from input in C++ Medium
  6. Parse CSV sales data with stringstream in C++ Medium
  7. Formatted multiplication table with std::format in C++ Easy
  8. Show a number in decimal, hex, octal and binary in C++ Medium
  9. Read key=value settings, skipping comments, in C++ Hard
  10. Fast input: sum of many numbers with sync_with_stdio in C++ Medium

Functions and Default Arguments 10 questions

  1. GCD and LCM of two numbers using recursion in C++ Easy
  2. Fast exponentiation (power) with recursion in C++ Medium
  3. Area of shapes with function overloading in C++ Easy
  4. Rotate three variables using references in C++ Easy
  5. Check palindromes recursively in C++ Medium
  6. Return min, max, mean and median with a struct in C++ Medium
  7. Memoized Fibonacci with a static cache in C++ Medium
  8. Tower of Hanoi: count and list the moves in C++ Medium
  9. Calculator with a table of std::function operations in C++ Hard
  10. Count ways to climb stairs (1 or 2 steps) recursively in C++ Medium

Arrays and Vectors 10 questions

  1. Reverse an array in place in C++ Easy
  2. Find the second largest element in one pass in C++ Medium
  3. Rotate a vector left by k positions in C++ Medium
  4. Merge two sorted vectors in C++ Medium
  5. Remove duplicates from a vector in C++ Easy
  6. Matrix multiplication with 2D vectors in C++ Medium
  7. Count digit frequencies with an array in C++ Easy
  8. Maximum subarray sum (Kadane's algorithm) in C++ Hard
  9. Find pairs with a given sum using two pointers in C++ Medium
  10. Answer range sum queries with prefix sums in C++ Medium

std::string 10 questions

  1. Check whether a string is a palindrome in C++ Easy
  2. Count vowels, consonants, digits and spaces in C++ Easy
  3. Reverse each word of a sentence in C++ Medium
  4. Check whether two strings are anagrams in C++ Medium
  5. Character frequency with std::map in C++ Easy
  6. Capitalize the first letter of every word in C++ Easy
  7. Find the longest word in a sentence in C++ Easy
  8. Compress a string by counting repeated characters in C++ Medium
  9. First non-repeating character in a string in C++ Medium
  10. Remove duplicate characters from a string in C++ Medium

Pointers and Dynamic Memory 10 questions

  1. Return minimum and maximum through pointer parameters in C++ Easy
  2. Reverse an array using two pointers in C++ Easy
  3. Length and copy of a C string with pointers in C++ Medium
  4. A growable array with new[] and delete[] in C++ Hard
  5. Dynamic matrix: read, transpose and free in C++ Medium
  6. Singly linked list: insert, delete and print in C++ Hard
  7. Reverse a linked list in place in C++ Medium
  8. Count letters and digits with a char pointer in C++ Easy
  9. Merge two sorted arrays into a new dynamic array in C++ Medium
  10. An RAII class that owns a dynamic array in C++ Hard

References: lvalue and rvalue 10 questions

  1. Sort three numbers using reference parameters in C++ Easy
  2. Update student records in place with auto& in C++ Easy
  3. Sum, mean and maximum via reference parameters in C++ Easy
  4. Word counter using a reference to map values in C++ Medium
  5. Swap matrix rows and scale a row through references in C++ Medium
  6. Class with reference-returning accessors in C++ Medium
  7. Count copies and moves of a class in C++ Hard
  8. A logging wrapper with perfect forwarding in C++ Hard
  9. Fill results from threads using std::ref in C++ Hard
  10. Trim and lowercase a string in place in C++ Medium

OOPS Concepts 10 questions

  1. Bank account class with deposit and withdraw in C++ Easy
  2. Rectangle class with area, perimeter and comparison in C++ Easy
  3. Shape hierarchy with virtual area and perimeter in C++ Medium
  4. Employee and Manager classes with inheritance in C++ Medium
  5. Student class with static count and class average in C++ Easy
  6. Library and Book classes using composition in C++ Medium
  7. Complex number class with operator overloading in C++ Medium
  8. A bounded Stack class with encapsulation in C++ Medium
  9. Temperature class with getters and setters in C++ Easy
  10. Vehicle interface with fuel cost calculation in C++ Hard

Classes and Objects 10 questions

  1. Date class with validation and next day in C++ Medium
  2. Fraction class with reduction and arithmetic in C++ Medium
  3. 2x2 matrix class with multiply, determinant and power in C++ Hard
  4. Student report card with an array of objects in C++ Easy
  5. Inventory class managing a vector of items in C++ Medium
  6. Point class: distance, midpoint and centroid in C++ Easy
  7. Polynomial class: evaluate, add and differentiate in C++ Hard
  8. Fixed-size circular queue class in C++ Hard
  9. Time class that adds durations and wraps at midnight in C++ Medium
  10. User registry with auto-generated IDs in C++ Medium

Constructors and Destructors 10 questions

  1. Dynamic array class with the rule of five in C++ Hard
  2. A simple String class with deep copy in C++ Medium
  3. Bank account with delegating constructors in C++ Medium
  4. A move-only resource handle class in C++ Hard
  5. RAII scope logger that reports on every exit in C++ Medium
  6. Track created, copied and destroyed objects in C++ Medium
  7. Fraction class with a validating constructor in C++ Easy
  8. Stack class with an initializer_list constructor in C++ Medium
  9. Polymorphic copying with a virtual clone() function in C++ Hard
  10. Matrix class with several constructors in C++ Medium

Static and Friend Members 10 questions

  1. Thread-safe singleton configuration manager in C++ Medium
  2. Savings accounts sharing a static interest rate in C++ Easy
  3. Count created and currently alive objects in C++ Medium
  4. Friend stream operators for a Point class in C++ Easy
  5. Money class with symmetric friend operators in C++ Medium
  6. A friend diagnostics class for a sensor in C++ Medium
  7. One friend function shared by two classes in C++ Medium
  8. A reusable per-class instance counter with CRTP in C++ Hard
  9. Static factory with a cache (flyweight) in C++ Hard
  10. Matrix-vector multiplication with a friend operator in C++ Medium

Encapsulation 10 questions

  1. Bank account with a read-only transaction history in C++ Medium
  2. Password class enforcing a password policy in C++ Medium
  3. Grade book with validated marks and a cached average in C++ Medium
  4. An immutable Money value type in C++ Medium
  5. Order lifecycle as an encapsulated state machine in C++ Hard
  6. Inventory with stock levels and reorder alerts in C++ Medium
  7. Range-limited temperature controller with clamping in C++ Easy
  8. Matrix class with bounds-checked access in C++ Medium
  9. Logger class using the pimpl idiom in C++ Hard
  10. Library membership with behaviour instead of setters in C++ Medium

Inheritance 10 questions

  1. Vehicle, Car and ElectricCar: multilevel inheritance in C++ Easy
  2. Hierarchical inheritance: shapes sharing base data in C++ Easy
  3. Student-athlete with multiple inheritance in C++ Medium
  4. Copier: diamond problem with virtual inheritance in C++ Hard
  5. Custom exception hierarchy in C++ Medium
  6. Savings and current accounts with a shared base in C++ Medium
  7. GUI widget hierarchy with base and derived drawing in C++ Medium
  8. Animals sharing a base class in C++ Easy
  9. A counting stack built on a base container in C++ Medium
  10. Hybrid inheritance: teaching assistant class in C++ Hard

Polymorphism and Virtual Functions 10 questions

  1. Payment gateway with polymorphic payment methods in C++ Medium
  2. Notification system with interchangeable channels in C++ Easy
  3. Game characters with virtual attack and damage in C++ Medium
  4. Export a report as CSV or JSON with polymorphism in C++ Medium
  5. Sort a mixed collection of shapes by area in C++ Medium
  6. A plugin registry that creates objects by name in C++ Hard
  7. Shapes with std::variant and visitors in C++ Hard
  8. CRTP mixin that adds comparisons and printing in C++ Hard
  9. Data import pipeline with the NVI pattern in C++ Medium
  10. Bank account month-end interest with polymorphism in C++ Medium

Abstraction 10 questions

  1. Bank account abstraction with deposit and withdraw in C++ Easy
  2. Shopping cart class hiding prices and discounts in C++ Medium
  3. Key-value store with swappable storage backends in C++ Medium
  4. RAII scope logger that reports when each block ends in C++ Medium
  5. Matrix class that hides its flat storage in C++ Medium
  6. Generic mean, median and mode with concepts in C++ Hard
  7. Logger interface with console and filter loggers in C++ Medium
  8. Task scheduler ADT backed by a priority queue in C++ Medium
  9. Unit-safe distance and speed types in C++ Hard
  10. Library catalog with issue and return operations in C++ Medium

Abstract Classes and Interfaces 10 questions

  1. Vehicle rental system with an abstract Vehicle class in C++ Easy
  2. Sensor dashboard built on a Sensor interface in C++ Medium
  3. Exporting student marks to CSV, TSV and Markdown in C++ Medium
  4. Ride fare calculator with swappable pricing rules in C++ Medium
  5. Unit testing a service with a mock interface in C++ Hard
  6. Text processing pipeline of interchangeable stages in C++ Medium
  7. Game entities implementing Movable and Damageable in C++ Hard
  8. Turn-based game skeleton with an abstract class in C++ Medium
  9. Payment gateway interface with a factory and refunds in C++ Hard
  10. Shape area report with is_abstract checks in C++ Medium

Operator Overloading 10 questions

  1. Fraction class with arithmetic operators in C++ Medium
  2. Big integer + and * with operator overloading in C++ Hard
  3. Polynomial addition, multiplication and evaluation in C++ Medium
  4. Date class with + days and date difference in C++ Hard
  5. Matrix class with +, *, == and transpose operators in C++ Medium
  6. String builder using operator<< for chaining in C++ Easy
  7. Money class with safe arithmetic and GST in C++ Medium
  8. Type-safe permission flags with |, & and ~ operators in C++ Hard
  9. Ranking cricket players with a custom <=> in C++ Medium
  10. Circular buffer with a wrapping operator[] in C++ Medium

Templates 10 questions

  1. Generic binary search function template in C++ Easy
  2. Matrix class template with compile-time dimensions in C++ Hard
  3. Variadic min, max and average of any arguments in C++ Medium
  4. Type-safe event bus with templates in C++ Hard
  5. Generic LRU cache class template in C++ Hard
  6. Safe string-to-number parsing template in C++ Medium
  7. Join any container into a string with a separator in C++ Easy
  8. Compile-time lookup tables with constexpr templates in C++ Hard
  9. Running statistics class template for any number type in C++ Medium
  10. Print every element of a std::tuple with templates in C++ Hard

STL Containers 10 questions

  1. Top K frequent words with map and priority_queue in C++ Medium
  2. Sliding window maximum using a deque in C++ Hard
  3. Group anagrams using a map of sorted keys in C++ Medium
  4. Browser back and forward history with two stacks in C++ Medium
  5. Live leaderboard with map and multimap in C++ Hard
  6. Job scheduler with priorities and arrival order in C++ Medium
  7. Two-sum and pair counting with unordered_map in C++ Easy
  8. Inventory manager with map and low-stock report in C++ Easy
  9. Shortest path in a grid with BFS and a queue in C++ Hard
  10. Merge overlapping meeting intervals with a vector in C++ Medium

STL Algorithms and Iterators 10 questions

  1. Palindrome checker using std::equal and iterators in C++ Easy
  2. Dense ranking of scores with lower_bound in C++ Medium
  3. Kth largest element and median with nth_element in C++ Medium
  4. Run-length encoding using std::find_if in C++ Medium
  5. Binary search on the answer with partition_point in C++ Hard
  6. Parse and analyse CSV-like data with ranges in C++ Hard
  7. Matrix row and column sums with iota and transform in C++ Easy
  8. Merge sorted log files by timestamp with std::merge in C++ Medium
  9. Clean up usernames with erase_if and unique in C++ Easy
  10. Combine parallel lists with views::zip in C++23 Hard

Lambda Expressions 10 questions

  1. Calculator using a map of lambdas in C++ Easy
  2. Memoised Fibonacci with a caching lambda in C++ Hard
  3. Form validation with a list of lambda rules in C++ Medium
  4. Sort employees by several criteria with lambdas in C++ Medium
  5. Retry helper that takes an operation as a lambda in C++ Medium
  6. Scope guard that runs a lambda on exit in C++ Hard
  7. Function composition pipeline with lambdas in C++ Hard
  8. Rate limiter with a mutable lambda in C++ Medium
  9. GUI-style buttons with lambda callbacks in C++ Easy
  10. Generic group-by function taking a key lambda in C++ Medium

Smart Pointers 10 questions

  1. Singly linked list built with unique_ptr in C++ Medium
  2. Shape factory returning unique_ptr from text commands in C++ Easy
  3. Shared document for several editors with shared_ptr in C++ Medium
  4. Observer pattern with weak_ptr subscribers in C++ Hard
  5. Undo and redo with a stack of unique_ptr commands in C++ Medium
  6. Org chart with shared_ptr and weak_ptr links in C++ Hard
  7. Connection pool with a custom unique_ptr deleter in C++ Hard
  8. Pimpl idiom with unique_ptr in C++ Medium
  9. Texture cache that frees unused entries with weak_ptr in C++ Medium
  10. Temporary file with a unique_ptr FILE handle in C++ Medium

Move Semantics 10 questions

  1. Dynamic array class implementing the rule of five in C++ Hard
  2. Move-only temporary file wrapper class in C++ Medium
  3. Generic factory with perfect forwarding in C++ Hard
  4. Efficient string concatenation with rvalue overloads in C++ Medium
  5. Sort heavy objects with moves, not copies, in C++ Medium
  6. Message queue that moves messages in and out in C++ Medium
  7. See how a move steals a vector buffer in C++ Easy
  8. Count copies and moves with emplace_back in C++ Medium
  9. Text pipeline moving strings between stages in C++ Medium
  10. Move polymorphic objects without slicing in C++ Hard

Modern C++ Features (C++11 to C++23) 10 questions

  1. Key=value config parser with optional and string_view in C++ Medium
  2. JSON-like value type with std::variant and recursion in C++ Hard
  3. CSV bill with string_view and std::format in C++ Medium
  4. Sign-up validation returning std::expected errors in C++23 Hard
  5. Area and perimeter of shapes stored as std::variant in C++ Medium
  6. Word frequency counter written in modern C++ style Easy
  7. Compile-time CRC-style checksum table with consteval in C++ Hard
  8. Server settings with designated initialisers in C++ Easy
  9. Property bag of mixed types with std::any in C++ Medium
  10. Refactor an old-style C++ program into modern C++ Medium

Exception Handling 10 questions

  1. Safe calculator that reports errors with exceptions in C++ Easy
  2. Bank transfer with rollback using exceptions in C++ Medium
  3. Read and validate user input with exceptions in C++ Medium
  4. Retry a flaky operation and give up after N attempts in C++ Medium
  5. Application error hierarchy with a top-level handler in C++ Hard
  6. Guaranteed cleanup with RAII when processing fails in C++ Medium
  7. Matrix class that throws on dimension mismatch in C++ Medium
  8. Collect errors from worker threads with exception_ptr in C++ Hard
  9. Same parser with exceptions and with std::expected in C++ Hard
  10. Scoped transaction that rolls back unless committed in C++ Hard

File Handling (fstream) 10 questions

  1. Save and load student records to a text file in C++ Easy
  2. Word frequency of a text file in C++ Medium
  3. Binary record database with find and update in C++ Hard
  4. Filter ERROR lines from a log file into another file in C++ Easy
  5. Merge two sorted number files into one in C++ Medium
  6. Read a key=value configuration file with defaults in C++ Medium
  7. File statistics tool like wc in C++ Easy
  8. Daily attendance log appended across program runs in C++ Medium
  9. Organise files by extension with std::filesystem Hard
  10. Write a file's lines in reverse order and numbered in C++ Easy

Multithreading 10 questions

  1. Parallel sum of a large array with N threads in C++ Easy
  2. Simple thread pool with a task queue in C++ Hard
  3. Count primes in parallel with an atomic counter in C++ Medium
  4. Thread-safe bank with per-account mutexes in C++ Medium
  5. Three-stage thread pipeline with blocking queues in C++ Hard
  6. Parallel matrix multiplication by rows in C++ Medium
  7. Concurrent checksums with std::async in C++ Easy
  8. Read-mostly cache with shared_mutex in C++ Hard
  9. Phased heat simulation with std::barrier in C++ Hard
  10. Parallel search with jthread and stop_token in C++ Medium

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links