C++ quiz and interview questions

Comprehensive C++ quiz and questions from basic to advanced level that help you to review your C++ language knowledge and become the master of C++ language. Please read the following question carefully and select the correct anwser, you have to make your choice before going to the next question.

Recent added questions

  • What are the basics concepts of OOP?

    Answer:

    [ A must OOP / c++ interview question for freshers (some times asked in interviews for 1-2 years experienced also), which everybody answers as well. But the point is, it's not about the answer, but how you apply these OOPs concepts in real life. You should be able to give real life examples for each of these concepts, so prepare yourself with few examples before appearing for the interview. It has seen that even the experienced people get confused when it comes to the difference between basic OOP concepts, especially abstraction and encapsulation.]

    • Classes and Objects

    Refer Questions 2 and 3 for the concepts about classes and objects

    • Encapsulation

    Encapsulation is the mechanism by which data and associated operations/methods are bound together and thus hide the data from outside world. It's also called data hiding. In c++, encapsulation achieved using the access specifiers (private, public and protected). Data members will be declared as private (thus protecting from direct access from outside) and public methods will be provided to access these data. Consider the below class

    class Person
    {
       private:
          int age;
       public:
          int getAge(){
            return age;
          }
          int setAge(int value){
            if(value > 0){
                age = value;
            }
          }
    };

    In the class Person, access to the data field age is protected by declaring it as private and providing public access methods. What would have happened if there was no access methods and the field age was public? Anybody who has a Person object can set an invalid value (negative or very large value) for the age field. So by encapsulation we can preventing direct access from outside, and thus have complete control, protection and integrity of the data.

    • Data abstraction

    Data abstraction refers to hiding the internal implementations and show only the necessary details to the outside world. In C++ data abstraction is implemented using interfaces and abstract classes.

    class Stack
    {
        public:
            virtual void push(int)=0; 
            virtual int pop()=0;
    };
    class MyStack : public Stack
    {
        private:
            int arrayToHoldData[]; //Holds the data from stack
        public:
            void push(int) {
                // implement push operation using array
            }
            int pop(){
                // implement pop operation using array
            }
    };

    In the above example, the outside world only need to know about the Stack class and its push, pop operations. Internally stack can be implemented using arrays or linked lists or queues or anything that you can think of. This means, as long as the push and pop method performs the operations work as expected, you have the freedom to change the internal implementation with out affecting other applications that use your Stack class.

    • Inheritance

    Inheritance allows one class to inherit properties of another class. In other words, inheritance allows one class to be defined in terms of another class.

    class SymmetricShape
    {
        public:
            int getSize()
            {
                return size;
            }
            void setSize(int w)
            {
                size = w;
            }
        protected:
            int size;
    };
    // Derived class
    class Square: public SymmetricShape
    {
        public:
            int getArea()
            { 
                return (size * size); 
            }
    };

    In the above example, class Square inherits the properties and methods of class SymmetricShape. Inheritance is the one of the very important concepts in C++/OOP. It helps to modularise the code, improve reusability and reduces tight coupling between components of the system.

  • What is the output of this program?

        #include <iostream>
        using namespace std;
        void Sum(int a, int b, int & c)
        {
            a = b + c;
            b = a + c;
            c = a + b;
        }
        int main()
        {
            int x = 2, y =3;
            Sum(x, y, y);
            cout << x << " " << y;
            return 0; 
        }

     

  • What will happen when we use void in argument passing?

  • What is the output of this program?

        #include <iostream>
        using namespace std;
        int add(int a, int b);
        int main()
        {
            int i = 5, j = 6;
            cout << add(i, j) << endl;
            return 0;
        }
        int add(int a, int b )
        {
            int sum = a + b;
            a = 7;
            return a + b;
        }

     

  • What is the output of this program?

        #include <iostream>
        using namespace std;
        void square (int *x)
        {
    	*x = (*x + 1) * (*x);
        }
        int main ( )
        {
    	int num = 10;
            square(&num);
            cout << num; 
            return 0;
        }

     

  • What is the output of this program?

        #include <iostream>
        using namespace std;
        long factorial (long a)
        {
            if (a > 1)
                return (a * factorial (a + 1));
            else
                return (1);
        }
        int main ()
        {
            long num = 3;
            cout << num << "! = " << factorial ( num );
            return 0;
        }

     

  • What is the new value of x?

        #include <iostream>
        using namespace std;
        void fun(int &x)
        {
            x = 20;
        }
        int main()
        {
             int x = 10;
             fun(x);
             cout << "New value of x is " << x;
             return 0;
        }

     

  • What is the output of this program?

        #include <iostream>
        using namespace std;
        void copy (int& a, int& b, int& c)
        {
            a *= 2;
            b *= 2;
            c *= 2;
        }
        int main ()
        {
            int x = 1, y = 3, z = 7;
            copy (x, y, z);
            cout << "x =" << x << ", y =" << y << ", z =" << z;
            return 0;
        }

     

View all C++ quiz questions

© 2017 QuizBucket.org