MCQs – C++ functions

(Last Updated On: March 13, 2019)
Q) In C++ code , variables can be passed to a function by
  1. Pass by value
  2. Pass by reference
  3. Pass by pointer
  4. All the above
View Answer
Answer: D
In C++ programming, all pass by value, reference and pointer are used. Note that pass by reference in C Programming language is not available. However, sometimes people use the term pass by reference instead of pass by address or pass by pointers. Read here pass by value and pass by pointer in C programming and difference between pass by pointer and pass by reference in C++ language.

 

Q) Constant function in C++ can be declared as
  1. void display()
  2. void display() const
  3. const void display()
  4. void const display()
View Answer
Answer: B

Constant function in a class is used to prevent modification of class member variables inside its body. When we only want to read member variable and use it in function body with no modification then we should use const function. Read detail of constant function in C++ object oriented programming.


Q) True and false statements about inline function in given C++ code example is/are

(I)Static function of a class can be called by class name using scope resolution operator  i.e.  : :
(II)Static function can receive both static and non-static data members of a class
(III)Static function is not the part of an object of a class

  1. I and II
  2. I only
  3. I and III
  4. I, II and III
View Answer
Q) Which of the following functions are provided by compiler by default if we don’t write in a C++ class?
  1. Copy constructor
  2. Assignment
  3. Constructor
  4. All the above
View Answer
Answer: D

All functions are provided by compiler if we don’t write in a class. Read more on default functions provided by compiler in C++