java programming
java programming
1. Ans: The Java compiler (javac) converts Java source code into platform-independent
bytecode.
2. The generated bytecode can run on any system with a compatible Java Virtual Machine
(JVM).
3. The compiler checks for syntax errors and ensures type safety during code translation.
5. The Java interpreter, part of the JVM, executes the bytecode instruction by instruction.
6. It enables Java's "write once, run anywhere" philosophy by abstracting hardware details.
7. The JVM may use a Just-In-Time (JIT) compiler at runtime to convert bytecode to native code.
8. The interpreter handles memory management tasks like garbage collection during execution.
10. Together, the compiler and interpreter create a development environment that balances
portability and performance.
Ans:An identifier is a name used to uniquely identify variables, functions, classes, or other program
entities.
There may be a maximum length limit for identifiers imposed by the language.
Ans: A constructor is a special method used to initialize objects of a class, having the same name as
the class and no return type. It is automatically called when an object is created to assign initial
values to the object's field
characterstics: It has the same name as the class and does not have a return type.
Constructors set the initial state of an object by assigning values to its fields.
They cannot be inherited by subclasses but can be called within them using super().
Constructor overloading occurs when a class has more than one constructor with different
parameter lists.
Each constructor must have a unique signature, which differs in the number or type of parameters.
The appropriate constructor is called based on the arguments passed during object creation.
String name;
int age;
Person() {
name = "Unknown";
age = 0;
this.name = name;
this.age = age; } }
Q6: Write a short note on garbage collection in Java
1. Ans: Garbage Collection in Java is the automatic process of reclaiming memory from unused
objects.
3. The process works on objects allocated in the heap memory that are no longer referenced.
4. The Garbage Collector (GC) identifies and removes objects that are unreachable from the
program.
5. An object is considered garbage if it’s no longer reachable by any active part of the program.
6. Objects are managed in generations (Young, Old) to optimize garbage collection efficiency.
7. Minor GC occurs quickly for young generation objects, while Major GC is slower and deals
with old generation objects.
8. Objects can override the finalize() method, which is called before garbage collection to
release resources.
9. Developers can suggest garbage collection using System.gc(), but it’s not guaranteed to run.
10. Frequent garbage collection can impact performance, particularly during major collections.
Q7: Draw and explain different types of inheritance with simple diagram
Ans: Single inheritance is when a class inherits from only one parent class.
This setup allows the child class to use the properties and methods of its single parent, making the
code easier to manage.
Multi-level inheritance involves a class inheriting from a derived class, creating a chain of
inheritance.This structure allows for a hierarchy of classes, where each class can build upon the
features of its parent class and extend its functionality to its own subclasses.
Multiple inheritance allows a class to inherit features and behaviors from more than one parent
class.This means a derived class can combine properties from multiple sources, providing more
flexibility in design.
Hierarchical inheritance occurs when multiple child classes inherit from a single-parent class.This
structure allows different child classes to share the properties and methods of the same parent,
promoting code reuse and organization.
Hybrid inheritance combines different inheritance types, like single, multiple, and hierarchical,
within one structure.This allows you to use various features and functionalities from multiple parent
classes, creating a versatile and reusable codebase.
Q8: Write and explain multilevel inheritance with suitable example
Ans: Inheritance Chain: Multilevel inheritance forms a chain where a subclass inherits from a
superclass, and another subclass inherits from it.
Access to Multiple Superclasses: The subclass inherits properties and methods from both its
immediate superclass and the grandparent class.
Hierarchical Structure: It creates a hierarchical structure where each subclass inherits from the
class above it. Method and Property Inheritance: Subclasses inherit methods and properties, and
can override or extend them if needed.
Improves Code Reusability: Common functionality is inherited, promoting code reuse and
reducing redundancy. Potential for Method Overriding: Subclasses can override inherited methods
to provide specific functionality at different levels
Q9: What is base class and derived class? Explain how are derived class object is created
Base Class (Parent Class):A base class is the class from which other classes (derived classes) inherit
properties and methods.It contains common functionality that can be shared across multiple derived
classes.
Derived Class (Child Class):A derived class is a class that inherits from a base class.It can access the
properties and methods of the base class and may also add its own properties or methods or
override inherited ones.
A derived class inherits the features (fields and methods) of the base class.
When an object of the derived class is created, the constructor of the base class is called first
(implicitly or explicitly) to initialize the properties inherited from it.
You can create an object of the derived class using the new keyword, just like creating an object of a
base class.
An abstract class is a class that cannot be instantiated directly and is meant to be subclassed by
other classes. It may contain abstract methods (methods without implementation) as well as non-
abstract methods (methods with implementation).
CHARACTERSTICS: An abstract class cannot be instantiated directly using the new keyword.
It can have abstract methods, which are methods without a body and must be implemented by
subclasses.
Abstract classes can also have concrete (non-abstract) methods with implementations.
Abstract classes can have constructors, which are invoked when objects of subclasses are created.
They can have member variables, like any other class, with access modifiers (private, public, etc.).
A subclass must implement all abstract methods from an abstract class unless the subclass is also
abstract