1)Object-Oriented Programming Concepts in Java
Ans: [Link]= A class is a blueprint for creating an object
[Link]= An object is an instance of a class. It represents a real-world entity
3. Encapsulation= Encapsulation is the process of binding data and methods into
a single unit
[Link]= Inheritance allows one class to acquire the properties and
behaviors of another class.
[Link]= Polymorphism means “many forms.” It allows methods or
objects to behave differently based on the context.
6. Abstraction= Abstraction is the process of hiding complex implementation
details and showing only necessary.
[Link]= A constructor is a special method in a class used to initialize
objects. It is automatically called when an object is created.
[Link] Keyword= The this keyword refers to the current object of the class. It is
used to differentiate between instance variables and local variables with the
same name.
[Link] Keyword= The super keyword refers to the immediate parent class
object. It is used to call parent class methods or constructors from the subclass.
10. Interface= An interface is a collection of abstract methods. It defines a
contract that implementing classes must follow. It is used to achieve abstraction
and multiple
inheritance in Java.
2) features of java
Ans: 1. Simple= Java is easy to learn and use.
2. Object-Oriented= Everything in Java revolves around objects and classes.
3. Platform Independent= Java programs can run on any platform with a Java
Virtual Machine (JVM).
4. Secure= Java provides strong security through bytecode verification,
exception handling, and lack of direct memory access.
5. Robust= Java emphasizes reliability with features like automatic garbage
collection, exception handling, and strong type checking.
6. Multithreaded= Java allows multiple threads to run simultaneously.
7. Portable= Java code is compiled into bytecode, which can be executed on any
machine with a JVM,
8. Dynamic= Java can load classes and methods dynamically at runtime.
9. Distributed
Java supports distributed computing with technologies like RMI and networking
packages,
[Link] Performance= Java achieves high performance through Just-In-Time
(JIT) compilation and optimized execution in the JVM.
[Link]= Java programs are executed by the JVM, which interprets
bytecode into machine code at runtime.
[Link] Neutral= Java bytecode is not specific to any machine
architecture, allowing it to run on any system with a JVM.
3) Java Environment
Ans: The Java Environment is the setup or system in which Java programs are
developed,
compiled, and executed. It mainly consists of three parts:
1. Java Development Kit (JDK): The JDK is a complete software development kit
used to develop Java applications. It contains tools and utilities for writing,
compiling, and debugging Java programs. Major components include:
o Compiler (javac) – Converts source code into bytecode.
1
11) Static Block in Java
Ans:A static block (also called static initialization block) is a block of code inside
a class that runs only once, when the class is loaded into memory, before any
object is created.
13)Static field in java
Ans:A static field (also called a class variable) is a variable that belongs to the
class, not to any specific object of that class.
14) Static method in java
Ans: A static method in Java is a independent of any particular object of the
[Link] is declared using the static [Link] method access only static
field or member. That means:You can call it directly using the class name,
without creating any object of the class.
15) Object class in java
Ans: In Java, the Object class is the parent (superclass) of all [Link] is
defined in the package [Link], which is automatically imported in every Java
program.
16) Method of object class
Ans: The Object class is the parent of all classes in Java. Every class you create
automatically inherits these methods.
1. toString()
Converts an object into a String. By default, it returns ClassName@HashCode,
but it is usually overridden to show actual data.
Example: [Link]();
2. equals(Object obj)
Compares two objects for equality. By default, it checks if they share the same
memory address (==), but it's often overridden to compare field values.
Example: [Link](user2);
3. hashCode()
Returns an integer value (hash) representing the object. It is used in
hashing-based collections like HashMap.
Example: [Link]();
4. getClass()
Returns the runtime class of the object. It’s used to get metadata about the
class (Reflection).
Example: [Link]().getName();
17) A Package is a namespace that organizes a set of related classes and
interfaces. Conceptually, it is similar to different folders on your computer-one
for photos, one for work, and one for music-to prevent clutter and confusion.
Types of Packages
Built-in (Standard) Packages: These come pre-installed with the Java
Development Kit (JDK).
[Link]: Containsfunda mental classes (automatically imported, like String and
System).
[Link]: Contains utility classes like collections and date/time.
[Link]: Handles input and output streams.
User-defined Packages: Created by developers to structure their own
applications.
2
5) Data Types in Java
Ans: A data type defines the type of data a variable can hold. It determines the
memory
size, range of values, and operations that can be performed. Java has two main
categories: primitive and non-primitive.
1. Primitive Data Types:
Primitive types are built-in and used to store simple values. Java has 8 primitive
data types:
1. boolean Data Type- Represents one of two logical values: true or false.
2. byte Data Type:- An 8-bit signed integer used to save memory in large
numeric arrays.
3. short Data Type- A 16-bit signed integer often used when memory is limited
and values are moderate in size.
4. int Data Type- A 32-bit signed integer commonly used for whole numbers.
5. long Data Type- A 64-bit signed integer used when int is not sufficient for
large values.
6. float Data Type- A 32-bit single-precision floating-point type used for
fractional values.
7. double Data Type- A 64-bit double-precision floating-point type and the
default for decimal numbers.
8. char Data Type- A 16-bit Unicode character used to store single symbols or
letters.
2. Non-Primitive (Reference) Data Types:
Non-primitive types are defined by the programmer and store references
(memory
addresses) of objects. They include classes, arrays, interfaces, and Strings.
1. String- String represents a sequence of characters enclosed in double quotes.
2. Class
3. Interface- An interface defines a contract of abstract methods that
implementing classes must define. It provides a way to achieve abstraction and
multiple inheritance in Java.
4. Array- An array stores multiple elements of the same type in a single
structure. Java arrays are objects, dynamically allocated, and indexed from 0.
6) Final Variable in Java
Ans: A final variable is a variable whose value cannot be changed once it is
assigned. It must be initialized at the time of declaration or in a constructor (for
instance variables).
Example:
public class TestFinalLocal {
public static void main(String[] args) {
final int x = 10; // value assigned once
[Link]("Value of x: " + x);
}
}
3
7) Accepting Input in java
Ans: In Java, accepting input from the user is commonly done using the Scanner
class from
[Link] package.
1. Using Scanner Class:
Step 1: Import Scanner
import [Link];
Step 2: Create Scanner Object
Scanner sc = new Scanner([Link]);
Step 3: Accept Input
Use methods like nextInt(), nextDouble(), nextLine(), next() to read different
types of input.
[Link] Command Line Arguments:
Command-line arguments are inputs passed to a Java program when it is
executed from the command line or terminal. They are read using the main
method parameter String[] args. Each argument is passed as a String, and
multiple arguments are stored in an array of Strings.
Example:
public class CommandLineExample {
public static void main(String[] args) {
[Link]("Number of arguments: " + [Link]);
for (int i = 0; i < [Link]; i++) {
[Link]("Argument " + i + ": " + args[i]);
}
}
}
[Link] BufferedReader: BufferedReader is a class in Java used to read text from
an input stream (like the keyboard or a file) efficiently. It belongs to the package
[Link]. It reads data line by line or character by character.
8) Access specifiers in java
Ans:Access specifiers, also known as access modifiers, in Java are keywords that
set the accessibility (visibility) of classes, interfaces, variables, methods, and
constructors.
1)Private Access Modifier:
The private access modifier is specified using the keyword private. The methods
or data members declared as private are accessible only within the. class in
which they are declared.
2) Default Access Modifier:
When no access modifier is specified for a class, method, or data member, it is
said to have the default access modifier by default. This means only classes
within the same package can access it.
3) Protected Access Modifier:
The protected access modifier is specified using the keyword protected. The
methods or data members declared as protected are accessible within the same
package or subclasses in different packages.
4) Public access specifier:
The public access modifier is specified using the keyword public. Public
members are accessible from everywhere in the program. There is no restriction
on the scope of public data members.
4
9) Types of constructor
Ans: 1. Default Constructor
A constructor that does not accept any arguments.
It takes default value.
Example:
class Bike {
Bike() { [Link]("Bike created"); }
public static void main(String[] args) {
new Bike(); // Prints: Bike created
}
}
2. Parameterized Constructor
A constructor that has a specific list of parameters.
Example:
class Car {
String model;
Car(String m) { model = m; }
public static void main(String[] args) {
Car c = new Car("Tesla");
[Link]([Link]); // Prints: Tesla
}
}
3. Copy Constructor
A constructor that takes an object of the same class as a parameter.
Example:
class Point {
int x, y;
Point(int x, int y) { this.x = x; this.y = y;}
Point(Point p) {
this.x = p.x;
this.y = p.y;
}
public static void main(String[] args) {
Point p1 = new Point(10, 20);
Point p2 = new Point(p1); // p2 is now a copy of p1
. }
10) Constructor overloading in java
Ans: Constructor Overloading in Java means having more than one constructor
in the same name,but with different number or types of parameters.
Example:
class Room {
int length, width;
Room()
{. length = 10; width = 10; }
Room(int side)
{ length = width = side; }
Room(int l, int w)
{ length = l; width = w; }
}
5
o Java Runtime Environment (JRE) – For running Java programs.
o Development tools – Like debugger, jar, javadoc, etc.
2. Java Runtime Environment (JRE)= The JRE provides the environment to run
Java [Link] includes: o Java Virtual Machine (JVM).
o Class libraries and other supporting files It does not include development tools
(like compiler). It is meant for executing already compiled Java programs.
3. Java Virtual Machine (JVM)= The JVM is the core part of the Java
environment. It converts bytecode into machine code for the specific operating
system.
4) Java Tools
Ans: Java tools are utility programs provided with the JDK to develop and
manage Java applications.
1. jdb (Java Debugger): Used to debug Java programs, allowing you to step
through code, set breakpoints, and inspect [Link] command-line Java
Debugger, used for finding and fixing bugs.
Example Program:
class DebugExample {
public static void main(String[] args) {
int a = 5;
int b = 10;
int sum = a + b;
[Link]("Sum = " + sum);
} }
2. javap (Java Disassembler): Used to view the structure of compiled class files,
including methods and fields.
Example Program:
class Display {
void show() {
[Link]("Hello Java");
} }
3. javadoc(Java Documentation Tool): Generates HTML documentation from
special comments in Java source code.
Example Program:
public class DocExample {
public void display() {
[Link]("Hello from javadoc example!");
} }
[Link](Compiler): javac is used to compile java program and compiles the the
code into byte code
this code run on any platform
Example:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
} }
5. java:java tool is used to run the java program
Example:
class HelloWorld {
public static void main(String[] args) {
[Link]("Hello, World!");
}
6
}
18) Types of inheritance
Ans: 1. Single Inheritance: A single subclass inherits from a single superclass. It
is the simplest form of inheritance.
Example: A Dog class inheriting from an Animal class.
2. Multilevel Inheritance: A subclass inherits from another subclass, creating a
"chain" of inheritance.
Example: BabyDog inherits from Dog, which inherits from Animal.
3. Hierarchical Inheritance; Multiple subclasses inherit from one single
superclass.
4. Multiple Inheritance : One subclass tries to inherit from more than one
superclass.
5. Hybrid Inheritance: A combination of two or more types of inheritance (e.g.,
Multilevel + Hierarchical).
19) Runtime Polymorphism in Java
Ans: Runtime Polymorphism (also called Dynamic Method Dispatch) occurs when
a call to an overridden method is resolved at runtime, not at compile time.
20) Abstraction
Ans: Abstraction means hiding internal details and showing only the important
functionality to the user.
21) Abstract Class (Java)
Ans: A class that cannot be created (instantiated) directly
Can have: Abstract methods (no body). Concrete methods (with body).
Declared using abstract keyword. Used as a base class for other classes.
22) Abstract Method
Ans: A method without implementation (no body). Must be implemented by
subclass. Declared using abstract keyword. Cannot be static, final, or private
23) Exception
Ans: An exception is an event that disrupts the normal flow of a program’s
execution. It is an object representing an error or unexpected behavior that
occurs during runtime.
24) Exception Handling in Java
Ans:Exception Handling is a powerful mechanism that allows developers to
handle runtime errors (exceptions) in a structured way—making programs more
robust and error-tolerant.
25) Errors
Ans: Error is a mistake or fault in a program that causes it to produce incorrect
results or
stop execution.
Types of Errors in Java
Errors in Java can be broadly classified into three main types:
[Link]-Time Errors
These are detected by the Java compiler during compilation. Caused by syntax
mistakes or incorrect code structure.
Examples:
int a = 10
[Link](a);
Missing semicolon will cause a compile-time error.
[Link] Errors (Exceptions)
Occur while the program is running. Caused by invalid operations such as
dividing by zero, accessing invalid array index, etc.
7
Example:
8
int a = 5, b = 0;
int c = a / b; // ArithmeticException at runtime
[Link] Errors
The program compiles and runs, but the output is incorrect due to a logic
mistake. The compiler does not detect them.
Example:
int a = 10, b = 5;
int sum = a - b; // Logical error (should be a + b)
26) Exception Handling Keywords in Java
Ans:Java provides five keywords to manage exceptions effectively: try, catch,
finally, throw, throws
1. try Block: The try block is used to write code that might throw an exception.
It is always followed by either a catch or a finally block.
Example:
try {
int a = 10 / 0; // risky code
}
2. catch Block: The catch block handles the exception thrown by the try block.
It specifies the type of exception to handle.
Example:
catch (ArithmeticException e) {
[Link]("Cannot divide by zero!");
}
3 finally: Code that always executes, whether an exception occurs or
[Link] used for cleanup tasks like closing files, connections, etc.
Example:
public class FinallyExample {
public static void main(String[] args) {
try {
int arr[] = {1, 2, 3};
[Link](arr[5]); // risky code
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Exception caught!");
} finally {
[Link]("Finally block executed!");
}}}
[Link]: throw is used to manually throw an exception. It immediately
terminates the current method and passes the exception to the caller.
Syntax:
throw new ExceptionType("Error message");
[Link]:throws is used in method declaration to indicate that the method may
throw an exception. The caller of the method must handle the exception using
try-catch or propagate it further.
27)What is AWT?
Ans:AWT is a Java GUI framework used to create components like windows,
buttons, menus, and text fields for desktop applications. It is part of Java
Foundation Classes (JFC) and belongs to the [Link] package. AWT is
platform-dependent, meaning it uses the system’s native components.
9
28) What is Swing?
Ans:Swing is a set of classes in the [Link] package used to create
window-based applications. Unlike AWT, Swing components are written entirely
in Java, making them platform-independent and lightweight.
29) Key Features of Swing
Ans: Platform Independent
Written in Java → runs on any system with JVM
-Lightweight Components: Not dependent on OS components. Mostly pure Java
-Rich Components: Provides many GUI elements like JButton, JLabel, JTable,
JTree, etc.
-MVC Architecture: Separates data (Model), UI (View), and logic (Controller)
-Event Handling
Handles actions like clicks, keyboard, mouse using event model Layout Managers
Controls layout using BorderLayout, FlowLayout, GridLayout, CardLayout
30) Difference between AWT and SWING
ANS:
AWT Swing
1. AWT is an API for building GUI in 1. Swing is part of JFC used to build
Java. advanced GUIs.
2. Components are heavyweight (use 2. Components are lightweight (pure
OS native resources). Java).
3. Less functionality and fewer 3. More powerful and rich
components. components.
[Link] on native OS, so behavior 4. Platform independent, consistent
may vary. behavior.
5. Not fully based on MVC. 5. Follows MVC architecture.
6. Limited customization. 6. Highly customizable (look & feel).
7. Uses [Link] package. 7. Uses [Link] package.
8. Faster in some cases due to native 8. Slightly slower but more flexible.
components. 9. Advanced components (JButton,
9. Basic components (Button, Label, JTable, JTree).
TextField) 10. Easier to build complex
10. Harder to build complex GUIs. applications.
11. Requires more manual coding 11. Many features are built-in.
10