0% found this document useful (0 votes)
4 views

java notes impotant

The document provides an overview of various concepts in Java programming, including constructors, exception handling, applets, inheritance, and the differences between related classes and interfaces. It explains the roles of keywords like 'this', 'finally', and the functionalities of classes such as String, StringBuilder, and BufferedReader. Additionally, it covers the lifecycle of threads, static variables and methods, and introduces Swing components used in Java GUI development.

Uploaded by

joopandey092019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

java notes impotant

The document provides an overview of various concepts in Java programming, including constructors, exception handling, applets, inheritance, and the differences between related classes and interfaces. It explains the roles of keywords like 'this', 'finally', and the functionalities of classes such as String, StringBuilder, and BufferedReader. Additionally, it covers the lifecycle of threads, static variables and methods, and introduces Swing components used in Java GUI development.

Uploaded by

joopandey092019
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

1). What is constructor ? explain types of constructor?

Ans). A constructor is a special type of method in a class that is


automatically called when an object of that class is instantiated.
Constructors are used to initialize the object's state (i.e., assign values
to its data members) when the object is created. In many
programming languages, constructors have the same name as the
class itself.
Types of Constructors

1. Default Constructor (No-Argument Constructor):


• A default constructor does not take any arguments. It is
automatically provided by the compiler if no other
constructors are defined.
• It initializes object members with default values (e.g.,
zero for numeric types, null for reference types in
languages like Java, etc.).
2. Parameterized Constructor:
• A parameterized constructor takes one or more
arguments. It allows for the initialization of objects with
specific values at the time of their creation.
3. Copy Constructor:
• A copy constructor creates a new object as a copy of an
existing object. It is commonly used in languages like
C++.

2). Explain finally keyword ?


Ans). The finally keyword is used in programming languages like
Java, C#, Python, and others, to define a block of code that is always
executed after a try/catch block, regardless of whether an exception
was thrown or caught.

3). What is the use of this keyword?


Ans). The this keyword is used in object-oriented programming
languages like Java, C#, and C++ to refer to the current instance of
the class. It is a reference to the object on which the method or
constructor was called.

4). What is JVM ?


Ans). A Java virtual machine (JVM), an implementation of the Java
Virtual Machine Specification, interprets compiled Java binary code
(called bytecode) for a computer's processor (or "hardware
platform") so that it can perform a Java program's instructions. Java
was designed to allow application programs to be built that could be
5). What is an applet ?
Ans). An applet is a small application designed to run within a web
browser or an applet viewer. Applets are written in the Java
programming language and embedded into an HTML page. They are
primarily used to provide interactive features to web applications that
cannot be provided by HTML alone.
6). Explain applet lifecycle ?
Ans), The lifecycle of a Java applet is managed by the browser or an
applet viewer and consists of several stages, each represented by a
specific method that the applet class can override. These stages
ensure that the applet is properly initialized, started, stopped, and
destroyed. The key lifecycle methods are init(), start(), stop(), and
destroy(), with an additional paint(Graphics g) method for handling
drawing operations. Here’s an in-depth look at each stage of the
applet lifecycle.
7). What is applet programming ?
Ans). Applet programming refers to the creation of small Java
programs called applets that are designed to run within a web
browser or an applet viewer. Applets are embedded in web pages and
can provide interactive features, such as animations, games, and
dynamic user interfaces, that are beyond the capabilities of HTML
alone.
Key Features of Applet Programming
1. Graphical User Interface (GUI): Applets often include GUI
components for interaction with users.
2. Security: Applets run in a restricted environment (sandbox) to
prevent unauthorized access to the user’s system.
3. Platform Independence: Like other Java applications,
applets are platform-independent and can run on any system
with a compatible JVM.

8). Explain in literals java ?


Ans). In Java, a literal represents a constant value that can be
assigned to a variable. Literals can be of various types, including
integers, floating-point numbers, characters, strings, and boolean
values. 1. Integer Literals: represent whole numbers without any
decimal points. They can be expressed in decimal (base 10), octal
(base 8), or hexadecimal (base 16) notation. 2. Floating-Point Literals:
represent numbers with decimal points. They can be expressed in
standard or scientific notation. 3. Character Literals: represent single
characters enclosed in single quotes (''). 4. String Literals: represent
sequences of characters enclosed in double quotes ("").
9). What is exception ? explain try, catch & finally block ?
Ans). An exception in Java is an event that occurs during the
execution of a program and disrupts the normal flow of the program's
instructions. It represents an error or an unexpected condition that a
program encounters during runtime.
1)The try block is used to specify a block of code that may throw an
exception. 2)The catch block is used to handle the exception if it is
thrown. 3)The finally block is used to execute the code after
the try and catch blocks have been executed.

10). What is exception handling explain in details ?


Ans). Exception handling is a crucial aspect of programming that
deals with the occurrence of errors or exceptional conditions during
the execution of a program. Exception handling allows developers to
detect, respond to, and recover from unexpected situations in a
controlled manner, thereby ensuring that the program continues to
execute gracefully even in the presence of errors.

Exception:
• An exception is an event that disrupts the normal flow of
program execution.
• It represents an error or an unexpected condition that occurs
during runtime.

Handling Exceptions:
• Catching Exceptions: Exception handlers are used to
catch and handle exceptions.
• Throwing Exceptions: Developers can explicitly throw
exceptions using the throw keyword.

11). How exception handling is done in java ?


Ans). Exception handling in Java is done using a structured
mechanism that involves the use of try, catch, finally, throw, and
throws keywords. This mechanism allows developers to detect,
11). How exception handling is done in java ?
Ans). Exception handling in Java is done using a structured
mechanism that involves the use of try, catch, finally, throw, and
throws keywords. This mechanism allows developers to detect,
respond to, and recover from unexpected situations or errors that
may occur during the execution of a program.
12). Define inheritance. List types of inheritance in java ?
Ans). Inheritance in Java is a mechanism where a new class (subclass)
is derived from an existing class (superclass). It allows the subclass to
inherit the fields and methods of the superclass, enabling code reuse
and promoting the creation of hierarchical relationships between
classes.
Types of inheritance in Java:
1. Single Inheritance: A subclass inherits from only one
superclass.
2. Multilevel Inheritance: A subclass inherits from a
superclass, and then another subclass inherits from the
intermediate subclass, forming a chain of inheritance.
3. Hierarchical Inheritance: Multiple subclasses inherit from
a single superclass.
4. Multiple Inheritance (through interfaces): A class can
implement multiple interfaces, effectively inheriting method
signatures from each interface.
5. Hybrid Inheritance: This is a combination of two or more
types of inheritance.

13). Different between string and string builder ?


Ans). The main difference between String and StringBuilder in Java
lies in their mutability and performance characteristics:

1. Immutability:
• String: Strings in Java are immutable, meaning once a
string object is created, its state cannot be changed. Any
operation that appears to modify a string actually creates a
new string object.
• StringBuilder: is mutable, allowing you to modify the
contents of the string without creating a new object each
time.

2. Performance:
• String: Because strings are immutable, performing
operations like concatenation or substring creation involves
creating new string objects, which can be inefficient,
especially for large strings.
• StringBuilder: StringBuilder, being mutable, allows you
to modify the contents of a string without creating new
objects.

3. Thread Safety:

• String: Strings are immutable and inherently thread-safe,


meaning they can be safely shared among multiple threads
without risk of data corruption.
• StringBuilder: StringBuilder is not thread-safe. If
multiple threads need to modify the same StringBuilder
instance concurrently, external synchronization
mechanisms must be used to ensure thread safety.

14). Different between abstract class & interface ?


Ans). 1. Definition:
• Abstract Class: An abstract class is a class that
cannot be instantiated on its own and may contain
abstract methods (methods without a body) along with
concrete methods.
• Interface: An interface is a reference type in Java that
is similar to a class but contains only constants, method
signatures, default methods, static methods, and nested
types.
2. Instantiation:
• Abstract Class: You cannot create instances of an
abstract class directly. You need to extend it and provide
implementations for its abstract methods in the subclass.
• Interface: Interfaces cannot be instantiated at all.
They define a contract that classes can choose to
implement.
3. Inheritance:
• Abstract Class: Java supports single inheritance,
meaning a class can extend only one abstract class (or
any other class), but it can implement multiple
interfaces.
• Interface: Java supports multiple inheritance through
interfaces. A class can implement multiple interfaces,
allowing it to inherit method signatures from all of them.

15). Different between file input strean & file output stream
15). Different between file input strean & file output stream
Ans). In Java, FileInputStream and FileOutputStream are classes used
for input and output operations with files, respectively. Here are the
main differences between them:

1. Purpose:
• FileInputStream: This class is used for reading data
from a file.
• FileOutputStream: This class is used for writing data to
a file.

2. Direction of Data Flow:


• FileInputStream: Reads data from a file into your
program.
• FileOutputStream: Writes data from your program into
a file.

3. Usage:
• FileInputStream: You typically use this class when you
need to read data from a file, such as reading text files,
binary files, etc.
• FileOutputStream: You typically use this class when you
need to write data to a file, such as writing text files,
binary files, etc.

16). Different between overloading and overriding along


with example ?
Ans). Overriding
1).Implements “runtime polymorphism” 2).The method call is
determined at runtim based on the object type 3).Occurs between
superclass and subclass 4). Have the same signature (name and
method arguments)5). On error, the effect will be visible at runtime
Overloading
1). Implements “compile time polymorphism” 2). The method call is
determined at compile time 3). Occurs between the methods in the
same class 4). Have the same name, but the parameters are different
5). On error, it can be caught at compile time

17). Explain thread lifecycle event ?


17). Explain thread lifecycle event ?
Ans). The lifecycle of a thread in Java consists of several states, and
as a thread progresses through these states, it undergoes various
events.
1).New: When a thread is created, it is in the new state. In this state,
the thread has been instantiated, but it has not yet been started. The
start() method is invoked to transition the thread to the next state,
which is the runnable state.

2).Runnable: After a thread has been started, it enters the runnable


state. In this state, the thread is ready to run, but it may not be
currently executing because the CPU scheduler has not selected it to
run yet.

3).Running: When a thread is executing its tasks, it is in the running


state. In this state, the thread is actively executing its code. A thread
remains in the running state until its run() method completes, or it is
preempted by the scheduler due to time slicing or a higher-priority
thread becoming runnable.

4)Blocked/Waiting: Threads can transition to the blocked or


waiting state when they are temporarily unable to proceed. This could
happen for various reasons, such as waiting for I/O operations to
complete, waiting for locks, or waiting for other threads to notify
them.

5).Timed Waiting: Threads can also enter a timed waiting state,


where they wait for a specific period of time before resuming
execution. This can occur when a thread calls methods like sleep() or
join() with a timeout parameter.

18) Explain static variables & static methods. ?


Ans). Static variables and static methods in Java are associated with
the class itself rather than with instances of the class.

Static Variables:
• Static variables, also known as class variables, are declared
using the static keyword.
• They are initialized only once when the class is loaded into
memory, and their values are shared among all instances of the
class.
• Static variables can be accessed using either the class name or
an instance of the class, but it's recommended to access them
using the class name for clarity.

Static Methods:
• Static methods, also known as class methods, are declared
using the static keyword.
• They belong to the class rather than to any specific instance of
the class.
• Static methods can be called using the class name without the
need to create an instance of the class.

19). Explain the use of runnable interface ?


Ans). java.lang.Runnable is an interface that is to be implemented by
a class whose instances are intended to be executed by a thread. There
are two ways to start a new Thread – Subclass Thread and implement
Runnable. There is no need of subclassing a Thread when a task can
be done by overriding only run() method of Runnable.
1)Create a Runnable implementer and implement the run() method.
2).Instantiate the Thread class and pass the implementer to the
Thread, Thread has a constructor which accepts Runnable instances.
3)Invoke start() of Thread instance, start internally calls run() of the
implementer. Invoking start() creates a new Thread that executes the
code written in run(). Calling run() directly doesn’t create and start a
new Thread, it will run in the same thread. To start a new line of
execution, call start() on the thread.

22). Explain bufferedreader class in java ?


Ans). The BufferedReader class of Java is used to read the stream of
characters from the specified source (character-input stream). The
constructor of this class accepts an InputStream object as a parameter.

This class provides a method named read() and readLine() which


reads and returns the character and next line from the source
(respectively) and returns them.
• Instantiate an InputStreamReader class bypassing your
InputStream object as a parameter.
• Then, create a BufferedReader, bypassing the above obtained
InputStreamReader object as a parameter.
• Now, read data from the current reader as String using the
readLine() or read() method.
21). Write in detail about applet skelton ?
Ans). An applet skeleton is a basic structure or template for creating
Java applets. It includes the essential methods and structure required
for an applet to run within a web browser. The primary class for
creating an applet is java.applet.Applet, and it must be extended to
create a custom applet.
Here are the key methods and their purposes in an applet
skeleton:
1).init(): This method is called when the applet is first loaded. It's
typically used for initializing variables, setting up the applet's initial
state, and performing any necessary setup.
2).start(): When the applet becomes visible or is restarted after
being paused, this method is called. It's often used for starting
animations or any other ongoing activities.
3).stop(): This method is called when the applet becomes invisible or
is paused. It's used to stop any ongoing activities or animations to
conserve resources.
4).destroy(): When the applet is no longer needed or the web page
is closed, this method is called. It's used for cleaning up resources and
performing any final tasks.
5).paint(Graphics g): This method is called whenever the applet
needs to be redrawn. It's where you define the applet's visual
appearance and display content.

4). What is JVM ?


Ans). A Java virtual machine (JVM), an implementation of the Java
Virtual Machine Specification, interprets compiled Java binary code
(called bytecode) for a computer's processor (or "hardware
platform") so that it can perform a Java program's instructions. Java
was designed to allow application programs to be built that could be
run on any platform without having to be rewritten or recompiled by
the programmer for each separate platform. A Java virtual machine
makes this possible because it is aware of the specific instruction
lengths and other particularities of the platform.

23), what is swing ? explain any five swing components


used in java ?
Ans ). Swing is a set of GUI (Graphical User Interface) components
for Java programs. It provides a rich set of controls and components
for building desktop applications with graphical user interfaces.
23), what is swing ? explain any five swing components
used in java ?
23), what is swing ? explain any five swing components
used in java ?
Ans ). Swing is a set of GUI (Graphical User Interface) components
for Java programs. It provides a rich set of controls and components
for building desktop applications with graphical user interfaces.
Swing is part of the Java Foundation Classes (JFC) and is built on top
of the Abstract Window Toolkit (AWT), providing more advanced and
customizable GUI components than AWT.

Here are five Swing components commonly used in Java:

1. JButton: is a simple button component that can display text,


an image, or both. It is used to trigger actions when clicked by
the user.

2. JTextField: is a component used for accepting user input


through text entry. It provides a single-line editable text field.

3. JLabel: is used to display text or an image on the GUI


without allowing the user to edit it. It is commonly used for
displaying static text or providing information to the user.

4. JComboBox: is a component that provides a drop-down list


of items from which the user can select one option. It is used
for selecting from a predefined set of choices.

5. JTextArea: is a multi-line text area component that allows


the user to enter and edit multiple lines of text. It is used for
accepting longer text input or displaying large amounts of text.

20). Write ant two access specifiers with suitable example


Ans). here are two access specifiers in Java along with examples:

1. Public:

• The public access specifier in Java allows a class, method,


or field to be accessed from anywhere in the Java
program, whether it is in the same package or a different
package.

• When a class, method, or field is declared as public, it


can be accessed by any other class in the program.
2. Private:
20). Write ant two access specifiers with suitable example
Ans). here are two access specifiers in Java along with examples:

3. Public:

• The public access specifier in Java allows a class, method,


or field to be accessed from anywhere in the Java
program, whether it is in the same package or a different
package.

• When a class, method, or field is declared as public, it


can be accessed by any other class in the program.

4. Private:

• The private access specifier in Java restricts the access of a class


member (field or method) to only within the same class. It
cannot be accessed from outside the class, not even from
subclasses.

• private members are encapsulated within the class and are


not visible to any other class, including subclasses

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy