Advanced JAVA-2 marks
Advanced JAVA-2 marks
UNITS) 1) What is
an array?
In Java, an array is a data structure that allows you to store multiple
values of the same data type in a contiguous memory block.
Arrays in Java are widely used for storing and manipulating collections of
data.
2) Write the advantages and disadvantages of an array?
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort
the data efficiently.
o Random access: We can get any data located at an index position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
3) What is strings?
String Pool, also known as SCP (String Constant Pool), is a special storage
space in Java heap memory that is used to store unique string objects.
Whenever a string object is created, it first checks whether the String object
with the same string value is already present in the String pool or not, and
if it is available, then the reference to the string object from the string pool
is returned.
Otherwise, the new string object is added to the string pool, and the
respective reference will be returned.
5) Is String immutable in Java? If so, then what are the benefits of Strings
being Immutable?
Yes, Strings are immutable in Java. Immutable objects mean they can't be
changed or altered once they've been created.
However, we can only modify the reference to the string object.
String objects in Java are immutable and final, so we can't change their
value after they are created.
Each time we manipulate a string, a new String object is created, and all
previous objects will be garbage, placing a strain on the garbage collector.
This is why The Java team developed StringBuffer.
A StringBuffer is a mutable object, meaning it can be changed, but the
string is an immutable object, so it cannot be changed once it has been
created.
}
8) How to reverse an array?
package arrays;
import java.util.Scanner;
while(start<end)
{
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
for(int i:arr)
{
System.out.println(i);
}
}
}
In Java, a thread always exists in any one of the following states. These states are:
1. New
2. Active
3. Blocked / Waiting
4. Timed Waiting
5. Terminated
10) What is multithreading?
Yes, calling run() method directly is valid, but it will not work as a
thread instead it will work as a normal object.
There will not be context-switching between the threads.
When we call the start() method, it internally calls the run() method,
which creates a new stack for a thread while directly calling the run() will
not create a new stack.
In Java, when we create the threads, they are supervised with the help of
a Thread Scheduler, which is the part of JVM.
Thread scheduler is only responsible for deciding which thread should
be executed.
Java thread scheduler also works for deciding the following for a thread:
It selects the priority of the thread.
It determines the waiting time for a thread
It checks the Nature of thread
15) What is race-condition?
16)What is ThreadPriority?
Priorities in threads is a concept where each thread is having a priority which in
layman’s language one can say every object is having priority here which is
represented by numbers ranging from 1 to 10.
The default priority is set to 5 as excepted.
Minimum priority is set to 1.
Maximum priority is set to 10.
Here 3 constants are defined in it namely as follows:
1. public static int NORM_PRIORITY
2. public static int MIN_PRIORITY
3. public static int MAX_PRIORITY
17)What is deadlock?
Java API, which stands for "Application Programming Interface," refers to a set
of pre-defined classes, methods, and interfaces provided by the Java programming
language for developers to use when building Java applications.
Servlets are a key part of the Java Platform, Enterprise Edition (Java
EE), and they are commonly used to create web applications.
Servlets are less expensive than CGI. CGI is more expensive than Servlets.
Servlets can handle the cookies. CGI cannot handle the cookies.
Custom tag libraries, often referred to simply as tag libraries, are a feature in web
development that allows developers to define and use their own custom tags in JSP
(JavaServer Pages) and other Java-based web frameworks.
Implicit objects in web development are predefined objects that are automatically
available to server-side scripts (e.g., in JavaServer Pages or JSP, Active Server
Pages or ASP, and other similar technologies) without the need for explicit
declaration.