Java Questions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the difference between == and equals() for comparing objects?

== checks for reference equality, while equals() checks for value equality. For custom objects, you should override the equals() method to define logical equality.

What is the difference between == and .equals() in Java?

== compares object references, while .equals() compares the content of objects. For primitive types, == compares the values.

What is the difference between Map and Set?

A Map stores key-value pairs, where each key is unique, while a Set stores unique elements without any specific order.

What is a constructor in Java?

A constructor is a special method that is called when an object is instantiated. It initializes the object's properties. Constructors can be overloaded.

What is the difference between a constructor and a method?

A constructor is called when an object is created and does not have a return type, while a method is called to perform a specific task and has a return type.

What is a functional interface?

A functional interface is an interface that has exactly one abstract method. They can be used as the assignment target for lambda expressions.

What is a package in Java?

A package is a namespace that organizes a set of related classes and interfaces. It helps avoid naming conflicts and controls access.

What is the difference between shallow copy and deep copy?

A shallow copy creates a new object but copies the references of the original object's fields. A deep copy creates a new object and recursively copies all fields, creating copies of mutable objects.

What is a singleton class?

A singleton class is a class that allows only one instance of itself to be created and provides a global point of access to that instance.

What is the purpose of the static block?

A static block is used for static initializations of a class. It runs when the class is loaded into memory and can be used to initialize static variables.

Explain the difference between an abstract class and an interface.

Abstract classes can have concrete methods, instance variables, and constructors, while interfaces can only have abstract methods (prior to Java 8) and constants. Classes can extend only one abstract class, but they can implement multiple interfaces.

What is the difference between abstract class and interface?

An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods (prior to Java 8). A class can implement multiple interfaces but can extend only one abstract class.

What is an interface in Java?

An interface is a reference type in Java that can contain only constants, method signatures, default methods, static methods, and nested types. It cannot contain instance fields.

What are Java annotations?

Annotations are metadata that provide data about a program but are not part of the program itself. They can be used for various purposes, such as providing information to the compiler or runtime.

What is the difference between ArrayList and LinkedList in Java?

ArrayList is an ordered collection that uses an array to store elements. LinkedList is an ordered collection that uses doubly-linked nodes to store elements. ArrayList provides constant-time access to elements by index, while LinkedList provides constant-time access to elements at the beginning and end of the list.

What is the difference between checked and unchecked exceptions?

Checked exceptions are checked at compile-time, while unchecked exceptions are checked at runtime. Checked exceptions must be declared in the method signature or handled with a try-catch block.

What are the different types of exceptions in Java?

Exceptions in Java can be categorized into checked exceptions (e.g., IOException) and unchecked exceptions (e.g., NullPointerException).

Explain the concept of garbage collection in Java.

Garbage collection is the process of automatically freeing memory by removing objects that are no longer reachable or referenced in the application.

What is the difference between HashMap and Hashtable?

HashMap is not synchronized and allows null keys and values, while Hashtable is synchronized and does not allow null keys or values.

Explain the concept of inheritance in Java.

Inheritance is a mechanism where a new class (subclass) inherits properties and behaviors (methods) from an existing class (superclass). It promotes code reuse.

What are Java Collections?

Java Collections is a framework that provides classes and interfaces for storing and manipulating groups of objects. It includes lists, sets, maps, and queues.

What are lambda expressions in Java?

Lambda expressions are a feature introduced in Java 8 that allows you to express instances of single-method interfaces (functional interfaces) in a more concise way.

Explain the difference between List, Set, and Map.

List allows duplicate elements and maintains the order of insertion. Set does not allow duplicates and does not guarantee order. Map stores key-value pairs, where each key is unique.

What is method overloading?

Method overloading allows a class to have multiple methods with the same name but different parameter lists (types or number of parameters).

Explain the concept of method overloading and method overriding in Java.

Method overloading allows a class to have multiple methods with the same name but different parameters. Method overriding allows a subclass to provide a specific implementation of a method that is already provided by its superclass.

Explain method overriding in Java.

Method overriding occurs when a subclass provides a specific implementation of a method that is already defined in its superclass. The method must have the same name, return type, and parameters.

Explain the concept of polymorphism in Java.

Polymorphism allows methods to do different things based on the object that it is acting upon. It can be achieved through method overriding and method overloading.

What is the difference between Runnable and Callable?

Runnable does not return a result and cannot throw checked exceptions, while Callable can return a result and can throw checked exceptions.

What is the difference between String, StringBuilder, and StringBuffer?

String is immutable, meaning once created, it cannot be changed. StringBuilder and StringBuffer are mutable; however, StringBuffer is synchronized (thread-safe), while StringBuilder is not.

What is the java.lang.Object class?

The Object class is the root class of all Java classes. Every class in Java inherits from the Object class, which provides basic methods like equals(), hashCode(), and toString().

What is the java.util.Optional class?

The Optional class is a container object which may or may not contain a value. It is used to avoid NullPointerExceptions and to represent optional values.

What is the purpose of the Stream API in Java?

The Stream API is used to process sequences of elements (collections) in a functional style, allowing for operations like filtering, mapping, and reducing.

What is the purpose of the String class in Java?

The String class represents a sequence of characters. It provides methods for manipulating strings, such as concatenation, substring extraction, and searching.

What are the access modifiers in Java?

The access modifiers in Java are public, protected, private, and package-private (default). They control the visibility of classes, methods, and variables.

What is the purpose of the assert keyword?

The assert keyword is used to create assertions in the code. It helps in debugging by allowing you to test assumptions about your program.

What is the purpose of the clone() method?

The clone() method is used to create a copy of an object. The class must implement the Cloneable interface to allow cloning.

What is the purpose of the default keyword in interfaces?

The default keyword allows you to add new methods to interfaces with a default implementation without breaking existing implementations.

What is the significance of the default keyword in interfaces?

The default keyword allows you to add new methods to interfaces with a default implementation without breaking the existing implementations.

What is the purpose of the final keyword in Java?

The final keyword is used to create constants, prevent method overriding, and prevent class inheritance.

What is the purpose of the finalize() method?

The finalize() method is called by the garbage collector before an object is removed from memory. It can be overridden to perform cleanup operations.

Explain the purpose of the hashCode() and equals() methods.

The hashCode() method returns an integer hash code value for an object. The equals() method compares two objects for equality. These methods are used together to ensure that objects with the same content have the same hash code value.

What is the purpose of the instanceof operator?

The instanceof operator is used to test whether an object is an instance of a specific class or subclass.

What is the purpose of the main method?

The main method is the entry point of a Java application. It must be declared as public static void main(String[] args).

What is the significance of the main method in Java?

The main method is the entry point of any Java application. It must be declared as public static void main(String[] args).

What are the main principles of Object-Oriented Programming (OOP)?

The main principles of OOP are encapsulation, inheritance, polymorphism, and abstraction.

What is the significance of the native keyword?

The native keyword indicates that a method is implemented in platform-specific code (usually C or C++) outside of the Java runtime.

What is the purpose of the static keyword in Java?

The static keyword is used to create class-level variables and methods that can be accessed without creating an instance of the class.

What is the purpose of the super keyword?

The super keyword is used to refer to the immediate parent class object. It can be used to access parent class methods and constructors.

What is the purpose of the synchronized keyword?

The synchronized keyword is used to control access to a method or block by multiple threads, ensuring that only one thread can execute it at a time.

Explain the purpose of the synchronized keyword in Java.

The synchronized keyword is used to control access to critical sections of code to prevent race conditions and ensure thread safety.

What is the purpose of the this keyword in Java?

The this keyword refers to the current instance of a class. It is used to differentiate between instance variables and parameters with the same name.

What is the transient keyword?

The transient keyword is used in serialization to indicate that a field should not be serialized. It will not be saved when the object is converted to a byte stream.

What is the purpose of the try-with-resources statement?

The try-with-resources statement is used to automatically close resources (like files or sockets) after use, ensuring proper resource management.

What is the purpose of the volatile keyword?

The volatile keyword indicates that a variable's value will be modified by different threads. It ensures visibility of changes to variables across threads.

How do you handle exceptions in Java?

Use try-catch blocks to handle exceptions. The catch block specifies the type of exception to catch. The finally block is used for cleanup code that should run regardless of an exception.

How do you create a thread in Java?

You can create a thread by either extending the Thread class or implementing the Runnable interface and then passing an instance of the Runnable to a Thread object.

How do you implement a thread-safe singleton in Java?

You can implement a thread-safe singleton using the double-checked locking pattern or by using an enum, which is inherently thread-safe.

How do you sort a list in Java?

You can sort a list using the Collections.sort() method or the List.sort() method, providing a comparator if needed.

What is the difference between throw and throws?

throw is used to explicitly throw an exception, while throws is used in method declarations to indicate that a method can throw exceptions.


Set pelajaran terkait

New Opportunities pre-intermediate Module 2-2

View Set

Chapter 6B Voting: rights and IDs

View Set

UNIT 6 - Chapter 18 Caring for a Client with Cancer

View Set