This set of Java Questions and Answers focuses on “Liskov’s Principle”.
1. What does Liskov substitution principle specify?
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
View Answer
Explanation: 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.
2. What will be the correct option of the following Java code snippet?
interface ICust
{
}
class RegularCustomer implements ICust
{
}
class OneTimeCustomer implements ICust
{
}
a) ICust can be replaced with RegularCustomer
b) RegularCustomer can be replaced with OneTimeCustomer
c) OneTimeCustomer can be replaced with RegularCustomer
d) We can instantiate objects of ICust
View Answer
Explanation: According to Liskov substitution principle we can replace ICust with RegularCustomer or OneTimeCustomer without affecting functionality.
3. What will be the output of the following Java code snippet?
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());
}
}
a) Compilation failure
b) Runtime failure
c) 1
d) 2
View Answer
Explanation: Child object can be assigned to parent variable without change in behaviour.
4. What will be the output of the following Java code snippet?
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());
}
}
a) Compilation failure
b) 3
c) 1
d) 2
View Answer
Explanation: Child object can be assigned to parent variable without change in behaviour.
5. What will be the output of the following Java code?
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());
}
}
a) Compilation failure
b) 3
c) 1
d) 2
View Answer
Explanation: Parent object cannot be assigned to child class.
6. What will be the output of the following Java code?
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());
}
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
View Answer
Explanation: ClassCastException is thrown as we cannot assign parent object to child variable.
7. What will be the output of the following Java code?
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());
}
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
View Answer
Explanation: 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;
}
}
8. What will be the output of the following Java code?
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());
}
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
View Answer
Explanation: Interface cannot be instantiated. So we cannot create instances of shape.
9. What will be the output of the following Java code?
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());
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
View Answer
Explanation: 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.
10. What will be the output of the following Java code?
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());
}
a) Compilation failure
b) 3
c) Runtime Exception
d) 2
View Answer
Explanation: The method of the child class object is accessed. When we reassign objects, the methods of the latest assigned object are accessed.
Sanfoundry Global Education & Learning Series – Java Programming Language.
To practice all areas of Java, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.
- Practice BCA MCQs
- Apply for Java Internship
- Apply for Computer Science Internship
- Practice Programming MCQs
- Practice Information Technology MCQs