0% found this document useful (0 votes)
7 views14 pages

Java Basics: Keywords, Data Types, and Concepts

The document provides an overview of various Java concepts including the purpose of javap, data types, keywords like 'this' and 'final', and the definition of interfaces and exceptions. It explains features of Java such as its object-oriented nature, platform independence, and event delegation model. Additionally, it covers topics like method overloading vs overriding, access specifiers, and layout managers in GUI development.

Uploaded by

yashkhalate672
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views14 pages

Java Basics: Keywords, Data Types, and Concepts

The document provides an overview of various Java concepts including the purpose of javap, data types, keywords like 'this' and 'final', and the definition of interfaces and exceptions. It explains features of Java such as its object-oriented nature, platform independence, and event delegation model. Additionally, it covers topics like method overloading vs overriding, access specifiers, and layout managers in GUI development.

Uploaded by

yashkhalate672
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

a) What is the purpose of javap?

javap is a Java disassembler used to view the bytecode, methods, fields, and
internal structure of a compiled .class file.

b) List any two datatypes in Java.


int

float
(Other examples: char, double, boolean, byte, short, long, String)

c) Write the purpose of this keyword.

this keyword is used to refer to the current class object.


It helps to distinguish between instance variables and local variables with same
names.

d) What is the use of toString()?

toString() method returns string representation of an object.


It is used for displaying object values in readable form.

e) Write the use of abstract keyword.

abstract is used to declare:

Abstract classes (incomplete classes)

Abstract methods (methods without body)


It supports polymorphism and abstraction.

f) Define interface.

An interface is a collection of abstract methods and constants.


It is used to achieve multiple inheritance and define common behavior for classes.
g) List any two Reader Classes.
FileReader

BufferedReader
(Other examples: InputStreamReader, CharArrayReader)

h) How to check End of File (EOF)?

EOF is checked when a read() method returns -1.


Example:

int data = [Link]();if(data == -1) // EOF reached

i) What is Panel?

A Panel is a container component in AWT used to hold and organize UI


components like buttons, labels, text fields.

j) Give names of any two Layout Managers.


FlowLayout

BorderLayout

a) What is the purpose of the final keyword when applied to


variables?

When final is used with variables, it means the variable becomes constant and its
value cannot be changed once assigned.

b) What is the purpose of the javap tool in Java?

javap is a Java disassembler tool used to display bytecode, methods, fields, and
internal structure of a compiled .class file.

c) How is a 1D array declared in Java?

A one-dimensional array is declared as:


datatype arrayname[]; // or
datatype[] arrayname;

Example: int arr[] = new int[5];

d) What is the Object class in Java?

Object class is the superclass of all classes in Java.


Every class directly or indirectly inherits from the Object class.

e) What is a wrapper class in Java?

Wrapper classes are classes that convert primitive data types into objects.
Example: Integer, Float, Double, Boolean.

f) What is the role of the extends keyword in Java?

extends is used to inherit properties and methods from one class to another.
It supports single inheritance in Java.

g) What is the purpose of BufferedReader class?

BufferedReader is used to read text from input streams efficiently.


It reads data line by line and improves performance by buffering input internally.

h) Define Exception.

Exception is an unwanted or unexpected event that occurs during program


execution and disrupts normal program flow.

i) Name the package used for event handling in Java.


[Link]

j) Name any two GUI components used in Swing.


JButton

JLabel
(Other examples: JTextField, JCheckBox, JComboBox, JFrame)
a) Differentiate between Method Overloading
and Method Overriding (Detailed Answer)
Method Overloading and Method Overriding are two important concepts of
Polymorphism in Java.

Method Overloading

It means defining multiple methods with the same name but different
parameters.

Parameters must differ in number, type, or order.

It occurs within the same class.

It is an example of Compile-Time Polymorphism (Early Binding).

The return type may be same or different.

Example:

class Demo {
void display(int a) { }
void display(int a, int b) { }
}

Method Overriding

It means a child class provides its own implementation of a method that is


already defined in the parent class.

Method name, return type, and parameters must all be same.

It occurs between superclass and subclass.

It represents Run-Time Polymorphism (Late Binding).

Used for achieving dynamic behavior.

Example:

class Parent {
void show() { }
}class Child extends Parent {
void show() { } // overriding
}
Difference Table

Method Overloading Method Overriding


Same method name, different Same method name, same
parameters. parameters.
Occurs between parent & child
Occurs in the same class.
class.
Compile-time polymorphism. Run-time polymorphism.
Return type can be different. Return type must be the same.

b) State any four access specifiers in Java


(Detailed Explanation)
Access specifiers in Java define visibility and accessibility of classes, methods and
variables.

1. public

Accessible from anywhere in the program.

No restriction on visibility.

public int x; // available globally

2. private

Accessible only within the same class.

Not visible to any other class or subclass.

private int data; // accessed inside same class only

3. protected

Accessible within same package and subclasses (even in another package


using inheritance).

protected int value;

4. default (no keyword)

Accessible only within the same package.

Also known as package-private access.


int number; // default access

c) What is Marker Interface? (Detailed Answer)


A Marker Interface is an interface with no methods and no fields.
Its purpose is to mark or tag a class, providing additional metadata to the JVM.

Why it is used?

It tells JVM or compiler to treat the class specially.

Used in serialization, cloning, etc.

Examples:

Marker Interface Purpose


Serializable Allows object to be saved and restored.
Cloneable Allows object to be cloned (copy created).

Marker interfaces act like a label for the class.

d) What is Exception? List its any two types.


(Detailed Answer)
Exception

An exception is an unexpected or abnormal event that occurs during program


execution.
It interrupts the normal flow of the program.

Java provides Exception Handling to manage errors and prevent program crash using
try, catch & finally.

Two Types of Exceptions:

Checked Exception

Checked at compile time.

Must be handled using try-catch or throws.

Examples: IOException, SQLException


Unchecked Exception

Occurs during runtime.

Not checked at compile time.

Examples: ArithmeticException, NullPointerException

e) What is Swing? How is it different from


AWT? (Detailed – 4 to 6 Marks Answer)
Swing

Swing is an advanced GUI (Graphical User Interface) toolkit in Java used to build
window-based applications.
It is part of [Link] package.

Features of Swing

Lightweight components (not dependent on OS).

Rich set of components like JButton, JTable, JMenu, JProgressBar.

Supports pluggable Look-and-Feel (can change UI theme).

Difference Between Swing and AWT

Swing AWT
Components depend on
Platform-independent components.
underlying OS.
Lightweight components. Heavyweight components.
Found in [Link]. Found in [Link].
More powerful & flexible. Basic and limited UI features.
Supports advanced components like Does not support advanced
JTree, JTable. components.
b) What is Layout Manager? Explain it with
example.
A Layout Manager in Java is an object that controls how user interface components
such as buttons, labels, text fields, etc., are arranged inside a window or container
(like Frame, Panel, or JFrame).
Instead of giving fixed coordinates for every component, Layout Managers
automatically adjust size, position and spacing of components based on screen size
and layout type.

Why Layout Managers are needed?

They make GUI responsive (components adjust automatically)

No need to manually set x–y positions for every component

Useful when window is resized or screen resolution changes

Provide better readability and structure to GUI design

Common Types of Layout Managers

Layout
Uses
Manager
FlowLayout Places components left to right in a row
Divides container into 5 parts: North, South, East,
BorderLayout
West, Center
GridLayout Splits area into equal rows & columns
GridBagLayout Advanced, flexible arrangement

Example Program Using FlowLayout

import [Link].*;import [Link].*;


class LayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("FlowLayout Example");
[Link](new FlowLayout()); // set Layout Manager

[Link](new JButton("Button 1"));


[Link](new JButton("Button 2"));
[Link](new JButton("Button 3"));
[Link](300,200);
[Link](true);
}
}

Explanation:

FlowLayout() arranges components in a row horizontally.

If space is insufficient, components shift to next line automatically.

No manual coordinate setting required.

Example Using BorderLayout

Frame f = new Frame("BorderLayout Demo");


[Link](new BorderLayout());

[Link](new Button("NORTH"), [Link]);


[Link](new Button("SOUTH"), [Link]);
[Link](new Button("EAST"), [Link]);
[Link](new Button("WEST"), [Link]);
[Link](new Button("CENTER"), [Link]);

Here the screen is divided into 5 regions, and each button is placed in its respective
area.

c) Explain the uses of final keyword with


example. (Detailed Answer)
The final keyword in Java is a modifier used to impose restrictions.
It can be used with variables, methods and classes, and its function is different in
each case.

1. final Variable – Value cannot be changed

A final variable behaves like a constant.


Once a value is assigned, it cannot be modified.

final int speed = 100;// speed = 150; // ERROR – modification not


allowed
Use:

For defining constant values like PI, tax rate, or maximum speed.

Prevents accidental value updates.

2. final Method – Cannot be overridden

A method declared as final cannot be overridden by a subclass.

class A {
final void show() {
[Link]("Final method can't be overridden");
}
}
class B extends A {

Use:

When we want to prevent changes in inherited behavior.

Helps in maintaining security and reliability of important methods.

3. final Class – Cannot be inherited

If a class is declared as final, no other class can extend it.

final class Vehicle { }

Use:

To prevent inheritance for security reasons.

Example: String class in Java is final to avoid modification.

Summary of Uses of final

Use What it prevents


final variable Changing value
Use What it prevents
final method Method overriding
final class Class inheritance

a) Explain the features of Java. (Detailed


Answer)
Java is a powerful and popular object-oriented programming language developed by
James Gosling at Sun Microsystems. It is widely used to build desktop applications,
web applications, mobile applications and enterprise systems. The following are the
major features of Java:

1. Simple

Java is easy to learn, understand and write.


Complex features of C++ like pointers, operator overloading, multiple inheritance are
removed, making Java beginner-friendly.

2. Object-Oriented

Java uses Object-Oriented Programming concepts such as classes, objects,


inheritance, abstraction, polymorphism, encapsulation.
This makes program development modular, reusable and maintainable.

3. Platform Independent

Java code is compiled into bytecode using JVM.


This bytecode can run on any operating system — Windows, Linux, Mac OS.
Hence, Java follows the principle:

Write Once, Run Anywhere (WORA)

4. Portable

Java programs can be easily moved from one system to another without modification.
The size of primitive data types is fixed, making Java highly portable.
5. Secure

Java provides strong security features like bytecode verification, exception handling,
access modifiers, automatic memory management, and no direct pointer access.
It’s widely used for network-based and banking applications.

6. Robust

Java is reliable because it has strong memory management, garbage collection,


exception handling and type checking.
Programs are less likely to crash.

7. Multithreaded

Java allows multiple tasks to run simultaneously using threads.


Example: Listening to music while typing a document.

8. High Performance

JIT (Just-In-Time) Compiler converts bytecode into machine code efficiently, giving
better performance than traditional interpreted languages.

9. Distributed

Java supports distributed application development using RMI (Remote Method


Invocation) and networking libraries, enabling communication over networks.

10. Dynamic

Java loads classes dynamically at runtime.


It uses bytecode, making memory usage efficient.

Conclusion:
Java is secure, portable, object-oriented, simple and robust — which is why it is one
of the most widely used programming languages today.
b) Explain event delegation model in Java.
(Detailed Explanation)
The Event Delegation Model, also known as Event Handling Model, is a
mechanism in Java that handles user-generated events such as mouse clicks, key
presses, button clicks etc., in GUI applications.

Instead of handling events inside the component itself, Java delegates the event to an
external object called Listener.
This listener contains the code that must run when the event occurs.

Components of Event Delegation Model

The model is based on three major components:

Component Description
Event Source Object that generates event (e.g., Button, TextField)
Event Object Contains details of event (type, time, source, etc.)
Event Object that responds to event by implementing listener
Listener interface

How the Event Delegation Model Works

A user performs an action (click button, press key).

Component generates an event object.

Event is passed to listener which has registered with component.

Listener executes required action (method) when event happens.

Advantages of Event Delegation Model

Separates event handling code from UI component.

Makes GUI programs more organized, reusable & readable.

Efficient — only registered listeners receive event, not every component.

Supports multiple listeners for one event.


Example Program of Event Delegation

import [Link].*;import [Link].*;


class EventDemo extends Frame implements ActionListener {

Button b;

EventDemo() {
b = new Button("Click Me");
add(b);

[Link](this); // registering listener


setSize(300,200);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {


[Link]("Button Clicked!");
}

public static void main(String args[]) {


new EventDemo();
}
}

Explanation:

Button is the event source.

ActionEvent is the event object.

ActionListener is the listener interface.

When button is clicked → actionPerformed() executes.

You might also like