{"id":111729,"date":"2025-09-04T12:55:13","date_gmt":"2025-09-04T07:25:13","guid":{"rendered":"https:\/\/www.mygreatlearning.com\/blog\/?page_id=111729"},"modified":"2025-09-04T12:47:49","modified_gmt":"2025-09-04T07:17:49","slug":"cpp-exercises","status":"publish","type":"page","link":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/","title":{"rendered":"C++ Exercises"},"content":{"rendered":"\n\n    <style>\n        \/* General styles *\/\n        .cpp-input {\n            border-bottom: 1px dashed #9CA3AF !important;\n        }\n        .cpp-input:focus {\n            outline: none;\n            background-color: #E0E7FF;\n            border-bottom: 1px solid #3B82F6;\n        }\n\n        .cpp-select {\n            font-family: monospace;\n            background-color: #F3F4F6;\n            border: 1px dashed #9CA3AF;\n            font-size: 0.875rem; \/* 14px *\/\n            color: #1D4ED8;\n            border-radius: 4px;\n            padding: 1px;\n        }\n        .cpp-select:focus {\n            outline: none;\n            border: 1px solid #3B82F6;\n        }\n        \n        .cpp-code-template {\n            background-color: #F9FAFB;\n            padding: 1rem;\n            border-radius: 0.375rem;\n            font-size: 0.95rem;\n            color: #374151;\n            line-height: 1.6;\n        }\n\n        \/* CSS class for topic buttons for cleaner HTML and hover effects *\/\n        .topic-item-btn {\n            background-color: #FFFFFF;\n            padding: 1.5rem;\n            border-radius: 0.5rem;\n            box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);\n            text-align: left;\n            width: 100%;\n            border: 1px solid #E5E7EB;\n            cursor: pointer;\n            transition: all 0.2s ease;\n        }\n        .topic-item-btn:hover {\n            transform: translateY(-3px);\n            box-shadow: 0 10px 15px -3px rgba(0,0,0,0.1), 0 4px 6px -2px rgba(0,0,0,0.05);\n       background-color: white;\n        }\n    <\/style>\n\n\n<div class=\"cpp-ex-container\" style=\"font-family: sans-serif; background-color: #FFFFFF; color: #1F2937; max-width: 56rem; width: 100%; margin: 2rem auto; border-radius: 1rem; box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04); padding: 1.5rem;\">\n\n    <div id=\"cpp-ex-topic-selection-view\">\n        <h1 style=\"font-size: 1.875rem; line-height: 2.25rem; font-weight: 700; text-align: center; color: #111827; margin-bottom: 0.5rem;\" style=\"font-size: 1.875rem; line-height: 2.25rem; font-weight: 700; text-align: center; color: #111827; margin-bottom: 0.5rem;\" id=\"c-practice-exercises\">C++ Practice Exercises<\/h1>\n        <p style=\"text-align: center; color: #6B7280;\">Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.<\/p>\n        <p style=\"text-align: center; color: #6B7280; margin-bottom: 2rem;\">Select a topic to start the exercise.<\/p>\n        \n        <div id=\"cpp-ex-topic-grid\" style=\"display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 1.5rem;\">\n            <button class=\"topic-item-btn\" data-topic-key=\"CBasics\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"c-basics\">C++ Basics<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"VariablesDataTypes\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"variables-data-types\">Variables & Data Types<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Operators\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"operators\">Operators<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"ControlFlowIfElse\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"control-flow-if-else\">Control Flow (If\/Else)<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Loops\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"loops\">Loops<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Functions\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"functions\">Functions<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Arrays\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"arrays\">Arrays<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Vectors\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"vectors\">Vectors<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Strings\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"strings\">Strings<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Pointers\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"pointers\">Pointers<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"ClassesObjects\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"classes-objects\">Classes & Objects<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Structs\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"structs\">Structs<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"References\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"references\">References<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"FileIO\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"file-i-o\">File I\/O<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Inheritance\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"inheritance\">Inheritance<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <button class=\"topic-item-btn\" data-topic-key=\"Polymorphism\">\n                <h3 style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" style=\"font-size: 1.25rem; font-weight: 700; color: #1F2937; margin: 0 0 0.25rem 0;\" id=\"polymorphism\">Polymorphism<\/h3>\n                <p style=\"color: #6B7280; margin: 0;\">5 exercise questions<\/p>\n            <\/button>\n            <noscript>\n                <p style=\"text-align: center; background-color: #FEF2F2; color: #991B1B; padding: 1rem; border-radius: 0.5rem; grid-column: 1 \/ -1;\">\n                    The interactive exercises on this page require JavaScript. Please enable it in your browser to start practicing.\n                <\/p>\n            <\/noscript>\n        <\/div>\n    <\/div>\n\n    <div id=\"cpp-ex-exercise-views-container\">\n        <\/div>\n\n<\/div>\n\n<script>\n    const exercises = {\n        \"C++ Basics\": [\n            { question: \"Include the necessary header to use `std::cout`.\", template: [ { type: \"input\", size: 8, placeholder: \"#include\" }, \" <iostream>\" ], solution: [\"#include\"] },\n            { question: \"Print 'Hello, C++!' to the console.\", template: [ \"std::\", { type: \"input\", size: 5, placeholder: \"object\" }, \" << \\\"Hello, C++!\\\";\" ], solution: [\"cout\"] },\n            { question: \"Create an integer variable named `age` and assign `25` to it.\", template: [ { type: \"input\", size: 4, placeholder: \"type\" }, \" age = 25;\" ], solution: [\"int\"] },\n            { question: \"Write a single-line comment.\", template: [ { type: \"input\", size: 2, placeholder: \"syntax\" }, \" This is a comment\" ], solution: [\"\/\/\"] },\n            { question: \"What is the integer result of `10 \/ 4`?\", template: [ \"int result = 10 \/ 4; \/\/ result is \", { type: \"input\", size: 2, placeholder: \"num\" } ], solution: [\"2\"] }\n        ],\n        \"Variables & Data Types\": [\n            { question: \"Declare a constant integer `MAX_SCORE` with a value of 100.\", template: [ { type: \"input\", size: 6, placeholder: \"keyword\" }, \" int MAX_SCORE = 100;\" ], solution: [\"const\"] },\n            { question: \"What is the data type for a single character?\", template: [ { type: \"select\", options: [\"char\", \"string\", \"int\"] }, \" initial = 'J';\" ], solution: [\"char\"] },\n            { question: \"Declare a double-precision floating-point variable `price`.\", template: [ { type: \"input\", size: 7, placeholder: \"type\" }, \" price = 19.99;\" ], solution: [\"double\"] },\n            { question: \"How do you declare a variable `name` that can hold a string of text?\", template: [ \"std::\", { type: \"input\", size: 7, placeholder: \"type\" }, \" name;\" ], solution: [\"string\"] },\n            { question: \"Which type is used for true\/false values?\", template: [ { type: \"input\", size: 5, placeholder: \"type\" }, \" is_active = true;\" ], solution: [\"bool\"] }\n        ],\n        \"Operators\": [\n            { question: \"Which operator checks if two values are equal?\", template: [ \"if (a \", { type: \"input\", size: 3, placeholder: \"op\" }, \" b)\" ], solution: [\"==\"] },\n            { question: \"Increment an integer `count` by 1 (postfix).\", template: [ \"count\", { type: \"input\", size: 3, placeholder: \"op\" }, \";\" ], solution: [\"++\"] },\n            { question: \"What is the purpose of the modulus operator (`%`)?\", template: [ \"It returns the \", { type: \"select\", options: [\"remainder\", \"quotient\", \"product\"] }, \" of a division.\" ], solution: [\"remainder\"] },\n            { question: \"Write the shorthand for `x = x + 5;`.\", template: [ \"x \", { type: \"input\", size: 3, placeholder: \"op\" }, \" 5;\" ], solution: [\"+=\"] },\n            { question: \"Which operator represents a logical AND?\", template: [ \"if (is_valid \", { type: \"input\", size: 3, placeholder: \"op\" }, \" has_permission)\" ], solution: [\"&&\"] }\n        ],\n        \"Control Flow (If\/Else)\": [\n            { question: \"Write an `if` statement that checks if `x` is greater than 0.\", template: [ { type: \"input\", size: 3, placeholder: \"key\" }, \" (x > 0) { ... }\" ], solution: [\"if\"] },\n            { question: \"Complete the `if-else` statement to handle both possibilities.\", template: [ \"if (condition) {<br>&nbsp;&nbsp;&nbsp;&nbsp;...<br>} \", { type: \"input\", size: 5, placeholder: \"key\" }, \" {<br>&nbsp;&nbsp;&nbsp;&nbsp;...<br>}\" ], solution: [\"else\"] },\n            { question: \"What keyword is used to check another condition if the first `if` is false?\", template: [ \"if (x > 0) { ... } \", { type: \"input\", size: 7, placeholder: \"key\" }, \" (x < 0) { ... }\" ], solution: [\"else if\"] },\n            { question: \"Write a condition to check if `score` is NOT equal to 0.\", template: [ \"if (score \", { type: \"input\", size: 3, placeholder: \"op\" }, \" 0)\" ], solution: [\"!=\"] },\n            { question: \"Which statement is used for multi-way branching based on an integer's value?\", template: [ { type: \"select\", options: [\"switch\", \"if\", \"for\"] }, \" (option) { ... }\" ], solution: [\"switch\"] }\n        ],\n        \"Loops\": [\n            { question: \"Write a `for` loop that prints numbers from 0 to 4.\", template: [ \"for (int i = 0; i \", { type: \"input\", size: 2, placeholder: \"<\" }, \" 5; ++i) { ... }\" ], solution: [\"<\"] },\n            { question: \"Write a `while` loop that continues as long as `running` is true.\", template: [ { type: \"input\", size: 6, placeholder: \"key\" }, \" (running) { ... }\" ], solution: [\"while\"] },\n            { question: \"What keyword is used to exit a loop prematurely?\", template: [ { type: \"select\", options: [\"break\", \"stop\", \"exit\"] } ], solution: [\"break\"] },\n            { question: \"What keyword skips the current iteration and proceeds to the next?\", template: [ { type: \"select\", options: [\"continue\", \"skip\", \"next\"] } ], solution: [\"continue\"] },\n            { question: \"Which loop type is guaranteed to execute at least once?\", template: [ { type: \"input\", size: 3, placeholder: \"key\" }, \" { ... } while (condition);\" ], solution: [\"do\"] }\n        ],\n        \"Functions\": [\n            { question: \"Define a simple function called `greet` that prints 'Hello!'.\", template: [ { type: \"input\", size: 5, placeholder: \"type\" }, \" greet() { ... }\" ], solution: [\"void\"] },\n            { question: \"Define a function `add` that takes two integers and returns their sum.\", template: [ \"int add(int a, int b) {<br>&nbsp;&nbsp;&nbsp;&nbsp;\", { type: \"input\", size: 7, placeholder: \"key\" }, \" a + b;<br>}\" ], solution: [\"return\"] },\n            { question: \"Call the `add` function with arguments 5 and 3.\", template: [ \"int result = \", { type: \"input\", size: 10, placeholder: \"call\" }, \";\" ], solution: [\"add(5, 3)\"] },\n            { question: \"A declaration of a function without its body is called a...\", template: [ \"Function \", { type: \"input\", size: 9, placeholder: \"name\" } ], solution: [\"prototype\"] },\n            { question: \"What does it mean to pass an argument by reference?\", template: [ \"The function receives a reference, allowing it to \", { type: \"select\", options: [\"modify the original\", \"copy the original\"] }, \" variable.\" ], solution: [\"modify the original\"] }\n        ],\n        \"Arrays\": [\n            { question: \"Declare an integer array `scores` that can hold 10 elements.\", template: [ \"int scores[\", { type: \"input\", size: 3, placeholder: \"size\" }, \"];\" ], solution: [\"10\"] },\n            { question: \"Access the first element of an array named `data`.\", template: [ \"int first = data[\", { type: \"input\", size: 2, placeholder: \"idx\" }, \"];\" ], solution: [\"0\"] },\n            { question: \"Initialize an array `numbers` with the values 1, 2, and 3.\", template: [ \"int numbers[] = \", { type: \"input\", size: 9, placeholder: \"{...}\" }, \";\" ], solution: [\"{1, 2, 3}\"] },\n            { question: \"What is the index of the last element in an array of size `N`?\", template: [ \"The last index is \", { type: \"input\", size: 4, placeholder: \"expr\" }, \".\" ], solution: [\"N - 1\"] },\n            { question: \"Get the size of an array `arr` in bytes.\", template: [ \"size_t size = \", { type: \"input\", size: 7, placeholder: \"func\" }, \"(arr);\" ], solution: [\"sizeof\"] }\n        ],\n        \"Vectors\": [\n            { question: \"Which header must be included to use `std::vector`?\", template: [ \"#include <\", { type: \"input\", size: 7, placeholder: \"header\" }, \">\" ], solution: [\"vector\"] },\n            { question: \"Create a vector of integers named `ages`.\", template: [ \"std::\", { type: \"input\", size: 7, placeholder: \"type\" }, \"<int> ages;\" ], solution: [\"vector\"] },\n            { question: \"Add the integer `42` to the end of a vector named `my_vector`.\", template: [ \"my_vector.\", { type: \"input\", size: 10, placeholder: \"method\" }, \"(42);\" ], solution: [\"push_back\"] },\n            { question: \"Get the number of elements in a vector `v`.\", template: [ \"int count = v.\", { type: \"input\", size: 5, placeholder: \"method\" }, \"();\" ], solution: [\"size\"] },\n            { question: \"Access the element at index 2 in a vector `v`.\", template: [ \"int value = v.\", { type: \"input\", size: 4, placeholder: \"method\" }, \"(2);\" ], solution: [\"at\"] }\n        ],\n        \"Strings\": [\n            { question: \"Which header is required for `std::string`?\", template: [ \"#include <\", { type: \"input\", size: 7, placeholder: \"header\" }, \">\" ], solution: [\"string\"] },\n            { question: \"Create a string `message` initialized with \\\"Hello\\\".\", template: [ \"std::string message = \", { type: \"input\", size: 9, placeholder: \"value\" }, \";\" ], solution: [\"\\\"Hello\\\"\"] },\n            { question: \"Find the length of a `std::string` named `s`.\", template: [ \"int len = s.\", { type: \"input\", size: 7, placeholder: \"method\" }, \"();\" ], solution: [\"length\"] },\n            { question: \"Concatenate strings `s1` and `s2`.\", template: [ \"std::string result = s1 \", { type: \"input\", size: 2, placeholder: \"op\" }, \" s2;\" ], solution: [\"+\"] },\n            { question: \"Access the character at index 1 of a string `str`.\", template: [ \"char c = str[\", { type: \"input\", size: 2, placeholder: \"idx\" }, \"];\" ], solution: [\"1\"] }\n        ],\n        \"Pointers\": [\n            { question: \"Declare a pointer `p` that can point to an integer.\", template: [ \"int\", { type: \"input\", size: 2, placeholder: \"op\" }, \" p;\" ], solution: [\"*\"] },\n            { question: \"What does the `&` operator do when used with a variable?\", template: [ \"It returns the memory \", { type: \"input\", size: 8, placeholder: \"value\" }, \" of the variable.\" ], solution: [\"address\"] },\n            { question: \"Get the value stored at the address a pointer `p` points to.\", template: [ \"int value = \", { type: \"input\", size: 2, placeholder: \"op\" }, \"p;\" ], solution: [\"*\"] },\n            { question: \"What keyword represents a null pointer in modern C++?\", template: [ \"int* p = \", { type: \"select\", options: [\"nullptr\", \"NULL\", \"0\"] }, \";\" ], solution: [\"nullptr\"] },\n            { question: \"What keyword allocates memory on the heap?\", template: [ \"int* p = \", { type: \"input\", size: 4, placeholder: \"key\" }, \" int;\" ], solution: [\"new\"] }\n        ],\n        \"Classes & Objects\": [\n            { question: \"Define an empty class named `Car`.\", template: [ { type: \"input\", size: 6, placeholder: \"key\" }, \" Car {};\" ], solution: [\"class\"] },\n            { question: \"A special member function called when an object is created is a...\", template: [ { type: \"input\", size: 11, placeholder: \"name\" } ], solution: [\"constructor\"] },\n            { question: \"Create an instance (object) of the `Car` class named `myCar`.\", template: [ \"Car \", { type: \"input\", size: 6, placeholder: \"var\" }, \";\" ], solution: [\"myCar\"] },\n            { question: \"What keyword makes members accessible from outside the class?\", template: [ { type: \"select\", options: [\"public\", \"private\", \"protected\"] }, \":\" ], solution: [\"public\"] },\n            { question: \"In a member function, what does the `this` pointer hold?\", template: [ \"The \", { type: \"input\", size: 8, placeholder: \"value\" }, \" of the current object.\" ], solution: [\"address\"] }\n        ],\n        \"Structs\": [\n            { question: \"Define a struct `Point` with two integer members, `x` and `y`.\", template: [ { type: \"input\", size: 7, placeholder: \"key\" }, \" Point {<br>&nbsp;&nbsp;&nbsp;&nbsp;int x;<br>&nbsp;&nbsp;&nbsp;&nbsp;int y;<br>};\" ], solution: [\"struct\"] },\n            { question: \"What is the default member access for a `struct`?\", template: [ { type: \"select\", options: [\"public\", \"private\", \"protected\"] } ], solution: [\"public\"] },\n            { question: \"Create an instance of the `Point` struct named `p1`.\", template: [ \"Point \", { type: \"input\", size: 3, placeholder: \"var\" }, \";\" ], solution: [\"p1\"] },\n            { question: \"Access the `y` member of the `p1` struct instance.\", template: [ \"p1\", { type: \"input\", size: 2, placeholder: \"op\" }, \"y = 10;\" ], solution: [\".\"] },\n            { question: \"When might you prefer a `struct` over a `class`?\", template: [ \"For simple \", { type: \"input\", size: 5, placeholder: \"stuff\" }, \" aggregates with no complex behavior.\" ], solution: [\"data\"] }\n        ],\n        \"References\": [\n            { question: \"Declare a reference `ref` to an integer variable `x`.\", template: [ \"int\", { type: \"input\", size: 2, placeholder: \"op\" }, \" ref = x;\" ], solution: [\"&\"] },\n            { question: \"Can a reference be changed to refer to another variable after initialization?\", template: [ { type: \"select\", options: [\"No\", \"Yes\"] } ], solution: [\"No\"] },\n            { question: \"What is a primary use for reference parameters in functions?\", template: [ \"To avoid making a \", { type: \"input\", size: 5, placeholder: \"thing\" }, \" of a large object.\" ], solution: [\"copy\"] },\n            { question: \"If you modify a reference, what are you actually modifying?\", template: [ \"The \", { type: \"input\", size: 9, placeholder: \"thing\" }, \" variable it refers to.\" ], solution: [\"original\"] },\n            { question: \"Unlike a pointer, a reference cannot be...\", template: [ { type: \"select\", options: [\"null\", \"reassigned\", \"both\"] } ], solution: [\"both\"] }\n        ],\n        \"File I\/O\": [\n            { question: \"Which header is needed for file input\/output streams?\", template: [ \"#include <\", { type: \"input\", size: 8, placeholder: \"header\" }, \">\" ], solution: [\"fstream\"] },\n            { question: \"Declare an object `myFile` to write to a file named `data.txt`.\", template: [ \"std::\", { type: \"input\", size: 8, placeholder: \"class\" }, \" myFile(\\\"data.txt\\\");\" ], solution: [\"ofstream\"] },\n            { question: \"Write the string \\\"Hello\\\" to the file object `myFile`.\", template: [ \"myFile \", { type: \"input\", size: 3, placeholder: \"op\" }, \" \\\"Hello\\\";\" ], solution: [\"<<\"] },\n            { question: \"Declare an object `readFile` to read from `data.txt`.\", template: [ \"std::\", { type: \"input\", size: 8, placeholder: \"class\" }, \" readFile(\\\"data.txt\\\");\" ], solution: [\"ifstream\"] },\n            { question: \"Close a file stream object `myFile`.\", template: [ \"myFile.\", { type: \"input\", size: 6, placeholder: \"method\" }, \"();\" ], solution: [\"close\"] }\n        ],\n        \"Inheritance\": [\n            { question: \"A class that inherits from another class is called a...\", template: [ { type: \"input\", size: 8, placeholder: \"name\" }, \" class.\" ], solution: [\"derived\"] },\n            { question: \"Specify that class `Manager` publicly inherits from class `Employee`.\", template: [ \"class Manager : \", { type: \"input\", size: 7, placeholder: \"spec\" }, \" Employee {};\" ], solution: [\"public\"] },\n            { question: \"The class from which another class inherits is called the...\", template: [ { type: \"input\", size: 5, placeholder: \"name\" }, \" class.\" ], solution: [\"base\"] },\n            { question: \"Which access specifier allows members to be accessed only by the class and its derived classes?\", template: [ { type: \"select\", options: [\"protected\", \"private\", \"public\"] } ], solution: [\"protected\"] },\n            { question: \"Inheritance represents an \\\"...\\\" relationship.\", template: [ { type: \"input\", size: 5, placeholder: \"is-a\" }, \" relationship.\" ], solution: [\"is-a\"] }\n        ],\n        \"Polymorphism\": [\n            { question: \"What allows objects of different classes to be treated as objects of a common base class?\", template: [ { type: \"input\", size: 12, placeholder: \"concept\" } ], solution: [\"Polymorphism\"] },\n            { question: \"How is runtime polymorphism achieved in C++?\", template: [ \"Using \", { type: \"input\", size: 8, placeholder: \"feature\" }, \" functions.\" ], solution: [\"virtual\"] },\n            { question: \"What keyword declares a function that can be overridden in a derived class?\", template: [ { type: \"input\", size: 8, placeholder: \"key\" }, \" void makeSound();\" ], solution: [\"virtual\"] },\n            { question: \"A virtual function declared with `= 0` is called a...\", template: [ { type: \"input\", size: 5, placeholder: \"type\" }, \" virtual function.\" ], solution: [\"pure\"] },\n            { question: \"A class with at least one pure virtual function is called an...\", template: [ { type: \"input\", size: 9, placeholder: \"type\" }, \" class.\" ], solution: [\"abstract\"] }\n        ]\n    };\n\n    document.addEventListener('DOMContentLoaded', function() {\n        const topicSelectionView = document.getElementById('cpp-ex-topic-selection-view');\n        const exerciseViewsContainer = document.getElementById('cpp-ex-exercise-views-container');\n\n        \/\/ --- Utility Functions ---\n        function normalizeCode(codeStr) {\n            if (typeof codeStr !== 'string') return '';\n            return codeStr.replace(\/\u201c|\u201d|\u2018|\u2019\/g, '\"').trim();\n        }\n        \n        \/\/ --- View Switching Logic ---\n        function showTopicView(topicKey) {\n            topicSelectionView.style.display = 'none';\n            document.querySelectorAll('.cpp-ex-exercise-view').forEach(view => view.style.display = 'none');\n            const view = document.getElementById(`cpp-ex-view-${topicKey}`);\n            if (view) {\n                 view.style.display = 'block';\n            }\n        }\n\n        function showTopicSelection() {\n            topicSelectionView.style.display = 'block';\n            document.querySelectorAll('.cpp-ex-exercise-view').forEach(view => view.style.display = 'none');\n        }\n        \n        \/\/ --- Initialization ---\n        function initialize() {\n            const topics = Object.keys(exercises);\n\n            \/\/ Pre-build all Exercise Views (CSR part)\n            topics.forEach(topic => {\n                const topicKey = topic.replace(\/[^a-zA-Z0-9]\/g, '');\n                const questions = exercises[topic];\n\n                let questionsHTML = '';\n                questions.forEach((q, qIndex) => {\n                    let templateHTML = '';\n                    let inputCounter = 0;\n                    q.template.forEach(part => {\n                        if (typeof part === 'string') {\n                            templateHTML += part; \n                        } else if (part.type === 'input') {\n                            templateHTML += `<input type=\"text\" class=\"cpp-input\" id=\"cpp-ex-answer-${topicKey}-${qIndex}-${inputCounter}\" size=\"${part.size || 10}\" placeholder=\"${part.placeholder || '...'}\">`;\n                            inputCounter++;\n                        } else if (part.type === 'select') {\n                            const optionsHTML = part.options.map(opt => `<option value=\"${opt}\">${opt}<\/option>`).join('');\n                            templateHTML += `<select class=\"cpp-select\" id=\"cpp-ex-answer-${topicKey}-${qIndex}-${inputCounter}\">${optionsHTML}<\/select>`;\n                            inputCounter++;\n                        }\n                    });\n\n                    questionsHTML += `\n                        <div style=\"border: 1px solid #E5E7EB; border-radius: 0.5rem;\">\n                            <div style=\"padding: 1rem; border-bottom: 1px solid #E5E7EB;\">\n                                 <p style=\"font-weight: 500; margin:0; color: #374151;\">\n                                    <span style=\"font-weight: 700; color: #111827;\">Question ${qIndex + 1}:<\/span> ${q.question}\n                                <\/p>\n                            <\/div>\n                            <div class=\"cpp-code-template\"><pre style=\"margin:0; padding:0; white-space: pre-wrap;\"><code style=\"font-family: monospace;\">${templateHTML}<\/code><\/pre><\/div>\n                            <div id=\"cpp-ex-feedback-${topicKey}-${qIndex}\" style=\"margin-top: 0.5rem; padding: 0 1rem 0.5rem 1rem;\"><\/div>\n                        <\/div>\n                    `;\n                });\n\n                const exerciseViewHTML = `\n                    <div id=\"cpp-ex-view-${topicKey}\" class=\"cpp-ex-exercise-view\" style=\"display: none;\">\n                        <div style=\"display: flex; flex-direction: column; justify-content: space-between; align-items: flex-start; margin-bottom: 1.5rem; gap: 1rem;\">\n                            <h2 style=\"font-size: 1.5rem; line-height: 2rem; font-weight: 700; color: #111827; margin:0;\" style=\"font-size: 1.5rem; line-height: 2rem; font-weight: 700; color: #111827; margin:0;\" id=\"topic\">${topic}<\/h2>\n                            <button class=\"cpp-ex-back-btn\" style=\"background-color: #E5E7EB; color: #374151; font-weight: 500; padding: 0.5rem 1rem; border-radius: 0.5rem; border: none; cursor: pointer;\" onmouseover=\"this.style.backgroundColor='#D1D5DB'\" onmouseout=\"this.style.backgroundColor='#E5E7EB'\">Back to Topics<\/button>\n                        <\/div>\n                        <div style=\"display: flex; flex-direction: column; gap: 1.5rem;\">${questionsHTML}<\/div>\n                        <div id=\"cpp-ex-result-container-${topicKey}\" style=\"text-align: center; margin-top: 2rem;\"><\/div>\n                        <div style=\"text-align: center; margin-top: 1.5rem;\">\n                             <button class=\"cpp-ex-check-answers-btn\" data-topic-key=\"${topicKey}\" data-topic-name=\"${topic}\" style=\"background-color: #2563EB; color: #FFFFFF; font-weight: 700; padding: 0.75rem 2rem; border-radius: 0.5rem; font-size: 1.125rem; border: none; cursor: pointer; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);\" onmouseover=\"this.style.backgroundColor='#1D4ED8'\" onmouseout=\"this.style.backgroundColor='#2563EB'\">Check Answers<\/button>\n                        <\/div>\n                    <\/div>\n                `;\n                exerciseViewsContainer.innerHTML += exerciseViewHTML;\n            });\n\n            \/\/ --- Attach Event Listeners ---\n            document.querySelectorAll('.topic-item-btn').forEach(button => {\n                const topicKey = button.getAttribute('data-topic-key');\n                if (topicKey) {\n                    button.addEventListener('click', () => showTopicView(topicKey));\n                }\n            });\n\n            document.querySelectorAll('.cpp-ex-back-btn').forEach(btn => {\n                btn.addEventListener('click', showTopicSelection);\n            });\n\n            document.querySelectorAll('.cpp-ex-check-answers-btn').forEach(btn => {\n                btn.addEventListener('click', (event) => {\n                    const topicKey = event.currentTarget.getAttribute('data-topic-key');\n                    const topicName = event.currentTarget.getAttribute('data-topic-name');\n                    const questions = exercises[topicName];\n                    let correctCount = 0;\n\n                    questions.forEach((q, qIndex) => {\n                        const feedbackContainer = document.getElementById(`cpp-ex-feedback-${topicKey}-${qIndex}`);\n                        let isCorrect = true;\n                        let inputCounter = 0;\n\n                        q.template.forEach(part => {\n                            if (typeof part === 'object') {\n                                const userInputElem = document.getElementById(`cpp-ex-answer-${topicKey}-${qIndex}-${inputCounter}`);\n                                const userAnswer = normalizeCode(userInputElem.value);\n                                const correctAnswer = normalizeCode(q.solution[inputCounter]);\n                                if (userAnswer.toLowerCase() !== correctAnswer.toLowerCase()) {\n                                    isCorrect = false;\n                                }\n                                inputCounter++;\n                            }\n                        });\n\n                        if (isCorrect) {\n                            correctCount++;\n                            feedbackContainer.innerHTML = `<p style=\"color: #16A34A; font-weight: 600; margin:0;\">\u2713 Correct!<\/p>`;\n                        } else {\n                            \/\/ Reconstruct the correct answer string for display\n                            let correctAnswerHTML = '';\n                            let solutionCounter = 0;\n                            q.template.forEach(part => {\n                                if (typeof part === 'string') {\n                                    correctAnswerHTML += part.replace(\/<\/g, \"&lt;\").replace(\/>\/g, \"&gt;\");\n                                } else {\n                                    correctAnswerHTML += `<strong style=\"color: #166534;\">${q.solution[solutionCounter]}<\/strong>`;\n                                    solutionCounter++;\n                                }\n                            });\n\n                            feedbackContainer.innerHTML = `\n                                <p style=\"color: #DC2626; font-weight: 600; margin:0;\">\u2717 Incorrect.<\/p>\n                                <p style=\"color: #4B5563; margin-top: 0.25rem; margin-bottom: 0.25rem;\">The correct code is:<\/p>\n                                <div class=\"cpp-code-template\" style=\"background-color: #F0FDF4;\"><pre style=\"margin:0; padding:0; white-space: pre-wrap;\"><code style=\"font-family: monospace;\">${correctAnswerHTML}<\/code><\/pre><\/div>\n                            `;\n                        }\n                    });\n\n                    const resultContainer = document.getElementById(`cpp-ex-result-container-${topicKey}`);\n                    resultContainer.innerHTML = `\n                        <p style=\"font-size: 1.25rem; font-weight: 700;\">You answered ${correctCount} out of ${questions.length} questions correctly.<\/p>\n                        <p style=\"color: #6B7280; margin-top: 0.25rem;\">Review the feedback above and feel free to try again!<\/p>\n                    `;\n                });\n            });\n        }\n\n        \/\/ Run the initialization\n        initialize();\n    });\n<\/script>\n\n\n\n\n<div class=\"wp-block-group is-layout-constrained wp-block-group-is-layout-constrained\">\n<p>Also Check:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/cpp-projects\/\">C++ Projects with Source Code<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mygreatlearning.com\/blog\/cpp-interview-questions\/\">Top C++ Interview Questions and Answers<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.<\/p>\n","protected":false},"author":41,"featured_media":111735,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"full-width-container","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"disabled","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25860],"tags":[36801],"class_list":["post-111729","page","type-page","status-publish","has-post-thumbnail","hentry","category-software","tag-c-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.3 (Yoast SEO v27.3) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>C++ Practice Programs, Questions, and Problems for Beginners<\/title>\n<meta name=\"description\" content=\"Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++ Exercises\" \/>\n<meta property=\"og:description\" content=\"Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/\" \/>\n<meta property=\"og:site_name\" content=\"Great Learning Blog: Free Resources what Matters to shape your Career!\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GreatLearningOfficial\/\" \/>\n<meta property=\"og:image\" content=\"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1408\" \/>\n\t<meta property=\"og:image:height\" content=\"768\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@Great_Learning\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/\"},\"author\":{\"name\":\"Great Learning Editorial Team\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\"},\"headline\":\"C++ Exercises\",\"datePublished\":\"2025-09-04T07:25:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/\"},\"wordCount\":120,\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/cpp-exercise.webp\",\"keywords\":[\"C++ Programming\"],\"articleSection\":[\"IT\\\/Software Development\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/\",\"name\":\"C++ Practice Programs, Questions, and Problems for Beginners\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/cpp-exercise.webp\",\"datePublished\":\"2025-09-04T07:25:13+00:00\",\"description\":\"Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/cpp-exercise.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/09\\\/cpp-exercise.webp\",\"width\":1408,\"height\":768,\"caption\":\"C++ Exercise\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/cpp-exercises\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++ Exercises\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"name\":\"Great Learning Blog\",\"description\":\"Learn, Upskill &amp; Career Development Guide and Resources\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\"},\"alternateName\":\"Great Learning\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#organization\",\"name\":\"Great Learning\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/GL-Logo.jpg\",\"width\":900,\"height\":900,\"caption\":\"Great Learning\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/GreatLearningOfficial\\\/\",\"https:\\\/\\\/x.com\\\/Great_Learning\",\"https:\\\/\\\/www.instagram.com\\\/greatlearningofficial\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/in.pinterest.com\\\/greatlearning12\\\/\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/beaconelearning\\\/\"],\"description\":\"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.\",\"email\":\"info@mygreatlearning.com\",\"legalName\":\"Great Learning Education Services Pvt. Ltd\",\"foundingDate\":\"2013-11-29\",\"numberOfEmployees\":{\"@type\":\"QuantitativeValue\",\"minValue\":\"1001\",\"maxValue\":\"5000\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/#\\\/schema\\\/person\\\/6f993d1be4c584a335951e836f2656ad\",\"name\":\"Great Learning Editorial Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"contentUrl\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/wp-content\\\/uploads\\\/2022\\\/02\\\/unnamed.webp\",\"caption\":\"Great Learning Editorial Team\"},\"description\":\"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.\",\"sameAs\":[\"https:\\\/\\\/www.mygreatlearning.com\\\/\",\"https:\\\/\\\/in.linkedin.com\\\/school\\\/great-learning\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/Great_Learning\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCObs0kLIrDjX2LLSybqNaEA\"],\"award\":[\"Best EdTech Company of the Year 2024\",\"Education Economictimes Outstanding Education\\\/Edtech Solution Provider of the Year 2024\",\"Leading E-learning Platform 2024\"],\"url\":\"https:\\\/\\\/www.mygreatlearning.com\\\/blog\\\/author\\\/greatlearning\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"C++ Practice Programs, Questions, and Problems for Beginners","description":"Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/","og_locale":"en_US","og_type":"article","og_title":"C++ Exercises","og_description":"Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.","og_url":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/","og_site_name":"Great Learning Blog: Free Resources what Matters to shape your Career!","article_publisher":"https:\/\/www.facebook.com\/GreatLearningOfficial\/","og_image":[{"width":1408,"height":768,"url":"http:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp","type":"image\/webp"}],"twitter_card":"summary_large_image","twitter_site":"@Great_Learning","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/#article","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/"},"author":{"name":"Great Learning Editorial Team","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad"},"headline":"C++ Exercises","datePublished":"2025-09-04T07:25:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/"},"wordCount":120,"publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp","keywords":["C++ Programming"],"articleSection":["IT\/Software Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/","url":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/","name":"C++ Practice Programs, Questions, and Problems for Beginners","isPartOf":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/#primaryimage"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp","datePublished":"2025-09-04T07:25:13+00:00","description":"Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.","breadcrumb":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/#primaryimage","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp","width":1408,"height":768,"caption":"C++ Exercise"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mygreatlearning.com\/blog\/cpp-exercises\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/www.mygreatlearning.com\/blog\/"},{"@type":"ListItem","position":2,"name":"C++ Exercises"}]},{"@type":"WebSite","@id":"https:\/\/www.mygreatlearning.com\/blog\/#website","url":"https:\/\/www.mygreatlearning.com\/blog\/","name":"Great Learning Blog","description":"Learn, Upskill &amp; Career Development Guide and Resources","publisher":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization"},"alternateName":"Great Learning","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mygreatlearning.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.mygreatlearning.com\/blog\/#organization","name":"Great Learning","url":"https:\/\/www.mygreatlearning.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/06\/GL-Logo.jpg","width":900,"height":900,"caption":"Great Learning"},"image":{"@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/GreatLearningOfficial\/","https:\/\/x.com\/Great_Learning","https:\/\/www.instagram.com\/greatlearningofficial\/","https:\/\/www.linkedin.com\/school\/great-learning\/","https:\/\/in.pinterest.com\/greatlearning12\/","https:\/\/www.youtube.com\/user\/beaconelearning\/"],"description":"Great Learning is a leading global ed-tech company for professional training and higher education. It offers comprehensive, industry-relevant, hands-on learning programs across various business, technology, and interdisciplinary domains driving the digital economy. These programs are developed and offered in collaboration with the world's foremost academic institutions.","email":"info@mygreatlearning.com","legalName":"Great Learning Education Services Pvt. Ltd","foundingDate":"2013-11-29","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"1001","maxValue":"5000"}},{"@type":"Person","@id":"https:\/\/www.mygreatlearning.com\/blog\/#\/schema\/person\/6f993d1be4c584a335951e836f2656ad","name":"Great Learning Editorial Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","url":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","contentUrl":"https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2022\/02\/unnamed.webp","caption":"Great Learning Editorial Team"},"description":"The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.","sameAs":["https:\/\/www.mygreatlearning.com\/","https:\/\/in.linkedin.com\/school\/great-learning\/","https:\/\/x.com\/https:\/\/twitter.com\/Great_Learning","https:\/\/www.youtube.com\/channel\/UCObs0kLIrDjX2LLSybqNaEA"],"award":["Best EdTech Company of the Year 2024","Education Economictimes Outstanding Education\/Edtech Solution Provider of the Year 2024","Leading E-learning Platform 2024"],"url":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"}]}},"uagb_featured_image_src":{"full":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp",1408,768,false],"thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise-150x150.webp",150,150,true],"medium":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise-300x164.webp",300,164,true],"medium_large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise-768x419.webp",768,419,true],"large":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise-1024x559.webp",1024,559,true],"1536x1536":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp",1408,768,false],"2048x2048":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise.webp",1408,768,false],"web-stories-poster-portrait":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise-640x768.webp",640,768,true],"web-stories-publisher-logo":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise-96x96.webp",96,96,true],"web-stories-thumbnail":["https:\/\/www.mygreatlearning.com\/blog\/wp-content\/uploads\/2025\/09\/cpp-exercise-150x82.webp",150,82,true]},"uagb_author_info":{"display_name":"Great Learning Editorial Team","author_link":"https:\/\/www.mygreatlearning.com\/blog\/author\/greatlearning\/"},"uagb_comment_info":0,"uagb_excerpt":"Explore 80+ C++ practice programs, coding questions, and exercises to improve your programming skills. Ideal for beginners and students preparing for exams.","_links":{"self":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/pages\/111729","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/users\/41"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/comments?post=111729"}],"version-history":[{"count":9,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/pages\/111729\/revisions"}],"predecessor-version":[{"id":111748,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/pages\/111729\/revisions\/111748"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media\/111735"}],"wp:attachment":[{"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/media?parent=111729"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/categories?post=111729"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mygreatlearning.com\/blog\/wp-json\/wp\/v2\/tags?post=111729"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}