java interview questions Flashcards
List some important features of Java.
Ease of learning: Java is considered to be an easy-to-learn and understand language.
Security: Java is a secure language. The applications developed using this language are virus and tamper-proof.
OOP: OOP means Object-Oriented Programming Language. While Java is not considered to be purely object-oriented, the inclusion of features such as data abstraction, inheritance, and data-encapsulation, makes Java an object-oriented programming language. Thus, as such, everything in Java is considered an object.
Independent Platform: This is an important Java feature. Java does not depend on any specific platform and as long as the system has JRE installed, Java bytecode will be interpreted.
why java is secure?
Security API’s
Security manager
Void of Pointers
Memory management(garbage collector)
Compile-time checking
Cryptographic Security
Java Sandbox
Exception Handling
Java Class Loader
More details
Name the types of memory allocations in Java.
tAnother popular Java interview question is about memory allocation. Five important types of memory allocations in Java are - Heap memory, Class memory, Native method stock memory, Stack memory, and Program-counter memory.
what is stack memory in java?
Stack Memory in Java is used for static memory allocation and the execution of a thread. It contains primitive values that are specific to a method and references to objects referred from the method that are in a heap.
Access to this memory is in Last-In-First-Out (LIFO) order. Whenever we call a new method, a new block is created on top of the stack which contains values specific to that method, like primitive variables and references to objects.
When the method finishes execution, its corresponding stack frame is flushed, the flow goes back to the calling method, and space becomes available for the next method.
source
what are key features of stack memory in java?
Some other features of stack memory include:
- It grows and shrinks as new methods are called and returned, respectively.
- Variables inside the stack exist only as long as the method that created them is running.
It’s automatically allocated and deallocated when the method finishes execution. - If this memory is full, Java throws java.lang.StackOverFlowError.
- Access to this memory is fast when compared to heap memory.
- This memory is threadsafe, as each thread operates in its own stack.
source
what is heap space in java?
Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory.
These objects have global access and we can access them from anywhere in the application.
We can break this memory model down into smaller parts, called generations, which are:
- Young Generation – this is where all new objects are allocated and aged. A minor Garbage collection occurs when this fills up.
- Old or Tenured Generation – this is where long surviving objects are stored. When objects are stored in the Young Generation, a threshold for the object’s age is set, and when that threshold is reached, the object is moved to the old generation.
- Permanent Generation – this consists of JVM metadata for the runtime classes and application methods.
what are some key features of java heap memory?
- It’s accessed via complex memory management techniques that include the Young Generation, Old or Tenured Generation, and Permanent Generation.
- If heap space is full, Java throws java.lang.OutOfMemoryError.
- Access to this memory is comparatively slower than stack memory
- This memory, in contrast to stack, isn’t automatically deallocated. It needs Garbage Collector to free up unused objects so as to keep the efficiency of the memory usage.
- Unlike stack, a heap isn’t threadsafe and needs to be guarded by properly synchronizing the code.
source
what is JVM?
Java Virtual Machine (JVM) is an implementation of a virtual machine which executes a Java program.
The JVM first interprets the bytecode. It then stores the class information in the memory area. Finally, it executes the bytecode generated by the java compiler.
It is an abstract computing machine with its own instruction set and manipulates various memory areas at runtime.
Components of the JVM are:
Class Loaders
Run-Time Data Areas
Execution Engine
source
what is classloader in JVM?
We know that Java Program runs on Java Virtual Machine (JVM). When we compile a Java Class, JVM creates the bytecode, which is platform and machine-independent. The bytecode is stored in a .class file. When we try to use a class, the ClassLoader loads it into the memory.
source
what are built-in classloader in java?
There are three types of built-in ClassLoader in Java.
- Bootstrap Class Loader – It loads JDK internal classes. It loads rt.jar and other core classes for example java.lang.* package classes.
- Extensions Class Loader – It loads classes from the JDK extensions directory, usually $JAVA_HOME/lib/ext directory.
- System Class Loader – This classloader loads classes from the current classpath. We can set classpath while invoking a program using -cp or -classpath command line option.
source
what is JRE?
Java Runtime Environment (JRE) is a bundle of software components used to run Java applications.
Core components of the JRE include:
An implementation of a Java Virtual Machine (JVM)
Classes required to run the Java programs
Property Files
source
What is JDK?
Java Development Kit (JDK) provides environment and tools for developing, compiling, debugging, and executing a Java program.
Core components of JDK include:
JRE
Development Tools
source
what is class area of memory in java?
The class method area is the memory block that stores the class code, variable code(static variable, runtime constant), method code, and the constructor of a Java program. (Here method means the function which is written inside the class). It stores class-level data of every class such as the runtime constant pool, field and method data, the code for methods.
source
what is program counter register memory in java?
Each JVM thread that carries out the task of a specific method has a program counter register associated with it. The non-native method has a PC that stores the address of the available JVM instruction whereas, in a native method, the value of the program counter is undefined. PC register is capable of storing the return address or a native pointer on some specific platform.
source
what are native method stacks in java?
Also called C stacks, native method stacks are not written in Java language. This memory is allocated for each thread when it’s created And it can be of a fixed or dynamic nature.
source
As a language, Java is considered platform-independent. Why?
Java is considered to be platform-independent because it does not depend on any hardware or software. When the Java programmer compiles Java code, the compiler converts the code into bytecode that can be run on different platforms. The only constraint is that the platform must have JRE (runtime environment) or Java Virtual Machine (JVM) installed on it.
source
What is data-encapsulation in Java?
Data-encapsulation in Java is the process of hiding the variables (data) and the code applied to the data in a single unit. It is a significant feature of object-oriented programming. This helps Java developers to isolate different objects from each other and to create modules for each. By this, all objects would have their own behaviors, attributes, and functions. It prevents data or object mixing, hides data, enhances security.
source
What are wrapper classes in Java?
Java, as a programming language, supports primitive data types. Wrapper classes help convert these primitive data types into objects or reference types and vice versa. Thus, the wrapper classes are so named because they ‘wrap’ these data types into identifiable objects of the specific data classes the primitive data types belong to. This Java interview question can help the interviewer assess whether you are comfortable using both old and new data types.
source
What are constructors in Java?
In Java, constructors can be understood as a block of codes applied to an object to initialize it. It can be understood as a special method for initializing an object. Constructors are called when a class instance is created such as creating an object using the new() keyword. When a class does not contain a constructor, a default constructor, provided by the Java compiler, is automatically called.
There are two types of Java constructors - default and parameterized. When no parameters are defined, the constructor is called a default constructor. When specific parameters are defined for a constructor, it is called a parameterized constructor. While you may be using the Java programming language for some time, this Java interview question shows the interviewer that you know the technicalities too.
source
Why is Java not considered to be purely object-oriented?
Java supports primitive data types such as boolean, int, byte, long, float, etc. An object-oriented language must only support objects. However, the primitive data types are not objects, hence Java is not considered to be purely object-oriented.
source
how use of pointers slows down garbage collection ?
because, it may happen that you hold some reference to the object somewhere, and GC only collects objects which have no references left.
source
Does Java use pointers? If not, why?
No, unlike C++, Java doesn’t use pointers. Java’s main focus is simplicity. The use of pointers complicates the process, especially for new java programmers. The use of pointers can also lead to possible errors. Moreover, pointers grant direct access to memory and thus, compromise security. By excluding pointers from Java, a certain amount of abstraction is achieved. Java has automatic Garbage Collectors, the use of pointers can slow down the garbage collection process. This advanced Java interview question deals with the intricacies of the language and answering it can help set you apart from other candidates.
source
why do you need to use synchronization in java?
To prevent thread interleaving or interference
To provide consistency to the program
what is the difference between pass-by-value and pass-by-reference?
- Pass-by-reference: When a method is called, the method arguments reference the same variable in memory as the caller.
- Pass-by-value: When a method is called, the caller passes a copy of the argument variables to the method resulting in two values in memory.
source
Can you override static methods in Java?
No. While it is possible to hide the parent static method with a static method of the same signature class, this is not called overriding in the strict sense. This is because, during the run-time, no polymorphism can take place.
source
Can you access a static method a class A from outside using an object of type A?
No
can method1 and method2 print i and k?
public class Test { static int i; int k; public static void method1() { System.out.println(i); System.out.println(k); } public void method2() { System.out.println(i); System.out.println(k); } }
method1 can only print i and will throw error on printing k because, a non-static variable cannot be accessed from a static method or context.
method2 can print both i and k without any errors.
Can you override static methods in Java?
No. While it is possible to hide the parent static method with a static method of the same signature class, this is not called overriding in the strict sense.
public class Test { static int i; int k; public static void method1() { System.out.println(i); System.out.println(k); } public static void method2() { } public void method3() { System.out.println(i); System.out.println(k); } } public class Test2 extends Test { public static void method1() { // this is possible } @Override public static void method2() { // this is not possible, it will give error as: static methods cannot be annotated with @override } @Override public void method3() { // this is possible } }
What do you understand about ClassLoader in Java?
ClassLoader is an integral element of the JRE (Java Runtime Environment). The function of the ClassLoader is to dynamically load Java classes directly to the JVM (Java Virtual Machine) during program execution, without notifying the Java run time system. The file to be executed is first loaded by ClassLoaders. Bootstrap, Extension, and Application are the ClassLoaders in Java. This is an advanced Java interview question and answering it can establish your expertise.
What are the prospects of session management in servlets?
A session is any ongoing random conversation between a client and the server. The communication between the two is composed of requests and responses from both sides. There are several ways of managing a session. Applying cookies, using session management API, authenticating the user, hidden fields in HTML, and rewriting URL are some ways of session management. However, the most common way of managing a session is using a session ID when the communication between a client and the server takes place. This Java interview question shows your application orientation to the client.
What Is a JIT Compiler?
When we compile our Java program (e.g., using the javac command), we’ll end up with our source code compiled into the binary representation of our code – a JVM bytecode. This bytecode is simpler and more compact than our source code, but conventional processors in our computers cannot execute it.
To be able to run a Java program, the JVM interprets the bytecode. Since interpreters are usually a lot slower than native code executing on a real processor, the JVM can run another compiler which will now compile our bytecode into the machine code that can be run by the processor. This so-called just-in-time compiler is much more sophisticated than the javac compiler, and it runs complex optimizations to generate high-quality machine code.
more detailed explaination
Highlight the importance of Java as a language.
Java is a very secure programming language as it is hard to bug or tamper with. It is also very easy to learn and comprehend when developing applications. Most importantly, Java is a self-dependent language that runs on its own JRE platform.
Why is Java considered to be a platform-independent language?
Java is considered platform-independent as it does not depend on any software or hardware. However, it is flexible enough to be run on different platforms with the condition that the platform has a JRE environment installed on it.
Can you explain what you understand by the term Inheritance?
The term inheritance occurs when one class in a code extends to another. This means that codes used in one class can be reused in another class.
What is the purpose of encapsulation?
- Data Hiding: it is a way of restricting the access of our data members by hiding the implementation details. Encapsulation also provides a way for data hiding. The user will have no idea about the inner implementation of the class. It will not be visible to the user how the class is storing values in the variables. The user will only know that we are passing the values to a setter method and variables are getting initialized with that value.
- Increased Flexibility: We can make the variables of the class read-only or write-only depending on our requirements. If we wish to make the variables read-only then we have to omit the setter methods like setName(), setAge(), etc. from the above program or if we wish to make the variables write-only then we have to omit the get methods like getName(), getAge(), etc. from the above program
- Reusability: Encapsulation also improves the re-usability and is easy to change with new requirements.
Testing code is easy: Encapsulated code is easy to test for unit testing. - Freedom to programmer in implementing the details of the system: This is one of the major advantage of encapsulation that it gives the programmer freedom in implementing the details of a system. The only constraint on the programmer is to maintain the abstract interface that outsiders see.
source
What is the solution where multiple inheritances is not possible to achieve?
The solution to this problem is to use interfaces instead of inheritance. Interfaces allow a class to define multiple behaviors without the need to extend multiple classes.
What is a constructor in Java?
A block of code used to initialize an object is called a constructor. The constructor must share the same name as the class and it has no return type. Default and parameterized constructors are the two types of constructors.
What is the difference between the creation of a string using new() and that of literal?
A new object is created when using new() whereas using literal, the object already existing might return with the same name.
what is a thread in java?
A thread in Java is the direction or path that is taken while a program is being executed. Generally, all the programs have at least one thread, known as the main thread, that is provided by the JVM or Java Virtual Machine at the starting of the program’s execution. At this point, when the main thread is provided, the main() method is invoked by the main thread.
what is multithreading in java?
Multi threading in java is simply executing multiple threads in a program.
best video to understand
how to implement threads in java?
you can implement threads in two ways:
1. By extending Thread
class
2. By implementing Runnable
interface
which method is best while implementing multithreading in java? extending threads class or implementing runnable interface.
implementing runnable interface is a better choice because, it increases flexibility by letting you add more interfaces to implement in your class. Other classes can also extend your class.
Since, java doesn’t allow multiple inheritance, extending Thread
class limits you.
what is synchronization in java?
Synchronization in Java is the process that allows only one thread at a particular time to complete a given task entirely.
By default, the JVM gives control to all the threads present in the system to access the shared resource, due to which the system approaches race condition.
Now, let’s take a real-world example to understand the concept of race condition clearly and how you could avoid the same using Synchronization.
Imagine a classroom where three teachers are simultaneously present to teach the same class. In this scenario, the classroom acts as a shared resource, and the three teachers are the threads. All of them can’t teach at the same time. This scenario when looked at in the context of a computer language is referred to as Race Condition, where there are multiple threads present to do a given task.
To solve the above problem, we use Synchronization in Java. It is also a good practice especially when the threads update the data concurrently.
source
what is lambda in java?
Java 8 brought a powerful new syntactic improvement in the form of lambda expressions. A lambda is an anonymous function that we can handle as a first-class language citizen. For instance, we can pass it to or return it from a method.
Before Java 8, we would usually create a class for every case where we needed to encapsulate a single piece of functionality. This implied a lot of unnecessary boilerplate code to define something that served as a primitive function representation.
how to implement sychronization in java?
Synchronization in Java is done by using the synchronized keyword. This keyword can be used on top of a method or a block.
what are the types of synchronization in java?
There are two types of Synchronization in Java, as mentioned below-
Process Synchronization
When multiple threads are executed simultaneously process synchronization ensures that they reach a particular state and agree to a certain set of actions.
Thread Synchronization
When multiple threads want to access the same resource, thread synchronization makes sure that only one thread gains access at a time.
source
What is the importance of synchronization?
Synchronization helps to execute threads one after another. It also prevents memory constituency errors due to shared access methods.
Does Java use Pointers and give a reason?
Java does not use pointers. This is because they are not safe and can increase the complexity of the program. Pointers are the exact opposite of Java’s simplicity.
What is OOP?
OOP is an abbreviation for Object-Oriented Programming. It is a model where programs are organized around objects rather than functions and logic. Its main focus is on objects more than using logic and is mainly preferred for large and complex codes.
What is Double Brace Initialization?
we can combine the creation and initialization in a single statement; this is where we make use of double braces:
@Test public void whenInitializeSetWithDoubleBraces_containsElements() { Set<String> countries = new HashSet<String>() { { add("India"); add("USSR"); add("USA"); } }; assertTrue(countries.contains("India")); }
As can be seen from above example, we are:
Creating an anonymous inner class which extends HashSet
Providing an instance initialization block which invokes the add method and adds the country name to the HashSet
Finally, we can assert whether the country is present in the HashSet
source
why using double brace initialization is considered anti-pattern?
Disadvantages of using double braces are:
- Obscure, not widely known way to do the initialization
- It creates an extra class every time we use it
Doesn’t support the use of the “diamond operator” – a feature introduced in Java 7 - Doesn’t work if the class we are trying to extend is marked final
- Holds a hidden reference to the enclosing instance, which may cause memory leaks
- It’s due to these disadvantages that double brace initialization is considered as an anti-pattern.
source
what is composition in java?
Composition is a “belongs-to” type of relationship. It means that one of the objects is a logically larger structure, which contains the other object. In other words, it’s part or member of the other object.
Alternatively, we often call it a “has-a” relationship (as opposed to an “is-a” relationship, which is inheritance).
For example, a room belongs to a building, or in other words a building has a room. So basically, whether we call it “belongs-to” or “has-a” is only a matter of point of view.
Composition is a strong kind of “has-a” relationship because the containing object owns it. Therefore, the objects’ lifecycles are tied. It means that if we destroy the owner object, its members also will be destroyed with it. For example, the room is destroyed with the building in our previous example.
Note that doesn’t mean, that the containing object can’t exist without any of its parts. For example, we can tear down all the walls inside a building, hence destroy the rooms. But the building will still exist.
In terms of cardinality, a containing object can have as many parts as we want. However, all of the parts need to have exactly one container.
source
what is aggregation in java?
Aggregation is also a “has-a” relationship. What distinguishes it from composition, that it doesn’t involve owning. As a result, the lifecycles of the objects aren’t tied: every one of them can exist independently of each other.
For example, a car and its wheels. We can take off the wheels, and they’ll still exist. We can mount other (preexisting) wheels, or install these to another car and everything will work just fine.
Of course, a car without wheels or a detached wheel won’t be as useful as a car with its wheels on. But that’s why this relationship existed in the first place: to assemble the parts to a bigger construct, which is capable of more things than its parts.
Since aggregation doesn’t involve owning, a member doesn’t need to be tied to only one container. For example, a triangle is made of segments. But triangles can share segments as their sides.
source
what is association in java?
Association is the weakest relationship between the three. It isn’t a “has-a” relationship, none of the objects are parts or members of another.
Association only means that the objects “know” each other. For example, a mother and her child.
In Java, we can model association the same way as aggregation:
class Child {} class Mother { List<Child> children; }
But wait, how can we tell if a reference means aggregation or association?
Well, we can’t. The difference is only logical: whether one of the objects is part of the other or not.
Also, we have to maintain the references manually on both ends as we did with aggregation:
class Child { Mother mother; } class Mother { List<Child> children; }
What is a Singleton class in Java?
A singleton class is a class that is capable of possessing only one object at a time.
give some usecases for singleton in java?
Logging System
In a logging system, we may want to have only one instance of the logger, as multiple loggers can cause inconsistent and hard-to-read logs. In this case, we can apply the Singleton pattern to create a single logger instance that can be accessed from different parts of the code.
package org.example; public class Logger { private static Logger instance; private Logger() { // private constructor to prevent instantiation from outside the class } public static Logger getInstance() { if(instance == null) { instance = new Logger(); } return instance; } public void log(String message) { // log the message } public static void main(String[] args) { Logger instance1 = Logger.getInstance(); Logger instance2 = Logger.getInstance(); /** * both prints the same instance * org.example.Logger@7cf10a6f * org.example.Logger@7cf10a6f */ System.out.println(instance1); System.out.println(instance2); } }
Database Connection
In a web application that uses a database, we may want to create a single database connection instance to avoid creating multiple connections that can cause performance issues. By using the Singleton pattern, we can create a single database connection instance that can be shared across the application.
how to implement a singleton class in java?
The most popular approach is to implement a Singleton by creating a regular class and making sure it has:
a private constructor
a static field containing its only instance
a static factory method for obtaining the instance
package org.example; public class Logger { private static Logger instance; private Logger() { // private constructor to prevent instantiation from outside the class } public static Logger getInstance() { if(instance == null) { instance = new Logger(); } return instance; } public void log(String message) { // log the message } public static void main(String[] args) { Logger instance1 = Logger.getInstance(); Logger instance2 = Logger.getInstance(); /** * both prints the same instance * org.example.Logger@7cf10a6f * org.example.Logger@7cf10a6f */ System.out.println(instance1); System.out.println(instance2); } }
Define a Java string pool.
The Java string pool is a repository of strings stored in the Java virtual machine (JVM). The pool stores only one instance of each literal string.
What is the difference between JVM and JRE?
JRE has class libraries and other JVM files however, it does not have any tools for Java development such as a debugger and compiler. While JVM has a Just In Time(JIT) tool to convert all the Java codes into compatible machine language.
What is method overloading in Java?
It’s a process of creating multiple method signatures using one method. There are two ways of achieving this i.e changing the return type of the method and varying the number of arguments.
What are the five stages of a thread’s lifecycle?
The stages of a thread’s lifecycle are as follows:
- Newborn state- This is where a new thread is created. In this newborn state, the code has not been run or executed as yet.
- Runnable state- A thread that is ready to run is put into the runnable state. The code may be running or ready to run at any given time but at this stage, the thread scheduler provides the time for the thread to run.
- Running state- This stage is when the thread receives a CPU moving from the runnable to the running state.
- Blocked state- When the thread is inactive for a period of time temporarily, the thread is said to be in a blocked state.
- Dead state- Also known as the terminated state. This is when the thread has either finished its job and exists or when some unexpected event occurs such as a segmentation fault.
Define what is enumeration in Java.
Enumeration also known as enum, is an interface in Java. It allows the sequential access of the elements stored in the collection.
Can you define Java packages and their significance?
Java packages can be defined as a collection of classes and interfaces which are bundled together while they relate to one another. It also helps developers to group a code for reuse and after being packaged it can be imported in other classes for repurposing.
What is JPA?
JPA stands for Java Persistence API. It enables developers to create the persistence layer for desktop/ web applications.
Why is Java considered dynamic?
Java is considered as Dynamic because of Byte code [a class file]. A source code written in one platform,
that can be executed in any platform. And it also loads the class files at run time.
Anything that happens at run time is considered as Dynamic.
source
What are the steps involved when connecting to a database in Java?
Steps needed to connect to a database in Java are as follows:
- Registering the driver class
- Creating connection
- Creating statement
- Executing queries
- Closing connects
what is serialization in java?
Serialization is the conversion of the state of an object into a byte stream; deserialization does the opposite. Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes, which we can then save to a database or transfer over a network.
source
how to implement serialization in java?
The serialization process is instance-independent; for example, we can serialize objects on one platform and deserialize them on another. Classes that are eligible for serialization need to implement a special marker interface, Serializable.
Both ObjectInputStream and ObjectOutputStream are high level classes that extend java.io.InputStream and java.io.OutputStream, respectively. ObjectOutputStream can write primitive types and graphs of objects to an OutputStream as a stream of bytes. We can then read these streams using ObjectInputStream.
The most important method in ObjectOutputStream is:
public final void writeObject(Object o) throws IOException;
This method takes a serializable object and converts it into a sequence (stream) of bytes. Similarly, the most important method in ObjectInputStream is:
public final Object readObject() throws IOException, ClassNotFoundException;
This method can read a stream of bytes and convert it back into a Java object. It can then be cast back to the original object.
source
What is the difference between an array and a vector?
An array groups data of the same primitive type and is static, while vectors can hold data of different types. Vectors are dynamic though.
The Vector class is a thread-safe implementation of a growable array of objects. It implements the java.util.List interface and is a member of the Java Collections Framework. While it’s similar to ArrayList, these classes have significant differences in their implementations.
source
how vectors work in java?
The Vector class is designed to function as a dynamic array that can expand or shrink according to the application’s needs. Thus, we can access the objects of the Vector using the indices. Additionally, it maintains the insertion order and stores duplicate elements.
Every Vector aims to enhance its storage handling by keeping track of both the capacity and the capacityIncrement. The capacity is nothing but the size of the Vector. The size of the Vector increases as we add elements to it. Therefore, the capacity remains consistently equal to or greater than the Vector‘s size.
Every time the length of a Vector reaches its capacity, the new length is calculated:
newLength = capacityIncrement == 0 ? currentSize * 2 : currentSize + capacityIncrement
Copy
Similar to ArrayList, the iterators of the Vector class are also fail-fast. It throws a ConcurrentModificationException if we try to modify the Vector while iterating over it. However, we can access the iterator’s add() or remove() methods to structurally modify the Vector.
source
How is garbage collection done in Java?
Garbage collection in Java is done automatically by the Java Virtual Machine (JVM) at runtime. The JVM uses a garbage collection algorithm to identify and reclaim memory from objects that are no longer in use.
How do you create a copy of an object?
To create a copy of an object in Java, you can use the clone() method of the Object class. This method creates a shallow copy of the object, meaning that only the object’s references are copied, not its values.
Are primitive data types passed-by-value or passed-by-reference in java?
Primitives – like int and byte – are always stored and passed as the exact value and not a reference to the value. This is fine because the largest primitive – long – is typically the same amount of memory as the reference would be, and primitives are always immutable, so they can’t be changed anyway.
source
what is shallow copy?
In some cases, we may want to create a copy of a value so that two different pieces of code see different copies of the same value. This allows one to be manipulated differently from the others, for example.
The simplest way to do this is to make a shallow copy of the object. This means we create a new object that contains all the same fields as the original, with copies of the same values.
For relatively simple objects, this works fine. However, if our objects contain other objects, then only the reference to these will be copied. This, in turn, means that the two copies contain references to the same value in memory.
source
what is deep copy of an object?
This is where we copy each field from the original to the copy, but as we do so, we perform a deep copy of those instead of just copying the references.
This will then mean that the new copy is an exact copy of the original, but in no way connected so that no changes to one will be reflected in the other.
source
Outline the difference between queue and stack.
The main difference between stack and queue is that stack is based on LIFO (Last in first out) while a queue is based on FIFO( First in first out) principle. Both are used as placeholders for the collection of data.
During program compilation, what type of exceptions are caught?
Checked exceptions can be caught during the time of program compilation. The checked exceptions must be handled by using the try catch block in the code to successfully compile the code.