Find Output of C++ Programs Questions with Answers

Here's a collection of 25 C++ program snippets designed to test understanding of various C++ concepts such as object-oriented programming, templates, standard library usage, pointers, and more. Each snippet is followed by a question about its output, with multiple-choice answers and explanations.

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

#include <iostream>
using namespace std;
int main() {
    cout << "Hello, " << "world!";
    return 0;
}
a) Hello, world
b) Hello, world!
c) Compilation error
d) None of the above

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

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

3. What will be the output of the following C++ code involving a class constructor?

#include <iostream>
using namespace std;
class Box {
    public:
    Box() {
        cout << "Box created";
    }
};
int main() {
   Box* myBox = new Box();
   delete myBox;
   return 0;
}
a) Box created
b) No output
c) Error
d) Box created Box destroyed

4. What will be the output of the following C++ code using polymorphism?

#include <iostream>
using namespace std;
class Animal {
    public:
virtual void sound() { cout << "Some sound"; }
};
class Dog : public Animal {
    public:
void sound() override { cout << "Bark"; }
};
int main() {
    Animal* myPet = new Dog();
    myPet->sound();
    delete myPet;
    return 0;
}
a) Some sound
b) Bark
c) Error
d) No output

5. What will be the output of the following C++ code with a simple loop?

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

6. What will be the output of the following C++ code using dynamic casting?

#include <iostream>
using namespace std;
class Base {
    public:
virtual void print() { cout << "Base"; }
};
class Derived : public Base {
    public:
void print() override { cout << "Derived"; }
};
int main() {
    Base* b = new Derived();
    Derived* d = dynamic_cast<Derived*>(b);
    if (d) d->print();
    else cout << "Cast failed";
    delete b;
    return 0;
}
a) Base
b) Derived
c) Cast failed
d) Error

7. What will be the output of the following C++ code involving function templates?

#include <iostream>
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}
int main() {
    std::cout << max(10, 20);
    return 0;
}
a) 10
b) 20
c) Compilation error
d) None of the above

8. What will be the output of the following C++ code involving exception handling?

    #include <iostream>
    int main() {
    try {
        throw "Exception!";
        } catch (const char* msg) {
            std::cout << msg;
        }
        return 0;
    }
a) Exception
b) Exception!
c) Error
d) No output

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

#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3};
    v.push_back(4);
    std::cout << v[3];
    return 0;
}
a) 1
b) 2
c) 3
d) 4

10. What will be the output of the following C++ code using namespaces?

#include <iostream>
namespace first {
    int var = 5;
}
namespace second {
    double var = 3.1416;
}
int main() {
    std::cout << first::var << " " << second::var;
    return 0;
}
a) 5 3.1416
b) 5 3
c) Error
d) None of the above

11. What will be the output of the following C++ code involving constructors and destructors?

#include <iostream>
class Test {
    public:
    Test() { std::cout << "Created "; }
    ~Test() { std::cout << "Destroyed "; }
};
int main() {
    Test t;
    return 0;
}
a) Created Destroyed
b) Destroyed Created
c) Created
d) None of the above

12. What will be the output of the following C++ code using pointers and memory allocation?

#include <iostream>
int main() {
    int *p = new int(10);
    std::cout << *p << " ";
    delete p;
    p = new int(20);
    std::cout << *p;
    delete p;
    return 0;
}
a) 10 20
b) 20 10
c) 10 10
d) 20 20

13. What will be the output of the following C++ code involving an abstract class?

#include <iostream>
class Base {
    public:
    virtual void show() = 0;
};
class Derived : public Base {
    public:
    void show() { std::cout << "In Derived "; }
};
int main() {
    Base *bp = new Derived();
    bp->show();
    delete bp;
    return 0;
}
a) In Derived
b) In Base
c) Error
d) No output

14. What will be the output of the following C++ code involving a static variable in a function?

#include <iostream>
void test() {
    static int count = 0;
    count++;
    std::cout << count << " ";
}
int main() {
    for (int i = 0; i < 3; i++) {
        test();
    }
    return 0;
}
a) 1 1 1
b) 1 2 3
c) 3 3 3
d) None of the above

15. What will be the output of the following C++ code involving a vector and iterator?

#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {10, 20, 30};
    for (auto it = v.begin(); it != v.end(); ++it) {
        std::cout << *it << " ";
    }
    return 0;
}
a) 10 20 30
b) 10 20
c) 20 30
d) 30

16. What will be the output of the following C++ code involving multiple inheritance?

#include <iostream>
class A {
    public:
    void show() { std::cout << "A "; }
};
class B {
    public:
    void show() { std::cout << "B "; }
};
class C : public A, public B {
    public:
    void display() {
        A::show();
        B::show();
    }
};
int main() {
    C c;
    c.display();
    return 0;
}
a) A
b) B
c) A B
d) B A

17. What will be the output of the following C++ code using template specialization?

#include <iostream>
template <typename T>
T max(T a, T b) {
    return (a > b) ? a : b;
}
template <>
int max<int>(int a, int b) {
    std::cout << "Specialization for int ";
    return (a > b) ? a : b;
}
int main() {
    std::cout << max(10, 20);
    return 0;
}
a) 20
b) Specialization for int 20
c) 10
d) Specialization for int 10

18. What will be the output of the following C++ code using dynamic memory allocation?

#include <iostream>
#include <string>
int main() {
    std::string* str = new std::string("Hello");
    std::cout << *str << " ";
    delete str;
    str = new std::string("World");
    std::cout << *str;
    delete str;
    return 0;
}
a) Hello World
b) Hello Hello
c) World World
d) None of the above

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

#include <iostream>
int main() {
    int a = 1, b = 2;
    int c;
    c = a + b;
    std::cout << "Sum = " << c;
    return 0;
}
a) Sum = 3
b) Sum = 2
c) 3
d) Error

20. What will be the output of the following C++ code using a lambda expression?

#include <iostream>
#include <algorithm>
#include <vector>
int main() {
    std::vector<int> vec = {10, 20, 30};
    std::for_each(vec.begin(), vec.end(), [](int x) { std::cout << x << " "; });
    return 0;
}
a) 10 20 30
b) 30 20 10
c) Error
d) 10

21. What will be the output of the following C++ code involving the STL map?

#include <iostream>
#include <map>
int main() {
    std::map<int, std::string> m;
    m[1] = "apple";
    m[2] = "banana";
    std::cout << m[1];
    return 0;
}
a) apple
b) banana
c) 1
d) Error

22. What will be the output of the following C++ code using a try-catch block?

    #include <iostream>
    int main() {
    try {
        throw 20;
        } catch (int e) {
            std::cout << "An exception occurred. Exception Nr. " << e << '\n';
        }
        return 0;
    }
a) An exception occurred. Exception Nr. 20
b) An exception occurred
c) 20
d) Error

23. What will be the output of the following C++ code using inheritance and a virtual function?

#include <iostream>
class Base {
    public:
virtual void show() { std::cout << "Base class\n"; }
};
class Derived: public Base {
    public:
void show() { std::cout << "Derived class\n"; }
};
int main() {
    Base* b;  // Base class pointer
    Derived d;  // Derived class object
    b = &d;
    b->show();
    return 0;
}
a) Base class
b) Derived class
c) Error
d) None of the above

24. What will be the output of the following C++ code using multiple files?

#include <iostream>
#include <vector>
int main() {
    std::vector<int> vec = {10, 20, 30};
    for (int x : vec)
    std::cout << x << " ";
    return 0;
}
a) 10 20 30
b) Error
c) 10
d) 20

25. What will be the output of the following C++ code involving dynamic casting?

#include <iostream>
class Base {
    public:
virtual void show() { std::cout << "Base class\n"; }
};
class Derived: public Base {
    public:
void show() { std::cout << "Derived class\n"; }
};
int main() {
    Base* b = new Derived();
    Derived* d = dynamic_cast<Derived*>(b);
    if (d != nullptr) d->show();
    else std::cout << "Cast failed\n";
    delete b;
    return 0;
}
a) Base class
b) Derived class
c) Cast failed
d) Error

Comments