OOPS Using Java
2mark Questions
1. Write any 2 features of Java.
• Platform Independence: Java is designed to be a "write once, run anywhere"
language. This is achieved through the use of the Java Virtual Machine (JVM), which
allows Java programs to run on any device or operating system that has the JVM
installed.
This means that a Java program written on one platform can be executed on another
without any modifications
• Object-Oriented Programming (OOP): Java is based on the concept of "objects."
Objects are instances of classes, which can contain data (attributes) and methods
(functions).
This approach encourages modular and reusable code, making it easier to manage and
maintain complex software systems.
OOP principles such as inheritance, encapsulation, polymorphism, and abstraction are
fundamental to Java, allowing developers to build robust and scalable applications.
2. How Java is platform independent?
When a Java program is written, it is compiled by the Java compiler into an intermediate form
known as bytecode. This bytecode is a set of instructions that are platform-independent.
When a Java program is executed, the JVM translates the platform-independent
bytecode into native machine code. This means that a Java program written on one
platform can be executed on another without any modification
3. What is JVM? Explain various parts of JVM
1. Class Loader
The class loader is like a librarian that finds and organizes the Java classes your
program needs, making sure everything is ready to go.
2. Runtime Data Area
The runtime data area serves as memory storage for different program execution
components, similar to a workspace that has multiple divisions.
1. Method Area
2. Heap Area
3. Stack Area
4. PC Register
5. Native Method Stack Area
3. Execution Engine
The execution engine acts as a skilled translator, converting your Java code into a
language that your computer can understand and execute. It does this through two
methods:
1. Interpreter
2. JIT Compiler
4.What is byte code?
Bytecode in Java is an intermediate, platform-independent code generated by the Java compiler
after a Java source file (.java) is compiled. It is stored in .class files and is not directly executable by
the operating system's hardware.
Instead, bytecode is executed by the Java Virtual Machine (JVM), which interprets or compiles it into
native machine code at runtime
5.Define Java API
The Java API(Java Application Program interface) is a collection of pre-written packages, classes, and
interfaces, along with their methods and fields, provided by the Java Development Kit (JDK).
Ex: classes from java.util package, classes from java.io for input/output operations
6.What is JIT Compiler
The Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) , by compiling bytecode
into machine code, the JIT compiler optimizes execution speed and improves overall efficiency of Java
applications.
7.What is abstract class*****
An abstract class in Java is a class that cannot be instantiated, it contains abstract methods,
which are declared without implementation,
For example:
abstract class Animal
{
abstract void makeSound(); // Abstract method
-----
-----}
8. What is an Identifier
An identifier in Java is a name given to various elements in a program such as variables, methods,
classes, and objects.
Identifiers must start with a letter (A-Z or a-z), a dollar sign ($), or an underscore (_),
followed by any combination of letters, digits (0-9), dollar signs, and underscores. Identifiers are
case-sensitive
9. Write syntax to create class and object ****
Syntax to create a class
access specifier class ClassName
Fields (variables) ;
Methods public;
}
Syntax to create a object
ClassName objectName = new ClassName();
10. What is keyword? List some of the keywords in Java
Keyword in Java:
A keyword in Java is a reserved word that has a predefined meaning in the language syntax
cannot be used as identifiers (e.g., variable names, class names, method names).
List of Some Keywords in Java:
• Data Types: int, float, double, char, boolean, byte, short, long
• Control Flow: if, else, switch, case, default, while, do, for, break, continue,
return
• Access Modifiers: private, protected, public
• Class-Related: class, interface, extends, implements, abstract, final
• Exception Handling: try, catch, finally, throw, throws
• Object and Class: new, this, super, instanceof
• Others: static, void, volatile, package, import, enum
Can write any one example is enough
11.What is inheritance?
Inheritance in Java is concept where one class (child or subclass) inherits the properties and
behaviors (fields and methods) of another class (parent or superclass).
This allows for code reusability, method overriding
Syntax:
class ParentClass {
// Fields and methods
}
class ChildClass extends ParentClass {
// Additional fields and methods
}
12.What is interface?
An interface in Java is a reference type contain only method signatures, default methods, static
methods, and nested types. Interfaces cannot contain instance fields or constructors.
EX:
interface InterfaceName
{
// Abstract method
void method1();
// Default method
default void method2()
{
System.out.println("Default implementation");
}
// Static method
static void method3()
{
System.out.println("Static implementation");
}
}
13. What is exception handling in java
Exception handling in Java is used to handle runtime errors, ensuring the program can continue its
normal flow even when unexpected events occur
try {
// Code that may throw an exception
}
catch (ExceptionType1 e1)
{
// Code to handle exception of type ExceptionType1
}
catch (ExceptionType2 e2)
{
// Code to handle exception of type ExceptionType2
}
finally {
// Code that will always execute, whether exception is thrown or not
}
Key Components:
1. try: The block of code throw an exception is placed inside the try block.
2. catch: The catch block is used to handle the exception. It catches the exception
thrown by the try block and defines how to handle it.
3. finally: The finally block contains code that will always be executed, regardless of
whether an exception is thrown or not.
4. throw: The throw keyword is used to explicitly throw an exception.
5. throws: The throws keyword is used in the method signature to indicate that the
method might throw exceptions.
14. Define class and object
Class:
A class in Java is a blueprint or template for creating objects. It combines data and methods
as a single unit. A class encapsulates data and methods to manipulate that data.
access specifier class ClassName
{
// Fields (variables)
int field1;
// Methods
public void method1()
{
// Method code
}
}
Object:
An object is an instance of a class.Objects are created using the new keyword followed by the
class constructor.
ClassName objectName = new ClassName();
15. What is the use of the final keyword?*****
The final keyword in Java is used to define an entity that cannot be changed or modified.
It can be applied to variables, methods, and classes.
Final Variables:
A final variable is a constant; once assigned a value, it cannot be changed.
Ex:
final int a = 100;
Final Methods:
A final method cannot be overridden by subclasses. This is used to prevent altering the
method's implementation in subclasses.
public final void display() {
// Method body
}
16. List the different types of comments in Java?
1. Single-Line Comments:
Single-line comments begin with // and extend to the end of the line
// This is a single-line comment
2.Multi-Line Comments:
Multi-line comments, also known as block comments, begin with /* and end with */
17.Define multitasking and multithreading in java******
Multitasking is the ability of an operating system to execute multiple tasks or processes
There are two types of multitasking:
1. Process-Based Multitasking (Multiprocessing):
o Each process has its own address space and resources.
o Each process requires its own memory allocation.
Example: Running multiple Java applications simultaneously.
2. Thread-Based Multitasking (Multithreading):
o Multiple threads are executed within the same process.
o threads share the same memory space and resources.
Example: Running multiple threads within a single Java application
18.What is separator in java? List its types
Separators in Java:
Separators in Java are special characters that are used to separate different parts of a program
Types
Parentheses ()
Braces {}
Brackets []
Semicolon ;
Comma ,
Period .
Colon :
19.What is event in java? List out its types
An event is an object that represents an action by software, often generated as a result of user
interactions or other programmatic activities. They are used to trigger specific methods when
certain actions occur, such as : button click, mouse movement, or a key press.
High-level events.
ActionEvent:
• Generated when a button is clicked, a menu item is selected, or an item is double-
clicked
ItemEvent:
• Generated when an item is selected or deselected.
FocusEvent:
• Generated when a component gains or loses focus
Low-Level Events:
Low-level events are generated by low-level input devices such as the mouse and keyboard.
1. MouseEvent:
o Generated by mouse actions such as clicks, presses, releases, movements, and
drags.
2. KeyEvent:
• Generated by keyboard actions such as key presses and releases.
3. WindowEvent:
• Generated by window actions such as opening, closing.
20. What is Unicode character in java
Unicode is a universal character encoding standard that provides a unique number for every
character.
• 16-Bit Representation:
• Java uses 16-bit Unicode characters. This means each character is represented by a
16-bit (2-byte) value.
• This allows Java to support 65,536 unique characters.
• Character Data Type:
• The char data type in Java is used to store a single Unicode character.
21. What is null, true and false in java?
null, true, and false in Java:
These are special literals in Java that represent unique values or states within the
programming language.
null is a literal in Java that represents the absence of a value or a reference to an object. It can be
assigned to any reference type variable (e.g., objects, arrays).
String str = null; // str does not reference any String object
if (str == null) {
System.out.println("The string is null.");
true and false:
true and false are boolean literals in Java that represent the two possible values of a
boolean type.
22. Dynamic Loading in Java:
Dynamic loading in Java refers to the process of loading classes into the Java Virtual
Machine (JVM) at runtime rather than at compile time
23. what is swing in java
Swing is a part of Java's standard library that provides a rich set of GUI components for building
graphical user interfaces (GUIs)
24.What is java beans
JavaBeans are reusable software components for Java that can be manipulated visually in a
builder tool.
JavaBeans have properties that can be accessed and modified using getter and setter methods.
JavaBeans can generate and listen to events.
25.What is applet in java
Java programs that can be embedded in web pages and run in web browser using applet, providing
interactive features