Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Exam (elaborations)

Polymorphism & Inheritence MCQ on JAVA

Rating
-
Sold
-
Pages
56
Grade
A+
Uploaded on
26-12-2023
Written in
2021/2022

This document provides mcqs on polymorphism and inheritence of JAVA with answers and suitable explanation.

Institution
Course

Content preview

Inheritance & Polymorphism
1. What is Inheritance in C++?
a) Wrapping of data into a single class
b) Deriving new classes from existing classes
c) Overloading of classes
d) Classes with same names

Answer: b
Explanation: Inheritance is the concept of OOPs in which new classes are derived from existing
classes in order to reuse the properties of classes defined earlier.

2. How many specifiers are used to derive a class?
a) 1
b) 2
c) 3
d) 4

Answer: c
Explanation: There are 3 specifiers used to derive a class. They are private, protected and public.

3. Which specifier makes all the data members and functions of base class inaccessible by
the derived class?
a) private
b) protected
c) public
d) both private and protected

Answer: a
Explanation: Private access specifier is used to make all the data members and functions of the
base class inaccessible.

4. If a class is derived privately from a base class then ______________________________
a) no members of the base class is inherited
b) all members are accessible by the derived class
c) all the members are inherited by the class but are hidden and cannot be accessible
d) no derivation of the class gives an error

Answer: c
Explanation: Whenever a class is derived, all the members of the base class is inherited by the
derived class but are not accessible by the derived class.

5. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
int a, b;

, float d;
public:
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: private A
{

};
int main(int argc, char const *argv[])
{
B b;
cout<<sizeof(B);
return 0;
}
a) 8
b) 12
c) Error
d) Segmentation fault

Answer: b
Explanation: As class B is derived from class A and class A has three members with each of 4
bytes size hence size of B equal to 3 * 4 = 12 bytes.

6. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
int a;
void change(int i){
a = i;
}
void value_of_a(){
cout<<a;
}
};
class B: public A
{
int a = 15;
public:
void print(){
cout<<a;
}
};

int main(int argc, char const *argv[])

,{
B b;
b.change(10);
b.print();
b.value_of_a();

return 0;
}
a) 1010
b) 1510
c) 1515
d) 5110

Answer: b
Explanation: When change() is called it sets parents class ‘a’ variable = 10. When print() is called
then ‘a’ from class B is printed and wehn value_of_a() is called then ‘a’ from class A is printed.

7. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A
{
float d;
public:
A(){
cout<<"Constructor of class A\n";
}
};

class B: public A
{
int a = 15;
public:
B(){
cout<<"Constructor of class B\n";
}
};

int main(int argc, char const *argv[])
{
B b;
return 0;
}
a)
Constructor of class A
Constructor of class B
b) Constructor of class A
c) Constructor of class B
d)
Constructor of class B
Constructor of class A

, Answer: a
Explanation: When a derived class is declared it calls both its constructor and the base class
constructor. It first calls the base class constructor and then its own constructor.

8. What is a virtual function in C++?
a) Any member function of a class
b) All functions that are derived from the base class
c) All the members that are accessing base class data members
d) All the functions which are declared in the base class and is re-defined/overridden by the
derived class

Answer: d
Explanation: Virtual function is a function that is declared inside the base class and is re-defined
inside the derived class.

9. Which is the correct syntax of declaring a virtual function?
a) virtual int func();
b) virtual int func(){};
c) inline virtual func();
d) inline virtual func(){};

Answer: a
Explanation: To make a function virtual function we just need to add virtual keyword at the
starting of the function declaration.

10. What will be the output of the following C++ code?
#include <iostream>
#include <string>
using namespace std;
class A{
float d;
public:
virtual void func(){
cout<<"Hello this is class A\n";
}
};

class B: public A{
int a = 15;
public:
void func(){
cout<<"Hello this is class B\n";
}
};

int main(int argc, char const *argv[])
{
B b;
b.func();
return 0;

Written for

Course

Document information

Uploaded on
December 26, 2023
Number of pages
56
Written in
2021/2022
Type
Exam (elaborations)
Contains
Questions & answers

Subjects

$8.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
sohansarkarm13

Get to know the seller

Seller avatar
sohansarkarm13 Academy of Technology
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
2 year
Number of followers
0
Documents
3
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions