Programming MCQ

Programming MCQ

Which of the followings is/are automatically added to every class, if we do not write our own?

  • Copy constructor
  • Assignment operator
  • A Constructor without any parameter
  • All the above

All the above

 When a copy constructor may be called?

  • When an object of a class returned by a value
  • When an object of a class is passed by a value as an argument
  • When an object is constructed based on another object of same class
  • All of the above

All the above

#include
using namespace std;
class Point {
    Point() { cout << “”Constructor called””; }
};

int main()
{
   Point t1;
   return 0;
}

  • Compile error
  • Runtime error
  • Constructor called
  • linker error

Compile time error

#include
using namespace std;
class Point {
public:
    Point() { cout << “”Constructor called””; }
};

int main()
{
   Point t1, *t2;
   return 0;
}

  • Compile error
  • Constructor called
    Constructor called
  • Constructor called
  • Runtime error

Constructor called

#include
using namespace std;

class X
{
public:
    int x;
};

int main()
{
    X a = {10};
    X b = a;
    cout << a.x << “” “” << b.x;
    return 0;
}

  • Compile error
  • 10 followed by Garbage value
  • 10 10
  • 10 0

10 10

Which of the following is FALSE about references in C++

  • References cannot be NULL
  • A reference must be initialized when declared
  • Once reference is created, it cannot be later made to reference another object; it cannot be reset
  • References cannot refer to constant value.

References cannot refer to constant value.

Which of the following functions must use reference?

  • Assignment operator function
  • Copy constructor
  • Destructor
  • Parameterized Constructor

Copy constructor

Predict the output
#include
using namespace std;

int &fun()
{
    static int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}

  • Compiler error:Function cannot be used as value
  • 10
  • 30
  • runtime error

30

#include
using namespace std;

int &fun()
{
    int x = 10;
    return x;
}
int main()
{
    fun() = 30;
    cout << fun();
    return 0;
}

  • May cause runtime error
  • May cause compiler error
  • Always works fine
  • 0

May cause runtime error

#include
using namespace std;
int main()
{
  int x = 10;
  int& ref = x;
  ref = 20;
  cout << “”x = “” << x << endl ;
  x = 30;
  cout << “”ref = “” << ref << endl;
  return 0;
}

  • x = 20
  • Ref = 30
  • x = 20
    Ref = 20
  • x = 10
    Ref = 30

May cause runtime error

#include
using namespace std;
int main()
{
  int x = 10;
  int& ref = x;
  ref = 20;
  cout << “”x = “” << x << endl ;
  x = 30;
  cout << “”ref = “” << ref << endl;
  return 0;
}

  • x = 20
  • Ref = 30
  • x = 20
    Ref = 20
  • x = 10
    Ref = 30

x = 20

#include
using namespace std;

class Empty {};

int main()
{
  cout << sizeof(Empty);
  return 0;
}

  • 0
  • Garbage Value
  • Compiler error
  • Runtime error

Compiler error

Which of the following is true?

  • All objects of a class share all data members of class
  • Objects of a class do not share non-static members. Every object has its own copy.
  • Objects of a class do not share codes of non-static methods, they have their own copy
  • None of the above

Objects of a class do not share non-static members. Every object has its own copy.

Which of the following is true?

  • All objects of a class share all data members of class
  • Objects of a class do not share non-static members. Every object has its own copy.
  • Objects of a class do not share codes of non-static methods, they have their own copy
  • None of the above

Objects of a class do not share non-static members. Every object has its own copy.

A member function can always access the data in __________(in C++).

  • The class of which it is member
  • The object of which it is a member
  • The public part of its class
  • The private part of its class

The class of which it is member

Which of the following is not correct for virtual function in C++ ?

  • Must be declared in public section of the class
  • Virtual function can be static
  • Virtual function should be accessed using pointers
  • Virtual function is defined in base class

Virtual function can be static

Which of the following cannot be passed to a function in C++ ?

  • Constant
  • Structure
  • Array
  • Header file

Header file

Which of the following is not a correct statement?

  • Every class containing abstract method must be declared abstract.
  • Abstract class can be initiated.
  • Abstract class does not contain any definition of implementation
  • Abstract class can directly be initiated with ‘new’ operator.

Abstract class can directly be initiated with ‘new’ operator.

Want to know more about our courses?

Please Sign Up to Download

Please Sign Up to Download

Enquire Now

Please Sign Up to Download

Enquiry Form