C++ Online Test - MCQ Questions

Welcome to the C++ Programming Online Test! This assessment is carefully designed to test your understanding and proficiency in C++ programming. The test consists of 25 multiple-choice questions, each featuring a coding snippet that challenges various aspects of the C++ language, from basic syntax and data types to more complex concepts like pointers, references, and object-oriented programming.

Each question is created to test different facets of the C++ language, helping to evaluate your problem-solving skills and your ability to write and understand C++ code. As you progress through the test, you will encounter questions that require you to analyze code snippets, predict their outputs, or understand language-specific features.

We recommend you read each question carefully and choose the answer that best fits. After completing the test, you will receive explanations for each answer, providing you with the opportunity to learn and reinforce your understanding of C++.

Best of luck! We hope you find this test both challenging and educational, enhancing your programming skills and deepening your knowledge of C++.

1. What is the output of the following C++ code?

#include <iostream>
int main() {
    std::cout << 5/2;
}
a) 2
b) 2.5
c) 5
d) None of the above

2. What does the following C++ program print?

#include <iostream>
int main() {
    int x = 10;
    int y = ++x;
    std::cout << y;
}
a) 10
b) 11
c) 12
d) Error

3. What will be the output of the following C++ program?

#include <iostream>
int main() {
    char c = 255;
    std::cout << (int)c;
}
a) 255
b) -1
c) 0
d) 1

4. What will the following C++ code snippet output?

#include <iostream>
int main() {
    for (int i = 0; i < 5; i++) {
        if (i == 3)
           continue;
           std::cout << i << " ";
    }
}
a) 0 1 2 3 4
b) 0 1 2 4
c) 0 1 2
d) 0 1 2 3

5. What does the following C++ code do?

#include <iostream>
int main() {
    throw "An error occurred";
}
a) Prints "An error occurred"
b) Compiles but does nothing when run
c) Causes the program to exit with an error message
d) None of the above

6. What is the output of the following C++ program?

#include <iostream>
int main() {
    int x = 5;
    int* p = &x;
    *p = 6;
    std::cout << x;
}
a) 5
b) 6
c) Error
d) Undefined behavior

7. What will the following C++ code snippet output?

#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3, 4, 5};
    for (int n : v) {
        if (n % 2 == 0) std::cout << n << " ";
    }
}
a) 1 3 5
b) 2 4
c) 1 2 3 4 5
d) 2 4 5

8. What does the following C++ code print?

#include <iostream>
#include <string>
int main() {
    std::string s = "Hello";
    std::cout << s[1];
}
a) H
b) e
c) l
d) o

9. What will be the output of the following C++ code?

#include <iostream>
int main() {
    int arr[] = {10, 20, 30, 40, 50};
    std::cout << *arr;
}
a) 10
b) 20
c) 30
d) 40

10. What is the outcome of this C++ code?

#include <iostream>
#include <array>
int main() {
    std::array<int, 5> arr = {1, 2, 3, 4, 5};
    std::cout << arr.at(3);
}
a) 1
b) 2
c) 3
d) 4

11. What is the result of executing the following C++ code?

#include <iostream>
int main() {
    int i = 1;
    int j = 2;
    int k;
    k = i + j, i = j, j = k;
    std::cout << i << " " << j;
}
a) 1 2
b) 2 3
c) 2 1
d) 3 3

12. What does the following C++ program output?

#include <iostream>
int main() {
    int x = 0;
    std::cout << ++x + x++;
}
a) 1
b) 2
c) 3
d) Undefined behavior

13. What will the following C++ code snippet output?

#include <iostream>
#include <vector>
int main() {
    std::vector<int> vec(3, 100);
    for(auto it = vec.begin(); it != vec.end(); ++it) {
        std::cout << *it << " ";
    }
}
a) 100 100 100
b) 0 0 0
c) 1 1 1
d) 100 100

14. What is the output of this C++ program?

#include <iostream>
struct A {
    int x;
    A(int x) : x(x) {}
    A() : x(0) {}
};
int main() {
   A a;
   std::cout << a.x;
}
a) 0
b) 1
c) Undefined
d) Compilation error

15. What will be the output of the following C++ program?

#include <iostream>
int main() {
    int a = 10, b = 20;
    const int* p = &a;
    a = 30;
    std::cout << *p;
}
a) 10
b) 20
c) 30
d) Undefined

16. What is the result of the following C++ function?

#include <iostream>
void swap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
}
int main() {
    int x = 10, y = 20;
    swap(x, y);
    std::cout << x << " " << y;
}
a) 10 20
b) 20 10
c) Error
d) None of the above

17. What does the following C++ code output?

#include <iostream>
int main() {
    int arr[] = {1, 2, 3};
    int* ptr = arr;
    std::cout << *(ptr + 1);
}
a) 1
b) 2
c) 3
d) Compilation error

18. What will be the output of this C++ code?

#include <iostream>
int main() {
    int x = 5;
    std::cout << [&]() { return x + 5; }();
}
a) 5
b) 10
c) Error
d) None of the above

19. What does this C++ code print?

#include <iostream>
int main() {
    int x = 1;
    int y = 2;
    int z = 3;
    std::cout << (x, y, z);
}
a) 1
b) 2
c) 3
d) 6

20. What is the output of the following C++ program?

#include <iostream>
int main() {
    int a = 10;
    int b = ++a;
    int c = a++;
    std::cout << b << " " << c;
}
a) 11 11
b) 11 12
c) 12 11
d) 12 12

21. What will the following C++ code snippet output?

#include <iostream>
#include <map>
int main() {
    std::map<int, int> m;
    m[1] = 100;
    m[2] = 200;
    for (auto& x: m) {
        std::cout << x.second << " ";
    }
}
a) 100 200
b) 1 2
c) 1 100 2 200
d) None of the above

22. What does the following C++ program output?

#include <iostream>
#include <set>
int main() {
    std::set<int> s;
    s.insert(3);
    s.insert(1);
    s.insert(4);
    s.insert(1);
    s.insert(5);
    s.insert(9);
    s.insert(2);
    s.insert(6);
    s.insert(5);
    s.insert(3);
    s.insert(5);
    for (int n : s) {
        std::cout << n << " ";
    }
}
a) 1 2 3 4 5 6 9
b) 3 1 4 1 5 9 2 6 5 3 5
c) 1 3 4 5 9 2 6
d) 1 4 5 9 2 6 3

23. What will be the output of the following C++ program?

#include <iostream>
int main() {
    try {
        throw 20;
        } catch (int e) {
            std::cout << "An exception occurred. Exception Nr. " << e ;
        }
    }
a) An exception occurred.
b) An exception occurred. Exception Nr. 20
c) Error
d) None of the above

24. What does this C++ code output?

#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {10, 20, 30};
    std::cout << v[1];
}
a) 10
b) 20
c) 30
d) Error

25. What is the output of the following C++ code?

#include <iostream>
int main() {
    int x = 10;
    int &ref = x;
    ref = 20;
    std::cout << x;
}
a) 10
b) 20
c) Error
d) None of the above

Comments