250+ TOP MCQs on Liskov’s Principle and Answers

This set of Java Questions and Answers for Campus interviews 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

Answer: a
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.

advertisement

2. What will be the correct option of the following Java code snippet?

  1. interface ICust 
  2. {
  3. }
  4. class RegularCustomer implements ICust 
  5. {
  6. }
  7. class OneTimeCustomer implements ICust 
  8. {
  9. }

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

Answer: a
Clarification: 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?

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Square extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 2;
  13. 	}
  14. }
  15. class Main() 
  16. {
  17.    public static void main(String[] args)
  18.    {
  19. 	Shape shape = new Shape();
  20. 	Square square = new Square();
  21. 	shape = square;
  22. 	System.out.println(shape.area());
  23.     }
  24. }

a) Compilation failure
b) Runtime failure
c) 1
d) 2

Answer: d
Clarification: Child object can be assigned to parent variable without change in behaviour.

advertisement

4. What will be the output of the following Java code snippet?

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Rectangle extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 3;
  13. 	}
  14. }
  15. class Main() 
  16. {
  17.    public static void main(String[] args)
  18.    {
  19. 	Shape shape = new Shape();
  20. 	Rectangle rect = new Rectangle();
  21. 	shape = rect;
  22. 	System.out.println(shape.area());
  23.    }
  24. }

a) Compilation failure
b) 3
c) 1
d) 2

Answer: b
Clarification: Child object can be assigned to parent variable without change in behaviour.

5. What will be the output of the following Java code?

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Square extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 2;
  13. 	}
  14. }
  15. class Main() 
  16. {
  17.    public static void main(String[] args)
  18.    {
  19. 	Shape shape = new Shape();
  20. 	Square square = new Square();
  21. 	square = shape;
  22. 	System.out.println(square.area());
  23.    }
  24. }

a) Compilation failure
b) 3
c) 1
d) 2

Answer: a
Clarification: Parent object cannot be assigned to child class.

6. What will be the output of the following Java code?

advertisement

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Square extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 2;
  13. 	}
  14. }
  15. public class Rectangle extends Shape 
  16. {
  17. 	public int area()
  18.         {
  19. 		return 3;
  20. 	}
  21. }
  22. class Main() 
  23. {
  24.    public static void main(String[] args)
  25.    {
  26. 	Shape shape = new Shape();
  27. 	Square square = new Square();
  28.     	Rectangle rect = new Rectangle();
  29. 	rect = (Rectangle)shape;
  30. 	System.out.println(square.area());
  31.    }
  32. }

a) Compilation failure
b) 3
c) Runtime Exception
d) 2

Answer: c
Clarification: ClassCastException is thrown as we cannot assign parent object to child variable.

7. What will be the output of the following Java code?

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Square extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 2;
  13. 	}
  14. }
  15. public class Rectangle extends Shape 
  16. {
  17. 	public int area()
  18.         {
  19. 		return 3;
  20. 	}
  21. }
  22. class Main() 
  23. {
  24.       public static void main(String[] args)
  25.       {
  26. 	 Shape shape = new Shape();
  27. 	 Square square = new Square();
  28.    	 Rectangle rect = new Rectangle();
  29. 	 rect = (Rectangle)square;
  30. 	 System.out.println(square.area());
  31.       }
  32. }

a) Compilation failure
b) 3
c) Runtime Exception
d) 2

Answer: a
Clarification: We cannot assign one child class object to another child class variable.

  1. interface Shape 
  2. {
  3. 	public int area();
  4. }
  5. public class Square implements Shape 
  6. {
  7. 	public int area()
  8.         {
  9. 		return 2;
  10. 	}
  11. }
  12. public class Rectangle implements Shape 
  13. {
  14. 	public int area()
  15.         {
  16. 		return 3;
  17. 	}
  18. }

8. What will be the output of the following Java code?

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Square extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 2;
  13. 	}
  14. }
  15. public class Rectangle extends Shape 
  16. {
  17. 	public int area()
  18.         {
  19. 		return 3;
  20. 	}
  21. }
  22. class Main() 
  23. {
  24.        public static void main(String[] args)
  25.        {
  26. 	 Shape shape = new Shape();
  27. 	 Square square = new Square();
  28.    	 Rectangle rect = new Rectangle();
  29. 	 rect = (Rectangle)square;
  30. 	 System.out.println(square.area());
  31. 	}
  32. }

a) Compilation failure
b) 3
c) Runtime Exception
d) 2

Answer: a
Clarification: Interface cannot be instantiated. So we cannot create instances of shape.

advertisement

9. What will be the output of the following Java code?

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Square extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 2;
  13. 	}
  14. }
  15. public class Rectangle extends Shape 
  16. {
  17. 	public int area()
  18.         {
  19. 		return 3;
  20. 	}
  21. }
  22. public static void main(String[] args)
  23. {
  24. 	 Shape shape = new Square();
  25.    	 shape = new Rectangle();
  26. 	 System.out.println(shape.area());
  27. }

a) Compilation failure
b) 3
c) Runtime Exception
d) 2

Answer: b
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.

10. What will be the output of the following Java code?

  1. public class Shape 
  2. {
  3. 	public int area()
  4.         {
  5. 		return 1;
  6. 	}
  7. }
  8. public class Square extends Shape 
  9. {
  10. 	public int area()
  11.         {
  12. 		return 2;
  13. 	}
  14. }
  15. public class Rectangle extends Shape 
  16. {
  17. 	public int area()
  18.         {
  19. 		return 3;
  20. 	}
  21. }
  22. public static void main(String[] args)
  23. {
  24. 	 Shape square = new Square();
  25.    	 Shape rect = new Rectangle();
  26.      	 square = rect;
  27. 	 System.out.println(square.area());
  28. }

a) Compilation failure
b) 3
c) Runtime Exception
d) 2

Answer: b
Clarification: The method of the child class object is accessed. When we reassign objects, the methods of the latest assigned object are accessed.

Java for Campus Interviews, here is complete set on Multiple Choice Questions and Answers on Java.

contest

» Next – Java Questions & Answers – Coding best practices

Leave a Reply

Your email address will not be published. Required fields are marked *