{"id":18015567,"url":"https://github.com/nomemory/cpp-interview-questions","last_synced_at":"2025-07-22T01:05:11.538Z","repository":{"id":163330577,"uuid":"638100166","full_name":"nomemory/cpp-interview-questions","owner":"nomemory","description":"C++ interview questions","archived":false,"fork":false,"pushed_at":"2024-04-18T11:27:43.000Z","size":2,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-04T15:17:30.781Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nomemory.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-09T04:48:37.000Z","updated_at":"2024-12-10T18:34:30.000Z","dependencies_parsed_at":"2024-04-18T12:37:21.397Z","dependency_job_id":"0cb64920-8db3-447e-997b-32f3f3cf0011","html_url":"https://github.com/nomemory/cpp-interview-questions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nomemory/cpp-interview-questions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomemory%2Fcpp-interview-questions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomemory%2Fcpp-interview-questions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomemory%2Fcpp-interview-questions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomemory%2Fcpp-interview-questions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nomemory","download_url":"https://codeload.github.com/nomemory/cpp-interview-questions/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nomemory%2Fcpp-interview-questions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266405405,"owners_count":23923536,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-21T11:47:31.412Z","response_time":64,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-30T04:14:25.607Z","updated_at":"2025-07-22T01:05:11.490Z","avatar_url":"https://github.com/nomemory.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"* What is a reference ? \n* What is the difference between *pass-by-reference* and *pass-by-value* ?\n* What are the main differences between a `struct` and a `class` ? \n* Are there any scenarios where you would normally use a `struct` over a `class` ?\n* What are the main differences between a `reference` and a `pointer` ?\n* What is *polymorphism* in C ?\n* Explain the usage for the `virtual` keyword ?\n* What is wrong with the following code:\n\n```cpp\nclass A\n{\n    public:\n        A() {}\n        ~A(){}\n};\n\nclass B: public A\n{\n    public:\n        B():A(){}\n        ~B(){}\n};\n\nint main(void)\n{\n  A* a = new B();\n  delete a;\n}\n```\n\nAnswer: Undefined behavior because A destructor is not `virtual`.\n\n* What is the output:\n\n```cpp\nunsigned char half_limit = 150;\n\nfor (unsigned char i = 0; i \u003c 2 * half_limit; ++i)\n{\n    // do something;\n}\n```\n\nAnswer: Infinite Loop (overflow)\n\n* Explain what the keyword `static` is doing ?\n* What is the following:\n\n```cpp\n#include \u003ciostream\u003e\n#include \u003cstring\u003e\nusing namespace std;\n \nvoid demo() {\n    static int count = 0;\n    cout \u003c\u003c count \u003c\u003c \" \";\n    count++;\n}\n \nint main()\n{\n    for (int i = 0; i \u003c 5; i++)\n        demo();\n    return 0;\n}\n```\n\n* What is the O(n) for the following code ?\n\n```cpp\nint main() {\n  int n = 10; //n can be anything\n  int sum = 0;\n  float pie = 3.14;\n  int var = 1;\n\n  while (var \u003c n){\n    cout \u003c\u003c pie \u003c\u003c endl;\n    for (int j=0; j\u003cvar; j++)\n      sum+=1;\n    var*=2;  \n  }\n  cout\u003c\u003csum;\n}\n```\n\nAnswer is O(N)\n\n* Write a C++ program that uses meta-programming to calculate the factorial of a given number at compile-time.\n\n```cpp\n#include \u003ciostream\u003e\n\ntemplate \u003cint N\u003e\nstruct Factorial {\n    static const int value = N * Factorial\u003cN - 1\u003e::value;\n};\n\ntemplate \u003c\u003e\nstruct Factorial\u003c0\u003e {\n    static const int value = 1;\n};\n\nint main() {\n    constexpr int num = 5; // Change this number to calculate factorial at compile-time\n    std::cout \u003c\u003c \"Factorial of \" \u003c\u003c num \u003c\u003c \" is: \" \u003c\u003c Factorial\u003cnum\u003e::value \u003c\u003c std::endl;\n    return 0;\n}\n```\n\n# Code questions\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnomemory%2Fcpp-interview-questions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnomemory%2Fcpp-interview-questions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnomemory%2Fcpp-interview-questions/lists"}