0% found this document useful (0 votes)
15 views

Advanced JAVA-2 marks

Java

Uploaded by

kaviyakavi638261
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Advanced JAVA-2 marks

Java

Uploaded by

kaviyakavi638261
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

2 marks (ALL

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?

 Strings are used for storing text.

 A String variable contains a collection of characters surrounded by


double quotes:

 Example:String greeting = "Hello";

4) Explain String pool in Java.

 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.

6) State the difference between String and StringBuffer.

 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.

7) Write the program for reversing a string.


package strings;
import java.util.Scanner;

public class Reverse {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);


String s=sc.nextLine();
String strn="";
for(int i=0;i<s.length();i++)
{
strn=s.charAt(i)+strn;
}
System.out.println(strn);

}
8) How to reverse an array?
package arrays;
import java.util.Scanner;

public class Reverse {


public static void main(String[]args)
{
int arr[]={5,2,1,7,5};
int start=0;
int temp;
int end=arr.length-1;

while(start<end)
{
temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
for(int i:arr)
{
System.out.println(i);
}
}
}

9) Draw the lifecycle of Thread.

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?

 Multithreading is a process of executing multiple threads simultaneously.


Multithreading is used to obtain the multitasking. It consumes less
memory and gives the fast and efficient performance.

Its main advantages are:


 Threads share the same address space.
 The thread is lightweight.
 The cost of communication between the processes is low.

11) What do you understand by inter-thread communication?


 The process of communication between synchronized threads is
termed as inter-thread communication.
 Inter-thread communication is used to avoid thread polling in Java.
 The thread is paused running in its critical section, and another thread
is allowed to enter (or lock) in the same critical section to be executed.
 It can be obtained by wait(), notify(), and notifyAll() methods.
12) What are the advantages of multithreading?

Multithreading programming has the following advantages:


 Multithreading allows an application/program to be always reactive
for input, even already running with some background tasks
 Multithreading allows the faster execution of tasks, as threads
execute independently.
 Multithreading provides better utilization of cache memory as
threads share the common memory resources.
 Multithreading reduces the number of the required server as one
server can execute multiple threads at a time.

13) Can we call the run() method instead of start()?

 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.

14) What is Synchronization?

 Synchronization in Java is the capability to control the access of


multiple threads to any shared resource.
 Java Synchronization is better option where we want to allow only
one thread to access the shared resource.

14) What is Thread Scheduler in java?

 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?

 A Race condition is a problem which occurs in the multithreaded


programming when various threads execute simultaneously accessing a
shared resource at the same time.
 The proper use of synchronization can avoid the 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?

Deadlock in java is a programming situation where two or more threads are


blocked forever. Java deadlock situation arises with at least two threads and two or
more resources.

18) What is meant by java files?


 A Java file is a text-based file that contains source code written in the Java
programming language.
 It typically has a ".java" file extension.
 This file contains instructions that a Java compiler can understand and
translate into bytecode, which can then be executed by the Java
Virtual Machine (JVM).
 Java files contain classes, methods, variables, and other
programming constructs that define the behavior and structure of a
Java program.
19) What is Stream Filtering and Piping?
1. Stream Filtering: Stream filtering is about selecting specific items from a
collection or sequence that meet a certain condition. It's like sifting through
a list and keeping only the items that match a particular rule.
2. Piping (Pipelining): Piping involves passing the output of one operation as
the input to the next. It's like creating a chain of actions where each action
processes the data further. This is often used to streamline complex tasks by
breaking them into smaller, interconnected steps.

20) What is Swing?

 Swing is a Java library used to create graphical user interfaces (GUIs)


for desktop applications.
 It provides a set of components like buttons, text fields, and windows
that developers can use to build interactive and visually appealing
software interfaces.

21) What is AWT?

 AWT stands for "Abstract Window Toolkit."


 It is a graphical user interface (GUI) toolkit that was introduced in the
early versions of the Java programming language.
 AWT provides a set of classes and components that allow developers to
create graphical interfaces for their Java applications.
 It includes basic GUI elements such as buttons, text fields, checkboxes,
and windows.

22) What is JDBC?

 JDBC stands for Java Database Connectivity.


 It's a Java technology that lets Java programs interact with
relational databases.
 It provides classes to connect, query, and update

databases. 23)What is JDBC driver?

 A JDBC driver is a software component that enables Java applications to


connect and interact with specific types of databases using the Java
Database Connectivity (JDBC) API.
 It acts as an interface between the Java application and the
database, translating Java calls into database-specific calls and vice
versa.

24) What are the steps to connect to the database in java?

To connect to a database in Java:


1. Import: Begin by importing the necessary JDBC packages, such as java.sql .
2. Load Driver: Use Class.forName() to load the JDBC driver specific to the
database being used.
3. Connection URL: Create a connection URL with details like the
database location, port, and name.
4. Establish Connection: Use DriverManager.getConnection() to establish a
connection, providing the URL, username, and password.

25) Differentiate between Statement and PreparedStatement in JDBC.


 Statement:
 Executes SQL queries directly.
 Vulnerable to SQL injection attacks.
 Re-compiles query each time, affecting performance.
 Suitable for simple, one-time queries.
 Used for executing dynamic or changing queries.
 PreparedStatement:
 Pre-compiles SQL queries for reusability.
 Mitigates SQL injection risks.
 Improves performance as query is compiled only once.
 Suitable for frequently executed or repetitive queries.
 Used when query structure remains constant, but parameters change.
26) What is Socket?

 Socket programming in Java allows you to establish


communication between different devices over a network using
sockets.
 A socket is a combination of an IP address and a port number, which
together enable two-way communication between a client and a
server.

27) What is secure sockets?


 Secure Socket Layer (SSL) provides security to the data that is transferred
between web browser and server.
 SSL encrypts the link between a web server and a browser which ensures
that all data passed between them remain private and free from attack.
28) Write the protocols of secure sockets.
Secure Socket Layer Protocols:
 SSL record protocol
 Handshake protocol
 Change-cipher spec protocol
 Alert protocol
29)What is record protocol?
 The SSL (Secure Sockets Layer) record protocol is a key component of the
SSL/TLS (Transport Layer Security) protocol suite, which is used to
secure data transmission over the internet. ]
 The SSL record protocol is responsible for fragmenting, compressing
(optional), encrypting, and authenticating data to ensure the
confidentiality, integrity, and authenticity of information exchanged
between a client and a server.

30) What is TCP?


 TCP, or Transmission Control Protocol, is one of the core protocols of
the Internet Protocol (IP) suite.
 It operates at the transport layer of the OSI model and plays a crucial role
in providing reliable and connection-oriented communication between
devices on a network.
31) What is encryption?

 Encryption is the process of converting information or data into a code or


cipher to make it unreadable to anyone who doesn't have the decryption
key or the necessary knowledge to decipher it.
 It is used to protect sensitive information, such as personal messages,
financial data, or confidential documents, from unauthorized access or
interception.
32) What is decryption?

 Decryption is the process of converting encrypted data or information


back into its original, readable form.
 It involves using a decryption key or algorithm to reverse the
encryption process and make the data accessible to authorized users.
 Decryption is essential for retrieving and understanding encrypted
information, and it ensures that only those with the appropriate keys
or credentials can access the protected data.
33) What is client server computing?
 Client-server computing is a network architecture and model that defines
how applications and services are structured and distributed in a
networked environment. In a client-server model, there are two primary
components: the client and the server.
 These components work together to facilitate communication and
data processing in a networked system.
34) What is RMI?

 RMI, which stands for Remote Method Invocation, is a technology used in


Java for making one program call functions or methods in another
program, even if they are on different computers.

 It allows Java programs to communicate and work together across


a network.
35) What is Java API?

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.

36) What is meant by Java Servlets?

 Java Servlets are Java-based server-side components used to extend the


capabilities of a web server. They provide a way to dynamically
generate and process web content, typically in response to user requests.

 Servlets are a key part of the Java Platform, Enterprise Edition (Java
EE), and they are commonly used to create web applications.

37) Difference between Servlet and CGI.

Servlet CGI(Common Gateway Interface)

Servlets are portable and efficient. CGI is not portable

In Servlets, sharing data is possible. In CGI, sharing data is not possible.

Servlets can directly communicate CGI cannot directly communicate with


with the webserver. the webserver.

Servlets are less expensive than CGI. CGI is more expensive than Servlets.

Servlets can handle the cookies. CGI cannot handle the cookies.

38) What is meant by scripting?


Scripting in the context of web development refers to the use of scripting
languages, which are lightweight, interpreted programming languages, to enhance
the functionality and interactivity of web pages.

39) Tell about Custom Tag Libraries.

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.

40) Tell about implicit objects.

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.

You might also like