C++ quiz questions

C++ interview questions

  • 1.

    What gets printed by this program?

    #include <iostream>
    
    struct Shape
    {
      virtual Shape* duplicate()
      {
        return new Shape;
      }
      virtual void print()
      {
        std::cout << "SHAPE" << std::endl;
      }
      virtual ~Shape() {}
    };
    
    struct Box : public Shape
    {
      virtual Box* duplicate()
      {
        return new Box;
      }
      virtual void print()
      {
        std::cout << "BOX" << std::endl;
      }
    };
    
    int main(int argc, char** argv) 
    { 
      Shape* s1 = new Box;
    
      Box* b1 = s1->duplicate();
    
      b1->print();
    
      delete s1;
      delete b1;
      return 0; 
    }
    1. SHAPE

    2. BOX

    3. undefined

    4. code is ill-formed

    5. unspecified

    Answer
  • 2.

    What gets printed by this program?

    #include <iostream>
    
    struct Shape
    {
      virtual Shape* duplicate()
      {
        std::cout << "SHAPE" << std::endl;
        return new Shape;
      }
      virtual ~Shape() {}
    };
    
    struct Box : public Shape
    {
      virtual Box* duplicate()
      {
        std::cout << "BOX" << std::endl;
        return new Box;
      }
    };
    
    int main(int argc, char** argv) 
    { 
      Shape* s1 = new Box;
    
      Shape* s2 = s1->duplicate();
    
      delete s1;
      delete s2;
      return 0; 
    }
    1. SHAPE

    2. BOX

    3. undefined

    4. code is ill-formed

    5. unspecified

    Answer
  • 3.

    What gets printed by this program?

    #include <iostream>
    
    struct Shape
    {
      virtual void print()
      {
        std::cout << "SHAPE" << std::endl;
      }
    
      virtual ~Shape() {}
    };
    
    struct Box : public Shape
    {
      virtual void print(int i)
      {
        std::cout << "BOX" << std::endl;
      }
    };
    
    int main(int argc, char** argv) 
    { 
      Shape* s = new Box;
    
      s->print();
    
      delete s;
    
      return 0; 
    }
    1. SHAPE

    2. BOX

    3. undefined

    4. code is ill-formed

    5. unspecified

    Answer
  • 4.

    How many times is Hello World printed by this program?

    #include <iostream>
    
    struct BS
    {
      BS()
      {
        std::cout << "Hello World" << std::endl;
      }
    };
    
    struct mid1 : public BS { };
    struct mid2 : public BS { };
    struct mid3 : public BS { };
    struct mid4 : public BS { };
    
    struct DR : public virtual mid1, public virtual mid2, public virtual mid3, public mid4 { };
    
    int main(int argc, char** argv) 
    { 
      DR d;
      return 0; 
    }

     

    1. 0

    2. 3

    3. 4

    4. undefined

    5. code is ill-formed

    Answer
  • 5.

    What gets printed by this program?

    #include <iostream>
    
    struct Shape
    {
      virtual void print()
      {
        std::cout << "SHAPE" << std::endl;
      }
      virtual ~Shape() {}
    };
    
    struct Box : public virtual Shape
    {
      void print()
      {
        std::cout << "BOX" << std::endl;
      }
    };
    
    struct Sphere : public virtual Shape
    {
      void print()
      {
        std::cout << "SPHERE" << std::endl;
      }
    };
    
    struct GeoDisc : public Box, public Sphere
    {
    };
    
    int main(int argc, char** argv)
    {
      Shape* s = new GeoDisc;
    
      s->print();
    
      delete s;
    
      return 0;
    }
    1. SHAPE

    2. BOX

    3. SPHERE

    4. undefined

    5. code is ill-formed

    Answer
  • 6.

    What gets printed by this program?

    #include <iostream>
    
    struct Shape
    {
      void print()
      {
        std::cout << "SHAPE" << std::endl;
      }
    };
    
    struct Box : public Shape
    {
      void print()
      {
        std::cout << "BOX" << std::endl;
      }
    };
    
    int main(int argc, char** argv)
    {
      Shape* s1 = new Box;
    
      s1->print();
    
      return 0;
    }
    1. SHAPE

    2. BOX

    3. code is ill-formed

    4. unspecified

    Answer
  • 7.

    How many times is Hello World printed by this program?

    #include <iostream>
    
    struct BS
    {
      BS()
      {
        std::cout << "Hello World" << std::endl;
      }
      unsigned int color;
    };
    
    struct mid1 : virtual public BS { };
    struct mid2 : virtual public BS { };
    struct mid3 : public BS { };
    struct mid4 : public BS { };
    
    struct DR : public mid1, public mid2, 
                public mid3, public mid4 { };
    
    int main(int argc, char** argv) 
    { 
      DR d;
      return 0; 
    }
    1. 1

    2. 2

    3. 3

    4. code is ill-formed

    5. undefined

    Answer
  • 8.

    What is the output of the program?

    #include <iostream>
    
    struct BS
    {
      unsigned int color;
    };
    
    struct car : public BS
    {
    };
    
    struct truck : public BS
    {
    };
    
    struct city : public car, public truck
    {
    };
    
    int main(int argc, char** argv) 
    { 
      city c;
    
      c.color = 3;
    
      std::cout << c.color << std::endl;
    
      return 0; 
    }
    1. 0

    2. 3

    3. ill-formed

    4. undefined

    5. unspecified

    Answer
  • 9.

    Static member functions of classes can be virtual. True or false?

    1. true

    2. false

    Answer
  • 10.

    What is the output of the program?

    #include <iostream>
    
    int main(int argc, char** argv) 
    { 
      // assume address of x is 0x822222222
      int x = 3;
    
      int*& rpx = &x;
    
      std::cout << rpx << std::endl;
      
      return 0; 
    }
    1. 3

    2. 0x822222222

    3. ill-formed

    4. undefined

    Answer
  • 11.

    What is the output of the program?

    #include <iostream>
    
    class Foo
    {
    public:
    
      char c;
      static double sd;
      double d;
      int i;
    };
    
    int main(int argc, char** argv) 
    { 
      Foo f = { 72, 3.14 };
    
      std::cout << f.c + f.d + f.i << std::endl;
    
      return 0; 
    }
    1. 72

    2. 75.14

    3. ill-formed

    4. undefined

    Answer
  • 12.

    What is the output of the program?

    #include <iostream>
    
    struct A 
    { 
      virtual int foo(int x = 5) 
      { 
        return x * 2; 
      } 
    }; 
    
    struct B : public A 
    { 
      int foo(int x = 10) 
      { 
        return x * 3; 
      } 
    }; 
    
    int main(int argc, char** argv) 
    { 
      A* a = new B; 
    
      std::cout << a->foo() << std::endl; 
    
      return 0; 
    }
    1. 10

    2. 15

    3. 20

    4. 30

    5. ill-formed

    Answer
  • 13.

    Which, if any, of the member function definitions below are ill-formed?

    #include <iostream>
    
    int g_x = 44; 
    
    struct Foo 
    { 
      int m_x; 
    
      static int s_x; 
    
      Foo(int x) : m_x(x) {} 
    
      int a(int x = g_x) 
      { 
        return x + 1; 
      } 
    
      int b(int x = m_x) 
      { 
        return x + 1; 
      } 
    
      int c(int x = s_x) 
      { 
        return x + 1; 
      } 
    }; 
    
    int Foo::s_x = 22; 
    
    int main(int argc, char** argv) 
    { 
      Foo f(6); 
    
      std::cout << f.a() << std::endl; 
      std::cout << f.b() << std::endl; 
      std::cout << f.c() << std::endl; 
    
      return 0; 
    }
    1. a

    2. b

    3. c

    4. a and b

    5. none

    Answer
  • 14.

    What value gets printed by the program?

    #include <iostream>
    
    int foo(int x, int y = x) 
    { 
      return x+y+1; 
    } 
    
    int main(int argc, char** argv) 
    { 
      std::cout << foo(2) << std::endl; 
      return 0; 
    }
    1. 1

    2. 3

    3. 5

    4. ill-formed

    5. undefined

    Answer
  • 15.

    What value gets printed by the program?

    #include <iostream>
    
    int foo(int y);
    int foo(int x)
    {
      return x+1;
    }
    
    int main(int argc, char** argv)
    {
      int x = 3;
      int y = 6;
    
      std::cout << foo(x) << std::endl;
    
      return 0;
    }
    1. 3

    2. 4

    3. 9

    4. ill-formed

    5. undefined

    Answer
  • 16.

    What value gets printed by the program?

    #include <iostream>
    
    int foo(int x, int y)
    {
      return x+y;
    }
    
    double foo(double x, double y)
    {
      return x+y;
    }
    
    int main(int argc, char** argv)
    {
      double (*ptr)(int, int);
    
      ptr = foo;
    
      std::cout << ptr(3,8) << std::endl;
    
      return 0;
    }
    1. 3

    2. 8

    3. 11

    4. ill-formed

    5. undefined

    Answer
  • 17.

    What value gets printed by the program?

    #include <iostream>
    
    int foo(int x, int y)
    {
      return x+y;
    }
    
    int foo(const int x, const int y)
    {
      return x+y+1;
    }
    
    int main(int argc, char** argv)
    {
      const int x = 3;
      const int y = 2;
    
      std::cout << foo(x,y) << std::endl;
    
      return 0;
    }
    1. 3

    2. 5

    3. 6

    4. ill-formed

    5. undefined

    Answer
  • 18.

    What value gets printed by the program?

    #include <iostream>
    
    int main() 
    { 
      int sum = 0;
    
      int array[3][] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
    
      for (int i = 0; i < 3 ; ++i)
      {
        for (int j = 2; j < 3 ; j++)
        {
          sum += array[i][j];
        }
      }
    
      std::cout << sum << std::endl;
    
      return 0;
    }
    1. 9

    2. 15

    3. 21

    4. ill-formed

    5. undefined

    Answer
  • 19.

    Which of the following operators does NOT indicate a sequence point in the code?

    1. &&

    2. ||

    3. ?

    4. =

    5. ,

    Answer
  • 20.

    The code below would declare an array of references, if we had properly initalized the references. True or false?

    int main() 
    { 
      int& x[50];
    
      return 0;
    }
    1. true

    2. false

    Answer

© 2017 QuizBucket.org