FREE Manual Testing Batch Starts in Enroll Now | +91-8143353888 | +91-7780122379

Java

  • Q1: What is Java?
    A: Java is a high-level, object-oriented programming language developed by Sun Microsystems (now owned by Oracle) in 1995. It is platform-independent and designed to be used in distributed environments.
  • Q2: What are the main features of Java?
    A: The main features of Java are: Object-oriented: Java follows the object-oriented programming paradigm. Platform-independent: Java code can run on any platform with the help of the Java Virtual Machine (JVM). Simple and easy to learn: Java has a straightforward syntax and a small set of keywords. Robust and secure: Java provides strong memory management and built-in security features. Multi-threaded: Java supports concurrent programming by allowing multiple threads to run concurrently. Dynamic and extensible: Java supports dynamic class loading and the ability to extend classes through inheritance.
  • Q3: What is the difference between JDK, JRE, and JVM?
    A: JDK (Java Development Kit): JDK is a software development kit that provides tools for developing, compiling, and debugging Java applications. It includes the JRE and additional development tools. JRE (Java Runtime Environment): JRE is a runtime environment that is required to run Java applications. It includes the JVM and essential libraries. JVM (Java Virtual Machine): JVM is an abstract machine that executes Java bytecode. It provides the runtime environment for Java applications. What is the difference between an object and a class? A class is a blueprint or template that defines the properties and behaviors of objects, while an object is an instance of a class that represents a specific entity in memory.
  • Q4: What is the difference between a constructor and a method?
    A: A constructor is a special method that is used to initialize objects of a class. It has the same name as the class and does not have a return type. A method, on the other hand, is a function that performs a specific action and can have a return type.
  • Q5: What is the difference between the == operator and the equals() method?
    A: The == operator is used to compare the memory addresses of two objects, while the equals() method is used to compare the content or values of two objects. The equals() method can be overridden to provide custom comparison logic.
  • Q6: What is the difference between an abstract class and an interface?
    A: An abstract class is a class that cannot be instantiated and can contain both abstract and non-abstract methods. It serves as a base for subclasses. An interface, on the other hand, is a collection of abstract methods that defines a contract. A class can implement multiple interfaces, but it can extend only one abstract class.
  • Q7: What is method overriding in Java?
    A: Method overriding is a feature in Java that allows a subclass to provide a different implementation of a method that is already defined in its superclass. The overriding method must have the same name, return type, and parameters as the overridden method.
  • Q8: What is method overloading in Java?
    A: Method overloading is a feature in Java that allows a class to have multiple methods with the same name but different parameters. The compiler determines which method to invoke based on the arguments passed during the method call.
  • Q9: What is encapsulation in Java?
    A: Encapsulation is the process of hiding the internal details and data of a class from other classes. It is achieved by declaring private variables and providing public methods (getters and setters) to access and modify those variables.
  • Q10: What is a static method in Java?
    A: A static method is a method that belongs to the class itself, rather than to any specific instance of the class. It can be called directly on the class without creating an object of the class.
  • Q11: What is the final keyword in Java?
    A: The final keyword is used to make a variable, method, or class unchangeable or non-extendable. A final variable cannot be modified, a final method cannot be overridden, and a final class cannot be subclassed.
  • Q12: What are access modifiers in Java?
    A: Access modifiers are keywords that determine the accessibility or visibility of variables, methods, and classes in Java. The main access modifiers are: private: The variable, method, or class is only accessible within the same class. default (no modifier): The variable, method, or class is accessible within the same package. protected: The variable, method, or class is accessible within the same package and subclasses. public: The variable, method, or class is accessible from anywhere.
  • Q13: What is the difference between checked and unchecked exceptions?
    A: Checked exceptions are exceptions that are checked at compile-time and require the programmer to handle them explicitly using try-catch blocks or declare them in the method signature. Unchecked exceptions, also known as runtime exceptions, do not need to be explicitly handled or declared.
  • Q14: What is the difference between a StringBuffer and a StringBuilder?
    A: StringBuffer and StringBuilder are both classes that can be used to manipulate strings, but StringBuffer is thread-safe (synchronized) and StringBuilder is not. StringBuilder is generally more efficient for single-threaded scenarios.
  • Q15: What is the Java Memory Model (JMM)?
    A: The Java Memory Model is a specification that defines the behavior of threads and memory in Java programs. It guarantees the visibility of changes made by one thread to other threads and ensures the atomicity of certain operations.
  • Q16: What is the difference between the stack and the heap in Java?
    A: The stack and the heap are two memory areas used by Java programs. The stack is used for storing local variables and method calls, while the heap is used for storing objects and dynamically allocated memory.
  • Q17: What is the difference between a shallow copy and a deep copy?
    A: A shallow copy creates a new object that shares the same memory references as the original object, while a deep copy creates a new object with its own copies of the original object's data.
  • Q18: What are Java annotations?
    A: Java annotations are metadata tags that provide additional information about code elements, such as classes, methods, and variables. They can be used by the compiler, runtime, or other tools to process or modify the code.
  • Q19: What is the difference between static binding and dynamic binding?
    A: Static binding, also known as early binding, occurs at compile-time and is based on the type of the variable. Dynamic binding, also known as late binding, occurs at runtime and is based on the actual object type.
  • Q20: What is the difference between the String, StringBuilder, and StringBuffer classes?
    A: The String class is immutable, meaning its value cannot be changed once created. StringBuilder and StringBuffer are mutable and can be used for efficient string manipulation. StringBuffer is thread-safe, while StringBuilder is not.
  • Q21: What is the Java Virtual Machine (JVM)?
    A: The Java Virtual Machine is an abstract machine that executes Java bytecode. It provides a runtime environment for Java programs and ensures platform independence by translating bytecode into machine-specific instructions.
  • Q22: What is the garbage collection in Java?
    A: Garbage collection is the automatic process of reclaiming memory occupied by objects that are no longer reachable or in use. The JVM manages the memory allocation and deallocation, freeing developers from manual memory management.
  • Q23: What is the difference between a HashSet and a TreeSet?
    A: HashSet is an unordered collection that does not allow duplicate elements, while TreeSet is a sorted set that stores elements in ascending order. TreeSet has a higher memory and performance overhead compared to HashSet.
  • Q24: What is the difference between the Comparable and Comparator interfaces?
    A: The Comparable interface is used to define the natural ordering of objects and is implemented by the class itself. The Comparator interface is used to define custom comparison logic and is implemented by a separate class.
  • Q25: What is autoboxing and unboxing in Java?
    A: Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class type, while unboxing is the automatic conversion of a wrapper class type to its corresponding primitive type.
  • Q26: What is the difference between a HashMap and a Hashtable?
    A: HashMap is not synchronized and allows null keys and values, while Hashtable is synchronized and does not allow null keys or values. HashMap is generally preferred over Hashtable in non-thread-safe scenarios.
  • Q27: What is the Java ternary operator?
    A: The Java ternary operator (?:) is a shorthand version of the if-else statement. It takes three operands and returns one of two values based on a condition.
  • Q28: What are the different types of inner classes in Java?
    A: The different types of inner classes in Java are: Inner class: A non-static nested class that has access to the members of its enclosing class. Static nested class: A static class that is a member of its enclosing class. Local class: A class defined inside a block or method. Anonymous class: A class without a name that is declared and instantiated in a single statement.
  • Q29: What is the difference between method hiding and method overriding in Java?
    A: Method hiding occurs when a subclass defines a static method with the same signature as a static method in its superclass. Method hiding does not support polymorphism and is resolved at compile-time. Method overriding, on the other hand, occurs when a subclass provides a different implementation of a method that is already defined in its superclass. Method overriding supports dynamic polymorphism and is resolved at runtime.
  • Q30: What is a Java thread?
    A: A thread is a lightweight unit of execution within a program. In Java, threads are used to achieve concurrent execution and enable multiple tasks to run simultaneously.
  • Q31: What is the difference between a thread and a process?
    A: A process is an executing instance of a program, while a thread is a subset of a process. A process has its own memory space, while threads within a process share the same memory space.
  • Q32: What is synchronization in Java?
    A: Synchronization is the process of controlling the access to shared resources or critical sections of code to avoid data inconsistency and conflicts between concurrent threads. In Java, synchronization can be achieved using the synchronized keyword or using explicit locks.
  • Q33: What is the difference between the wait() and sleep() methods?
    A: The wait() method is used for inter-thread communication and causes the current thread to wait until another thread notifies it. The sleep() method is used to pause the execution of the current thread for a specified amount of time.
  • Q34: What are Java annotations and how are they used?
    A: Java annotations are metadata tags that provide additional information about code elements such as classes, methods, and variables. Annotations can be used by the compiler, runtime, or other tools to process or modify the code.
  • Q35: What is the Java Reflection API?
    A: The Java Reflection API allows a program to examine or modify the behavior of classes, interfaces, methods, and fields at runtime. It provides a way to access and manipulate the internal structure of classes and objects.
  • Q36: What is the difference between shallow copy and deep copy?
    A: Shallow copy creates a new object that references the same memory as the original object, while deep copy creates a new object with its own copy of the original object's data.
  • Q37: What is the difference between composition and inheritance?
    A: Composition is a design principle where a class contains references to other classes as member variables, while inheritance is a mechanism where a class inherits properties and behaviors from a superclass. Composition is generally considered more flexible than inheritance.
  • Q38: What is the Java serialization process?
    A: Serialization is the process of converting an object into a stream of bytes to be stored or transmitted. In Java, objects can be serialized by implementing the Serializable interface. The process can be reversed using deserialization.
  • Q39: What is the difference between checked and unchecked exceptions?
    A: Checked exceptions are exceptions that must be declared in the method signature or handled explicitly using try-catch blocks. Unchecked exceptions, also known as runtime exceptions, do not need to be declared or handled explicitly.
  • Q40: What is a lambda expression in Java?
    A: A lambda expression is a concise way to represent an anonymous function or behavior that can be passed around as a method argument or stored in a variable. It simplifies functional programming in Java.
  • Q41: What are functional interfaces in Java 8?
    A: Functional interfaces are interfaces that have exactly one abstract method and are used to support lambda expressions and functional programming. Examples include Runnable, Consumer, and Predicate.
  • Q42: What is the difference between method reference and lambda expression?
    A: A method reference is a shorthand syntax for referring to an existing method by name. It provides a way to pass a method as an argument without invoking it. Lambda expressions, on the other hand, are anonymous functions used to represent inline behavior.
  • Q43: What are the new features introduced in Java 8?
    A: Java 8 introduced several new features, including lambda expressions, functional interfaces, default methods in interfaces, the Stream API for functional-style operations on collections, and the Optional class for dealing with potentially null values.
  • Q44: What is the difference between ArrayList and LinkedList?
    A: ArrayList is an implementation of the List interface that uses a dynamic array to store elements. It provides fast random access but slower insertion and deletion. LinkedList is an implementation of the List interface that uses a doubly-linked list to store elements. It provides fast insertion and deletion but slower random access.
  • Q45: What is the difference between ConcurrentHashMap and Hashtable?
    A: ConcurrentHashMap is a concurrent version of HashMap introduced in Java 5. It allows concurrent access to the map without explicit synchronization and provides better performance in multi-threaded environments. Hashtable, on the other hand, is a legacy class that is synchronized and thread-safe but has lower performance.
  • Q46: What is the difference between an abstract class and an interface?
    A: An abstract class is a class that cannot be instantiated and can contain both abstract and non-abstract methods. It serves as a base for subclasses. An interface, on the other hand, is a collection of abstract methods that defines a contract. A class can implement multiple interfaces, but it can extend only one abstract class.
  • Q47: What is the diamond problem in multiple inheritance?
    A: The diamond problem occurs when a class inherits from two or more classes that have a common superclass. It creates ambiguity when the subclass tries to inherit the common superclass's members. In Java, this problem is avoided by allowing multiple interface inheritance but not multiple class inheritance.
  • Q48: What is the difference between static and non-static nested classes?
    A: A static nested class is a nested class that is defined as static. It does not have access to the enclosing class's non-static members. A non-static nested class, also known as an inner class, is associated with an instance of the enclosing class and can access its non-static members.
  • Q49: What are the differences between checked and unchecked exceptions?
    A: Checked exceptions are checked at compile-time and must be handled using try-catch blocks or declared in the method signature. Unchecked exceptions, also known as runtime exceptions, do not need to be declared or explicitly handled.