C++ quiz questions

C++ interview questions

  • 1.

    What is the output of the following code

     

    char symbol[3]={a,b,c};
    
    for (int index=0; index < 3; index++)
    
    cout << symbol [index];

     

    1. abc

    2. a b c

    Answer
  • 2.

    The members of a class, by default, are

    1. public

    2. protected

    3. private

    4. mandatory to specify

    Answer
  • 3.

    In which case is it mandatory to provide a destructor in a class?

     

    1. Almost in every class

    2. Class for which two or more than two objects will be created

    3. Class for which copy constructor is defined

    4. Class whose objects will be created dynamically

    Answer
  • 4.

    Consider the following statements

    char *ptr;
    ptr = hello;
    cout << *ptr;

    What will be printed?

    1. first letter

    2. entire string

    3. it is a syntax error

    4. last letter

    Answer
  • 5.

    Declaration of a pointer reserves memory space

    1. for the object.

    2. for the pointer.

    3. both for the object and the pointer

    4. none of these.

    Answer
  • 6.

    According to the C++11 standard, what is the output of this program?

    #include <iostream>
    
    extern "C" int x;
    extern "C" { int y; }
    
    int main() {
    
    	std::cout << x << y;
    
    	return 0;
    }

     

    1. Program is undefined

    2. Compilation error

    3. Program output something

    Answer
  • 7.

    According to the C++11 standard, what is the output of this program?

    #include <iostream> 
    
    typedef long long ll;
    
    void foo(unsigned ll) {
        std::cout << "1";
    }
    
    void foo(unsigned long long) {
        std::cout << "2";
    }
    
    int main() {
        foo(2ull);
    }

     

    1. 1

    2. 2

    3. 12

    4. 21

    Answer
  • 8.

    According to the C++11 standard, what is the output of this program?

    #include <iostream>
    
    void f(float) { std::cout << 1; }
    void f(double) { std::cout << 2; }
    
    int main() {
      f(2.5);
      f(2.5f);
    }

     

    1. 12

    2. 21

    Answer
  • 9.

    According to the C++11 standard, what is the output of this program?

    #include 
    
    void f(int) { std::cout << 1; }
    void f(unsigned) { std::cout << 2; }
    
    int main() {
      f(-2.5);
    }

     

    1. Compilation error

    2. -2

    3. -3

    Answer
  • 10.

    According to the C++11 standard, what is the output of this program?

    #include <iostream>
    
    int main() {
        int i=1;
        do {
            std::cout << i;
            i++;
            if(i < 3) continue;
        } while(false);
        return 0;
    }

     

    1. 1

    2. 2

    3. 3

    4. 4

    Answer
  • 11.

    According to the C++11 standard, what is the output of this program?

    #include <iostream>
    
    struct A {
      A() { foo(); }
      virtual ~A() { foo(); }
      virtual void foo() { std::cout << "1"; }
      void bar() { foo(); }
    };
    
    struct B : public A {
      virtual void foo() { std::cout << "2"; }
    };
    
    int main() {
      B b;
      b.bar();
    }

     

    1. 121

    2. 121

    3. 1221

    4. 122

    Answer
  • 12.

    What is the output:

    #include <iostream>
    
    int main() {
        char s[51];
    
        std::cout << dot::leader(s) <<  10 << std::endl;
        std::cout << hat::leader(s) << 120 << std::endl;
    }

     

    1.  ...............10
      
       ^^^^^^^^^^^^^^120
    2.  .............10
      
       ^^^^^^^^^^^^120
    3.  ...............10
      
       ^^^^^^^^^^^^^^^^120
    4.  .................10
      
       ^^^^^^^^^^^^^^120
    Answer

© 2017 QuizBucket.org