OOPS AND JAVA VIVA AND INTERVIEW QUESTIONS

 Top 100 OOPs and Java viva/interview questions 


A. Object-Oriented Programming (OOPs) Concepts

  1. What are the four main principles of OOPs?
  2. Define encapsulation. How is it implemented in Java?
  3. Explain inheritance and its types in Java.
  4. What is polymorphism? Differentiate between compile-time and runtime polymorphism.
  5. What is abstraction? How is it implemented in Java?
  6. What is the difference between an abstract class and an interface?
  7. Can a class extend multiple classes in Java? Why or why not?
  8. What are access modifiers? Explain their scope.
  9. What is method overloading? Provide an example.
  10. What is method overriding? How does it support polymorphism?
  11. What are constructors? What are their types?
  12. Can a constructor be overridden? Why?
  13. What is the super keyword? When is it used?
  14. Explain the this keyword in Java.
  15. Can you instantiate an abstract class? Why or why not?
  16. How does Java implement multiple inheritance?
  17. What is a static method? Can it be overridden?
  18. What is the difference between a class and an object?
  19. What is the difference between shallow copy and deep copy?
  20. Explain the concept of "is-a" and "has-a" relationships in OOP.

B. Java Core Concepts

  1. What is the difference between JDK, JRE, and JVM?
  2. What is bytecode? How is it executed in Java?
  3. What are the features of Java that make it platform-independent?
  4. What is the difference between == and .equals() in Java?
  5. What is a final keyword? Explain its use in variables, methods, and classes.
  6. Can we have multiple main() methods in Java?
  7. What is the difference between a local variable and an instance variable?
  8. What is the significance of the String class in Java?
  9. Why is String immutable in Java?
  10. What is a wrapper class? Why are they needed?
  11. Differentiate between String, StringBuffer, and StringBuilder.
  12. What is garbage collection? How does it work in Java?
  13. What are try, catch, and finally blocks in exception handling?
  14. What is the difference between throw and throws?
  15. Can a finally block be skipped? When?
  16. Explain the concept of auto-boxing and unboxing.
  17. What is the difference between ArrayList and LinkedList?
  18. How are HashMap and Hashtable different?
  19. What is the difference between an array and a collection in Java?
  20. What are functional interfaces in Java?

C. Java Advanced Topics

  1. What is a thread in Java? How is it created?
  2. What is the difference between Thread class and Runnable interface?
  3. What is synchronization? How is it achieved in Java?
  4. Explain the difference between notify() and notifyAll().
  5. What is a daemon thread? How is it created?
  6. What is the purpose of volatile keyword in Java?
  7. What is the transient keyword in Java?
  8. What is serialization? How is it implemented in Java?
  9. What is the difference between Serializable and Externalizable?
  10. What are annotations in Java? Provide examples.
  11. Explain the concept of Lambda expressions in Java.
  12. What is the Stream API in Java?
  13. How does the Optional class help avoid NullPointerException?
  14. What is the difference between Comparable and Comparator?
  15. What is the difference between HashSet and TreeSet?
  16. What is a ConcurrentHashMap? How is it different from HashMap?
  17. What are generics in Java? Why are they used?
  18. What is a Callable interface? How is it different from Runnable?
  19. What is the difference between Fork/Join framework and regular threads?
  20. Explain the concept of immutability in Java.

D. Java Applets and Graphics

  1. What is a Java applet? How is it different from an application?
  2. What are the lifecycle methods of an applet?
  3. What is the difference between Applet and JApplet?
  4. How do you embed an applet into an HTML file?
  5. What is the paint() method in applets? When is it called?
  6. What are the restrictions imposed on applets?
  7. What is AWT in Java? How is it different from Swing?
  8. Explain the difference between lightweight and heavyweight components.
  9. What is the Graphics class in Java? How is it used?
  10. How do you draw shapes (e.g., circles, rectangles) in Java?

E. Database and File Handling

  1. What is JDBC? How is it used in Java?
  2. What are the different types of JDBC drivers?
  3. Explain the steps to connect a Java application to a database.
  4. What is the purpose of the ResultSet interface in JDBC?
  5. What is a prepared statement? How is it different from a statement?
  6. Explain the difference between commit() and rollback() in JDBC.
  7. How is data stored and read from a file in Java?
  8. What is the difference between FileReader and BufferedReader?
  9. What is the purpose of the FileInputStream and FileOutputStream classes?
  10. What are the advantages of using NIO (New I/O) over traditional I/O in Java?

F. Miscellaneous

  1. What is reflection in Java? Provide an example.
  2. What are class loaders in Java?
  3. What is the difference between checked and unchecked exceptions?
  4. What is the java.lang package? Why is it imported by default?
  5. What are singleton classes in Java? How are they implemented?
  6. What is the enum keyword in Java? Provide an example.
  7. Explain the concept of design patterns in Java.
  8. What is the Factory design pattern? Provide an example.
  9. What is the Singleton design pattern? Why is it used?
  10. What is the Observer design pattern in Java?
  11. How does the System.gc() method work?
  12. What is the difference between Object.clone() and Object.equals()?
  13. What is the role of the hashCode() method in Java?
  14. What is the purpose of the instanceof keyword?
  15. Can an interface have a constructor? Why or why not?
  16. What is the difference between ArrayList and Vector?
  17. What is a deadlock? How can it be avoided in Java?
  18. What is the difference between final, finally, and finalize()?
  19. Explain the difference between AbstractMap and HashMap.
  20. What are the benefits of using Java over other programming languages?

KEYS

A. Object-Oriented Programming (OOPs) Concepts

  1. What are the four main principles of OOPs?
    Encapsulation, Inheritance, Polymorphism, and Abstraction.

  2. Define encapsulation. How is it implemented in Java?
    Wrapping data and methods together using private fields and public getters/setters.

  3. Explain inheritance and its types in Java.
    Mechanism where one class acquires properties of another; types: single, multilevel, hierarchical.

  4. What is polymorphism? Differentiate between compile-time and runtime polymorphism.
    Ability to take multiple forms; compile-time achieved through method overloading, runtime through method overriding.

  5. What is abstraction? How is it implemented in Java?
    Hiding implementation details and showing only essential features using abstract classes or interfaces.

  6. What is the difference between an abstract class and an interface?
    Abstract class can have concrete methods, while interfaces have only abstract (or default) methods (till Java 8).

  7. Can a class extend multiple classes in Java? Why or why not?
    No, because Java doesn’t support multiple inheritance to avoid ambiguity.

  8. What are access modifiers? Explain their scope.
    Keywords controlling visibility: public, protected, default, private.

  9. What is method overloading? Provide an example.
    Defining multiple methods with the same name but different parameters.

  10. What is method overriding? How does it support polymorphism?
    Redefining a method in a subclass; enables runtime polymorphism.

  11. What are constructors? What are their types?
    Special methods to initialize objects; types: default, parameterized, copy.

  12. Can a constructor be overridden? Why?
    No, because constructors are not inherited.

  13. What is the super keyword? When is it used?
    Refers to the immediate parent class object; used for constructor chaining and method access.

  14. Explain the this keyword in Java.
    Refers to the current class instance.

  15. Can you instantiate an abstract class? Why or why not?
    No, because abstract classes are incomplete by definition.

  16. How does Java implement multiple inheritance?
    Through interfaces.

  17. What is a static method? Can it be overridden?
    Belongs to the class, not objects; cannot be overridden, only hidden.

  18. What is the difference between a class and an object?
    Class is a blueprint; an object is an instance of a class.

  19. What is the difference between shallow copy and deep copy?
    Shallow copy copies references; deep copy duplicates entire objects.

  20. Explain the concept of "is-a" and "has-a" relationships in OOP.
    "Is-a" denotes inheritance, "has-a" denotes composition.


B. Java Core Concepts

  1. What is the difference between JDK, JRE, and JVM?
    JDK is a development kit, JRE runs Java programs, JVM executes bytecode.

  2. What is bytecode? How is it executed in Java?
    Intermediate code executed by JVM.

  3. What are the features of Java that make it platform-independent?
    Bytecode and JVM.

  4. What is the difference between == and .equals() in Java?
    == compares references; .equals() compares content.

  5. What is a final keyword? Explain its use in variables, methods, and classes.
    Makes variables constant, prevents method overriding, and disallows inheritance.

  6. Can we have multiple main() methods in Java?
    Yes, but only one with the signature public static void main(String[] args) is the entry point.

  7. What is the difference between a local variable and an instance variable?
    Local variables are declared inside methods; instance variables are class-level fields.

  8. What is the significance of the String class in Java?
    Immutable and widely used for text manipulation.

  9. Why is String immutable in Java?
    For thread safety and caching.

  10. What is a wrapper class? Why are they needed?
    Converts primitives to objects and vice versa.

  11. Differentiate between String, StringBuffer, and StringBuilder.
    String is immutable; StringBuffer is thread-safe; StringBuilder is faster but not thread-safe.

  12. What is garbage collection? How does it work in Java?
    Automatic memory management to reclaim unused objects.

  13. What are try, catch, and finally blocks in exception handling?
    Handle exceptions with guaranteed execution of finally.

  14. What is the difference between throw and throws?
    throw is used to explicitly throw an exception; throws declares exceptions.

  15. Can a finally block be skipped? When?
    Yes, if the JVM exits during execution.

  16. Explain the concept of auto-boxing and unboxing.
    Automatic conversion between primitives and wrapper classes.

  17. What is the difference between ArrayList and LinkedList?
    ArrayList uses dynamic arrays; LinkedList uses doubly linked lists.

  18. How are HashMap and Hashtable different?
    HashMap is non-synchronized; Hashtable is synchronized.

  19. What is the difference between an array and a collection in Java?
    Arrays are fixed-size, collections are dynamic.

  20. What are functional interfaces in Java?
    Interfaces with a single abstract method (e.g., Runnable).


C. Java Advanced Topics

  1. What is a thread in Java? How is it created?
    Lightweight process created via Thread class or Runnable interface.

  2. What is the difference between Thread class and Runnable interface?
    Runnable allows sharing threads; Thread directly represents a thread.

  3. What is synchronization? How is it achieved in Java?
    Prevents thread interference using the synchronized keyword.

  4. Explain the difference between notify() and notifyAll().
    notify() wakes one thread; notifyAll() wakes all waiting threads.

  5. What is a daemon thread? How is it created?
    Background thread using setDaemon(true).

  6. What is the purpose of volatile keyword in Java?
    Ensures visibility of changes to variables across threads.

  7. What is the transient keyword in Java?
    Excludes variables from serialization.

  8. What is serialization? How is it implemented in Java?
    Converting an object into bytes using Serializable interface.

  9. What is the difference between Serializable and Externalizable?
    Serializable uses default serialization; Externalizable requires custom implementation.

  10. What are annotations in Java? Provide examples.
    Metadata for code; e.g., @Override.

D. Java Applets and Graphics

  1. What is a Java applet? How is it different from an application?
    A Java applet is a small program embedded in web pages; it runs inside a browser.

  2. What are the lifecycle methods of an applet?
    init(), start(), stop(), and destroy().

  3. What is the difference between Applet and JApplet?
    JApplet is a Swing-based applet; Applet is part of AWT.

  4. How do you embed an applet into an HTML file?
    Using the <applet> tag with code and width/height attributes.

  5. What is the paint() method in applets? When is it called?
    Used to draw graphics; called by the AWT system.

  6. What are the restrictions imposed on applets?
    Restricted from accessing local file systems and executing arbitrary code.

  7. What is AWT in Java? How is it different from Swing?
    AWT is heavyweight and platform-dependent; Swing is lightweight and platform-independent.

  8. Explain the difference between lightweight and heavyweight components.
    Lightweight components are drawn in Java; heavyweight rely on native OS components.

  9. What is the Graphics class in Java? How is it used?
    Used for drawing shapes and images in AWT/Swing.

  10. How do you draw shapes (e.g., circles, rectangles) in Java?
    Using methods like drawRect() and drawOval() of the Graphics class.


E. Database and File Handling

  1. What is JDBC? How is it used in Java?
    JDBC is a Java API to connect and interact with databases.

  2. What are the different types of JDBC drivers?
    Type 1 (JDBC-ODBC Bridge), Type 2 (Native-API), Type 3 (Network Protocol), Type 4 (Thin Driver).

  3. Explain the steps to connect a Java application to a database.
    Load driver, establish connection, create statement, execute query, process results, close connection.

  4. What is the purpose of the ResultSet interface in JDBC?
    Used to retrieve and manipulate database query results.

  5. What is a prepared statement? How is it different from a statement?
    Precompiled SQL query; reduces SQL injection risk and improves performance.

  6. Explain the difference between commit() and rollback() in JDBC.
    commit() saves changes, rollback() undoes them.

  7. How is data stored and read from a file in Java?
    Using classes like FileReader, FileWriter, BufferedReader, and BufferedWriter.

  8. What is the difference between FileReader and BufferedReader?
    FileReader reads characters; BufferedReader reads text efficiently using buffering.

  9. What is the purpose of the FileInputStream and FileOutputStream classes?
    For reading/writing raw byte data.

  10. What are the advantages of using NIO (New I/O) over traditional I/O in Java?
    Non-blocking I/O, improved performance, and scalability.


F. Miscellaneous

  1. What is reflection in Java? Provide an example.
    Reflection allows runtime inspection of classes, methods, and fields.

  2. What are class loaders in Java?
    Responsible for loading classes into JVM at runtime.

  3. What is the difference between checked and unchecked exceptions?
    Checked exceptions are handled during compilation; unchecked are at runtime.

  4. What is the java.lang package? Why is it imported by default?
    It contains core classes like String, Math; imported for convenience.

  5. What are singleton classes in Java? How are they implemented?
    Classes ensuring only one instance exists; implemented using private constructors and static methods.

  6. What is the enum keyword in Java? Provide an example.
    Used to define constants; e.g., enum Day { MONDAY, TUESDAY }.

  7. Explain the concept of design patterns in Java.
    Standard solutions to common software design problems.

  8. What is the Factory design pattern? Provide an example.
    Creates objects without exposing the instantiation logic.

  9. What is the Singleton design pattern? Why is it used?
    Ensures only one instance of a class exists; used for shared resources.

  10. What is the Observer design pattern in Java?
    Defines a dependency where one object notifies others of changes.

  11. How does the System.gc() method work?
    Suggests garbage collection but doesn't guarantee it.

  12. What is the difference between Object.clone() and Object.equals()?
    clone() creates a copy; equals() compares objects.

  13. What is the role of the hashCode() method in Java?
    Provides a hash value for an object; used in hashing-based collections.

  14. What is the purpose of the instanceof keyword?
    Checks if an object is of a specific type.

  15. Can an interface have a constructor? Why or why not?
    No, because interfaces are not meant to be instantiated.

  16. What is the difference between ArrayList and Vector?
    ArrayList is non-synchronized; Vector is synchronized.

  17. What is a deadlock? How can it be avoided in Java?
    A state where threads block each other; avoided by proper lock ordering and timeout mechanisms.

  18. What is the difference between final, finally, and finalize()?
    final prevents modification, finally ensures execution, finalize() is for cleanup.

  19. Explain the difference between AbstractMap and HashMap.
    AbstractMap is a base class; HashMap provides a concrete implementation.

  20. What are the benefits of using Java over other programming languages?
    Platform independence, robust libraries, scalability, and ease of use.

Comments

Popular posts from this blog

OOPS AND JAVA

C PROGRAMMING