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

java programming

The document provides an overview of key concepts in Java, including the roles of the Java compiler and interpreter, rules for creating identifiers, characteristics of constructors, and types of inheritance. It also discusses garbage collection, the distinction between base and derived classes, and the nature of abstract classes. Additionally, it touches on constructor overloading and the differences between interfaces and abstract classes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

java programming

The document provides an overview of key concepts in Java, including the roles of the Java compiler and interpreter, rules for creating identifiers, characteristics of constructors, and types of inheritance. It also discusses garbage collection, the distinction between base and derived classes, and the nature of abstract classes. Additionally, it touches on constructor overloading and the differences between interfaces and abstract classes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Q1:Write a short note on java compiler and java interpreter

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.

4. It performs optimizations to improve the efficiency of the resulting bytecode.

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.

9. It manages runtime exceptions and errors to maintain program stability.

10. Together, the compiler and interpreter create a development environment that balances
portability and performance.

Q2: What is an identifier write the rules for creating identifiers

Ans:An identifier is a name used to uniquely identify variables, functions, classes, or other program
entities.

It must start with a letter (a–z or A–Z) or an underscore (_).

Subsequent characters can include letters, digits (0–9), or underscores

Identifiers cannot begin with a digit.

Special symbols (like @, #, $, %) are not allowed in identifiers.

Identifiers must not contain any spaces.

They are case-sensitive in many programming languages.

Identifiers cannot be the same as reserved keywords in the language.

There may be a maximum length limit for identifiers imposed by the language.

It is good practice to choose descriptive names to improve code readability.

Q3: Explain different types of variable in java


Q4:What is constructor write it's characteristic

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 are automatically invoked when an object is created.

They can be overloaded to provide different ways of initializing an object.

If no constructor is defined, the compiler provides a default constructor.

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().

Constructors can include logic to validate or transform initialization parameters.

Q5: Explain constructor overloading with suitable example

Constructor overloading occurs when a class has more than one constructor with different
parameter lists.

It allows creating objects with different initializations by passing varying arguments.

Each constructor must have a unique signature, which differs in the number or type of parameters.

Overloaded constructors help to provide flexibility in object creation.

The appropriate constructor is called based on the arguments passed during object creation.

Example: class Person {

String name;

int age;

Person() {

name = "Unknown";

age = 0;

Person(String name, int age) {

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.

2. It handles memory management without requiring manual memory deallocation by the


developer.

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.

Creation of derived class

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.

Q10: explain is abstract class and state its characterstics

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 provide partial implementation, leaving some methods to be implemented by


subclasses.

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

.Q11: explain difference between interface and abstract class

You might also like