Java Inheritance Questions PDF

Summary

This document contains a set of questions covering the topic of inheritance in Java. This includes questions about keywords, syntax and definitions of different terms.

Full Transcript

1. What is inheritance in Java? A) A mechanism to create new classes based on existing classes B) A mechanism for organizing data C) A way to handle exceptions D) A method of managing memory Answer: A ________________________________________ 2. Which keyword is used to inherit a class in Java...

1. What is inheritance in Java? A) A mechanism to create new classes based on existing classes B) A mechanism for organizing data C) A way to handle exceptions D) A method of managing memory Answer: A ________________________________________ 2. Which keyword is used to inherit a class in Java? A) extends B) implements C) inherits D) super Answer: A ________________________________________ 3. What is the main purpose of inheritance in Java? A) To enable code reuse B) To manage exceptions C) To define a class D) To increase memory consumption Answer: A ________________________________________ 4. Which of the following is the correct syntax for inheritance in Java? A) class Child inherits Parent B) class Child extends Parent C) class Child implements Parent D) class Child super Parent Answer: B ________________________________________ 5. What does the subclass inherit from the superclass in Java? A) Only methods B) Only variables C) Both methods and variables D) Nothing Answer: C ________________________________________ 6. Which of the following statements about inheritance is true in Java? A) A class can inherit multiple classes. B) A class can only inherit one class. C) Inheritance is not supported in Java. D) Inheritance is used only for interfaces. Answer: B ________________________________________ 7. Which of the following is the direct superclass of the Object class in Java? A) Class B) Thread C) No class D) Parent Answer: C ________________________________________ 8. Which of the following can be inherited in Java? A) Methods B) Variables C) Constructors D) A and B Answer: D ________________________________________ 9. Can a subclass override a private method in the superclass? A) Yes B) No C) It depends on the method D) It depends on access modifiers Answer: B ________________________________________ 10. What is method overriding in Java? A) When a subclass provides a specific implementation of a method already provided by its superclass B) When a subclass provides its own method C) When a method in the superclass is hidden D) When a method is called multiple times Answer: A ________________________________________ 11. What is the keyword used to call the superclass constructor? A) super B) super() C) this() D) parent() Answer: B ________________________________________ 12. What is the output of the following code? java Copy code class Parent { void display() { System.out.println("Parent class"); } } class Child extends Parent { void display() { System.out.println("Child class"); } } public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.display(); } } A) Parent class B) Child class C) Compilation error D) Runtime error Answer: B ________________________________________ 13. What will happen if a subclass does not provide a constructor? A) The compiler will generate an error B) A default constructor is provided by the compiler C) The class will be abstract D) It will not inherit the superclass Answer: B ________________________________________ 14. Can a subclass call a private method of its superclass? A) Yes, if the method is static B) No, private methods are not inherited C) Yes, if the method is final D) Yes, through reflection Answer: B ________________________________________ 15. What is the superclass of all classes in Java? A) Object B) Class C) String D) Thread Answer: A ________________________________________ 16. What is the output of the following code? java Copy code class Animal { void sound() { System.out.println("Animal makes sound"); } } class Dog extends Animal { void sound() { System.out.println("Dog barks"); } } public class Test { public static void main(String[] args) { Animal a = new Dog(); a.sound(); } } A) Animal makes sound B) Dog barks C) Compilation error D) Runtime error Answer: B ________________________________________ 17. Which of the following can be used to achieve runtime polymorphism in Java? A) Inheritance B) Method Overloading C) Method Overriding D) Both B and C Answer: D ________________________________________ 18. Which of the following is the correct statement for a superclass in Java? A) It is the class that inherits from another class. B) It is the class that provides methods and variables to be inherited. C) It is the class that can never be inherited. D) None of the above. Answer: B ________________________________________ 19. Which of the following access modifiers can be used for a method in a superclass so that it can be overridden by a subclass? A) private B) protected C) public D) A and B Answer: D ________________________________________ 20. What will happen if you override a method with a different return type? A) Compilation error B) The code will run, but the output will be wrong C) It is allowed if the return type is a subclass of the original return type D) None of the above Answer: C ________________________________________ 21. Can we override the constructor in Java? A) Yes B) No C) It depends on the access modifier D) It depends on the method type Answer: B ________________________________________ 22. What does the super keyword refer to? A) The superclass of the current class B) A private method in the superclass C) The current class D) A new instance of the class Answer: A ________________________________________ 23. Can a subclass call a constructor of its superclass? A) Yes, but only through the this keyword B) No, constructors cannot be inherited C) Yes, by using the super keyword D) Yes, automatically Answer: C ________________________________________ 24. Can a class be both a superclass and a subclass at the same time in Java? A) Yes B) No C) It depends on the number of classes involved D) It depends on the access modifier Answer: A ________________________________________ 25. What is a subclass in Java? A) A class that is inherited from another class B) A class that inherits from a superclass C) A class that does not have a superclass D) None of the above Answer: B ________________________________________ 26. What is the keyword used to declare an interface in Java? A) implements B) abstract C) interface D) super Answer: C ________________________________________ 27. Can a class implement multiple interfaces in Java? A) Yes B) No C) Only if the interfaces have no methods D) Only if the class is abstract Answer: A ________________________________________ 28. Which of the following can be inherited by a subclass? A) Static methods B) Instance methods C) Instance variables D) All of the above Answer: D ________________________________________ 29. What is the output of the following code? java Copy code class Parent { static void display() { System.out.println("Parent class"); } } class Child extends Parent { static void display() { System.out.println("Child class"); } } public class Main { public static void main(String[] args) { Parent obj = new Child(); obj.display(); } } A) Parent class B) Child class C) Compilation error D) Runtime error Answer: A ________________________________________ 30. Can an interface extend a class in Java? A) Yes B) No C) Yes, but only abstract classes D) Yes, but only concrete classes Answer: B ________________________________________ 31. Can a subclass access the private variables of its superclass in Java? A) Yes, directly B) No, unless they are made public C) Yes, through the protected access modifier D) Yes, through getter and setter methods Answer: D ________________________________________ 32. Can a constructor be inherited in Java? A) Yes, but it cannot be invoked B) No C) Yes, and it can be invoked D) Yes, but only in abstract classes Answer: B ________________________________________ 33. What happens if a method is marked as final in the superclass? A) The method can still be overridden in the subclass B) The method cannot be overridden in the subclass C) The method cannot be inherited D) The method will not be executed Answer: B ________________________________________ 34. What is the benefit of method overriding in Java? A) It allows a method to execute differently based on the object type B) It allows the superclass method to execute only C) It allows for multiple return types D) It helps in defining new methods in the subclass Answer: A ________________________________________ 35. What is the purpose of the super() constructor in a subclass constructor? A) To initialize the instance variables of the superclass B) To call the constructor of the current class C) To call the static methods of the superclass D) To initialize the instance variables of the current class Answer: A ________________________________________ 36. Which of the following statements is true about abstract classes in Java? A) An abstract class cannot be inherited B) An abstract class can have constructors C) An abstract class cannot have methods D) An abstract class must have at least one abstract method Answer: B ________________________________________ 37. Can a class be both abstract and final in Java? A) Yes B) No C) Yes, if it does not have any abstract methods D) Yes, but only in interfaces Answer: B ________________________________________ 38. Which type of inheritance is not allowed in Java? A) Single inheritance B) Multiple inheritance through interfaces C) Multiple inheritance through classes D) Multilevel inheritance Answer: C ________________________________________ 39. In Java, what happens if a subclass does not implement all the methods from an interface? A) The subclass will compile but throw an error at runtime B) The subclass will fail to compile C) The subclass will compile, and the methods can be left unimplemented D) The subclass will automatically implement the methods Answer: B ________________________________________ 40. What will happen if a subclass does not provide an implementation for an abstract method from its superclass? A) The subclass will not compile B) The superclass method will be invoked C) The program will throw an exception at runtime D) The subclass can invoke the superclass method Answer: A ________________________________________ 41. Which keyword is used in Java to prevent a method from being overridden? A) final B) static C) super D) abstract Answer: A ________________________________________ 42. Which of the following cannot be used to implement inheritance in Java? A) class B) interface C) abstract class D) enum Answer: D ________________________________________ 43. What is the result of the following code? java Copy code class Parent { void display() { System.out.println("Parent class"); } } class Child extends Parent { void display() { super.display(); System.out.println("Child class"); } } public class Main { public static void main(String[] args) { Child obj = new Child(); obj.display(); } } A) Parent class B) Child class C) Parent class followed by Child class D) Compilation error Answer: C ________________________________________ 44. What is an abstract class in Java? A) A class that cannot have methods B) A class that cannot have constructors C) A class that cannot be instantiated D) A class that cannot be inherited Answer: C ________________________________________ 45. What is the result of the following code? java Copy code class Vehicle { void run() { System.out.println("Vehicle is running"); } } class Car extends Vehicle { void run() { System.out.println("Car is running"); } } public class Test { public static void main(String[] args) { Vehicle v = new Car(); v.run(); } } A) Vehicle is running B) Car is running C) Compilation error D) Runtime error Answer: B ________________________________________ 46. What happens if you try to call a method from a null superclass reference? A) The code will run normally B) It will throw a NullPointerException C) It will invoke the method from the subclass D) None of the above Answer: B ________________________________________ 47. Can you inherit static methods in Java? A) Yes, but you cannot override them B) No, static methods cannot be inherited C) Yes, and you can override them D) Yes, and you can hide them Answer: A ________________________________________ 48. What is the result of the following code? java Copy code class A { void show() { System.out.println("Class A"); } } class B extends A { void show() { System.out.println("Class B"); } } public class Main { public static void main(String[] args) { A obj = new B(); - obj.show(); } } A) Class A B) Class B C) Compilation error D) Runtime error Answer: B ________________________________________ 49. Can a subclass access a protected member of its superclass in another package? A) Yes B) No C) Only if the subclass is in the same package D) Yes, if the superclass is public Answer: A ________________________________________ 50. Which of the following is not an advantage of inheritance in Java? A) Code reuse B) Better modularization C) Tight coupling D) Easier maintenance Answer: C 1. Which class is used to create a file in Java? A) FileReader B) FileWriter C) File D) FileInputStream Answer: C ________________________________________ 2. Which method is used to create a new file in Java? A) createNewFile() B) newFile() C) createFile() D) makeFile() Answer: A ________________________________________ ________________________________________ 6. Which of the following methods is used to delete a file in Java? A) deleteFile() B) remove() C) delete() D) removeFile() Answer: C ________________________________________ 7. What exception is thrown if a file does not exist when trying to read it in Java? A) FileNotFoundException B) IOException C) FileReadException D) NoSuchFileException Answer: A ________________________________________ 9. Which method is used to check if a file exists in Java? A) exists() B) fileExists() C) isExist() D) isFile() Answer: A 16. Which method is used to get the size of a file in Java? A) length() B) fileSize() C) getSize() D) size() Answer: A ________________________________________ 1. Which class is used to create a file in Java? A) FileReader B) FileWriter C) File D) FileInputStream Answer: C ________________________________________ 2. Which method is used to create a new file in Java? A) createNewFile() B) newFile() C) createFile() D) makeFile() Answer: A ________________________________________ ________________________________________ 6. Which of the following methods is used to delete a file in Java? A) deleteFile() B) remove() C) delete() D) removeFile() Answer: C ________________________________________ 7. What exception is thrown if a file does not exist when trying to read it in Java? A) FileNotFoundException B) IOException C) FileReadException D) NoSuchFileException Answer: A ________________________________________ 9. Which method is used to check if a file exists in Java? A) exists() B) fileExists() C) isExist() D) isFile() Answer: A 16. Which method is used to get the size of a file in Java? A) length() B) fileSize() C) getSize() D) size() Answer: A ________________________________________

Use Quizgecko on...
Browser
Browser