This set of Java Questions and Answers for Campus interviews on “Liskov’s Principle”.
1. What does Liskov substitution principle specify? Answer: a advertisement 2. What will be the correct option of the following Java code snippet? a) ICust can be replaced with RegularCustomer Answer: a 3. What will be the output of the following Java code snippet? a) Compilation failure Answer: d advertisement 4. What will be the output of the following Java code snippet? a) Compilation failure Answer: b 5. What will be the output of the following Java code? a) Compilation failure Answer: a 6. What will be the output of the following Java code? advertisement a) Compilation failure Answer: c 7. What will be the output of the following Java code? a) Compilation failure 8. What will be the output of the following Java code? a) Compilation failure Answer: a advertisement 9. What will be the output of the following Java code? a) Compilation failure Answer: b 10. What will be the output of the following Java code? a) Compilation failure Answer: b Java for Campus Interviews, here is complete set on Multiple Choice Questions and Answers on Java.
a) parent class can be substituted by child class
b) child class can be substituted by parent class
c) parent class cannot be substituted by child class
d) No classes can be replaced by each other
Clarification: Liskov substitution principle states that Objects in a program should be replaceable with instances of their sub types without altering the correctness of that program.
interface ICust
{
}
class RegularCustomer implements ICust
{
}
class OneTimeCustomer implements ICust
{
}
b) RegularCustomer can be replaced with OneTimeCustomer
c) OneTimeCustomer can be replaced with RegularCustomer
d) We can instantiate objects of ICust
Clarification: According to Liskov substitution principle we can replace ICust with RegularCustomer or OneTimeCustomer without affecting functionality.
public class Shape
{
public int area()
{
return 1;
}
}
public class Square extends Shape
{
public int area()
{
return 2;
}
}
class Main()
{
public static void main(String[] args)
{
Shape shape = new Shape();
Square square = new Square();
shape = square;
System.out.println(shape.area());
}
}
b) Runtime failure
c) 1
d) 2
Clarification: Child object can be assigned to parent variable without change in behaviour.
public class Shape
{
public int area()
{
return 1;
}
}
public class Rectangle extends Shape
{
public int area()
{
return 3;
}
}
class Main()
{
public static void main(String[] args)
{
Shape shape = new Shape();
Rectangle rect = new Rectangle();
shape = rect;
System.out.println(shape.area());
}
}
b) 3
c) 1
d) 2
Clarification: Child object can be assigned to parent variable without change in behaviour.
public class Shape
{
public int area()
{
return 1;
}
}
public class Square extends Shape
{
public int area()
{
return 2;
}
}
class Main()
{
public static void main(String[] args)
{
Shape shape = new Shape();
Square square = new Square();
square = shape;
System.out.println(square.area());
}
}
b) 3
c) 1
d) 2
Clarification: Parent object cannot be assigned to child class.
public class Shape
{
public int area()
{
return 1;
}
}
public class Square extends Shape
{
public int area()
{
return 2;
}
}
public class Rectangle extends Shape
{
public int area()
{
return 3;
}
}
class Main()
{
public static void main(String[] args)
{
Shape shape = new Shape();
Square square = new Square();
Rectangle rect = new Rectangle();
rect = (Rectangle)shape;
System.out.println(square.area());
}
}
b) 3
c) Runtime Exception
d) 2
Clarification: ClassCastException is thrown as we cannot assign parent object to child variable.
public class Shape
{
public int area()
{
return 1;
}
}
public class Square extends Shape
{
public int area()
{
return 2;
}
}
public class Rectangle extends Shape
{
public int area()
{
return 3;
}
}
class Main()
{
public static void main(String[] args)
{
Shape shape = new Shape();
Square square = new Square();
Rectangle rect = new Rectangle();
rect = (Rectangle)square;
System.out.println(square.area());
}
}
b) 3
c) Runtime Exception
d) 2
Clarification: We cannot assign one child class object to another child class variable.
interface Shape
{
public int area();
}
public class Square implements Shape
{
public int area()
{
return 2;
}
}
public class Rectangle implements Shape
{
public int area()
{
return 3;
}
}
public class Shape
{
public int area()
{
return 1;
}
}
public class Square extends Shape
{
public int area()
{
return 2;
}
}
public class Rectangle extends Shape
{
public int area()
{
return 3;
}
}
class Main()
{
public static void main(String[] args)
{
Shape shape = new Shape();
Square square = new Square();
Rectangle rect = new Rectangle();
rect = (Rectangle)square;
System.out.println(square.area());
}
}
b) 3
c) Runtime Exception
d) 2
Clarification: Interface cannot be instantiated. So we cannot create instances of shape.
public class Shape
{
public int area()
{
return 1;
}
}
public class Square extends Shape
{
public int area()
{
return 2;
}
}
public class Rectangle extends Shape
{
public int area()
{
return 3;
}
}
public static void main(String[] args)
{
Shape shape = new Square();
shape = new Rectangle();
System.out.println(shape.area());
}
b) 3
c) Runtime Exception
d) 2
Clarification: With parent class variable we can access methods declared in parent class. If the parent class variable is assigned child class object than it accesses the method of child class.
public class Shape
{
public int area()
{
return 1;
}
}
public class Square extends Shape
{
public int area()
{
return 2;
}
}
public class Rectangle extends Shape
{
public int area()
{
return 3;
}
}
public static void main(String[] args)
{
Shape square = new Square();
Shape rect = new Rectangle();
square = rect;
System.out.println(square.area());
}
b) 3
c) Runtime Exception
d) 2
Clarification: The method of the child class object is accessed. When we reassign objects, the methods of the latest assigned object are accessed.