1.
Basics and Syntax
Question 1: What is Java?
Answer: Java is a high-level, class-based, object-oriented programming
language that is designed to have as few implementation dependencies as
possible. It is a general-purpose programming language intended to let
application developers write once, run anywhere (WORA), meaning that
compiled Java code can run on all platforms that support Java without the
need for recompilation.
Question 2: Explain the JDK, JRE, and JVM.
Answer:
JVM (Java Virtual Machine): The JVM is an engine that provides a
runtime environment to drive the Java Code or applications. It converts
Java bytecode into machine language.
JRE (Java Runtime Environment): JRE is a part of software that is
designed to run other software. It contains the set of libraries + other
files that JVM uses at runtime.
JDK (Java Development Kit): The JDK is a software development
environment used for developing Java applications and applets. It
physically exists. It contains JRE + development tools.
Question 3: What are variables in Java?
Answer: Variables are containers for storing data values. In Java, each
variable must be declared with a data type that designates the type and
quantity of value it can hold. Java is a statically typed language, meaning
variables must be defined before they are used.
Question 4: What is typecasting in Java?
Answer: Typecasting is the process of converting a variable from one type
to another. In Java, there are two types of casting:
Widening Casting (Implicit): automatic type conversion from a smaller
to a larger type
Narrowing Casting (Explicit): needs explicit conversion to convert a
larger type to a smaller type
Question 5: How do you declare an array in Java?
Answer: An array is a container object that holds a fixed number of values
of a single type. The declaration of an array in Java is as follows:
1
2
int[] myIntArray = new int[10]; // declares an array of integers
String[] myStringArray = new String[50]; // declares an array of strings
Question 6: Explain the main method in Java.
Answer: The main method is the entry point for any Java program. It must
be public, static, return no value (void), and accept a String array as a
parameter. It signature is:
1
2
3
public static void main(String[] args) {
// code to be executed
}
Question 8: What is a constructor in Java?
Answer: A constructor in Java is a block of code similar to a method that's
called when an instance of an object is created. Unlike methods,
constructors have no explicit return type and have the same name as the
class itself.
Question 9: Explain method overloading in Java.
Answer: Method overloading is a feature that allows a class to have more
than one method having the same name, if their parameter lists are
different. It is related to compile-time (or static) polymorphism.
Question 10: What is a package in Java?
Answer: A package in Java is a namespace that organizes a set of related
classes and interfaces. Conceptually you can think of packages as being
similar to different folders on your computer.
2. Object-Oriented Programming (OOP)
Question 11: What is Object-Oriented Programming?
Answer: Object-oriented programming (OOP) is a programming paradigm
based on the concept of "objects", which can contain data in the form of
fields (often known as attributes or properties) and code in the form of
procedures (often known as methods).
Question 12: What are the main principles of OOP?
Answer:
Encapsulation: The binding (or wrapping) of data and methods that
operate on the data into a single unit called a ‘class’. It also means
hiding data (i.e., private variables) from direct access.
Abstraction: Hiding the complex implementation details of an operation
while exposing a simple interface.
Inheritance: Allows a new class to inherit properties and methods of an
existing class.
Polymorphism: The ability of different classes to provide a unique
interface by exposing a method that can behave differently.
Question 13: What is inheritance?
Answer: Inheritance in Java is a mechanism where one object acquires all
the properties and behaviors of a parent object. It is an important part of
OOPs (Object-Oriented programming systems).
Question 14: What is an interface?
Answer: An interface in Java is a reference type, similar to a class, that
can contain only constants, method signatures, default methods, static
methods, and nested types. Interfaces cannot contain instance fields. The
methods in interfaces are abstract by default.
Question 15: Explain the difference between abstract classes
and interfaces.
Answer:
Abstract Class: Can have both abstract and non-abstract methods.
Abstract classes are used to provide a base for subclasses to extend
and implement the abstract methods.
Interface: Typically contains abstract methods only. Starting from Java
8, it can also contain default and static methods. Interfaces are used to
implement abstraction.
Question 16: What is polymorphism?
Answer: Polymorphism in Java is the ability of an object to take on many
forms. Most commonly, it is when a parent class reference is used to refer
to a child class object.
Question 17: Explain method overriding.
Answer: Method overriding, in object-oriented programming, is a language
feature that allows a subclass or child class to provide a specific
implementation of a method that is already provided by one of its
superclasses or parent classes. The method that is overridden is called an
overridden method.
Question 18: What is the "super" keyword?
Answer: The super keyword in Java is a reference variable that is used to
refer to parent class objects. The keyword can be used to call superclass
methods and to access the superclass constructor.
Question 19: What are getters and setters in Java?
Answer: Getters and setters are methods that get and set the value of a
private variable. For example:
3. Exception Handling and Assertions
Question 21: What is exception handling?
Answer: Exception handling is a mechanism to handle runtime errors such
as ClassNotFoundException, IOException, SQLException, RemoteException, etc.
The core advantage of exception handling is to maintain the normal flow of
the application.
Question 22: What is a try-catch block?
Answer: try is the start of the block, and catch is used to handle the
exception. It must be followed by either finally or another catch block.
Question 23: What is the finally block?
Answer: The finally block in Java is used to place important code such as
clean-up code, e.g., closing the file, database connections, etc. The finally
block executes whether an exception is handled or not.
Question 24: What is an exception?
Answer: An exception is a problem that arises during the execution of a
program. Exceptions are caught and handled in Java with try-catch blocks.
Question 27: What is the throws keyword?
Answer: The throws keyword is used to declare an exception. It gives
information to the programmer that there may be an exception, so it is
better for the programmer to provide the exception handling code so that
normal flow can be maintained.
Question 28: What is the difference between throw and
throws?
Answer: throw keyword is used to explicitly throw an exception. throws is
used to declare an exception. Checked exceptions cannot be propagated
with throws.
Question 29: What is throw used for?
Answer: The throw keyword is used within a method. It is used to throw an
exception explicitly.
Question 34: What are wrapper classes?
Answer: Wrapper classes convert the Java primitives into the reference
types (objects). Every primitive data type has a class dedicated to it. These
are known as wrapper classes because they "wrap" the primitive data type
into an object of that class. Examples include Integer, Character, Double etc.
Question 35: Explain autoboxing and unboxing.
Answer: Autoboxing is the automatic conversion that the Java compiler
makes between the primitive types and their corresponding object wrapper
classes. For example, converting an int to an Integer, a double to a Double, etc.
Unboxing is the reverse process, where the object is converted back to a
primitive type.
7. JDBC
Question 61: What is JDBC?
Answer: JDBC (Java Database Connectivity) is an API that enables Java
programs to execute SQL statements. This allows Java applications to
interact with any SQL-compliant database.
Question 62: What are the core components of JDBC?
Answer: The core components of JDBC include DriverManager, Driver,
Connection, Statement, ResultSet, and SQLException.
Question 63: How do you connect to a database in Java?
Answer:
Connection conn =
DriverManager.getConnection("jdbc:subprotocol:subname", "user",
"password");
Question 64: What are "Statement" and "PreparedStatement"?
Answer:
Statement: Used to execute a simple SQL query with no parameters.
PreparedStatement: Used for executing SQL statements multiple times
or when you need to bind parameters to the query.
What is the Proxy
Server?
A proxy server speaks the client side of a protocol
to another server.
This is often required when clients have certain
restrictions on which servers they can connect to.
And when several users are hitting a popular
website, a proxy server can get the contents of the
web server's popular pages once, saving
expensive internetwork transfers while providing
faster access to those pages to the clients.
What are differences
between Swing and
AWT?
There is couple of differences between swing
and AWT.
AWT component are considered to be
heavyweight while Swing component are
lightweights.
Swing has plug gable look and feel.
AWT is platform dependent same GUI will
look different platform while Swing is
developed in Java and is platform dependent.
Why Swing
components are
called lightweight
component?
AWT component are associated with native
screen resource and called heavyweight
component.
While Swing components is uses the screen
resource of an ancestor instead of having their
own and that's why called lightweight or lighter
component.
What is a layout
manager and
what are
different types of
layout managers
available in Java
Swing?
A layout manager is an object that is used to
organize components in a container.
FlowLayout: The elements of a Flow Layout are
organized in a top to bottom, left to right fashion.
Border Layout: The elements of a BorderLayout are
organized at the borders(North, South East and
West) and the center of a container.
Card Layout: The elements of a CardLayout are
stacked, on top of the other, like a deck of cards.
Grid Layout: The elements of a GridLayout are of
equal size and laid out using the square of a grid.
Grid Bag Layout: The elements of a GridBagLayout
are organized according to a grid. However, the
elements may be different sizes and may occupy
more than one row or column of the grid. In
addition, the rows and columns may have different
sizes.
What is the
difference
between a
Window and a
Frame?
The Frame extends Window to define a main
application window that can have a menu bar.
What is a Java
Virtual Machine?
A Java Virtual Machine is a runtime environment
required for execution of a Java application.
Each Java application runs inside a runtime instance
of some concrete implementation of abstract
specifications of JVM.
It is JVM which is crux of platform independent
nature of the language.
What is an
applet?
Applet is a dynamic and interactive program that runs
inside a web page displayed by a java capable
browser.
What is the
lifecycle of
an applet?
init() method – can be called when an applet is first loaded.
start() method –can be called each time an applet is
started.
paint() method – can be called when the applets is
minimized or maximized.
stop() method – can be used when the browser moves off
the applet’s page.
destroy() method – can be called when the browser is
finished with the applet.