Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Name :- DEEPAK SARSWAT

Roll No. :- 2114100601

Program :- MCA

Semester:- 3RD

Course code & Name :- DCA7102 – PROGRAMING IN JAVA

SET- 1

Ans . 1 (A) Java Architecture

Java Architecture is a collection of components, i.e., JVM, JRE, and JDK. It integrates the
process of interpretation and compilation. It defines all the processes involved in creating a
Java program. Java Architecture explains each and every step of how a program is compiled
and executed.

Components of Java Architecture

o Java Virtual Machine (JVM)


o Java Runtime Environment (JRE)
o Java Development Kit (JDK)

Java Virtual Machine :- The main feature of Java is WORA. WORA stands for Write
Once Run Anywhere. The feature states that we can write our code once and use it
anywhere or on any operating system. Our Java program can run any of the platforms only
because of the Java Virtual Machine. It is a Java platform component that gives us an
environment to execute java programs. JVM's main task is to convert byte code into machine
code. JVM, first of all, loads the code into memory and verifies it. After that, it executes the
code and provides a runtime environment.
Java Runtime Environment :- It provides an environment in which Java programs are
executed. JRE takes our Java code, integrates it with the required libraries, and then starts the
JVM to execute it.

Java Development Kit :- It is a software development environment used in the development


of Java applications and applets. Java Development Kit holds JRE, a compiler, an interpreter
or loader, and several development tools in it.

(B) Unicode :- Unicode is a universal character encoding standard. This standard includes
roughly 100000 characters to represent characters of different languages. While ASCII uses only
1 byte the Unicode uses 4 bytes to represent characters. Hence, it provides a very wide variety of
encoding. It has three types namely UTF-8, UTF-16, UTF-32. Among them, UTF-8 is used
mostly it is also the default encoding for many programming languages.

DATA TYPES IN JAVA:-

Primitive data types

In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and
double. Java developers included these data types to maintain the portability of java as the
size of these primitive data types do not change from one operating system to another.

Data Type Size/Format (bits) Description Range

Byte 8 Byte length integer -128 to 128 (for


signed) 0 to 255
(for unsigned)
Short 16 Short integer -2 15 to 2 15 -1

Int 32 Integer -2 31 to 2 31 -1

Long 64 Long integer -2 63 to 2 63 -1

Float 32 Single precision +/- about 1039


floating point
Double 64 Double precision +/- about 10317
floating point
Char 16 A single character Any single
character
boolean 1 A boolean value true or false

Abstract / Derived Data Types:-

Based on primitive data types, the Abstract Datatypes have more functionality in comparison
primitive data types. An example of Abstract datatypes is String, it can store digits, alphabets
and special characters such as /, (); :$#. Normal numerical operations cannot be performed on
a variable of String datatype even if the data stored in it has digits.
ANS.2 (A) The super keyword in java is a reference variable that is used to refer to parent
class objects. An understanding of Inheritance and Polymorphism is needed in order to
understand the super keyword. The keyword “super” came into the picture with the concept
of Inheritance. It is majorly used in the following contexts.
1. Use of super with variables
This scenario occurs when a derived class and base class has the same data members. In
that case, there is a possibility of ambiguity for the JVM.

// Java code to show use of super keyword with variables

// Base class vehicle

class Vehicle {

int maxSpeed = 120;

// sub class Car extending vehicle

class Car extends Vehicle {

int maxSpeed = 180;

void display()

// print maxSpeed of base class (vehicle)

System.out.println("Maximum Speed: "

+ super.maxSpeed);

// Driver Program

class Test {

public static void main(String[] args)


{

Car small = new Car();

small.display();

Output
Maximum Speed: 120
2. Use of super with methods
This is used when we want to call the parent class method. So whenever a parent and child
class have the same-named methods then to resolve ambiguity we use the super keyword.

// Java program to show use of super with methods

// superclass Person

class Person {

void message()

System.out.println("This is person class\n");

// Subclass Student

class Student extends Person {

void message()

System.out.println("This is student class");

// Note that display() is

// only in Student class

void display()
{

// will invoke or call current

// class message() method

message();

// will invoke or call parent

// class message() method

super.message();

// Driver Program

class Test {

public static void main(String args[])

Student s = new Student();

// calling display() of Student

s.display();

Output
This is student class
This is person class
3. Use of super with constructors
The super keyword can also be used to access the parent class constructor. One more
important thing is that „super‟ can call both parametric as well as non-parametric
constructors depending upon the situation.

// Java Code to show use of


// super keyword with constructor

// superclass Person

class Person {

Person()

System.out.println("Person class Constructor");

// subclass Student extending the Person class

class Student extends Person {

Student()

// invoke or call parent class constructor

super();

System.out.println("Student class Constructor");

// Driver Program

class Test {

public static void main(String[] args)

Student s = new Student();


}

Output
Person class Constructor
Student class Constructor
(B) Exception Handling :- The Exception Handling in Java is one of the
powerful mechanism to handle the runtime errors so that the normal flow of the application
can be maintained. Java creates an object of appropriate exception class whenever there a
error occurs in your method. The term exception in Java means the occurrence of an
exceptional event. It is defined as an abnormality that occurs while the program is in
execution which hinders the successful execution of the program, which means the way the
program is meant to produce the result it doesn‟t do so.
Try :- The "try" keyword is used to specify a block where we should place an exception code.
It means we can't use try block alone. The try block must be followed by either catch or
finally.
try
{
// statement that may cause an exception
}
Catch :- The "catch" block is used to handle the exception. It must be preceded by try block
which means we can't use catch block alone. It can be followed by finally block later.
try
{
//statements that may cause an exception
}
catch ()
{
// error handling code
}
ANS.3 Streams Basic :- Java handles all input and output in the form of streams. A stream
is a sequence of bytes traveling from a source to a destination over a communication path. If
a program writes into a stream, it is the stream‟s source. Similarly, if it is reading from a
stream, it is the stream‟s destination. Streams are powerful because they abstract the details of
input/output (I/O) operations.
The two basic streams used are the input and the output streams. Each stream has a particular
functionality. You can only read from an input stream and conversely, you can only write to
an output stream. Some streams are classified as mode streams because they read from or
write to a specific place like a disk file or memory. Some streams are classified as filters.
Filters are used to read data from one stream and write it to another stream. The stream
classes of java.io packages are categorized into two groups based on the data type on which
they operate:
• Byte Stream: Provide support for handling I/O on bytes.
• Character stream: Provide support for handling I/O on characters.
The two major classes for byte streams are InputStream and OutputStream. These abstract
classes are sub-classed to provide a variety of I/O capabilities. The InputStream class lays the
foundation for the input class hierarchy, whereas the OutputStream class lays the foundation
for the output class hierarchy
Methods of the InputStream Class
Method Explanation
int read () Reads a byte of data from the input stream.
int read (byte [] b) Reads bytes of data from the input stream
and stores them in the array.
int read (byte [] b, int off, int len) Reads the number of bytes specified by the
third argument from the input stream and
stores it in the array. The second argument
specifies the position from where the bytes
have to be read.
int available () Returns the number of bytes available for
reading from the input stream.
void close () Close an input stream and releases all the
resources associated with it.

Methods of the DataInputStream class


Method Explanation
boolean readBoolean () Reads one byte and returns true if that byte is
nonzero, false if it is zero.
byte readByte () Reads a byte as an 8-bit signed value.
char readChar () Reads a Unicode character.
int readInt () Reads an integer value.

Methods of the OutputStream Class

Method Explanation
void write (int n) Writes the specified byte of data to the output
stream
void write (byte [] b ) Writes an array of bytes to the output stream.
void write (byte [] b, int off, int len ) Writes a segment of an array to the output
streams.
void flush () Force writes whenever the data accumulates
in the output stream
void close () Cause the output stream to close. Any data
present on it is stored before the stream is de-
allocated from the memory

Methods of the DataOutputStream Class

Method Explanation
void writeChar (int v ) Writes a character to the output stream.
void writeLong (long v) Writes a long integer to the output stream.
void writeInt (int v ) Writes an integer to the output stream.

SET -2

ANS.1 (A) the collection hierarchy :- The hierarchy of the entire collection framework
consists of four core interfaces such as Collection, List, Set, Map, and two specialized
interfaces named SortedSet and SortedMap for sorting. All the interfaces and classes for the
collection framework are located in java.util package .

• Extends: The keyword extends used to create inheritance between two classes and two
interfaces

• Implements: The term implements are used to create inheritance across classes and
interfaces.

The Collection interface, which is the root interface of all archives in the API, is the most
fundamental interface of the collections system (Application programming interface). In Java,
this is at the apex of the collection hierarchy. It allows you to add and remove elements from
the Collection using basic methods. The Iterable interface is augmented by the Collection
interface. There is only one procedure in the iterable interface called Iterator (). The iterator
method's purpose is to retrieve the iterator object. We may loop over the components of the
array that use this iterator object.

1. List Interface

2. Set Interface

3. SortedSets Interface.

4. Queue Interface

5. Deque Interface

6. Map Interface

7. Sorted Map Interface

(B) Java Annotation :- Java Annotation is a tag that represents the metadata i.e. attached
with class, interface, methods or fields to indicate some additional information which can be
used by java compiler and JVM. Annotations in Java are used to provide additional
information, so it is an alternative option for XML and Java marker interfaces.

Java Custom Annotation

Java Custom annotations or Java User-defined annotations are easy to create and use. The
@interface element is used to declare an annotation.

For example: @interface MyAnnotation{}

There are few points that should be remembered by the programmer.

I. Method should not have any throws clauses


II. Method should return one of the following: primitive data types, String, Class,
enum or array of these data types.
III. Method should not have any parameter.
IV. We should attach @ just before interface keyword to define annotation.
V. It may assign a default value to the method.

Types of Annotation

1. Marker Annotation

2. Single-Value Annotation

3. Multi-Value Annotation

Marker Annotation :- An annotation that has no method, is called marker annotation. For
example: @interface MyAnnotation{}

The @Override and @Deprecated are marker annotations


Single-Value Annotation :- An annotation that has one method, is called single-value
annotation. For example:

@interface MyAnnotation{

int value();

We can provide the default value also. For example:

@interface MyAnnotation{

int value() default 0;

Let's see the code to apply the single value annotation.

@MyAnnotation(value=10)

The value can be anything.

Multi-Value Annotation :- An annotation that has more than one method, is called Multi-
Value annotation. For example:

@interface MyAnnotation {

int value1();

String value2();

String value3();

We can provide the default value also. For example:

@interface MyAnnotation {

int value1() default 1;

String value2() default "";

String value3() default "xyz";

Let's see the code to apply the multi-value annotation.


@MyAnnotation(value1=10,value2="ArunKumar",value3="Ghaziabad")

ANS.2 (A) component classes of swing :-

 JAppletapi :- Implements a Java applet.


 JButtonapi :- Implements a button component.
 JCheckBoxapi :- Implements a check-box component.
 JColor-Chooser :- Displays and manages a color-chooser dialog.
 JComboBoxapi :- Implements a combo-box component.

import javax.swing.*;

public class FirstSwingExample {

public static void main (String[] args) {

JFrame f = new JFrame();

JButton b = new JButton("Click");

b.setBounds(130,100,100,40);

f.add(b);

f.setSize(400,500);

f.setLayout(null);

f.setVisible(true);

(B). multithreading :- Java is a multi-threaded programming language which means we can


develop multi-threaded program using Java. A multi-threaded program contains two or more
parts that can run concurrently and each part can handle a different task at the same time
making optimal use of the available resources specially when your computer has multiple
CPUs. By definition, multitasking is when multiple processes share common processing
resources such as a CPU. Multi-threading extends the idea of multitasking into applications
where you can subdivide specific operations within a single application into individual
threads. Each of the threads can run in parallel. The OS divides processing time not only
among different applications, but also among each thread within an application. Multi-
threading enables you to write in a way where multiple activities can proceed concurrently in
the same program.

Life Cycle of a Thread :- A thread goes through various stages in its life cycle. For example,
a thread is born, started, runs, and then dies. The following diagram shows the complete life
cycle of a thread.
 New − A new thread begins its life cycle in the new state. It remains in this state until
the program starts the thread. It is also referred to as a born thread.
 Runnable − After a newly born thread is started, the thread becomes runnable. A
thread in this state is considered to be executing its task.
 Waiting − Sometimes, a thread transitions to the waiting state while the thread waits
for another thread to perform a task. A thread transitions back to the runnable state
only when another thread signals the waiting thread to continue executing.
 Timed Waiting − A runnable thread can enter the timed waiting state for a specified
interval of time. A thread in this state transitions back to the runnable state when that
time interval expires or when the event it is waiting for occurs.
 Terminated (Dead) − A runnable thread enters the terminated state when it completes
its task or otherwise terminates.

ANS.3 :- Java Server Pages (JSP) :- Java Server Pages (JSP) is a programming tool on the
application server side that supports platform-independent and dynamic methods to
construct Web-based applications. Much as Servlet technology does, the JSP method
provides a web application. It can be considered an expansion of Servlet because it offers
more features than servlet. Since we can differentiate design and development, the JSP
pages are simpler to manage than Servlet. HTML tags and JSP tags are present in Java
Server Pages. To access enterprise servers, Java Server Pages has an approach to the
entire community of Java APIs, including the JDBC API. This tutorial will walk you to
the path of building your own web application in convenient and simple steps using Java
Server Pages.

How does JSP look? Before considering more technical details regarding JSP, let us see
how a JSP file looks and what its static parts are and what its dynamics parts are:

Consider the HelloWorld.jsp below:

1. <HTML>

2. <HEAD>

3. <META NAME=”GENERATOR “ Content = “Microsoft Visual Studio

6.0”>

4. <TITLE></TITLE>

5. </HEAD>

6. <BODY>

7. <center><h1>
8. <% out.println(“Hello World!”); %>

9. </h1><center>

10. </BODY>

11. </HTML>

As you can see a JSP page looks almost like a simple HTML file. In the above
HelloWorld.jsp line number eight is the one accounting for the dynamic content. All the
other lines are static HTML content. If you observe line eight carefully, you can notice
that the written code resembles servlet code in some way. Once a JSP is written next
question is how do you test it? To test JSP we are going to use Java Web Server. The steps
are as following:

• Copy the HelloWorld.jsp to c:\JavaWebServer2.0\public_html directoryas it is the


document root of the Java Web Server. (Assuming that the Java Web Server is ins talled in
c:\)

• Now start the Java Web Server‟s web service.

• Open your browser window.

• Now in the address bar type in an address as http://IPAddress:: 8080/HelloWorld.jsp,


where IP address is the IP address of your machine

You might also like