C++ quiz questions

C++ interview questions

  • 1.

    Which of the following lines should NOT compile?

    1 
    2 int main()
    3 {
    4     int a[54] = {};
    5 
    6     int b[54] = {};
    7 
    8     int* x = a;
    9 
    10    int* const y = a;
    11 
    12    b = x;
    13 
    14    b = y;
    15 
    16    return 0;
    17 } 
    1. 8, 10, 12, 14

    2. 8, 12

    3. 12, 14

    4. none

    Answer
  • 2.

    True or false? Every expression is an lvalue or an rvalue?

    1. true

    2. false

    Answer
  • 3.

    What value is printed out for the variable x?

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

    2. zero

    Answer
  • 4.

    Of the variable definitions below, which ones have external linkage and can be accessed from another translation unit?

    int w = 1;
    static int x = 2;
    const int y = 3;
    extern const int z = 4;
    
    int main(int argc, char** argv)
    {
        return 0;
    }
    1. w, x, y and z

    2. w, y and z

    3. w and z

    4. y and z

    5. none

    Answer
  • 5.

    Which of the following functions are found when called in main during name lookup?

    #include <iostream>
    
    namespace standards
    {
        struct datastructure
        {
        };
    
        void foo(const datastructure& ds)
        {
        }
    
        void bar()
        {
        }
    }
    
    int main(int argc, char** argv)
    {
        standards::datastructure ds;
    
        foo(ds);
    
        bar();
    
        return 0;
    }
    1. foo

    2. bar

    3. foo and bar

    4. neither

    Answer
  • 6.

    What value for y gets printed in the program below?

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

    2. 13

    3. undefined

    Answer
  • 7.

    In namespace foo, the function bar can access the variable x also declared in namespace foo?

    #include <iostream>
    
    namespace foo
    {
        void bar()
        {
            x++;
        }
    
        int x;
    }
    
    int main(int argc, char** argv)
    {
        return 0;
    }
    1. true

    2. false

    Answer
  • 8.

    What value does foo print out?

    #include <iostream>
    
    const int SIZE = 5;
    
    struct tester
    {
        void foo()
        {
            std::cout << SIZE << std::endl;
        }
    
        enum 
        {
            SIZE = 3
        };
    };
    
    int main(int argc, char** argv)
    {
        tester t;
        t.foo();
        return 0;
    }
    1. 5

    2. 3

    3. undefined

    Answer
  • 9.

    What value does size print out?

    #include <iostream>
    
    const int SIZE = 5;
    
    struct tester
    {
        int array[SIZE];
    
        enum 
        {
            SIZE = 3
        };
    
        void size()
        {
            std::cout << sizeof(array) / sizeof(int);
        }
    };
    
    int main(int argc, char** argv)
    {
        tester t;
        t.size();
        return 0;
    }
    1. 5

    2. 3

    3. undefined

    Answer
  • 10.

    From line 6 in the code below which labels can you access using a goto statement?

    1
    2
    3  void foo(int x)
    4  {
    5  pointa:
    6     
    7  pointb:
    8         
    9  pointc:
    10     printf("end of function\n");
    11 }   
    12     
    13 int main(int argc, char** argv)
    14 {   
    15     foo(5);
    16     
    17 pointd:
    18 
    19     return 0;
    20 }
    1. a, b, c, d

    2. a, b, c

    3. b, c

    4. b, c, d

    Answer
  • 11.

    The variable x is accesible in the else clause of this program?

    int main(int argc, char** argv)
    {
        if ( argc > 2 )
        {
            int x = 5;
        }
        else
        {
      
        }
    
        return 0;
    }
    1. true

    2. false

    Answer
  • 12.

    Given the code below, the variable y can be accessed in which blocks of code?

    1  int main(int argc, char** argv)
    2  {
    3
    4   if ( argc > 10 )
    5   {
    6
    7   }
    8   else if (int y = argc - 1 )
    9   {
    10
    11  }
    12  else
    13  {
    14
    15  }
    16
    17  return 0;
    18  }
    1. lines 4-15

    2. lines 4-17

    3. lines 8-11

    4. lines 8-15

    5. lines 8-17

    Answer
  • 13.

    Which of the following variables can be accessed in foo's function try block handler?

    void foo(int x) try
    {
        int y = 2;
        throw 1;
    }
    catch(int e)
    {
    }
    
    int main(int argc, char** argv)
    {
        foo(3);
        return 0;
    }
    1. x and y and e

    2. x and e

    3. y and e

    4. e

    Answer
  • 14.

    The code below is legal?

    int x = 5;
    
    template <typename T>
    class x
    {
        T member;
    };
    
    int main(int argc, char** argv)
    {
        class x<int> y;
    
        return 0;
    }
    1. true

    2. false

    Answer
  • 15.

    The below is legal

    int x = 5;
    
    class x
    {
    };
    
    int main(int argc, char** argv)
    {
        class x y;
    
        return 0;
    }
    1. true

    2. false

    Answer
  • 16.

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

    const int x = 5;
    int main(int argc, char** argv)
    {
        int x[x];
    
        int y = sizeof(x) / sizeof(int);
    
        return 0;
    }
    1. 0

    2. 5

    3. 20

    4. undefined

    Answer
  • 17.

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

    int x = 5;
    int main(int argc, char** argv)
    {
        int x = x;
        return 0;
    }
    1. 0

    2. 5

    3. undefined

    Answer
  • 18.

    non-const static member variables must be defined outside of the class for them to be used

    struct test
    {
        static int x;
    };
    
    int test::x;
    1. true

    2. false

    Answer
  • 19.

    The code below declares and defines variable x

    extern int x;

     

    1. true

    2. false

    Answer
  • 20.

    The process of building new classes from existing one is called ______

     

    1. Polymorphism

    2. Structure

    3. Inheritance

    4. Cascading

    Answer

© 2017 QuizBucket.org