C++ quiz questions

C++ interview questions

  • 1.

    What is the size of wchar_t in C++?

    1. 2

    2. 4

    3. 2 or 4

    4. based on the number of bits in the system

    Answer
  • 2.

    What gets printed by the code below?

    01 #include <iostream>
    02
    03 class A
    04 {
    05 public:
    06    void foo() const { std::cout << "A"; }
    07 };
    08
    09 class B
    11 {
    12 public:
    13    void foo() const { std::cout << "B"; }
    14 };
    15
    16 class C : public A, public B
    17 {
    18    using A::foo;
    19 };
    20
    21 int main()
    22 {
    23    C c;
    24    c.foo();
    25    return 0;
    26 }
    1. A

    2. B

    3. AB

    4. Compiler error on line 24

    5. Undefined behavior

    Answer
  • 3.

    According to the C++ standard which of the following declarations for the "main" function are valid?

    1 int main(int argc, char** argv)
    2 int main(char** argv, int argc)
    3 int main()
    4 inline int main(int argc, char* argv[])
    5 int main(char* argv[], int argc)
    6 void main()
    7 int main(int argc, char* argv[])
    8 static int main()
    9 int main(int argc, char* argv[], char* options[])
    1. 1, 6, 9

    2. 3, 4, 5, 7

    3. 1, 4, 8

    4. 3, 7, 9

    5. 3, 6

    Answer
  • 4.

    What gets printed?

    01 #include <iostream>
    02
    03 int main()
    04 {
    05    for (int i = 0; i < 4; ++i)
    06    {
    07       switch (i)
    08       {
    09          case 0  : std::cout << "0";
    10          case 1  : std::cout << "1"; continue;
    11          case 2  : std::cout << "2"; break;
    12          default : std::cout << "D"; break;
    13       }
    14       std::cout << ".";
    15    }
    16    return 0;
    17 }
    1. 0.1.2.

    2. 01.2.D.

    3. 011.2.D

    4. 0112.D.

    5. Compiler error on line 10

    Answer
  • 5.

    Given the code below what will be the output to stdout?

    #include <cstdio>
    
    00 void foo(int v1, int v2, int v3, int v4)
    01 {
    02    printf("%d %d %d %d\n",v1,v2,v3,v4);
    03 }
    04
    05 int main()
    06 {
    07    int lut[] = { 1, 2, 3, 4 };
    08    int idx = 0;
    09    foo(lut[idx++],lut[idx++],lut[idx++],lut[idx++]);
    10    return 0;
    11 }
    1. 1 2 3 4

    2. 0 1 2 3

    3. 4 3 2 1

    4. Compiler Error

    5. Undefined behavior

    Answer
  • 6.

    Which of the following implementations of the reset function is best for initializing the array to all zero.

    class foo{
    public:
        foo(){
            reset();
        }
    private:
        void reset(){
    
        // A // memset(x, 0, 50);
        // B // memset(x, 0, sizeof(x));
        // C // memset(x, 0, 50 * 4);
        // D // memset(x, 0, 50 * sizeof(x));
        }
    
        long x[50];
    };
    1. A

    2. B

    3. C

    4. D

    5. none of them will work

    Answer
  • 7.

    What should get printed in the program below?

    #include <iostream>
    using namespace std;
    
    class foo{
    public:
        foo() : z(x+1), y(2), x(3)
        {
            cout << "z: " << z << endl;
        }
    private:
        int x;
        int y;
        int z;
    };
    
    int main(int argc, char** argv){
        foo f;
        return 0;
    }
    1. 1

    2. 2

    3. 3

    4. 4

    5. undefined

    Answer
  • 8.

    If you have an array of characters allocated with new. Which of the following is the best way to modify the size of the array?

    1. Use the realloc function from libc

    2. Delete the existing array, then allocate a new array and copy the data from the old array to the new array

    3. Allocate a new array, copy the data from old array to the new array and then delete the old array

    4. It is not possible to do such an operation in C++

    Answer
  • 9.

    What gets printed?

    #include <iostream>
    
    struct mybase
    {
      int x;
      
      template <int RANGE>
      virtual void print()
      {
        std::cout << RANGE + x + 1 << std::endl;
      }
    };
    
    struct myderived : public mybase
    {
      template <int RANGE> 
      void print()
      {
        std::cout << RANGE + x + 2 << std::endl;
      }
    };
    
    int main(int argc, char** argv)
    {
      mybase* b = new myderived;
    
      b->x = 1;
    
      b->print<5>();
    
      return 0;
    }
    1. 2

    2. 6

    3. 7

    4. 8

    5. code is ill-formed

    Answer
  • 10.

    What gets printed?

    #include <iostream>
    
    int foo(int i)
    {
      return 2;
    }
    
    double foo(double d)
    {
      return 4.0;
    }
    
    struct Computer
    {
      int foo(int i)
      {
        return 8; 
      }
    };
    
    struct Gateway : public Computer
    {
      double foo(double d)
      {
        return 16.0; 
      }
    };
    
    int main(int argc, char** argv)
    {
      Gateway g;
    
      std::cout << foo(1) + foo(1.0) + g.foo(1) + g.foo(1.0) << std::endl;
    
      return 0;
    }
    1. 20

    2. 30

    3. 38

    4. 40

    5. The code is ill-formed

    Answer
  • 11.

    Which lines below are ill-formed?

    1 #include <iostream>
    2
    3 struct A
    4 {
    5   A(int& var) : r(var) {}
    6
    7   int &r;
    8 };
    9
    10 int main(int argc, char** argv)
    11 {
    12   int x = 23;
    13
    14   A a1(x);
    15
    16   A a2 = a1;
    17
    18   a2 = a1;
    19
    20   return 0;
    21 }
    1. 14

    2. 16

    3. 18

    4. 14, 16

    5. 16, 18

    Answer
  • 12.

    What gets printed?

    #include <iostream>
    
    struct A
    {
      A() : val() {}
      A(int v) : val(v) {}
      A(A a) : val(a.val) {} 
    
      int val;
    };
    
    int main(int argc, char** argv)
    {
      A a1(5);
      A a2(a1);
    
      std::cout << a1.val + a2.val << std::endl;
    
      return 0;
    }
    1. 0

    2. 5

    3. 10

    4. code is ill-formed

    5. unspecified

    Answer
  • 13.

    What gets printed?

    #include <iostream>
    
    struct A
    {
      A() : val(0) {}
      A(int v) : val(v) {}
      A(A& a) : val(a.val) {} 
    
      int val;
    };
    
    int main(int argc, char** argv)
    {
      const A a1;
      const A a2(5);
      const A a3 = a2;
    
      std::cout << a1.val + a2.val + a3.val << std::endl;
    
      return 0;
    }
    1. 0

    2. 5

    3. 10

    4. code is ill-formed

    5. undefined

    Answer
  • 14.

    What is the maximum number of implicitly defined constructors that this struct will have?

    struct A
    {
      A(A& a) { }
      A(double d) {}
      int val;
    };
    1. 0

    2. 1

    3. 2

    4. implementation specified

    5. undefined

    Answer
  • 15.

    What gets printed?

    #include <iostream>
    
    struct Car
    {
      Car() : price(20000) {}
      Car(double b) : price(b*1.1) {}
      double price;
    };
    
    struct Toyota : public virtual Car
    {
      Toyota(double b) : Car(b) {}
    };
    
    struct Prius : public Toyota
    {
      Prius(double b) : Toyota(b)  {}
    };
    
    int main(int argc, char** argv)
    {
      Prius p(30000);
    
      std::cout << p.price << std::endl;
    
      return 0;
    }
    1. 20000

    2. 22000

    3. 30000

    4. 33000

    5. code is ill-formed

    Answer
  • 16.

    What gets printed for the value of z?

    #include <iostream>
    
    struct Foo
    {
      Foo(int n) : x(n++), y(n++), z(n++) {}
      int x;
      int y;
      int z;
    };
    
    int main(int argc, char** argv)
    {
      Foo f(3);
    
      std::cout << "x: " << f.x << std::endl;
      std::cout << "y: " << f.y << std::endl;
      std::cout << "z: " << f.z << std::endl;
    
      return 0;
    }
    1. 3

    2. 4

    3. 5

    4. code is ill-formed

    5. undefined

    Answer
  • 17.

    Which of the following statements are true?

    1. Conversion functions of class A convert from class A into another type
    2. Conversion functions of class A are used to convert from another given type into class A
    3. Converting constructors must be callable with a single argument
    1. 1

    2. 2

    3. 3

    4. 1 and 3

    5. 2 and 3

    Answer
  • 18.

    Which lines below should not compile?

    1 struct A
    2 {
    3    A(int x) : n(x) {}
    4    int n;
    5 };
    6
    7 int main(int argc, char** argv)
    8 {
    9    A a1;
    10   A a2(2);
    11   A a3(a2);
    12   return 0;
    13 }
    1. none

    2. 9

    3. 10

    4. 11

    5. 9 and 10 and 11

    Answer
  • 19.

    Which lines below should not compile?

    1 #include <iostream>
    2
    3 class Bar
    4 {
    5 protected:
    6   static int x;
    7   int y;
    8 };
    9
    10 int  Bar::x = 33;
    11
    12 class Barrel : public Bar
    13 {
    14 public:
    15   void foo(Bar* b, Barrel* d)
    16   {
    17     b->y = 0;
    18     d->y = 0;
    19     Bar::x = 0;
    20     Barrel::x = 0;
    21   }
    22 };
    23
    24 int main(int argc, char** argv)
    25 {
    26   Barrel b;
    27   b.foo(&b, &b);
    28   return 0;
    29 }
    1. none

    2. 17

    3. 18

    4. 19

    5. 20

    Answer
  • 20.

    What gets printed by this program?

    #include <iostream>
    
    struct Shape
    {
      virtual void print()
      {
        std::cout << "SHAPE" << std::endl;
      }
      virtual ~Shape() {}
    };
    
    struct Box : private Shape
    {
      virtual void print()
      {
        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

© 2017 QuizBucket.org