C++ quiz questions

C++ interview questions

  • 1.

    The code below is legal. True or false?

    #include <iostream>
    
    const char* Foo()
    {
      return "Hello World";
    }
    
    int main() 
    { 
      const char* str = Foo();
      std::cout << str << std::endl;
    
      return 0;
    }
    1. true

    2. false

    Answer
  • 2.

    What value gets printed by the program?

    #include <iostream>
    
    struct Foo
    {
      Foo(int d) : x(d) {}
      int x;
    };
    
    int main() 
    { 
      double x = 3.14;
    
      Foo f( int(x) );
    
      std::cout << f.x << std::endl;
    
      return 0;
    }
    1. 0

    2. 3

    3. 3.14

    4. nothing, its ill-formed

    5. implementation-defined

    Answer
  • 3.

    What value gets printed by the program?

    #include <iostream>
    
    int main() 
    { 
      int x = 3;
    
      switch(x)
      {
        case 0:
          int x = 1;
          std::cout << x << std::endl;
        break;
        case 3:
          std::cout << x << std::endl;
        break;
        default:
          x = 2;
          std::cout << x << std::endl;
      }
    
      return 0;
    }
    1. 0

    2. 1

    3. 2

    4. 3

    5. nothing, it is ill-formed

    Answer
  • 4.

    What value gets printed by the program?

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

    2. 2

    3. 3

    4. 6

    5. undefined

    Answer
  • 5.

    What value gets printed by the program?

    #include <iostream>
    
    struct Foo
    {
      int x;
    
      operator int()
      {
        return 21;
      }
    };
    
    int main(int argc, char** argv)
    {
      Foo f;
      f.x = 11;
    
      std::cout << (0?3:f) << std::endl;
    
      return 0;
    }
    1. 0

    2. 3

    3. 11

    4. 21

    5. undefined

    Answer
  • 6.

    What value gets printed by the program?

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

    2. 2

    3. 3

    4. 4

    5. undefined

    Answer
  • 7.

    True or False, the program below will print NOT EQUAL

    #include <iostream>
    
    struct Foo
    {
    };
    
    struct Bar 
    {
    };
    
    int main(int argc, char** argv)
    {
      Foo* f = new Foo;
      Bar* b = new Bar;
    
      if ( f == b )
        std::cout << "EQUAL" << std::endl;
      else
        std::cout << "NOT EQUAL" << std::endl;
    
      return 0;
    }
    1. true

    2. false

    Answer
  • 8.

    Will "Hello World" be printed by the program below?

    struct Foo
    {
      int x;
      int y;
    };
    
    int main(int argc, char** argv)
    {
      Foo f;
    
      if ( &f.x < &f.y )
      {
        std::cout << "Hello World" << std::endl;
      }
    
      return 0;
    }
    1. yes

    2. no

    3. implementation defined

    4. unspecified

    5. undefined

    Answer
  • 9.

    Will "Hello World" be printed by the program below?

    int main(int argc, char** argv)
    {
      int array[33];
    
      if ( &array[4] < &array[23] )
      {
        std::cout << "Hello World" << std::endl;
      }
    
      return 0;
    }
    1. yes

    2. no

    3. implementation defined

    4. unspecified

    5. undefined

    Answer
  • 10.

    Given the assumptions in the comments, what values is printed for x?

    int main(int argc, char** argv)
    {
      // assume result printed is 4
      std::cout << sizeof(int) << std::endl;
    
      int x = 0x1000;
    
      x = x << 32;
      
      std::cout << std::hex << x << std::endl;
    
      return 0;
    }
    1. 0x00000000

    2. 0xFFFFFFFF

    3. implementation defined

    4. undefined

    Answer
  • 11.

    If assumptions in the code below are true what value would be the final value printed by this program?

    int main(int argc, char** argv)
    {
      // assume result printed is 4
      std::cout << sizeof(int) << std::endl;
    
      int *x = new int;
      
      // assume result printed is 0x60000000
      std::cout << x << std::endl;
    
      std::cout << x + 3 << std::endl;
    
      return 0;
    }
    1. 0x60000000

    2. 0x60000003

    3. 0x6000000C

    4. implementation defined

    5. undefined

    Answer
  • 12.

    What is the value of x at the end of main?

    int main(int argc, char** argv)
    {
      int x = 50 % -7;
      return 0;
    }
    1. 1

    2. -1

    3. implementation defined

    4. undefined

    5. compiler error

    Answer
  • 13.

    Which lines of code below should cause the program to be undefined?

     1 struct Foo
     2 {
     3   virtual ~Foo() {}
     4 };
     5
     6 struct Bar : public Foo
     7 {
     8 };
     9
    10 int main(int argc, char** argv)
    11 {
    12   Foo* f = new Bar;
    13   delete f;
    14   f = 0;
    15   delete f;
    16
    17   Foo* fa = new Bar[10];
    18   delete [] fa;
    19   fa = 0;
    20   delete fa;
    21
    22   return 0;
    23 }
    1. none

    2. 13

    3. 15

    4. 18

    5. 20

    Answer
  • 14.

    In addition to c-style, which casts can be used to cast an int to a pointer or a pointer to an int?

    1. static_cast

    2. dynamic_cast

    3. reinterpret_cast

    4. static_cast or dynamic_cast

    5. none of the above

    Answer
  • 15.

    In addition to c-style, which casts can be used to cast an int into an enum?

    1. static_cast

    2. dynamic_cast

    3. reinterpret_cast

    4. static_cast or reinterpret_cast

    5. none of the above

    Answer
  • 16.

    In addition to const_cast, which cast can be used to cast away constness?

    1. static_cast

    2. dynamic_cast

    3. reinterpret_cast

    4. none of the above

    Answer
  • 17.

    Which lines of code below should not compile?

    1  struct Foo
    2  {
    3  };
    4 
    5  struct Bar
    6  {
    7  };
    8 
    9 
    10 int main()
    11 {
    12     Foo* f = new Foo;
    13 
    14     Bar* b1 = f;
    15     Bar* b2 = static_cast<Bar*>(f);
    16     Bar* b3 = dynamic_cast<Bar*>(f);
    17     Bar* b4 = reinterpret_cast<Bar*>(f);
    18     Bar* b5 = const_cast<Bar*>(f);
    19 
    20     return 0;
    21 }
    1. 14

    2. 14,15,18

    3. 14,17,18

    4. 14,15,16,18

    Answer
  • 18.

    What value should be printed for x?

    #include <iostream>
    
    int main()
    {
        int x = int() = 3;
    
        std::cout << x << std::endl;
    
        return 0;
    }
    1. 0

    2. 3

    3. undefined

    4. won't compile

    Answer
  • 19.

    A function call is always an rvalue. True or false?

    1. true

    2. false

    Answer
  • 20.

    Which of the following lines should NOT compile?

    1 int main()
    2 {
    3     int a = 2;
    4 
    5     int* b = &a;
    6 
    7     int const* c = b;
    8 
    9     b = c;
    10 
    11    return 0;
    12 } 
    1. 7 and 9

    2. 7

    3. 9

    4. none

    Answer

© 2017 QuizBucket.org