Java_OOPS_Viva_Questions_with_Answers

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Java OOPS Viva Questions with Answers

1. What is Object-Oriented Programming (OOP)?

Answer: OOP is a programming paradigm based on the concept of objects, which contain data

(attributes) and methods (functions). It emphasizes modularity and reuse.

2. What are the main principles of OOP?

Answer: The main principles are:

- Encapsulation

- Inheritance

- Polymorphism

- Abstraction

3. Can you explain the concept of a class and an object in Java?

Answer: A class is a blueprint for creating objects, and an object is an instance of a class.

4. What is inheritance in Java? Can you provide an example?

Answer: Inheritance allows a class to acquire properties and methods of another class. Example:

class B extends A.

5. What is polymorphism? How is it implemented in Java?

Answer: Polymorphism means 'many forms'. It is implemented via method overloading

(compile-time) and method overriding (runtime).

6. What is the difference between method overloading and method overriding?

Answer: Overloading occurs within the same class with different method signatures. Overriding

happens in a subclass to modify a parent class method.

7. What is encapsulation? How does Java achieve it?

Answer: Encapsulation is the bundling of data and methods. Java achieves it by using private
variables and public getter/setter methods.

8. Can you explain the concept of abstraction with an example?

Answer: Abstraction hides implementation details and shows only the essential features. Example:

Abstract classes or interfaces.

9. What is the difference between an abstract class and an interface?

Answer: Abstract class: Can have abstract and concrete methods.

Interface: Only abstract methods (Java 8+ allows default/static methods).

10. What is the 'this' keyword in Java?

Answer: 'this' refers to the current instance of the class.

11. How is the 'super' keyword used in Java?

Answer: 'super' is used to refer to the immediate parent class's methods or constructors.

12. What is the significance of constructors in Java?

Answer: Constructors initialize an object when it is created. They have the same name as the class.

13. Can you differentiate between default constructor and parameterized constructor?

Answer: Default: No parameters.

Parameterized: Takes arguments to initialize object attributes.

14. What is the importance of the 'final' keyword in Java?

Answer: It prevents modification. Final variables: constants, final methods: cannot be overridden,

final classes: cannot be subclassed.

15. How is exception handling related to OOP in Java?

Answer: It ensures proper flow of the program using try, catch, and finally blocks, encapsulating

error handling.
16. What are static methods and variables in Java?

Answer: Static methods/variables belong to the class rather than any specific object.

17. What is the difference between aggregation and composition?

Answer: Aggregation: Weak relationship (e.g., car and driver).

Composition: Strong relationship (e.g., car and engine).

18. What is the role of access modifiers in OOP?

Answer: Access modifiers (public, private, protected, default) control the visibility of members.

19. Can you explain the concept of inner classes in Java?

Answer: Inner classes are defined within another class. Types: static, non-static, local, and

anonymous.

20. What is the significance of the 'toString()' method?

Answer: 'toString()' provides a string representation of an object.

21. How does Java achieve runtime polymorphism?

Answer: Via method overriding and dynamic method dispatch.

22. What is the difference between deep copy and shallow copy?

Answer: Shallow copy: Copies references only. Deep copy: Copies all object fields.

23. Can you explain the use of packages in Java?

Answer: Packages organize classes and interfaces, avoiding name conflicts.

24. How does garbage collection work in Java?

Answer: It automatically reclaims memory by removing unused objects.

25. What is the difference between '== operator' and 'equals() method'?

Answer: '==' compares memory locations, while 'equals()' compares content.


26. What is an interface in Java?

Answer: An interface defines a contract that implementing classes must fulfill.

27. Can an abstract class have a constructor?

Answer: Yes, abstract classes can have constructors to initialize fields.

28. What is a singleton class?

Answer: A class that allows only one instance to be created. Example: using a private constructor.

29. What is the difference between 'abstract' and 'final' classes?

Answer: Abstract: Cannot be instantiated.

Final: Cannot be subclassed.

30. What are Java annotations?

Answer: Annotations provide metadata about the program, e.g., @Override, @Deprecated.

31. What is the difference between checked and unchecked exceptions?

Answer: Checked: Compile-time exceptions (e.g., IOException).

Unchecked: Runtime exceptions (e.g., NullPointerException).

32. Can you override a static method in Java?

Answer: No, static methods belong to the class and are not overridden but hidden.

33. What is a marker interface?

Answer: An empty interface with no methods. Example: Serializable, Cloneable.

34. What is the difference between StringBuffer and StringBuilder?

Answer: StringBuffer: Thread-safe.

StringBuilder: Faster but not thread-safe.

35. What is the use of the instanceof operator?


Answer: It checks whether an object is an instance of a specific class or subclass.

36. What are default and static methods in interfaces?

Answer: Default: Provide default implementation in interfaces.

Static: Methods callable without an object.

37. What is the difference between ArrayList and LinkedList?

Answer: ArrayList: Faster for random access.

LinkedList: Better for frequent insertions/deletions.

38. What is method chaining?

Answer: Calling multiple methods in a single statement. Example: obj.method1().method2();

39. What are wrapper classes in Java?

Answer: Classes that encapsulate primitive data types (e.g., Integer, Double).

40. What is a functional interface?

Answer: An interface with a single abstract method, used in lambda expressions.

41. Can you explain method hiding in Java?

Answer: When a static method in a subclass has the same name and signature as in the parent

class.

42. What is the difference between 'throw' and 'throws'?

Answer: 'throw': Used to explicitly throw an exception.

'throws': Declares exceptions a method may throw.

43. What are Java streams?

Answer: A new abstraction introduced in Java 8 to process collections of data.

44. What is reflection in Java?


Answer: An API to inspect and manipulate classes, methods, and fields at runtime.

45. What is the difference between FileReader and BufferedReader?

Answer: FileReader reads character by character.

BufferedReader reads larger chunks for efficiency.

You might also like