0% found this document useful (0 votes)
8 views5 pages

corejava503

Uploaded by

asmitadavrung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
8 views5 pages

corejava503

Uploaded by

asmitadavrung
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Assignment No 1

1. Explain JVM and Components of JVM?

Java Virtual Machine (JVM) is a virtual machine that enables computers to run Java
programs and programs written in other languages that are also compiled to Java bytecode.
JVM performs three main tasks:

1. Loads code: Loads compiled Java bytecode from .class files.


2. Verifies code: Ensures the loaded bytecode does not violate Java's security
constraints.
3. Executes code: Interprets or just-in-time compiles the bytecode into native machine
code and executes it.

Components of JVM:

• Class Loader: Responsible for loading class files. It verifies the correctness of the
class file and loads it into the JVM memory.
• Method Area: Stores class structures like metadata, the constant runtime pool, and
the code for methods.
• Heap: Runtime data area where objects are allocated.
• Stack: Stores local variables and partial results, and plays a part in method invocation
and return.
• Program Counter Register: Contains the address of the Java virtual machine
instruction currently being executed.
• Native Method Stack: Contains all the native methods used in the application.
• Execution Engine: Executes the instructions contained in the methods of loaded
classes. It contains:
o Interpreter: Reads and executes bytecode instructions one at a time.
o Just-In-Time (JIT) Compiler: Compiles bytecode into native machine code
for better performance.
o Garbage Collector: Automatically manages memory by reclaiming memory
used by objects no longer referenced.

2. What is an Interface in JAVA?

An interface in Java is a blueprint of a class. It has static constants and abstract


methods.

The interface in Java is a mechanism to achieve abstraction. There can be only


abstract methods in the Java interface, not method body. It is used to achieve
abstraction and multiple inheritance in Java.

In other words, you can say that interfaces can have abstract methods and
variables. It cannot have a method body.

o Java Interface also represents the IS-A relationship.


o It cannot be instantiated just like the abstract class.
o Since Java 8, we can have default and static methods in an interface.
o Since Java 9, we can have private methods in an interface.

java
Copy code
interface Animal {
void eat();
void sleep();
}

3. What are the access specifiers in JAVA?

Java provides four access specifiers (or access modifiers) that set the accessibility of classes,
methods, and other members:

• public: The member is accessible from any other class.


• protected: The member is accessible within its own package and by subclasses.
• default (no specifier): The member is accessible only within its own package.
• private: The member is accessible only within its own class.

4. What is a Package?

A package in Java is a namespace that organizes classes and interfaces by functionality.


Using packages, developers can avoid class name conflicts and control access to classes,
making code maintenance easier. Packages can be built-in (from the Java API) or user-
defined.

java
Copy code
package com.example.project;
public class MyClass {
// Class code here
}

5. What is an Abstract Class?

An abstract class in Java is a class that cannot be instantiated on its own and is meant to
be subclassed. Abstract classes may contain abstract methods, which are methods
without a body. Subclasses of an abstract class must provide implementations for all
abstract methods.
Points to Remember
o An abstract class must be declared with an abstract keyword.
o It can have abstract and non-abstract methods.
o It cannot be instantiated.
o It can have constructors and static methods also.
o It can have final methods which will force the subclass not to change
the body of the method.

java
Copy code
abstract class Animal {
abstract void makeSound();

void sleep() {
System.out.println("Sleeping");
}
}

6. Explain the Structure of a Java Program.

A typical Java program consists of the following structure:

1. Package declaration: Specifies the package name.


2. Import statements: Imports other Java classes or entire packages.
3. Class declaration: Defines the class and contains fields, methods, and constructors.
4. Main method: Entry point of the application where the program execution begins.

java
Copy code
package com.example;

import java.util.Scanner;

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Assignment No 2

1. What is a Constructor? Explain Constructor Overloading.

A constructor in Java is a block of code used to initialize an object. It is called when an


instance of a class is created. Constructors have the same name as the class and do not have a
return type.

Constructor Overloading: The process of defining multiple constructors of the


same class is referred to as Constructor overloading. However, each constructor
should have a different signature or input parameters. In other words,
constructor overloading in Java is a technique that enables a single class to have
more than one constructor that varies by the list of arguments passed. Each
overloaded constructor is used to perform different task in the class.

The Java compiler identifies the overloaded constructors on the basis of their
parameter lists, parameter types and the number of input parameters. Hence, the
constructors that are overloaded should have different signatures. A
constructor’s signature contains its name and parameter types. An ambiguity
issue arises when two of the class constructors have an identical signature.

java
Copy code
class MyClass {
int x;

MyClass() {
x = 0;
}

MyClass(int a) {
x = a;
}
}

2. Difference between Class and Object.

Class Object

A class is said to be a blue print for the


An instance of class is known of an object
objects

Class is a user defined data type Object is a variable of a class

It contains data members and member Data and methods of a class can be
functions accessed by these objects

A class can have a number of objects An object is bound to only a single class

Memory is allocated every time when the


No memory is allocated for a class
object is created

It is a logical entity It is a physical entity

A class can be created only once Objects can be created number of times

They can’t be manipulated as it is not


Objects can be manipulated
associated with any memory

It doesn’t have values bound to it Objects have their own values


A class is created at the beginning of a Objects can be created anywhere
program throughout the program

3. Explain the Concept of Garbage Collection.

Garbage Collection (GC) in Java is the process of automatically identifying and discarding
objects that are no longer needed to free up memory. The JVM’s garbage collector reclaims
memory, improving efficiency and preventing memory leaks. It works by tracking object
references and removing those that are unreachable.
Java garbage collection is an automatic process. Automatic garbage
collection is the process of looking at heap memory, identifying which
objects are in use and which are not, and deleting the unused objects. An
in-use object, or a referenced object, means that some part of your
program still maintains a pointer to that object. An unused or
unreferenced object is no longer referenced by any part of your program.
So the memory used by an unreferenced object can be reclaimed. The
programmer does not need to mark objects to be deleted explicitly. The
garbage collection implementation lives in the JVM.

4. Write a Java Program to Find Factorial of a Number Using Recursion.

java
Copy code
public class Factorial {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}

public static void main(String[] args) {


int number = 5; // Example number
int result = factorial(number);
System.out.println("Factorial of " + number + " is " + result);
}
}

You might also like