Java Questions & Answers – Liskov’s Principle

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

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

  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
View Answer

Answer: a
Explanation: According to Liskov substitution principle we can replace ICust with RegularCustomer or OneTimeCustomer without affecting functionality.
advertisement
advertisement

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

Note: Join free Sanfoundry classes at Telegram or Youtube
  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
View Answer

Answer: d
Explanation: 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
View Answer

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

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
View Answer

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

6. 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)shape;
  30. 	System.out.println(square.area());
  31.    }
  32. }

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

Answer: c
Explanation: 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
View Answer

Answer: a
Explanation: 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
View Answer

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

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
View Answer

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

  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
View Answer

Answer: b
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 for Campus Interviews, here is complete set on 1000+ Multiple Choice Questions and Answers on Java.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.