Java Unit-1 Assignment Answers
Java Unit-1 Assignment Answers
Abstraction. Using simple things to represent complexity. We all know how to turn the TV on, but we
don’t need to know how it works in order to enjoy it. In Java, abstraction means simple things like
objects, classes and variables represent more complex underlying code and data. This is important
because it lets you avoid repeating the same work multiple times.
Encapsulation. The practice of keeping fields within a class private, then providing access to those
fields via public methods. Encapsulation is a protective barrier that keeps the data and code safe
within the class itself. We can then reuse objects like code components or variables without allowing
open access to the data system-wide.
Polymorphism. Allows programmers to use the same word in Java to mean different things in
different contexts. One form of polymorphism is method overloading. That’s when the code itself
implies different meanings. The other form is method overriding. That’s when the values of the
supplied variables imply different meanings.
Ans : Java Virtual Machine (JVM) is a engine that provides runtime environment to drive the Java Code or
applications. It converts Java bytecode into machines language. JVM is a part of Java Runtime Environment
(JRE). In other programming languages, the compiler produces machine code for a particular system.
However, Java compiler produces code for a Virtual Machine known as Java Virtual Machine.
1) ClassLoader
The class loader is a subsystem used for loading class files. It performs three major functions viz. Loading,
Linking, and Initialization.
2) Method Area
JVM Method Area stores class structures like metadata, the constant runtime pool, and the code for
methods.
3) Heap
All the Objects, their related instance variables, and arrays are stored in the heap. This memory is common
and shared across multiple threads.
4) JVM language Stacks
Java language Stacks store local variables, and it’s partial results. Each thread has its own JVM stack, created
simultaneously as the thread is created. A new frame is created whenever a method is invoked, and it is
deleted when method invocation process is complete.
5) PC Registers
PC register store the address of the Java virtual machine instruction which is currently executing. In Java,
each thread has its separate PC register.
Native method stacks hold the instruction of native code depends on the native library. It is written in
another language instead of Java.
7) Execution Engine
It is a type of software used to test hardware, software, or complete systems. The test execution engine
never carries any information about the tested product.
The Native Method Interface is a programming framework. It allows Java code which is running in a JVM to
call by libraries and native applications.
Native Libraries is a collection of the Native Libraries(C, C++) which are needed by the Execution Engine.
1) Simple
Java is easy to learn and its syntax is quite simple, clean and easy to understand.The confusing and
ambiguous concepts of C++ are either left out in Java or they have been re-implemented in a cleaner
way.
2) Object Oriented
In java, everything is an object which has some data and behaviour. Java can be easily extended as it
is based on Object Model. Following are some basic concept of OOP’s.
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation
3) Robust
Java makes an effort to eliminate error prone codes by emphasizing mainly on compile time error
checking and runtime checking. But the main areas which Java improved were Memory
Management and mishandled Exceptions by introducing automatic Garbage Collector and Exception
Handling.
4) Platform Independent
Unlike other programming languages such as C, C++ etc which are compiled into platform specific
machines. Java is guaranteed to be write-once, run-anywhere language. On compilation Java
program is compiled into bytecode. This bytecode is platform independent and can be run on any
machine.
5) Secure
When it comes to security, Java is always the first choice. With java secure features it enable us to
develop virus free, temper free system. Java program always runs in Java runtime environment with
almost null interaction with system OS, hence it is more secure.
6) Multi Threading
Java multithreading feature makes it possible to write program that can do many tasks
simultaneously. Benefit of multithreading is that it utilizes same memory and other resources to
execute multiple threads at the same time, like While typing, grammatical errors are checked along.
7) Architectural Neutral
Compiler generates bytecodes, which have nothing to do with a particular computer architecture,
hence a Java program is easy to intrepret on any machine.
8) Portable
Java Byte code can be carried to any platform. No implementation dependent features. Everything
related to storage is predefined, example: size of primitive data types
9) High Performance
Java is an interpreted language, so it will never be as fast as a compiled language like C or C++. But,
Java enables high performance with the use of just-in-time compiler.
10) Distributed
Java is also a distributed language. Programs can be designed to run on computer networks. Java has
a special class library for communicating using TCP/IP protocols. Creating network connections is
very much easy in Java as compared to C/C++.
4. Difference between C and JAVA.
Ans :
C Java
C is a Procedural Programming
Java is Object-Oriented language.
Language.
C is a middle-level language
Java is a high-level language because
because binding of the gaps takes
translation of code takes place into
place between machine level
machine language using compiler or
language and high-level
interpreter.
languages.
Ans :
C++ Java
Compiled directly into machine code Compiled into bytecode and then interpreted
by JVM
Syntax is closer to low-level languages like Syntax is closer to high-level languages like
Assembly and C Python and Ruby
Faster performance due to direct machine code Slower performance due to bytecode
execution interpretation
Standard Template Library (STL) provides robust Java provides collection framework for data
and efficient implementation of data structures structures, but not as efficient as STL in C++
Ans : "finalize" is a method in java that is defined in the Object class and can be overridden by
subclasses. The finalize method in java is called by the garbage collector before an object is garbage
collected. This method can be used to perform any necessary cleanup before the object is destroyed,
such as releasing resources or detaching event listeners. However, it’s important to note that the
garbage collector is not guaranteed to call the finalize method in java and it’s not guaranteed to call
it only once.
The finalize() method in Java serves the purpose of performing cleanup operations before an object
is garbage collected. It can be used to release resources, stop background threads, detach event
listeners, and perform other types of cleanup. However, relying on the finalize() method for resource
release is not recommended due to its unpredictable invocation and potential performance issues. It
is best practice to use try-finally or try-catch-resources constructs for timely and reliable resource
cleanup.
Ans : The this keyword refers to the current object in a method or constructor.
The most common use of the this keyword is to eliminate the confusion between class attributes and
parameters with the same name (because a class attribute is shadowed by a method or constructor
parameter). If you omit the keyword in the example above, the output would be "0" instead of "5".
Ans : In Java, a Constructor is a block of codes similar to the method. It is called when an instance of
the class is created. At the time of calling the constructor, memory for the object is allocated in the
memory. It is a special type of method that is used to initialize the object. Every time an object is
created using the new() keyword, at least one constructor is called.
Primarily there are three types of constructors in Java are mentioned below:
1. Default Constructor
A constructor that has no parameters is known as default the constructor.
2. Parameterized Constructor
A constructor that has parameters is known as parameterized constructor.
3. Copy Constructor
Unlike other constructors copy constructor is passed with another object which copies the data
available from the passed object to the newly created object.