0% found this document useful (0 votes)
248 views

Polymorphism Interview Questions and Answers

(1) Polymorphism in Java means "one name many forms" where an object can produce different behaviors in different circumstances. (2) There are two types of polymorphism in Java: compile-time (static) polymorphism and run-time (dynamic) polymorphism. Compile-time polymorphism is achieved through method overloading while run-time polymorphism uses method overriding. (3) Polymorphism can only be achieved through an object's behavior, not its properties. An object's methods can have different implementations based on the object's type at run-time.

Uploaded by

sureshroopa2k15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
248 views

Polymorphism Interview Questions and Answers

(1) Polymorphism in Java means "one name many forms" where an object can produce different behaviors in different circumstances. (2) There are two types of polymorphism in Java: compile-time (static) polymorphism and run-time (dynamic) polymorphism. Compile-time polymorphism is achieved through method overloading while run-time polymorphism uses method overriding. (3) Polymorphism can only be achieved through an object's behavior, not its properties. An object's methods can have different implementations based on the object's type at run-time.

Uploaded by

sureshroopa2k15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Polymorphism Interview Questions and Answers

Q1 What is Polymorphism in Java?

You can find the answer here.

Q2 What are the different types of Polymorphism in Java?

There are two types of Polymorphism in Java:

1. Static Polymorphism (Compile time Polymorphism)

2. Dynamic Polymorphism (Runtime Polymorphism)

Q3 What is the difference between Polymorphism and Inheritance in Java?

The major differences between Polymorphism and Inheritance in Java are listed below:

1. Inheritance shows the parent-child relationship between two classes whereas


Polymorphism takes the advantage of that relationship to make the program more
dynamic.

2. One of the advantages of Inheritance is code reusability in child class. The child class
inherits behavior from the parent class whereas Polymorphism enables the child class to
redefine already defined behavior inside the parent class.

Q4 What is static polymorphism (compile time polymorphism) in Java?

Static Polymorphism is a form of polymorphism that decides which method to execute at


compile time.

Q5 How you can achieve static polymorphism in Java?

Static polymorphism in Java can be achieved through Method overloading, Constructor


overloading, and Operator overloading.

Q6 What is Method overloading in Java?

You can find the method overloading in detail here.

Q7 What is dynamic polymorphism (runtime polymorphism) in Java?

Dynamic polymorphism or runtime polymorphism is a form of polymorphism where a call


to an overridden method is resolved at runtime rather than compile-time. It is also known
as Dynamic method dispatch.

Q8 How you can achieve dynamic polymorphism in Java?

Dynamic polymorphism in Java can be achieved through Method overriding.

Q9 Output of this program is :


class PolymorphismDemoClass {
void m1(String x){
System.out.println("One");
}
}
class PolymorphismDemoClassChild extends PolymorphismDemoClass {

}
public class Test{
public static void main(String[] args){
PolymorphismDemoClass parent = new PolymorphismDemoClassChild();
parent.m1(new PolymorphismDemoClass());
}
}

a. Compile time error

b. Runtime error

c. Prints One

d. Compiles properly and prints nothing

The correct answer is A.

Q10 Which statements are true

a. Private methods can be overridden

b. Static binding means overloading

c. Overriding can only be done in subclass

d. Main method can be overloaded

The correct answers are B, C, AND D.

Q11 Output of the program is

public class Boxing1 {


static void method(int x) {
System.out.println("prints int");
}
static void method(Integer i) {
System.out.println("prints Integer");
}
public static void main(String args[]){
short no=10;
method(no);
}
}

a. Compile Time Error

b. prints int then prints Integer

c. prints int

d. prints Integer

The correct answer is C.

Q12 Output of the program is

public class PolymorphismDemoClass3 {


static void overloadedMethod(Integer x) {
System.out.println("Integer");
}
static void overloadedMethod(Integer... x) {
System.out.println("Integer...");
}

public static void main(String args[]) {


int x=251;
overloadedMethod(x);
}
}

a. Integer

b. Integer...

c. Compile Time Error

d. Integer then Integer....

The correct answer is A.

Q13 Output of the program is

public class PolymorphismDemoClass4{


static void overloadedMethod(Long l) {
System.out.println("Long");
}
public static void main(String args[]){
int a=30;
overloadedMethod(a);
}
}

a. Compile Time Error

b. prints Long

c. Runtime Exception

d. None

The correct answer is A.

Q14 Output of the program is

public class Test {


public static void print (int i) {
System.out.println("int");
}
public static void print (long i) {
System.out.println("long");
}
public static void main(String a[]) {
int i = 5;
Test.print(i);
}
}

a. Compile Time Error

b. long

c. int

d. Runtime Exception

The correct answer is C.

Q15 Output of the program is

public class Test2 {


public static void print (Integer i) {
System.out.println("Integer");
}
public static void print (Long i) {
System.out.println("Long");
}
public static void main(String a[]) {
int i = 5;
Test2.print(i);
}
}

a. Compile Time Error

b. Long

c. Integer

d. Runtime Exception

The correct answer is C.

Q16 Output of the program is

public class Test3 {


public static void print (Double i){
System.out.println("Double");
}
public static void print (Long i){
System.out.println("Long");
}
public static void main(String a[]){
int i = 5;
Test3.print(i);
}
}

a. Compile Time Error

b. prints Long

c. prints Double

d. Runtime Exception

The correct answer is A.

Q17 Output of the program is

public class Test4 {


public static void print (Double i) {
System.out.println("Double");
}
public static void print (Long i) {
System.out.println("Long");
}
public static void print (Number i) {
System.out.println("Number");
}
public static void main(String a[]) {
int i = 5;
Test4.print(i);
}
}

a. Compile Time Error

b. Long

c. Double

d. Number

The correct answer is D.

Q18 Output of the program is

public class Test5 {


public static void print (Double i) {
System.out.println("Double");
}
public static void print (Long i) {
System.out.println("Long");
}
public static void print (Integer i) {
System.out.println("Integer");
}
public static void main(String a[]){
byte i = 5;
Test5.print(i);
}
}

a. Compile Time Error

b. prints Long

c. prints Double

d. prints Integer

The correct answer is A.

Q19 Output of the program is

public class Test6 {


public static void print (byte... i) {
System.out.println("byte");
}
public static void print (long i) {
System.out.println("long");
}
public static void print (Integer i) {
System.out.println("Integer");
}
public static void main(String a[]) {
byte i = 5;
Test6.print(i);
}
}

a. Compile Time Error

b. long

c. byte

d. Integer

The correct answer is B.

Q20 Output of the program is

public class Test7 {


public static void print (int i) {
System.out.println("int");
}
public static void main(String a[]) {
byte i = 5;
Test7.print(i);
}
}

a. Compile Time Error

b. int

c. Runtime error

The correct answer is B.

Q21 Output of the program is

public class Test8 {


public static void main (String[] args)
{
int i = 5;
overloadedMethod(i);
}

static void overloadedMethod (Integer x)


{
System.out.println("Integer");
}

static void overloadedMethod (long x)


{
System.out.println("long");
}
}

a. Compile Time Error

b. long

c. long Integer

d. Integer

The correct answer is B.

Q22 Output of the program is

public class Test9 {


public static void main (String[] args)
{
byte b = 5;
overloadedMethod(b);
}

static void overloadedMethod (Object b)


{
Byte obj = (Byte)b;
System.out.println("byte: "+obj);
}
}

a. Compile Time Error

b. byte : memory address of object obj

c. byte: 5

d. Runtime error

The correct answer is C.

Q23 Output of the program is

public class Test10


{
// Overloaded methods
public void overloadedMethod(Integer i)
{
System.out.println("Integer ");
}
public void overloadedMethod(String name)
{
System.out.println("String ");
}
public void overloadedMethod(Double name)
{
System.out.println("Double ");
}

// Driver code
public static void main(String [] args)
{
Test10 obj = new Test10();
obj.overloadedMethod(null);
}
}

a. Compile Time Error

b. prints Double

c. prints Integer

d. prints String

The correct answer is A.

Q24 Output of the program is

public class Test11


{
// Overloaded methods
public void overloadedMethod(Integer x)
{
System.out.println("Integer");
}
public void overloadedMethod(String name)
{
System.out.println("String");
}
public void overloadedMethod(Double name)
{
System.out.println("Double ");
}

public static void main(String [] args)


{
Test11 obj = new Test11();
Integer arg = null;
obj.overloadedMethod(arg);
}
}

a. Compile Time Error

b. Double

c. Integer

d. String

The correct answer is C.


(1) What is polymorphism in java?
In java, Polymorphism means "one name many forms".
In other words, you can say whenever an object
producing different-different behavior in different-
different circumstances is called polymorphism in java.

(2) Give real-life examples of polymorphism


Let's understand real-time example of polymorphism.

1) Related to person

 Suppose you are in the classroom that time you will


behave like a student.
 Suppose when you at home you behave like son or
daughter.
 Suppose you are in the market at that time you
behave like a customer.

2) Related to product

Let's take an example of an Air conditioner which


produces hot air in winter and cold air in summer.

So there is one name but it producing different -


different output according to the situation.

(3) How many types are of polymorphism in java?


There are two types of polymorphism in java and these
are given below.

 Compile-Time polymorphism or Static


polymorphism.
 Run-time polymorphism or Dynamic polymorphism.

(4) Can we achieve polymorphism through data


member?
No, Polymorphism is always achieved via behavior of an
object only properties of an object do not play any role
in case of polymorphism.

(5) What are compile-time and run-time polymorphism?


Compile-Time Polymorphism

The best example of compile-time or static


polymorphism is the method overloading. Whenever an
object is bound with their functionality at compile time is
known as compile-time or static polymorphism in java.

Run-Time Polymorphism

The best example of run-time or dynamic polymorphism


is the method overriding. Whenever an object is bound
with their functionality at run-time is know as run-time
or dynamic polymorphism in java.

(6) What is method overloading?


Whenever we have more than one same name method
but a different number of arguments or different data
types in the same class is known as method overloading
in java. Method overloading the example of compile-time
polymorphism.
For example:

class Addition

void add(int a, int b)//with 2 arguments

System.out.println(a+b);

void add(int a, int b, int c)//change no of arguments i.e


3

System.out.println(a+b+c);

public static void main(String args[])

Addition a = new Addition();

a.add(10,20);

a.add(20,5,6);

Output: 30
31
(7) What is method overriding?
In java, whenever we have same name method in parent
and child class with the same number of arguments and
same data types is known as method overriding in java.
Method overriding is the example of dynamic
polymorphism.

For example:

class Parent
{

void show()

System.out.println("Parent");

class Child extends Parent

void show()

{
System.out.println("Child");

}
public static void main(String args[])

{
Parent p =new Child();

p.show();
}

Output: Child

(8) How to achieve static polymorphism?


Compile - time polymorphism is also known as static
polymorphism. We can achieve static polymorphism
through method overloading in java.

(9) How to achieve dynamic polymorphism?


Run-time polymorphism is also known as dynamic
polymorphism. We can achieve dynamic polymorphism
through method overriding.

(10) Difference between method overloading and


method overriding?
Click on this link to see the differences between method
overloading and method overriding in java.

(11) What is up-casting in java?


Whenever we put the reference id of a child class object
into the parent class reference variable is known as
upcasting in java.

For example:
Parent p = new Child();//upcasting declaration

(12) What is static binding?


Static binding is also known as compile - time
polymorphism. Method overloading is the example of
static binding.

In static binding, The type of object is determined at


compile-time.

Static binding is also known as early binding.

(13) What is dynamic binding?


Dynamic binding is also known as run-time
polymorphism. Method overriding is the example of
dynamic binding.

In dynamic binding, The type of object is determined at


run-time.

Dynamic binding is also known as late binding.

(14) Can we achieve method overloading by changing


the return type?
No, We cannot achieve method overloading through
return type in java.
(15) What is runtime polymorphism?
Runtime polymorphism is also known as method
overriding and it is achieved at run-time.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy