Oopj Summary
Oopj Summary
Object-Oriented Programming (OOP) is a programming paradigm that organizes code into objects, which are instances of
classes. It emphasizes encapsulation, inheritance, and polymorphism to model real-world entities and their interactions.
The key features of OOP are encapsulation, inheritance, and polymorphism. Encapsulation involves bundling data and
methods that operate on that data into a single unit (class). Inheritance allows a class to inherit properties and behaviors
from another class. Polymorphism enables objects to be treated as instances of their parent class, promoting flexibility and
reusability.
A class is a blueprint or template that defines the structure and behavior of objects. An object is an instance of a class,
representing a tangible or conceptual entity with its own state and behavior.
JDK (Java Development Kit) is a software development kit that includes tools for developing, debugging, and monitoring
Java applications. JRE (Java Runtime Environment) is the runtime environment where Java applications are executed. It
includes the JVM (Java Virtual Machine) and core libraries.
Q5 What is JVM:
JVM (Java Virtual Machine) is a virtual machine that executes Java bytecode. It abstracts the underlying hardware and
provides a runtime environment for Java programs, enabling platform independence.
JVM architecture consists of classloader, memory area (heap, stack, method area), execution engine, native method
interface, and Java Native Interface (JNI). Classloader loads classes, the memory area manages memory, the execution
engine interprets and executes bytecode, and JNI facilitates interaction with native code.
Java source code is compiled into bytecode by the Java compiler. This bytecode is platform-independent and is then
interpreted or compiled at runtime by the JVM.
Java programs are executed by the JVM. The JVM loads the bytecode, performs verification, and then executes the
program. During execution, the JVM manages memory, handles exceptions, and ensures platform independence.
Different versions of JDK (Java Development Kit) include JDK 5, JDK 6, JDK 7, JDK 8, JDK 9, and so on. Each version introduces
new features, improvements, and updates to the Java programming language.
Primitive data types in Java include int, float, double, char, boolean, byte, short, and long. They store simple values directly.
Operators in Java perform operations on variables and values. They include arithmetic operators (+, -, *, /), relational
operators (>, <, ==, !=), logical operators (&&, ||, !), bitwise operators, assignment operators, and more.
Control structures in Java include decision-making structures (if-else, switch) and looping structures (for, while, do-while).
They control the flow of program execution based on conditions.
Q13 What are conditional statements? Explain with an example:
Conditional statements like if, else if, and else allow the execution of different code blocks based on certain conditions. For
example:
java
if (num > 0) {
System.out.println("Positive number");
System.out.println("Negative number");
} else {
System.out.println("Zero");
java
Jumping statements in Java include break, continue, and return. They alter the flow of control within loops and methods.
Reference data types store references (memory addresses) to objects. Examples include classes, interfaces, arrays, and
enumerations.
A 2D array in Java is an array of arrays, providing a grid-like structure. A multidimensional array extends this concept to
more than two dimensions.
Command-line arguments are values passed to a program when it is executed from the command line. They allow input to
be provided to the program at runtime.
Scanner class in Java is used for taking user input. Common methods include nextInt(), nextDouble(), nextLine(), etc.
System.in is an InputStream object in Java that represents the standard input stream, typically connected to the console. It
is used with classes like Scanner to read input from the keyboard.
Constructors in Java are special methods used to initialize objects. Different variants include:
Constructor overloading is having multiple constructors within a class, each with a different set of parameters. This provides
flexibility when creating objects.
- finally: A block of code that is always executed, whether an exception is thrown or not.
Polymorphism allows objects of different types to be treated as objects of a common type. Method overriding occurs when
a subclass provides a specific implementation for a method already defined in its superclass. Method overloading involves
having multiple methods with the same name but different parameters within the same class.
Abstraction is the concept of hiding implementation details and showing only essential features. An abstract class can have
abstract methods and concrete methods, while an interface contains only abstract methods. For example:
interface Drawable {
void draw();
Inheritance allows a class to inherit properties and behaviors from another class. For example:
class Animal {
void eat() {
System.out.println("Animal is eating");
void bark() {
System.out.println("Dog is barking"); } }
Checked exceptions are checked at compile-time and must be handled using try-catch or declared using throws. Unchecked
exceptions (RuntimeExceptions) are not checked at compile-time.
Q28 What is the significance of polymorphism?
Polymorphism enhances flexibility and code reusability by allowing objects to be treated as instances of their parent class.
This simplifies code and promotes a clean design.
Command-line arguments are values passed to a program when it's run from the command line. They allow the user to
input data into the program at runtime.
Jump statements in Java alter the flow of control. For example, break is used to exit a loop prematurely:
if (i == 3) {
System.out.println(i);
In Java, Strings are immutable, meaning their values cannot be changed after they are created. Any operation that appears
to modify a String actually creates a new String.
Auto-boxing is the process of converting a primitive data type to its corresponding wrapper class (e.g., int to Integer).
Unboxing is the reverse process, converting the wrapper class object back to its primitive data type.
A class variable is a variable declared within a class, outside any method, and is shared by all instances of the class.
Example:
class MyClass {
MyClass() {
count++;
- StringTokenizer: Used for tokenizing strings into substrings based on specified delimiters.
An inner class is a class defined within another class. It can access the outer class's members, and the outer class can access
the inner class's members.
An applet is a small Java program that runs within a web browser. It is typically used to create interactive web applications.
Q37 What is event handling?
Event handling in Java involves responding to user-generated events like button clicks or mouse movements. This is
achieved through event listeners and handlers.
Wrapper classes in Java provide a way to use primitive data types as objects. They include classes like Integer, Double, and
Boolean.
An exception is an event that occurs during the execution of a program and disrupts the normal flow of instructions.
Exceptions need to be handled using try-catch blocks.
- finally: The block of code that is always executed, whether an exception occurs or not. It's often used for cleanup
operations.
A generic catch block catches any exception, providing a common handling mechanism. However, it might make it
challenging to identify the specific exception type.
In Java, UI components are organized in a hierarchy. Components like buttons and text fields are part of containers like
panels. Containers can hold other containers or components, forming a hierarchy.
Multithreading is the concurrent execution of two or more threads to maximize CPU utilization. Threads are independent
paths of execution within a program.
A thread is the smallest unit of execution in a program. Multithreading allows multiple threads to run concurrently, sharing
resources and improving overall performance.
Threads in Java can be created by extending the Thread class or implementing the Runnable interface. Additionally, the
ExecutorService framework and the Callable interface can be used.
currentThread() is a method in the Thread class that returns a reference to the currently executing thread object.
- join(): Waits for a thread to terminate before moving on to the next operation.
- sleep(): Pauses the execution of the current thread for a specified duration.
A daemon thread in Java is a thread that runs in the background, providing support to non-daemon threads. When all non-
daemon threads finish, the program terminates, regardless of whether daemon threads are still running.
An inner class is a class defined within another class. It can access the outer class's members, and the outer class can access
the inner class's members.
Q50 Explain different mechanisms for event handling:
Event handling in Java can be achieved through the observer pattern, where listeners are registered to handle specific
events. Alternatively, the listener interfaces provided by Java (e.g., ActionListener) can be implemented to respond to
events.
AWT (Abstract Window Toolkit) is a set of graphical user interface (GUI) components provided by Java. It includes buttons,
text fields, and other components to create windowed applications.
An adapter class in Java is a class that provides default implementations for listener interfaces so that you can choose to
override only the methods you need. For example:
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
System.out.println("Mouse Clicked");
The life cycle of an applet in Java involves methods like init(), start(), stop(), and destroy(). These methods are called at
various stages of an applet's execution.
JFC (Java Foundation Classes) is a collection of Java APIs for building rich desktop applications. Swing, part of JFC, provides
advanced GUI components.
- JTextArea: A multi-line text area where users can input or view larger amounts of text.
- Collection: An interface that represents a group of objects, often used to group objects for manipulation.
- Collections: A utility class in Java that provides various methods for working with collections, such as sorting and
searching.
AWT is the older GUI toolkit in Java, while Swing is part of the newer Java Foundation Classes (JFC). Swing provides more
advanced and customizable GUI components compared to AWT.
An adapter class in Java provides default implementations for listener interfaces, allowing you to choose which methods to
override. It simplifies event handling.
The List interface in Java is part of the Java Collections Framework and represents an ordered collection of elements. It
allows duplicate elements and provides methods for adding, removing, and accessing elements by index.
Q60 What is JavaFX?
JavaFX is a platform for creating and delivering desktop applications. It provides a rich set of UI controls, graphics, media,
and more.
In JavaFX, a Scene represents the content inside a stage. It acts as a container for all the visual elements that make up a user
interface.
Event handling in Java can be achieved using various mechanisms, including implementing listener interfaces (e.g.,
ActionListener), using anonymous inner classes, and leveraging lambda expressions in newer Java versions.
AWT (Abstract Window Toolkit) is an older GUI toolkit in Java, while Swing is part of the Java Foundation Classes (JFC) and
provides more advanced and customizable GUI components compared to AWT.
Swing-based applications are lightweight because they use their own set of components instead of relying on the host
operating system's components. This leads to consistent behavior across different platforms.
- Map: A collection of key-value pairs, where each key is associated with exactly one value.
In Java, a Bean class is a class that follows certain conventions, such as providing a default constructor and getter and setter
methods. Beans are often used in JavaBeans, a reusable software component model.
Setter methods are used to set the values of private variables in a class, and getter methods are used to retrieve those
values. They are part of the encapsulation concept in OOP.
Serialization in Java is the process of converting an object's state into a byte stream. This stream can be saved to a file or
transmitted over a network, and later deserialized to recreate the object.
FileInputStream is used to read data from a file, and FileOutputStream is used to write data to a file in Java.
- Byte streams (InputStream and OutputStream): Deal with raw binary data.
- Char streams (Reader and Writer): Deal with character data and automatically handle character encoding.
BufferedReader in Java is used to read text from a character-based input stream with buffering. It provides more efficient
reading of characters by storing them in a buffer.
Predefined streams in Java include System.in, System.out, and System.err. They represent the standard input, standard
output, and standard error streams, respectively.
Q73 Explain features of Java:
Some features of Java include platform independence, object-oriented programming, strong type-checking, automatic
memory management (garbage collection), and a rich set of APIs.
static {
System.out.println("Static block");
System.out.println("Instance block");
A deadlock in Java occurs when two or more threads are blocked forever, each waiting for the other to release a lock. It can
lead to a complete halt in the program.
- yield(): Suggests that the current thread give up its execution to let other threads run. It's a hint to the scheduler.
- join(): Waits for the specified thread to finish its execution before the current thread resumes.
ArrayList is a dynamic array implementation in Java. It can dynamically grow or shrink in size. Example:
import java.util.ArrayList;
list.add("Java");
list.add("Python");
The Java Collections Framework provides a set of interfaces and classes for handling collections of objects. It includes
implementations like ArrayList, LinkedList, HashSet, etc.
Inner classes are often used in event handling to encapsulate the code related to a specific event listener. This promotes
better organization and encapsulation of the code.
Q81 What is an anonymous inner class?
An anonymous inner class in Java is a class without a name that is defined and instantiated at the same time. It's often used
for one-time use, such as implementing an interface or extending a class.
Thread priority in Java is an integer value associated with a thread that indicates its priority level. Higher priority threads are
scheduled to run before lower priority threads.
Thread prevention methods include using synchronization mechanisms like locks, semaphores, and the synchronized
keyword. These methods prevent multiple threads from accessing shared resources concurrently.
Data abstraction is the process of hiding the implementation details and showing only the essential features of an object. In
Java, abstract classes and interfaces are used to achieve abstraction.
Data hiding is an OOP concept where the internal details of an object are hidden and only the necessary details are
exposed. It is achieved through access modifiers like private, protected, and public.
LayoutManager in Java is an interface that provides a way to arrange and manage components in a container. It controls
how components are displayed within a container.
- Instance block: Executed when an object is created. Associated with an instance of the class.
- Static block: Executed when the class is loaded. Associated with the class itself.
The Object class is the root class for all Java classes. Every class in Java is implicitly a subclass of Object. It provides methods
like equals(), hashCode(), and toString().
Object cloning in Java is the process of creating an exact copy of an object. It can be achieved by implementing the
Cloneable interface and overriding the clone() method.
Q90 What is an inner class? Explain static inner and anonymous inner class:
- Static inner class: A nested class declared as static. It can be instantiated without creating an instance of the outer class.
- Anonymous inner class: A class without a name, defined and instantiated at the same time. Often used for one-time use.
java
java
Polymorphism in Java allows objects of different types to be treated as objects of a common type. It can be implemented
through method overloading and method overriding.
- final: A keyword used to declare a constant variable, a method that cannot be overridden, or a class that cannot be
inherited.
- finally: A block of code that is always executed, whether an exception is thrown or not. Used in exception handling.
Q94 Explain the following methods with the help of the Applet class:
import java.applet.Applet;
import java.awt.Graphics;
The paint() method in Java is called by the system to request that the applet or component repaint itself. It's commonly
overridden in applets.
import java.applet.Applet;
import java.awt.Graphics;
Q99 What is a deadlock?
public class MyApplet extends Applet {
A deadlock occurs when two or more threads
public void paint(Graphics g) { are blocked, each waiting for the other to
release a lock. It results in a state where no
g.drawString("Hello, Java!", 20, 20); } } thread can proceed.
Q96 Differentiate TextField and JTextField:
- JTextField: A class in Swing (part of JFC) used for the same purpose but with more features and customization options.
JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client
applications.
- JPanel: A class in Swing (part of JFC) used for the same purpose but with more features and customization options.