UNIT-1
1. What is Object-Oriented Programming (OOP)?
Answer:
Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
objects, which can contain data in the form of fields (attributes) and code in the form of methods
(functions).
2. Name the four main principles of OOP.
Answer:
The four main principles of OOP are Encapsulation, Inheritance, Polymorphism, and
Abstraction.
3. What is abstraction in OOP?
Answer:
Abstraction in OOP refers to hiding the implementation details and showing only the necessary
functionality to the user.
4. What is an object in OOP?
Answer:
An object is an instance of a class in OOP. It represents a real-world entity and contains both
data (attributes) and methods (functions).
5. What is a class in Java?
Answer:
A class in Java is a blueprint or template from which objects are created. It defines the properties
(fields) and behaviors (methods) that the objects of the class will have.
6. What is the purpose of the this keyword in Java?
Answer:
The this keyword refers to the current instance of a class. It is used to differentiate between
instance variables and local variables when they have the same name.
7. What is the scope of a local variable in Java?
Answer:
The scope of a local variable in Java is limited to the method or block in which it is declared. It is
not accessible outside of that method or block.
8. What is garbage collection in Java?
Answer:
Garbage collection in Java is the automatic process of reclaiming memory by deleting objects
that are no longer in use.
9. What is method overloading in Java?
Answer:
Method overloading in Java occurs when two or more methods in the same class have the same
name but different parameters (either in number or type).
10. What is inheritance in Java?
Answer:
Inheritance in Java is the mechanism by which one class can inherit the properties and behaviors
(fields and methods) of another class.
11. What is method overriding in Java?
Answer:
Method overriding in Java occurs when a subclass provides its own implementation of a method
that is already defined in its superclass.
12. What is the significance of final keyword in Java?
Answer:
The final keyword in Java can be used to declare constants, prevent method overriding, and
prevent inheritance of classes.
13. What is recursion in Java?
Answer:
Recursion in Java is a process in which a method calls itself in order to solve a problem. The
method must have a base case to stop the recursion.
14. What is an array in Java?
Answer:
An array in Java is a data structure that stores a fixed-size sequence of elements of the same type.
15. What are control statements in Java?
Answer:
Control statements in Java are used to control the flow of execution in a program. Examples
include if, else, switch, for, for-each.while, and do-while.
16. What is type casting in Java?
Answer:
Type casting in Java refers to converting a variable from one data type to another, either
automatically (implicit) or manually (explicit).
17. What is a constructor in Java?
Answer:
A constructor in Java is a special method used to initialize objects. It has the same name as the
class and does not have a return type.
18. What is an inner class in Java?
Answer:
An inner class in Java is a class defined within another class. It can access the members (fields
and methods) of the outer class.
19. What is a string in Java?
Answer:
A string in Java is an object of the String class, used to represent a sequence of characters.
20. What is the purpose of the switch statement in Java?
Answer:
The switch statement in Java is used to execute one out of multiple possible blocks of code based
on the value of an expression.
UNIT-2
Inheritance and Polymorphism
1. What is inheritance in Java?
o Answer:
Inheritance in Java is the mechanism by which one class (subclass) inherits the
properties and behaviors (fields and methods) from another class (superclass).
2. What is a base class in inheritance?
o Answer:
A base class (or superclass) is the class from which other classes (subclasses)
inherit attributes and methods.
3. What is a subclass in Java?
o Answer:
A subclass in Java is a class that inherits properties and methods from another
class (its superclass) and can also define its own additional properties and
methods.
4. What is meant by substitutability in inheritance?
o Answer:
Substitutability means that objects of a subclass can be used in place of objects of
the superclass, and the program will still function correctly due to inheritance and
polymorphism.
5. What are the different forms of inheritance?
o Answer:
The different forms of inheritance in Java include specialization, specification,
construction, extension, limitation, and combination.
6. What are the benefits of inheritance?
o Answer:
Benefits of inheritance include code reuse, extension of existing functionality, and
the ability to create a more structured and organized system.
7. What are the costs of inheritance?
o Answer:
The costs of inheritance include increased complexity, tight coupling between the
parent and child classes, and potential difficulties in maintenance and
modification.
8. What does the super keyword do in inheritance?
o Answer:
The super keyword is used to refer to the immediate parent class, allowing access
to parent class constructors, methods, and fields.
9. What is method overriding in Java?
o Answer:
Method overriding occurs when a subclass provides its own implementation of a
method that is already defined in its superclass, with the same method signature.
10. What is an abstract class in Java?
o Answer:
An abstract class in Java is a class that cannot be instantiated and may contain
abstract methods that must be implemented by subclasses.
11. What is the Object class in Java?
o Answer:
The Object class is the root class of all classes in Java. Every class inherits from
Object either directly or indirectly.
12. What is the purpose of the final keyword in inheritance?
o Answer:
The final keyword prevents method overriding and class inheritance. It can be
used to declare a constant or to prevent a class from being subclassed.
Packages in Java
13. What is a package in Java?
o Answer:
A package in Java is a namespace used to organize classes and interfaces into
logical groups for better code management and accessibility.
14. How do you define a package in Java?
o Answer:
A package is defined using the package keyword at the top of a Java file, followed
by the package name (e.g., package com.example;).
15. What is the CLASSPATH in Java?
o Answer:
The CLASSPATH in Java is an environment variable that tells the Java runtime
where to find class files and packages when running a Java application.
16. How can you import a package in Java?
o Answer:
A package can be imported using the import keyword followed by the package
name (e.g., import java.util.*;).
17. What is the difference between a class and an interface in Java?
o Answer:
A class defines the implementation of methods and variables, whereas an
interface defines only method signatures, without implementations. A class can
implement an interface but must provide implementations for the methods defined
in the interface.
18. What is the purpose of the implements keyword in Java?
o Answer:
The implements keyword is used by a class to implement an interface, thereby
providing concrete implementations for all the methods defined in that interface.
19. Can an interface in Java contain variables?
o Answer:
Yes, an interface in Java can contain variables, but they are implicitly public,
static, and final.
20. Can an interface in Java extend another interface?
o Answer:
Yes, an interface in Java can extend another interface. A class that implements the
extended interface must implement all the methods of both the parent and child
interfaces.
Exploring java.io Package
21. What is the java.io package in Java?
o Answer:
The java.io package provides classes for system input and output through data
streams, serialization, and file handling.
22. What is a stream in Java?
o Answer:
A stream in Java is a sequence of data elements that can be read from or written to
an input/output device. Streams are categorized as byte streams and character
streams.
23. What is the difference between FileInputStream and FileReader in Java?
o Answer:
FileInputStream reads data in bytes, whereas FileReader reads data in characters,
making FileReader more suitable for handling text files.
24. What does the BufferedReader class do?
o Answer:
The BufferedReader class in Java is used to read text from an input stream,
buffering characters for efficient reading of characters, arrays, and lines.
25. What is the purpose of the Serializable interface in Java?
o Answer:
The Serializable interface in Java is used to mark a class whose objects can be
serialized (converted into a byte stream) for storage or transmission.
26. What is the File class used for in Java?
o Answer:
The File class in Java is used to represent file and directory pathnames in an
abstract manner. It provides methods for creating, deleting, and querying file
properties.
UNIT-3
Exception Handling
1. What is exception handling in Java?
o Answer:
Exception handling in Java is a mechanism to handle runtime errors, ensuring
normal program flow by using try, catch, throw, throws, and finally blocks.
2. What are the benefits of exception handling?
o Answer:
The benefits include cleaner code, easier debugging, and the ability to handle
errors gracefully without crashing the program.
3. What is the difference between termination and resumptive models in exception
handling?
o Answer:
In the termination model, the program stops execution after an exception occurs,
while in the resumptive model, execution resumes after handling the exception.
4. What is the exception hierarchy in Java?
o Answer:
The exception hierarchy in Java is a class structure where Throwable is the root
class, with two main subclasses: Error and Exception.
5. What is the purpose of the try block in Java?
o Answer:
The try block is used to enclose code that might throw an exception, allowing it to
be caught and handled.
6. What is the purpose of the catch block in Java?
o Answer:
The catch block is used to handle exceptions that occur in the corresponding try
block, enabling the program to recover from errors.
7. What is the difference between throw and throws in Java?
o Answer:
throw is used to explicitly throw an exception, while throws is used in method
signatures to declare that a method can throw exceptions.
8. What is the purpose of the finally block in Java?
o Answer:
The finally block is used to execute important code (like closing resources) after
the try and catch blocks, regardless of whether an exception was thrown or not.
9. What are built-in exceptions in Java?
o Answer:
Built-in exceptions are predefined exceptions in Java, such as
NullPointerException, ArrayIndexOutOfBoundsException, and IOException.
10. How can you create your own exception subclasses in Java?
o Answer:
You can create custom exception subclasses by extending the Exception or
RuntimeException class and providing constructors for your exception.
String Handling
11. What is String handling in Java?
o Answer:
String handling in Java involves manipulating strings using the String class,
which provides methods to create, modify, and manipulate strings.
12. What is the difference between String and StringBuilder in Java?
o Answer:
String objects are immutable (cannot be changed), while StringBuilder objects are
mutable, allowing efficient modifications.
13. How do you concatenate strings in Java?
o Answer:
Strings can be concatenated using the + operator or the concat() method of the
String class.
14. What does the equals() method do in the String class?
o Answer:
The equals() method compares two strings for content equality, returning true if
they have the same characters, and false otherwise.
15. What is the StringBuffer class used for in Java?
o Answer:
The StringBuffer class is used to create and manipulate mutable strings. It is
similar to StringBuilder but is thread-safe.
Multithreading
16. What is the difference between multithreading and multitasking?
o Answer:
Multithreading refers to running multiple threads within a single process, whereas
multitasking involves running multiple processes concurrently.
17. What is the thread lifecycle in Java?
o Answer:
The thread lifecycle includes states such as New, Runnable, Blocked, Waiting,
Timed Waiting, and Terminated.
18. How can you create a thread in Java?
o Answer:
A thread can be created by either extending the Thread class or implementing the
Runnable interface.
19. What are thread priorities in Java?
o Answer:
Thread priorities determine the order in which threads are scheduled for
execution. They can be set using Thread.setPriority() and range from
Thread.MIN_PRIORITY to Thread.MAX_PRIORITY.
20. What is synchronization in Java?
o Answer:
Synchronization in Java is the process of controlling access to a shared resource
by multiple threads to avoid data inconsistency.
21. What is inter-thread communication in Java?
o Answer:
Inter-thread communication allows threads to communicate and synchronize with
each other using methods like wait(), notify(), and notifyAll().
22. What are thread groups in Java?
o Answer:
A thread group is a way to manage a collection of threads in Java. It allows you to
perform operations on all threads in the group simultaneously, such as setting
priorities or interrupting threads.
23. What are daemon threads in Java?
o Answer:
Daemon threads are low-priority threads that run in the background and are
terminated automatically when all user threads have finished execution.
Enumerations, Autoboxing, Annotations, Generics
24. What is an enumeration (enum) in Java?
o Answer:
An enumeration is a special Java type used to define collections of constants. It
provides type safety and can have fields, methods, and constructors.
25. What is autoboxing in Java?
o Answer:
Autoboxing is the automatic conversion of primitive types to their corresponding
wrapper classes (e.g., int to Integer).
26. What are annotations in Java?
o Answer:
Annotations are metadata that provide additional information about code. They
are used to provide instructions to the compiler or runtime environment.
27. What are generics in Java?
o Answer:
Generics in Java enable you to write classes, interfaces, and methods that can
operate on objects of various types, while providing compile-time type safety.
UNIT-4
Event Handling
1. What is an event in Java?
o Answer:
An event is an action or occurrence, such as a button click or a key press, that the
program needs to respond to. Events are generated by user interactions or system
activities.
2. What is an event source in Java?
o Answer:
An event source is an object that generates events, such as a button or a text field,
which triggers an event when interacted with by the user.
3. What is an event class in Java?
o Answer:
An event class in Java represents an event object that contains information about
the event, such as the type of event and the source of the event (e.g., ActionEvent,
MouseEvent).
4. What is an event listener in Java?
o Answer:
An event listener is an interface that listens for specific events and defines
methods to handle those events (e.g., ActionListener, MouseListener).
5. What is the Delegation Event Model in Java?
o Answer:
The Delegation Event Model is a design pattern where event handling is delegated
to event listeners rather than being handled directly by the event source.
6. What is the role of the MouseListener interface in Java?
o Answer:
The MouseListener interface handles mouse events, such as mouse clicks, mouse
enter, and mouse exit, and defines methods like mouseClicked(), mouseEntered(),
etc.
7. What is the role of the KeyListener interface in Java?
o Answer:
The KeyListener interface handles keyboard events, such as key presses and
releases, and defines methods like keyPressed(), keyReleased(), etc.
8. What is an adapter class in Java?
o Answer:
An adapter class is a class that provides default implementations of the methods
in event listener interfaces. It is used to simplify event handling when only a
subset of methods needs to be overridden.
9. How does the ActionListener interface handle events?
o Answer:
The ActionListener interface handles action events, such as button clicks, and
provides the actionPerformed() method to process these events.
AWT (Abstract Window Toolkit)
10. What is the AWT class hierarchy in Java?
o Answer:
The AWT class hierarchy consists of various components such as Component (the
root class), Container (a subclass of Component), and Window (a subclass of
Container). GUI components are derived from these classes.
11. What is a label in AWT?
o Answer:
A label is a non-editable text element used to display static text in a GUI,
typically used for identifying or describing other components.
12. What is a button in AWT?
o Answer:
A button is a GUI component that can be clicked to trigger an action. It can be
created using the Button class and is typically used to perform a specific task.
13. What is a canvas in AWT?
o Answer:
A canvas is a blank rectangular area where graphics can be drawn or images
displayed, allowing users to create custom drawing and painting within a GUI.
14. What is the purpose of a scrollbar in AWT?
o Answer:
A scrollbar allows users to navigate through content that doesn't fit within the
available space. It can be horizontal or vertical and is used with containers like
Panel or TextArea.
15. What is a text component in AWT?
o Answer:
A text component is used to display or accept text input from the user. Examples
include TextField, TextArea, and PasswordField.
16. What is a checkbox in AWT?
o Answer:
A checkbox is a GUI component that allows the user to select one or more
options. It can be checked or unchecked, and is commonly used for selecting
multiple choices.
17. What is a checkbox group in AWT?
o Answer:
A checkbox group is a container that allows only one checkbox to be selected at a
time. It is used to group related checkboxes for exclusive selection.
18. What is a choice list in AWT?
o Answer:
A choice list is a dropdown list of options from which the user can select a single
item. It is represented by the Choice class.
19. What is a panel in AWT?
o Answer:
A panel is a container used to group components together for organizing the
layout of a GUI. It can be used to organize components within a Frame.
20. What is a scroll pane in AWT?
o Answer:
A scroll pane is a container that provides scrollbars when the content inside it
overflows. It is typically used to display large amounts of text or other content.
21. What is a dialog in AWT?
o Answer:
A dialog is a pop-up window used to communicate with the user, typically for
showing messages or prompting for user input. Dialogs include MessageDialog,
FileDialog, etc.
22. What is a menubar in AWT?
o Answer:
A menubar is a container for menu items (such as File, Edit, etc.) that allow users
to interact with the application through drop-down menus.
23. What is graphics in AWT?
o Answer:
The Graphics class in AWT provides methods for drawing shapes, text, and
images on a component or canvas.
Layout Managers
24. What is a layout manager in AWT?
o Answer:
A layout manager in AWT controls the arrangement of components within a
container. It automatically adjusts component positions and sizes to fit the
container.
25. What is the BorderLayout in AWT?
o Answer:
BorderLayout divides the container into five regions: North, South, East, West,
and Center. Components are placed in these regions based on the layout manager.
26. What is the GridLayout in AWT?
o Answer:
GridLayout arranges components in a grid of rows and columns. All cells in the
grid are of equal size.
27. What is the FlowLayout in AWT?
o Answer:
FlowLayout arranges components sequentially, left to right, top to bottom,
wrapping to the next line when the container's width is exceeded.
28. What is the CardLayout in AWT?
o Answer:
CardLayout arranges components in a stack, where only one component is visible
at a time. It is often used for implementing tabbed panes.
29. What is the GridBagLayout in AWT?
o Answer:
GridBagLayout is a flexible and complex layout manager that allows components
to be placed in a grid of rows and columns with varying cell sizes, and allows
fine-grained control over component positioning.
UNIT-5
Applets
1. What is an applet in Java?
o Answer:
An applet is a small Java program that runs within a web browser or applet
viewer, typically used to provide interactive features in a web page.
2. What is the difference between an applet and an application in Java?
o Answer:
An applet is a Java program that runs inside a web browser or applet viewer and
requires a browser plugin, while an application is a standalone program that runs
directly on a system without a browser.
3. What is the life cycle of an applet?
o Answer:
The life cycle of an applet includes the following methods: init() (initialization),
start() (starts execution), paint() (displays output), stop() (halts execution), and
destroy() (cleans up before the applet is destroyed).
4. What are the types of applets in Java?
o Answer:
The two types of applets are:
Local Applet is written on our own, and then we will embed it into web
pages.
A Remote Applet: is designed and developed by another developer.
5. How do you pass parameters to an applet?
o Answer:
Parameters are passed to an applet using the <param> tag in the HTML file or
applet code that embeds the applet. The getParameter() method is used to retrieve
these parameters.
Swing
6. What is Swing in Java?
o Answer:
Swing is a set of GUI components in Java that is part of the Java Foundation
Classes (JFC). It provides more sophisticated and flexible user interface elements
compared to AWT.
7. What are the limitations of AWT in Java?
o Answer:
AWT is limited by its reliance on native OS components, which results in
inconsistent appearance across platforms, lack of flexibility, and fewer component
options compared to Swing.
8. What is the MVC architecture in Swing?
o Answer:
The Model-View-Controller (MVC) architecture separates the data (Model), user
interface (View), and logic (Controller). In Swing, the model represents the data,
the view displays the UI, and the controller handles user interactions.
9. What are containers in Swing?
o Answer:
Containers are components that hold other components. Examples include
JFrame, JPanel, and JDialog, which manage the layout and organization of user
interface elements.
10. What is JApplet in Swing?
o Answer:
JApplet is a Swing version of the traditional applet, providing the same
functionality with more advanced Swing-based components and better
appearance.
11. What is JFrame in Swing?
o Answer:
JFrame is a top-level container in Swing that represents a window with a title bar,
border, and standard window controls (like minimize and close).
12. What is JComponent in Swing?
o Answer:
JComponent is the base class for all Swing components, providing common
functionality for components like buttons, labels, text fields, etc.
13. What are icons and labels in Swing?
o Answer:
Icon: A graphical image that can be displayed on Swing components like
buttons.
Label: A non-interactive component used to display text or images in a
GUI.
14. What is the JButton class in Swing?
o Answer:
JButton is a Swing component that represents a clickable button used to trigger
actions when clicked by the user.
15. What are checkboxes in Swing?
o Answer:
Checkboxes in Swing allow users to select or deselect an option. The JCheckBox
class is used to create checkboxes.
16. What are radio buttons in Swing?
o Answer:
Radio buttons are used to select one option from a group of choices. They are
created using the JRadioButton class, and usually belong to a ButtonGroup to
ensure only one can be selected at a time.
17. What is a combo box in Swing?
o Answer:
A combo box in Swing is a drop-down list that allows users to select an option
from a list. It is represented by the JComboBox class.
18. What is a tabbed pane in Swing?
o Answer:
A tabbed pane allows multiple panels to be displayed in a single window, where
each panel is accessible through a tab. It is represented by the JTabbedPane class.
19. What is a scroll pane in Swing?
o Answer:
A scroll pane is a container that provides a scrollbar when the content inside it
overflows. The JScrollPane class is used to make components like text areas or
tables scrollable.
20. What are trees in Swing?
o Answer:
A tree in Swing represents hierarchical data in the form of a tree structure. The
JTree class is used to create and manage trees in Swing applications.
21. What are tables in Swing?
o Answer:
Tables in Swing are used to display tabular data. The JTable class is used to create
tables, and it supports features like sorting, editing, and multi-selection.