Java 2 Marks
Java 2 Marks
[2 Marks]
● Class: A blueprint or template for creating objects. It defines the attributes (properties)
and behaviors (methods) that objects of that class will have.
● Object: An instance of a class. It has actual values for the properties defined in the
class and can perform actions using the class methods
1. Compiled: Java source code is first converted into bytecode by the Java compiler
(javac).
2. Interpreted: The JVM (Java Virtual Machine) then reads and executes the bytecode on
any system.
A variable is a storage location in memory that holds data. It has a name, a data type, and a
value that can change during execution.
In Java, an object is created using the new keyword, which allocates memory for the object and
calls the class constructor.
Example:
class Car {
void display() {
System.out.println("This is a car.");
}
}
○ Used to handle raw binary data like images, audio, and files.
○ Examples: FileInputStream, FileOutputStream.
2. Character Streams (for text data):
● The Abstract Window Toolkit (AWT) in Java is a set of APIs used for creating
Graphical User Interfaces (GUIs) and handling user interactions.
● It is part of the Java Foundation Classes (JFC) and provides components like
windows, buttons, text fields, and menus.
10)What is an applet?
● An applet is a small Java program that runs inside a web browser or an applet viewer.
● It is used to create interactive web applications.
// Constructor
ClassName() {
// Initialization code
}
// Methods (functions)
void methodName() {
// Method logic
}
}
Memory Creates new objects for Uses the same object, reducing
Usage modifications memory usage
Usage Best for fixed text values Best for dynamic string modifications
● Method overloading in Java allows multiple methods in the same class to have the
same name but different parameter lists (number, type, or order of parameters).
● It helps improve code readability and reusability.
● An interface in Java is a blueprint for a class that defines a set of methods without
implementation.
● It is used to achieve abstraction and multiple inheritance.
Usage Best for text files Best for images, audio, video
Panel:
Frame:
20)What is multithreading?
● Encapsulation: Encapsulation is the hiding of data within a class and allowing access
only through getter and setter methods.
● Inheritance: Inheritance allows a child class to inherit properties and methods from a
parent class.
● A constructor is a special method in Java used to initialize objects when they are
created.
● Has the same name as the class.
An array in Java is a collection of elements of the same data type, stored in contiguous
memory locations.
Declaration:
Initialization:
Wrapper classes in Java provide a way to use primitive data types (int, char, boolean,
etc.) as objects.
Example:
System.out.println(obj); // Outputs: 10
Final Classes:
Implementation Can have both abstract and Only abstract methods (until Java 8,
concrete methods which introduced default methods)
Variables Can have instance variables Only public, static, and final variables
28)What are layout managers? What is the use of layout managers in java?
● A local applet is a Java applet that is stored on the user's computer and does not
require an internet connection to run.
● A remote applet is a Java applet that is stored on a remote server and downloaded via
the internet when accessed by a web browser.
An exception in Java is an unexpected event that occurs during program execution, disrupting
the normal flow.
Types of Exceptions:
● Platform Independence
● Object-Oriented
● Simple & Easy to Learn
● Secure.
● Robust
● Interpreted & Compiled
Types of polymorphism:
33)What are instance variables? How are they different from class variables?
● Instance Variables: These are variables that belong to an individual object of a class.
Each object has its own copy of instance variables, meaning changes to one object’s
instance variable do not affect others. They are declared inside a class but outside any
method and do not use the static keyword.
● Class Variables (Static Variables): These are shared across all instances of a class.
They are declared using the static keyword, meaning they belong to the class itself
rather than any specific object. Any change to a class variable affects all instances of the
class.
Definition Blueprint for creating Defines a contract that classes must follow
objects
Methods Can have both concrete Only abstract methods (before Java 8), can
and abstract methods have default/static methods (Java 8+)
Use Case Defines behavior and Provides a standard structure for classes to
properties of objects follow
Serialization
Serialization is the process of converting an object into a byte stream so that it can be saved to
a file, sent over a network, or stored in a database. This allows objects to be persisted and
transferred across different environments.
Deserialization
Deserialization is the reverse process—converting a byte stream back into an object. This
allows previously stored objects to be reconstructed.
Event:
In Java, an event is an action or occurrence that happens within a program, typically triggered
by user interactions or system-generated changes. Examples include clicking a button, pressing
a key, moving the mouse, or resizing a window.
Event Handling
Event handling is the mechanism that allows Java programs to respond to events. Java follows
the Delegation Event Model, where:
Label in AWT
A Label is a passive control used to display text or images in a GUI. It does not support user
interaction.
Button in AWT
4. Volatile Keyword
The Java Virtual Machine (JVM) is an essential part of the Java Runtime Environment (JRE)
that executes Java programs by converting bytecode into machine code. It enables Java's
platform independence, allowing programs to run on any system with a JVM.
Method Area Stores class-level data like method definitions and constants.
Stack Memory Stores method calls and local variables for each thread.
Execution Engine Converts bytecode into machine code using the JIT Compiler.
The Just-In-Time (JIT) compiler is a part of the Java Runtime Environment (JRE) that
improves the performance of Java applications by compiling bytecode into native machine code
at runtime.
44)What is an identifier?
An identifier in Java is the name used to identify variables, methods, classes, interfaces, and
other programming elements. It must be unique and follow specific naming rules.
Examples
Class Syntax
class ClassName {
// Instance variables
int variable;
// Constructor
ClassName() {
variable = 10;
}
// Method
void display() {
System.out.println("Variable: " + variable);
}
}
● A keyword in Java is a reserved word that has a predefined meaning in the language.
● These words cannot be used as identifiers (such as variable names, method names, or
class names).
null Represents the absence of a value or reference. Used for objects but not
primitives.
true Boolean literal indicating a positive condition (boolean flag = true;).
An event in Java is an action or occurrence that happens within a program, typically triggered
by user interactions or system-generated changes. Examples include clicking a button, pressing
a key, or resizing a window.
● Encapsulation: All properties are private and accessed via public getter and setter
methods.
● No-Argument Constructor: Must have a public no-arg constructor for easy
instantiation.
● Swing is a GUI (Graphical User Interface) toolkit in Java that provides a rich set of
components for building desktop applications.
● It is part of the Java Foundation Classes (JFC) and is an extension of AWT (Abstract
Window Toolkit), offering more flexibility and functionality.
// Constructor
ClassName() {
// Initialization code
}
// Methods
returnType methodName(parameters) {
// Method body
}
}
● Dynamic loading in Java refers to the process of loading classes at runtime rather than
at compile time.
● This allows programs to load and use classes only when needed, improving flexibility
and efficiency
In Java, byte stream classes are used for handling input and output operations with 8-bit
bytes. These classes are part of the java.io package and are mainly divided into two
categories:
Functionality Terminates the loop entirely Skips the current iteration and
moves to the next one
Execution Exits the loop immediately Continues with the next iteration
Flow of the loop
Compatibility Works with loops (for, while, Works only with loops (for,
do-while) and switch statements while, do-while)
Cause External factors (e.g., file I/O, Programming errors (e.g., null references,
database issues) array index out of bounds)
Definition Blueprint for creating objects Programming language used to define classes
80)What is JDK?
● The Java Development Kit (JDK) is a cross-platformed software development
environment that offers a collection of tools and libraries necessary for
developing Java-based software applications and applets.
● It is a core package used in Java, along with the JVM (Java Virtual Machine)
and the JRE (Java Runtime Environment).