C++ quiz questions

C++ interview questions

  • 1.

    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.

    View
  • 2.

    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; 
        }

     

    1. 2 3

    2. 6 9

    3. 2 15

    4. compile time error

    Answer
  • 3.

    What will happen when we use void in argument passing?

    1. It will not return value to its caller

    2. It will return value to its caller

    3. both a & b are correct

    4. none of the mentioned

    Answer
  • 4.

    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;
        }

     

    1. 11

    2. 12

    3. 13

    4. compile time error

    Answer
  • 5.

    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;
        }

     

    1. 100

    2. compile time error

    3. 144

    4. 110

    Answer
  • 6.

    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;
        }

     

    1. 6

    2. 24

    3. segmentation fault

    4. compile time error

    Answer
  • 7.

    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;
        }

     

    1. 10

    2. 20

    3. 15

    4. none of the mentioned

    Answer
  • 8.

    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;
        }

     

    1. 2 5 10

    2. 2 4 5

    3. 2 6 14

    4. none of the mentioned

    Answer
  • 9.

    By default how the value are passed in c++?

    1. call by value

    2. call by reference

    3. call by pointer

    4. none of the mentioned

    Answer
  • 10.

    Which is used to keep the call by reference value as intact?

    1. static

    2. const

    3. absolute

    4. none of the mentioned

    Answer
  • 11.

    How many ways of passing a parameter are there in c++?

    1. 1

    2. 2

    3. 3

    4. 4

    Answer
  • 12.

    How many minimum number of functions are need to be presented in c++?

    1. 0

    2. 1

    3. 2

    4. 3

    Answer
  • 13.

    What is the scope of the variable declared in the user definied function?

    1. whole program

    2. only inside the {} block

    3. both a and b

    4. none of the mentioned

    Answer
  • 14.

    What is the output of this program?

        #include <iostream>
        using namespace std;
        void fun(int x, int y)
        {
            x = 20;
            y = 10;
        }
        int main()
        {
            int x = 10;
            fun(x, x);
            cout << x;
            return 0;
        }

     

    1. 10

    2. 20

    3. compile time error

    4. none of the mentioned

    Answer
  • 15.

    What is the output of this program?

        #include <iostream>
        using namespace std;
        void mani()
        void mani()
        {
            cout<<"hai";
        }
        int main()
        {
            mani();
            return 0;
        }

     

    1. hai

    2. haihai

    3. compile time error

    4. none of the mentioned

    Answer
  • 16.

    Which is more effective while calling the functions?

    1. call by value

    2. call by reference

    3. call by pointer

    4. none of the mentioned

    Answer
  • 17.

    How many max number of arguments can present in function in c99 compiler?

    1. 99

    2. 90

    3. 102

    4. 127

    Answer
  • 18.

    which of the following is used to terminate the function declaration?

    1. )

    2. ;

    3. none of the mentioned

    Answer
  • 19.

    What are mandatory parts in function declaration?

    1. return type,function name

    2. return type,function name,parameters

    3. both a and b

    4. none of the mentioned

    Answer
  • 20.

    Where does the execution of the program starts?

    1. user-defined function

    2. main function

    3. void function

    4. none of the mentioned

    Answer

© 2017 QuizBucket.org