Test: Function - 1


15 Questions MCQ Test Learn to Program with C++: Beginner to Expert | Test: Function - 1


Description
This mock test of Test: Function - 1 for Back-End Programming helps you for every Back-End Programming entrance exam. This contains 15 Multiple Choice Questions for Back-End Programming Test: Function - 1 (mcq) to study with solutions a complete question bank. The solved questions answers in this Test: Function - 1 quiz give you a good mix of easy questions and tough questions. Back-End Programming students definitely take this Test: Function - 1 exercise for a better result in the exam. You can find other Test: Function - 1 extra questions, long questions & short questions for Back-End Programming on EduRev as well by searching above.
QUESTION: 1

Which of the following is true about virtual functions in C++.

Solution:
QUESTION: 2

What are mandatory parts in function declaration?

Solution:

In a function, return type and function name are mandatory all else are just used as a choice.

QUESTION: 3

Output of following program

#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() { cout<<" In Base n"; }
};
 
class Derived: public Base
{
public:
    void show() { cout<<"In Derived n"; }
};
 
int main(void)
{
    Base *bp, b;
    Derived d;
    bp = &d;
    bp->show();
    bp = &b;
    bp->show();
    return 0;
}

Solution:

Initially base pointer points to a derived class object. Later it points to base class object,

QUESTION: 4

How many max number of arguments can present in function in c99 compiler?

Solution:
QUESTION: 5

What is the output of the program?

#include<iostream>
using namespace std;

class Base
{
public:
    virtual void show() = 0;
};

int main(void)
{
    Base b;
    Base *bp;
    return 0;
}

Solution:

Since Base has a pure virtual function, it becomes an abstract class and an instance of it cannot be created. So there is an error in line "Base b". Note that there is no error in line "Base *bp;". We can have pointers or references of abstract classes.

QUESTION: 6

What is the output of this program?

#include < iostream >
using namespace std;
void mani()
void mani()
{
cout << "hai";
}
int main()
{
main();
return 0;
}

Solution:

We have to use the semicolon to declare the function in line 3. If we did means, the program will execute.

QUESTION: 7

What is the output of the program?

#include<iostream>
using namespace std;
 
class Base
{
public:
    virtual void show() = 0;
};
 
class Derived: public Base
{
public:
    void show() { cout<<"In Derived n"; }
};
 
int main(void)
{
    Derived d;
    Base &br = d;
    br.show();
    return 0;
}

Solution:
QUESTION: 8

What is the scope of the variable declared in the user definied function?

Solution:

The variable is valid only in the function block as in other.

QUESTION: 9

Can a destructor be virtual? Will the following program compile?

#include <iostream>
using namespace std;
class Base {
public:
  virtual ~Base() {}  
};
int main() {
   return 0;
}

Solution:

A destructor can be virtual. We may want to call appropriate destructor when a base class pointer points to a derived class object and we delete the object. If destructor is not virtual, then only the base class destructor may be called. For example, consider the following program.

QUESTION: 10

How many ways of passing a parameter are there in c++?

Solution:

There are three ways of passing a parameter. They are pass by value,pass by reference and pass by pointer.

QUESTION: 11

Can static functions be virtual? Will the following program compile?

#include<iostream> 
using namespace std;    

class Test
{
   public:
      virtual static void fun()  { }
};

Solution:

Static functions are class specific and may not be called on objects. Virtual functions are called according to the pointed or referred object.

QUESTION: 12

By default how the value are passed in c++?

Solution:
QUESTION: 13

What is the output of the program?

#include <iostream>
using namespace std;
  
class A
{
public:
    virtual void fun() { cout << "A::fun() "; }
};
  
class B: public A
{
public:
   void fun() { cout << "B::fun() "; }
};
  
class C: public B
{
public:
   void fun() { cout << "C::fun() "; }
};
  
int main()
{
    B *bp = new C;
    bp->fun();
    return 0;
}

Solution:

The important thing to note here is B::fun() is virtual even if we have not uses virtual keyword with it. When a class has a virtual function, functions with same signature in all descendant classes automatically become virtual. We don't need to use virtual keyword in declaration of fun() in B and C. They are anyways virtual.

QUESTION: 14

How many types of returning values are present in c++?

Solution:

The three types of returning values are return by value, return by reference and return by address.

QUESTION: 15

Predict the output of following C++ program

#include<iostream>

using namespace std;

class Base
{
public:

    virtual void show() { cout<<" In Base n"; }

};

class Derived: public Base

{

public:

    void show() { cout<<"In Derived n"; }

};

int main(void)

{

    Base *bp = new Derived;

    bp->Base::show();  // Note the use of scope resolution here

    return 0;

}

Solution:

A base class function can be accessed with scope resolution operator even if the function is virtual.

Related tests