C++ Online Quiz

Welcome to this C++ online quiz designed to test and expand your knowledge of the C++ programming language. C++ is a middle-level language that facilitates the development of both high-level applications and low-level system components. This blog post contains a mix of multiple-choice questions ranging from basic syntax to more complex programming concepts, aimed at beginners and intermediate learners. Get ready to challenge your understanding and perhaps learn something new!

1. What does the following C++ code snippet output?

#include <iostream>
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}
a) Hello, World!
b) Hello, World! followed by a new line
c) Hello World
d) Compilation Error

2. In C++, which keyword is used to create a constant variable?

a) var
b) const
c) #define
d) static

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

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

4. Which of the following is true about the size of int and float data types in C++?

a) The size of int is greater than the size of float.
b) The size of float is greater than the size of int.
c) The size of int and float is always the same.
d) The size of int and float can vary depending on the compiler.

5. What will be the result of the following C++ code?

#include <iostream>
using namespace std;
int main() {
    int a = 5, b = 10;
    cout << (a > b ? a : b);
    return 0;
}
a) 5
b) 10
c) 1
d) Compilation error

6. Which function is used to allocate memory dynamically in C++?

a) malloc()
b) alloc()
c) new
d) create

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

#include <iostream>
using namespace std;
int main() {
    int i = 10;
    int& ref = i;
    ref++;
    cout << i;
    return 0;
}
a) 10
b) 11
c) Compilation error
d) None of the above

8. What is the default access specifier for members of a class in C++?

a) public
b) private
c) protected
d) None of the above

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

#include <iostream>
int main() {
    int x = 0;
    std::cout << "Number: " << ++x << std::endl;
    return 0;
}
a) Number: 0
b) Number: 1
c) Compilation error
d) Number: 2

10. Which of the following operators cannot be overloaded in C++?

a) +
b) -
c) ::
d) %

11. What is the role of a destructor in a C++ class?

a) To initialize the class objects
b) To allocate memory to the class
c) To deallocate memory and perform cleanup tasks
d) To define the variables in the class

12. How can you prevent a class in C++ from being inherited?

a) Mark the class as final
b) Mark the class as static
c) Mark the class as sealed
d) Mark the class as private

13. Which keyword is used to define a namespace in C++?

a) namespace
b) using
c) package
d) library

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

#include <iostream>
using namespace std;
class Base {
    public:
virtual void print() { cout << "Base" << endl; }
};
class Derived : public Base {
    public:
void print() { cout << "Derived" << endl; }
};
int main() {
    Base* b = new Derived();
    b->print();
    return 0;
}
a) Base
b) Derived
c) Error
d) None of the above

15. What is the correct syntax to handle exceptions in C++?

a) try {} catch() {}
b) try {} except() {}
c) try {} catch {} {}
d) try {} catch() {...}

16. What is function overloading in C++?

a) Defining multiple functions with the same name but different implementations
b) Defining multiple functions with the same name and parameters but different return types
c) Creating a function that can accept varying numbers of arguments
d) Changing the output of the function based on its input parameters

17. Which of the following is a correct way to declare a vector in C++ that stores integers?

a) vector<int> v;
b) vector v<int>;
c) int<vector> v;
d) vector<int> v[];

18. What does the following C++ code snippet output?

#include <iostream>
using namespace std;
int main() {
    cout << 5 / 2;
    return 0;
}
a) 2.5
b) 2
c) 3
d) Error

19. How can a C++ program directly manipulate memory?

a) Using the memory keyword
b) Using the pointers concept
c) Using the direct class
d) Using the handle function

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

#include <iostream>
using namespace std;
int main() {
    int a[] = {1, 2, 3, 4, 5};
    int* p = a;
    cout << *(p+3);
    return 0;
}
a) 1
b) 2
c) 3
d) 4

21. What keyword is used to indicate that a member of a class is associated with the class itself rather than with objects of the class?

a) this
b) static
c) const
d) struct

22. What is the role of the std::endl in C++ output streams?

a) It creates a new line and flushes the stream.
b) It separates integers from strings.
c) It terminates the program.
d) It performs error checking.

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

#include <iostream>
using namespace std;
int main() {
    int x = 10, y = 0;
    try {
        if (y == 0)
        throw "Division by zero error";
        cout << x / y;
        } catch (const char* msg) {
            cerr << msg;
        }
        return 0;
    }
a) Division by zero error
b) 10
c) 0
d) Unhandled exception

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

#include <iostream>
int main() {
    int num = 10;
    int &refNum = num;
    refNum += 10;
    std::cout << num;
    return 0;
}
a) 10
b) 20
c) 30
d) Compilation Error

25. In C++, how do you specify that a method should not modify any member variables of the class?

a) Use the static keyword
b) Use the const keyword after the method declaration
c) Use the final keyword
d) Use the fixed keyword

Comments