Java Previous Year
Java Previous Year
In Java, a constructor is a special method used to initialize objects of a class. It has the
same name as the class and no return type. Constructors are invoked automatically when
an object of the class is created.
Java allows constructor overloading, which means you can define multiple constructors
in a class with different parameters. This allows you to initialize objects in different ways.
program example:
class Employee {
String name;
int age;
2. What is the use of late binding? Write a program to check whether entered string is
having more then two digits in it or not.
Late Binding (Dynamic Binding) refers to the decision of which method to call being
made at runtime rather than compile-time. In object-oriented programming, this typically
happens when methods are overridden in subclasses.
• In early binding (also called static binding), the method to be executed is determined at
compile-time.
• In late binding, it is determined at runtime. This feature is essential for polymorphism,
where the method that is called depends on the actual object (i.e., runtime type) rather
than the reference type.
scanner.close();
}
}
3. What is the use of Wrapper classes Write a program where we can say down casting
may fail
Wrapper classes in Java provide a way to use primitive data types (like int, char, double)
as objects. Each primitive type has a corresponding wrapper class in Java,
void fetch() {
System.out.println("Dog fetches the ball");
}
}
4. What is the use of interface over abstract class? How we can use anonymous inner
class?
5. What are command line arguments ?Write a program to show the use of static
import.
Command-line arguments are parameters passed to the main method when running a Java
program. These arguments are stored in a String array (args) in the main() method and
can be used to customize the behavior of the program based on input provided at runtime.
6. What is the use of final keyword? Write a program to show the use of protected
keyword
7. Define and explain Thread life cycle Write a program to show the use of
synchronized keyword.
class SharedResource {
private int count = 0;
WorkerThread(SharedResource resource) {
this.resource = resource;
}
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
8. What is difference between character and byte streams? Write a program to write
state of an object into a file
import java.io.*;
} catch (IOException e) {
e.printStackTrace();
}
9. What is the use of Object class in java? Write a program to remove all duplicate
characters from a string
1. equals(): Compares two objects for equality. By default, it checks whether two references
point to the same memory location. Can be overridden to compare object properties.
2. hashCode(): Returns a hash code value for the object. It is used in hash-based collections
like HashMap, HashSet, etc.
3. toString(): Returns a string representation of the object. By default, it returns the class
name followed by the object's hash code. Often overridden to provide meaningful output.
4. clone(): Creates and returns a copy of the object.
5. finalize(): Called by the garbage collector before an object is destroyed.
6. getClass(): Returns the runtime class of the object.
import java.util.LinkedHashSet;
return result.toString();
}
10. What is the use of InetAddress class? Write a code to send you name from one
machine to another machine using socket class in order to receive your name in
uppercase.
import java.io.*;
import java.net.*;
} catch (IOException e) {
e.printStackTrace();
}
}
}
11. What is the role of URL class in java? Write a program in support to show where we
can say StringBuffer is faster then String class
12. What is the use of Collection Framework? Write a program to show the working of
Set interface.
import java.util.HashSet;
import java.util.Set;
13. What are JDBC drivers? Write a program to insert your name into database table if
name does not exists
JDBC (Java Database Connectivity) drivers are software components that enable Java
applications to interact with databases. They provide the necessary functionality to
connect to a database, execute SQL queries, and retrieve results. There are four types of
JDBC drivers:
1. Type 1: JDBC-ODBC Bridge Driver:
o Uses ODBC drivers to connect to databases.
o Not recommended for production use due to performance issues.
2. Type 2: Native-API Driver:
o Converts JDBC calls into database-specific calls using the native API of the
database.
o Requires native libraries and is not portable across different databases.
3. Type 3: Network Protocol Driver:
o Uses a middleware server to translate JDBC calls into database-specific calls.
o More flexible and portable but requires a network server.
4. Type 4: Thin Driver:
o Pure Java driver that directly converts JDBC calls into the database-specific
protocol.
o Most widely used and recommended for production due to its performance and
portability.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
14. Define and explain Serviet life cycle. How we can process get and post requests?
Explain by the help of proper example
A servlet is a Java class that handles HTTP requests and responses in a web application.
The life cycle of a servlet is managed by the servlet container (e.g., Apache Tomcat). It
consists of the following phases:
1. Loading and Instantiation:
o The servlet class is loaded into memory by the servlet container.
o An instance of the servlet is created.
2. Initialization:
o The init() method is called once to initialize the servlet.
o This method is used to perform tasks such as loading configuration data and
initializing resources.
3. Request Handling:
o The servlet container calls the service() method to handle requests.
o This method can process both GET and POST requests (and others like PUT,
DELETE) by delegating to specific methods (doGet(), doPost(), etc.).
o Each request is handled in a separate thread.
4. Destruction:
o When the servlet needs to be removed (e.g., when the server shuts down), the
destroy() method is called.
o This method is used to clean up resources (e.g., closing database connections).
15. What is the use of garbage collector? Write all the steps to create executable jar file.
javac HelloWorld.java
Manifest-Version: 1.0
Main-Class: HelloWorld