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?

Enquire Now

Enquire Now

Please Sign Up to Download

Please Sign Up to Download

Enquire Now

Please Sign Up to Download

Enquiry Form