ORIENTAL INSTITUTE OF SCIENCE & TECHNOLOGY, BHOPAL



DEPARTMENT OF INFORMATION TECHNOLOGY

LAB MANUAL

Name:

Course Code: IT-506

Course: JAVA TECHNOLOGY

Session:

Prepared By

List of Programs

|Experiment No. |Aim |Date of Submission |Signature & Remarks |

| |Installation of J2SDK | | |

| |Write a program to show Scope of Variables. | | |

| |Write a program to show Concept of CLASS in JAVA. | | |

| |Write a program to show How Exception Handling is in JAVA. | | |

| |Write a program to show Polymorphism. | | |

| |Write a program to show use and Advantages of CONTRUCTOR. | | |

| |Write a program to show Interfacing between two classes. | | |

| |Write a program to Add a Class to a Package. | | |

| |Write a program to show Life Cycle of a Thread. | | |

| |Write a program to demonstrate AWT. | | |

| |Write a Program to show Data Base connectivity Using JAVA. | | |

| |Write a Program to show “HELLO JAVA ” in Explorer using Applet | | |

| |Write a Program to show Connectivity using JDBC. | | |

| |Write a program to demonstrate multithreading using Java. | | |

| |Write a program to demonstrate applet life cycle. | | |

Experiment-1

1. Objective: Installation of J2SDK.

2. Algorithm:

Steps for Installation:

Download and install Java for Windows computer?

This article applies to:

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0

The procedure to install Java broadly consists of:

1. Download and Install

2. Test Installation

Windows System Requirements 

Note: Installing Java requires that you can gain administrator access to Windows on your computer. 

Download and Install

It is recommended; before you proceed with online installation you may want to disable your Internet firewall. In some cases the default firewall settings are set to reject all automatic or online installations such as the Java online installation. If the firewall is not configured appropriately it may stall the download/install operation of Java under certain conditions. Refer to your specific Internet firewall manual for instructions on how to disable your Internet Firewall. 

• Go to the Manual download page

• Click on Windows Online

• The File Download dialog box appears prompting you to run or save the download file 

• To run the installer, click Run.

• To save the file for later installation, click Save. 

Choose the folder location and save the file to your local system. 

Tip: Save the file to a known location on your computer, for example, to your desktop.

Double-click on the saved file to start the installation process.

• The installation process starts. Click the Install button to accept the license terms and to continue with the installation. 

[pic]

• Oracle has partnered with companies that offer various products. The installer may present you with option to install these programs when you install Java. After ensuring that the desired programs are selected, click the Next button to continue the installation. 

[pic]

• A few brief dialogs confirm the last steps of the installation process; click Close on the last dialog. This will complete Java installation process. 

[pic]

3. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

Experiment-2

1. Objective: Write a program to show Scope of Variables.

2. Explanation:-

Types of Variable

| |

|There are three types of variables in java |

|local variable |

|instance variable |

|static variable |

| |

|Local Variable: A variable that is declared inside the method is called local variable. |

| |

| |

|Instance Variable: A variable that is declared inside the class but outside the method is called instance variable. It is not declared |

|as static. |

| |

| |

|Static variable: A variable that is declared as static is called static variable. It cannot be local. |

| |

| |

| |

| |

3. Algorithm:

1. Create a class A

2. Declare instance variable and initialize it.

3. Declare static variable and initialize it.

4. Declare a local variable within a method.

5. Print all the variables value.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6.Output:-

Experiment-3

1. Objective: Write a program to show Concept of CLASS in JAVA.

2. Explanation-program to find sum of two numbers using class and object.

3. Algorithm:-

Step 1: Start

Step 2: Define a class sum

Step 3: Define methods getdata and sum

Step 4: Define clsmain

Step 5: Read the two numbers.

Step 6: Call getdata and sum

Step 7: Print the sum

Step 8: Stop

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:

6. Output:

Experiment-4

1. Objective: Write a program to show How Exception Handling is in JAVA.

2. Explanation:- The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the runtime errors so that normal flow of the application can be maintained. In this page, we will know about exception, its type and the difference between checked and unchecked exceptions.

Types of Exception:

|There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. The sun micro system |

|says there are three types of exceptions: |

|Checked Exception |

|Unchecked Exception |

|Error |

Five keywords used in Exception handling:

|try |

|catch |

|finally |

|throw |

|throws |

3. Algorithm:-

1. Create class Excep.

2. create the main class

3. Write the statement within the try block which can generate exception.

4. Create the catch block to handle the exception and also pass the arguments for the Exception.

5. Use finally at the last.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:

6. Output:

Experiment-5

1. Objective: Write a program to show Polymorphism.

2. Explanation:- Runtime polymorphism or Dynamic Method Dispatch is a process in which a call to an overridden method is resolved at runtime rather than compile-time. In this process, an overridden method is called through the reference variable of a superclass. The determination of the method to be called is based on the object being referred to by the reference variable. Let's first understand the upcasting before Runtime Polymorphism.

3. Algorithm:

1. Start the program.

2. Create an abstract class with class name Shape.

3. Create a constructor with arguments and declare variables dim1, dim2 and PI.

4. Declare an abstract method area() inside the class.

5. Create the classes Rectangle, Triangle, Circle, and Ellipse to find the area.

6. Define abstract method area() inside the subclasses and call the constructor of class Shape using super keyword.

7. In main(), create the objects for all classes and pass values to fields of constructors.

8. Create a reference variable figuref for abstract class.

9. Using reference variable of class Shape, call the method area() of all subclasses

10. Print the areas for all shapes.

11. Stop the program

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5 Coding:

6. OutPut:

Experiment - 6

1. Objective: - Write a program to show use and Advantages of CONTRUCTOR.

2. Explanation: - Constructor is a special type of method that is used to initialize the object. Constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.

Rules for creating constructor

There are basically two rules defined for the constructor.

1. Constructor name must be same as its class name

2. Constructor must have no explicit return type

Types of constructors

There are two types of constructors:

1. default constructor (no-arg constructor)

2. parameterized constructor

3. Algorithm: -

1: Start the program.

2: Create a class with a class name Complex Number.

3: Create a constructor with the arguments a and b with integer data type.

4: Define a method getcomplexvalue( ) to get the values of a and b.

5: Define static method named as addition, subtraction, multiplication to perform

particular function defined inside the method.

6: Create an object for the class Complex Number an d pass the values in the

argument list.

7: Call the method by using object to get the values.

8: Print the result.

9: Stop the program.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output:

Experiment - 7

1. Objective: Write a program to show Interfacing between two classes.

2. Explanation: An interface is a blueprint of a class. It has static constants and abstract methods. The interface is a mechanism to achieve fully abstraction in java. There can be only abstract methods in the interface. It is used to achieve fully abstraction and multiple inheritances in Java. Interface also represents IS-A relationship. It cannot be instantiated just like abstract class.

There are mainly three reasons to use interface. They are given below.

• It is used to achieve fully abstraction.

• By interface, we can support the functionality of multiple inheritance.

• It can be used to achieve loose coupling.

3. Algorithm:-

1. Create an interface

2. Create class

3. Implements interface in class

4. Create main method

5. Use the methods of interface and implement it in class.

6. Create the reference of interface and initialize it with object of class.

7. Call the overridden methods in main method using reference.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

Output:

Experiment-8

1. Objective: Write a program to Add a Class to a Package.

2. Explanation: A package is a group of similar types of classes, interfaces and sub- packages. Package can be categorized in two form, built-in package and user-defined package. There are many built-in packages such as java, lang, awt, javax, swing, net, io, util, sql etc. Here, we will have the detailed learning of creating and using user-defined packages.

Advantage of Package

Package is used to categorize the classes and interfaces so that they can be easily maintained.

• Package provids access protection.

• Package removes naming collision.

3. Algorithm:-

1. Package keyword is used to create a package.

2. Create class

3. Compile the Package (if not using IDE):-

If you are not using any IDE, you need to follow the syntax given below:

javac -d directory javafilename  

For example

javac -d . Simple.java  

The -d switch specifies the destination where to put the generated class file. You can use any directory name like /home (in case of Linux), d:/abc (in case of windows) etc. If you want to keep the package within the same directory, you can use. (dot).

3. How to run the Package (if not using IDE)-:

You need to use fully qualified name e.g. mypack.Simple etc to run the class.

| To Compile: javac -d . Simple.java |

| To Run: java mypack.Simple |

Output: Welcome to package

|The -d is a switch that tells the compiler where to put the class file i.e. it represents destination. The represents the current |

|folder. |

4. Access package from another package?

|There are three ways to access the package from outside the package. |

|import package.*; |

|import package.classname; |

|fully qualified name. |

3. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output:-

Experiment-9

1. Objective: Write a program to show Life Cycle of a Thread.

2. Explanation:-

|Multithreading is a process of executing multiple threads simultaneously. Thread is basically a lightweight subprocess, a smallest unit |

|of processing. Multiprocessing and multithreading, both are used to achieve multitasking. But we use multithreading than multiprocessing|

|because threads share a common memory area. They don't allocate separate memory area so save memory, and context-switching between the |

|threads takes less time than processes. Multithreading is mostly used in games, animation etc. A thread can be in one of the five states|

|in the thread. According to sun, there is only 4 states new, runnable, non-runnable and terminated. There is no running state. But for |

|better understanding the threads, we are explaining it in the 5 states. The life cycle of the thread is controlled by JVM. The thread |

|states are as follows: |

| |

|A thread can be in one of the five states in the thread. According to sun, there is only 4 states new, runnable, non-runnable and |

|terminated. There is no running state. But for better understanding the threads, we are explaining it in the 5 states. The life cycle of|

|the thread is controlled by JVM. The thread states are as follows: |

|New |

|Runnable |

|Running |

|Non-Runnable (Blocked) |

|Terminated |

3. Algorithm:

1. There are two ways to create a thread:

▪ By extending Thread class

▪ By implementing Runnable interface.

2. Create Thread class:-

Thread class provide constructors and methods to create and perform operations on a thread.Thread class extends Object class and implements Runnable interface.

Use Constructors of Thread class:

• Thread()

• Thread(String name)

• Thread(Runnable r)

• Thread(Runnable r,String name)

Runnable interface:

The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. Runnable interface have only one method named run().

|Starting a thread: |

|start() method of Thread class is used to start a newly created thread. It performs |

|following tasks: |

|A new thread starts(with new callstack). |

|The thread moves from New state to the Runnable state. |

|When the thread gets a chance to execute, its target run() method will run. |

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,

Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:

6. Output:

Experiment-10

1. Objective: Write a program to demonstrate AWT.

2. Explanation: Abstract Windowing Toolkit (AWT) is used for GUI programming in java.

• Container:

|The Container is a component in AWT that can contain another components like buttons, text fields, labels etc. The classes that extends |

|Container class are known as container. |

Window:

|The window is the container that have no borders and menu bars. You must use frame, dialog or another window for creating a window. |

Panel:

|The Panel is the container that doesn't contain title bar and Menu Bars. It can have other components like button, textfield etc. |

Frame:

|The Frame is the container that contain title bar and can have MenuBars. It can have other components like button, textfield etc. |

3. Algorithm:-

1. There are two ways to create a frame:

• By extending Frame class (inheritance)

• By creating the object of Frame class (association)

2. Create class

3. Extends the Frame class

4. Create component objects like Button.

5. Set Layout on for Frame

6. Set size of Frame

7. Close Frame

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000,Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:

6. Output:-

Experiment-11

1. Objective: Write a Program to show Data Base Connectivity Using JAVA

2. Explanation: - JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:

1. JDBC-ODBC bridge driver

2. Native-API driver (partially java driver)

3. Network Protocol driver (fully java driver)

4. Thin driver (fully java driver)

3. Algorithm:-

Register the driver class:-

The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class.

Creating connection:- The getConnection() method of DriverManager class is used to establish connection with the database.

Creating statement:- The createStatement() method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database.

Executing queries:- The executeQuery() method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table.

Closing connection:- By closing connection object statement and ResultSet will be closed automatically. The close() method of Connection interface is used to close the connection.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output:-

Experiment-12

1. Objective: Write a Program to show “HELLO JAVA” in Explorer using Applet.

2. Explanation:- Applet is a special type of program that is embedded in the webpage to Generate the dynamic content. It runs inside the browser and works at client side. There are many advantages of applet. They are as follows:

• It works at client side so less response time.

• Secured

• It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc.

There are two ways to run an applet

• By html file.

• By appletViewer tool (for testing purpose).

3. Algorithm:-

1. Create an applet that contains applet tag in comment

2. Compile it.

3. After that run it by: appleviewer First.java.

4. Now Html file is not required but it is for testing purpose only.

Or

1. Execute the applet by html file.

2. Create an applet

3 compile it.

4. After that create an html file and place the applet code in html file.

5. Now click the html file.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output

Experiment-13

1. Objective:- Write a Program to show Connectivity using JDBC.

2. Explanation. Develop a simple OPAC system for library using event-driven and concurrent

Programming paradigms of Java. Use JDBC to connect to a back-end database.

3. ALGORITHM:-

STEP 1: Create a Master Database1 (Book Details) having the following fields: BookNo., Book Name, Author, No. of pages, Name of Publisher, Cost.

STEP 2: Create a Master Database2(User Details) having the following fields : UserID,

Department.

STEP 3: Create a Transaction Database having the following fields: UserID, Book No., Date of Renewal / Date of Return, Fine.

STEP 4: Create a panel consisting of buttons ADD, UPDATE, DELETE. Associate these

button actions with listeners(with Master Database 1).

STEP 5: Create a panel consisting of buttons ADD, UPDATE, DELETE. Associate these

button actions with listeners(with Master Database 2).

STEP 6: Create another panel consisting of buttons UserID, BookID, Return/Renewal,

Fine.

STEP 7: Associate these buttons with listeners(with Transaction Database).

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6.Output:-

Experiment-14

1. Objective: Write a program to demonstrate multithreading using Java.

2. Explanation: There are two sets of Java APIs for graphics programming: AWT (Abstract Windowing Toolkit) and Swing.Java Graphics APIs - AWT and Swing - provide a huge set of reusable GUI components, such as button, text field, label, choice, panel and frame for building GUI applications. The java.awt package contains the core AWT graphics classes. The java.awt.event package supports event handling. The java.awt.geom package contains models for a 2D point like x and y axis. In this program the application has a ball that moves and bounces off the edges of a rectangle is designed by using GUI in java. Move() -Moves the ball to the next position, reversing direction if it hits one of the edges.

getShape() - Gets the shape of the ball at its current position Using Jcomponent in java add() - Add a ball to the panel.

BounceThread - Shows animated bouncing balls

BallRunnable - A runnable that animates a bouncing ball.

BounceFrame - Usig JFrame frame is designed with panel and buttons

addButton - Adds a button to a container

addBall - Adds a bouncing ball to the canvas and starts a thread to make it bounce

3. ALGORITHM:

1. Create the class ball which implements a ball which moves and bounces of the edges of a rectangle.

2. Using the move() method move the ball to the nect postion by reversing the direction if it bits one of the edges.

3. Get the shapes of the ball at its current position.

4. Invoke the runnable() method to show the animated bouncing balls.

5. Using the sleep() method assign the time for ball to bounce in a given interval of time.

6. Construct the frame with the component for showing bouncing ball and start and close

buttons.

7. Add the thread to the frame and button to the container.

8. Create a constructor to assign values to them.

9. Add a bouncing ball to the canvas and starts a thread to make it bounce.

10. Display the output.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output:-

Experiment-15

1. Objective: - Write a program to demonstrate applet life cycle.

2. Explanation

An Applet is a Java class that extends the java.applet.Applet class. A JVM is required to view an applet. The JVM can be either a plug-in of the Web browser or a separate runtime environment.

Life Cycle of an Applet:

• init: This method is intended for whatever initialization is needed for your applet. It is called after the param tags inside the applet tag have been processed.

• start: This method is automatically called after the browser calls the init method. It is also called whenever the user returns to the page containing the applet after having gone off to other pages.

• stop: This method is automatically called when the user moves off the page on which the applet sits. It can, therefore, be called repeatedly in the same applet.

• destroy: This method is only called when the browser shuts down normally. Because applets are meant to live on an HTML page, you should not normally leave resources behind after a user leaves the page that contains the applet.

• paint: Invoked immediately after the start() method, and also any time the

• applet needs to repaint itself in the browser. The paint() method is actually

• inherited from the java.awt

3. Algorithm:

1. Import the required class and packages.

2. Create a class Draw test for creating an applet.

3. Initialize panels and controls in the init() method.

4. Define the destroy () method to destroy the same.

5. Create an instance for draw test class and call the init(0 and start() methods in the main

Method.

6. Add a new frame to the applet window and resize it to 300x300.

7. Declare a method get applet infor to display the applet information.

8. Declare two constants LINES,POINTS which are going to be the modes.

9. Define the paint method and perform the required operations.

10. Display the result according to the mode selected.

4. Required Software/ Software Tool:-

• Platform(s): Windows 8, Windows 7, Vista, Windows XP, Windows 2000, Windows 2003, Windows 2008 Server

• Java version(s): 6.0, 7.0 (jdk1.6,jdk1.7)

• Editor :- Net Beans IDE 6.9.1

• Database:- Oracle9i / Mysql

• Web server:- Apache Tomcat 5.5

5. Coding:-

6. Output:-

List of VIVA Questions

1) What is difference between JDK, JRE and JVM?

JVM

JVM is an acronym for Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java byte code can be executed.

JVMs are available for many hardware and software platforms (so JVM is plate form dependent).

JRE

JRE stands for Java Runtime Environment. It is the implementation of JVM and physically exists.

JDK

JDK is an acronym for Java Development Kit. It physically exists. It contains JRE + development tools.

[pic]

2) How many types of memory areas are allocated by JVM?

Many types:

1. Class(Method) Area

2. Heap

3. Stack

4. Program Counter Register

5. Native Method Stack

[pic]

3) What is JIT compiler?

Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

[pic]

4) What is platform?

A platform is basically the hardware or software environment in which a program runs. There are two types of platforms software-based and hardware-based. Java provides software-based platform.

[pic]

5) What is the main difference between Java platform and other platforms?

The Java platform differs from most other platforms in the sense that it's a software-based platform that runs on top of other hardware-based platforms.It has two components:

1. Runtime Environment

2. API(Application Programming Interface)

[pic]

6) What gives Java its 'write once and run anywhere' nature?

The bytecode. Java is compiled to be a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific and hence can be fed to any platform.

[pic]

7) What is classloader?

The classloader is a subsystem of JVM that is used to load classes and interfaces.There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

[pic]

8) Is Empty .java file name a valid source file name?

Yes, save your java file by .java only, compile it by javac .java and run by java yourclassname Let's take a simple example:

//save by .java only  

class A{  

public static void main(String args[]){  

System.out.println("Hello java");  

}  

}  

//compile by javac .java  

//run by     java A  

compile it by javac .java

run it by java A

9) Is delete,next,main,exit or null keyword in java?

No.

[pic]

10) If I don't provide any arguments on the command line, then the String array of Main method will be empty or null?

It is empty. But not null.

[pic]

11) What if I write static public void instead of public static void?

Program compiles and runs properly.

[pic]

12) What is the default value of the local variables?

The local variables are not initialized to any default value, neither primitives nor object references.

[pic]

Core Java - OOPs Concepts: Initial OOPs Viva Questions

There is given more than 50 OOPs (Object-Oriented Programming and System) Viva questions. But they have been categorized in many sections such as constructor Viva questions, static Viva questions, Inheritance Viva questions, Abstraction Viva question, Polymorphism Viva questions etc. for better understanding.

[pic]

13) What is difference between object oriented programming language and object based programming language?

Object based programming languages follow all the features of OOPs except Inheritance. Examples of object based programming languages are JavaScript, VBScript etc.

14) What will be the initial value of an object reference which is defined as an instance variable?

The object references are all initialized to null in Java.

[pic]

Core Java - OOPs Concepts: Constructor Viva Questions

[pic]

15) What is constructor?

• Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.

[pic]

16) What is the purpose of default constructor?

• The default constructor provides the default values to the objects. The java compiler creates a default constructor only if there is no constructor in the class.

[pic]

17) Does constructor return any value?

Ans:yes, that is current instance (You cannot use return type yet it returns a value).

[pic]

18)Is constructor inherited?

No, constructor is not inherited.

[pic]

19) Can you make a constructor final?

No, constructor can't be final.

[pic]

Core Java - OOPs Concepts: static keyword Viva Questions

[pic]

20) What is static variable?

• static variable is used to refer the common property of all objects (that is not unique for each object) e.g. company name of employees,college name of students etc.

• static variable gets memory only once in class area at the time of class loading.

[pic]

21) What is static method?

• A static method belongs to the class rather than object of a class.

• A static method can be invoked without the need for creating an instance of a class.

• static method can access static data member and can change the value of it.

[pic]

22) Why main method is static?

because object is not required to call static method if It were non-static method,jvm creats object first then call main() method that will lead to the problem of extra memory allocation.

[pic]

23) What is static block?

• Is used to initialize the static data member.

• It is excuted before main method at the time of classloading.

[pic]

24) Can we execute a program without main() method?

Ans) Yes, one of the way is static block.

[pic]

25) What if the static modifier is removed from the signature of the main method?

Program compiles. But at runtime throws an error "NoSuchMethodError".

[pic]

26) What is difference between static (class) method and instance method?

|static or class method |instance method |

|1)A method i.e. declared as static is known as static method. |A method i.e. not declared as |

| |static is known as instance |

| |method. |

|2)Object is not required to call static method. |Object is required to call |

| |instance methods. |

|3)Non-static (instance) members cannot be accessed in static context (static method, |static and non-static |

|static block and static nested class) directly. |variables both can be accessed|

| |in instance methods. |

|4)For example: public static int cube(int n){ return n*n*n;} |For example: public void |

| |msg(){...}. |

[pic]

Core Java - OOPs Concepts: Inheritance Viva Questions

[pic]

27) What is this in java?

It is a keyword that that refers to the current object.

[pic]

28)What is Inheritance?

Inheritance is a mechanism in which one object acquires all the properties and behavior of another object of another class. It represents IS-A relationship. It is used for Code Reusability and Method Overriding.

[pic]

29) Which class is the superclass for every class.

Object class.

[pic]

30) Why multiple inheritance is not supported in java?

• To reduce the complexity and simplify the language, multiple inheritance is not supported in java in case of class.

[pic]

31) What is composition?

Holding the reference of the other class within some other class is known as composition.

[pic]

32) What is difference between aggregation and composition?

Aggregation represents weak relationship whereas composition represents strong relationship. For example: bike has an indicator (aggregation) but bike has an engine (compostion).

33) Why Java does not support pointers?

Pointer is a variable that refers to the memory address. They are not used in java because they are unsafe(unsecured) and complex to understand.

[pic]

34) What is super in java?

It is a keyword that refers to the immediate parent class object.

[pic]

35) Can you use this() and super() both in a constructor?

No. Because super() or this() must be the first statement.

[pic]

36)What is object cloning?

The object cloning is used to create the exact copy of an object. 

[pic]

Core Java - OOPs Concepts: Method Overloading Viva Questions

[pic]

37) What is method overloading?

If a class have multiple methods by same name but different parameters, it is known as Method Overloading. It increases the readability of the program.

[pic]

38) Why method overloading is not possible by changing the return type in java?

Becauseof ambiguity.

[pic]

39) Can we overload main() method?

Yes, You can have many main() methods in a class by overloading the main method.

[pic]

Core Java - OOPs Concepts: Method Overriding Viva Questions

[pic]

40) What is method overriding:

If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding. It is used for runtime polymorphism and to provide the specific implementation of the method.

[pic]

41) Can we override static method?

No, you can't override the static method because they are the part of class not object.

[pic]

42) Why we cannot override static method?

It is because the static method is the part of class and it is bound with class whereas instance method is bound with object and static gets memory in class area and instance gets memory in heap.

[pic]

43) Can we override the overloaded method?

Yes.

[pic]

44) Difference between method Overloading and Overriding.

|Method Overloading |Method Overriding |

|1) Method overloading increases the readability of the program. |Method overriding provides the specific implementation of the |

| |method that is already provided by its super class. |

|2) method overlaoding is occurs within the class. |Method overriding occurs in two classes that have IS-A |

| |relationship. |

|3) In this case, parameter must be different. |In this case, parameter must be same. |

45) Can you have virtual functions in Java?

Yes, all functions in Java are virtual by default.

[pic]

46) What is covariant return type?

Now, since java5, it is possible to override any method by changing the return type if the return type of the subclass overriding method is subclass type. It is known as covariant return type. 

[pic]

Core Java - OOPs Concepts: final keyword Viva Questions

[pic]

47) What is final variable?

If you make any variable as final, you cannot change the value of final variable(It will be constant).

[pic]

48) What is final method?

Final methods can't be overriden.

[pic]

49) What is final class?

Final class can't be inherited. 

[pic]

50) What is blank final variable?

A final variable, not initalized at the time of declaration, is known as blank final variable.

[pic]

51) Can we intialize blank final variable?

Yes, only in constructor if it is non-static. If it is static blank final variable, it can be initialized only in the static block.

[pic]

52) Can you declare the main method as final?

Yes, such as, public static final void main(String[] args){}.

53) What is Runtime Polymorphism?

Runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time.

In this process, an overridden method is called through the reference variable of a super class. The determination of the method to be called is based on the object being referred to by the reference variable.

[pic]

54) Can you achieve Runtime Polymorphism by data members?

No.

[pic]

55) What is the difference between static binding and dynamic binding?

In case of static binding type of object is determined at compile time whereas in dynamic binding type of object is determined at runtime.

[pic]

Core Java - OOPs Concepts : Abstraction Viva Questions

[pic]

56) What is abstraction?

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Abstraction lets you focus on what the object does instead of how it does it.

[pic]

57) What is the difference between abstraction and encapsulation?

Abstraction hides the implementation details whereas encapsulation wraps code and data into a single unit.

[pic]

58) What is abstract class?

A class that is declared as abstract is known as abstract class. It needs to be extended and its method implemented. It cannot be instantiated.

[pic]

59) Can there be any abstract method without abstract class?

No, if there is any abstract method in a class, that class must be abstract.

[pic]

60) Can you use abstract and final both with a method?

No, because abstract method needs to be overridden whereas you can't override final method.

[pic]

61) Is it possible to instantiate the abstract class?

No, abstract class can never be instantiated.

[pic]

62) What is interface?

Interface is a blueprint of a class that have static constants and abstract methods.It can be used to achieve fully abstraction and multiple inheritance.

[pic]

63) Can you declare an interface method static?

No, because methods of an interface is abstract by default, and static and abstract keywords can't be used together.

[pic]

64) Can an Interface be final?

No, because its implementation is provided by another class.

[pic]

65) What is marker interface?

An interface that have no data member and method is known as a marker interface.For example Serializable, Cloneable etc.

[pic]

66) What is difference between abstract class and interface?

|Abstract class |Interface |

|1)An abstract class can have method body (non-abstract methods). |Interface have only |

| |abstract methods. |

|2)An abstract class can have instance variables. |An interface cannot |

| |have instance |

| |variables. |

|3)An abstract class can have constructor. |Interface cannot have|

| |constructor. |

|4)An abstract class can have static methods. |Interface cannot have|

| |static methods. |

|5)You can extends one abstract class. |You can implement |

| |multiple interfaces. |

[pic]

67) Can we define private and protected modifiers for variables in interfaces?

No, they are implicitly public.

[pic]

68) When can an object reference be cast to an interface reference?

An object reference can be cast to an interface reference when the object implements the referenced interface.

[pic]

Core Java - OOPs Concepts : Package Viva Questions

[pic]

69) What is package?

A package is a group of similar type of classes interfaces and sub-packages. It provides access protection and removes naming collision.

[pic]

70) Do I need to import java.lang package any time? Why ?

No. It is by default loaded internally by the JVM.

[pic]

71) Can I import same package/class twice? Will the JVM load the package twice at runtime?

One can import the same package or same class multiple times. Neither compiler nor JVM complains about it.But the JVM will internally load the class only once no matter how many times you import the same class.

[pic]

72) What is static import ?

By static import, we can access the static members of a class directly, there is no to qualify it with the class name.

73) What is Exception Handling?

Exception Handling is a mechanism to handle runtime errors. It is mainly used to handle checked exceptions.

[pic]

74) What is difference between Checked Exception and Unchecked Exception?

1)Checked Exception:

The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.

2)Unchecked Exception:

The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.

[pic]

75) What is the base class for Error and Exception?

Throwable.

[pic]

76) Is it necessary that each try block must be followed by a catch block?

It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

[pic]

77) What is finally block?

• finally block is a block that is always executed.

[pic]

78) Can finally block be used without catch?

• Yes, by try block. finally must be followed by either try or catch.

[pic]

79) Is there any case when finally will not be executed?

finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

[pic]

80)What is difference between throw and throws?

|throw keyword |throws keyword |

|1)throw is used to explicitly throw an exception. |throws is used to declare an exception. |

|2)checked exceptions can not be propagated with throw |checked exception can be propagated with throws. |

|only. | |

|3)throw is followed by an instance. |throws is followed by class. |

|4)throw is used within the method. |throws is used with the method signature. |

|5)You cannot throw multiple exception |You can declare multiple exception e.g. public void method()throws |

| |IOException,SQLException. |

[pic]

81) Can an exception be rethrown?

Yes.

[pic]

82) Can subclass overriding method declare an exception if parent class method doesn't throw an exception ?

Yes but only unchecked exception not checked.

[pic]

83) What is exception propagation ?

Forwarding the exception object to the invoking method is known as exception propagation.

[pic]

[pic]

Core Java: String Handling Viva Questions

There is given a list of string handling Viva questions with short and pointed answers. If you know any string handling Viva question, kindly post it in the comment section.

[pic]

84) What is the meaning of immutable in terms of String?

The simple meaning of immutable is unmodifiable or unchangeable. Once string object has been created, its value can't be changed.

[pic]

85) Why string objects are immutable in java?

Because java uses the concept of string literal. Suppose there are 5 reference variables,all referes to one object "sachin".If one reference variable changes the value of the object, it will be affected to all the reference variables. That is why string objects are immutable in java.

[pic]

86) How many ways we can create the string object?

There are two ways to create the string object, by string literal and by new keyword.

[pic]

87) How many objects will be created in the following code?

1. String s1="Welcome";  

2. String s2="Welcome";  

3. String s3="Welcome";  

Only one object.

[pic]

88) Why java uses the concept of string literal?

To make Java more memory efficient (because no new objects are created if it exists already in string constant pool).

[pic]

89)How many objects will be created in the following code?

1. String s = new String("Welcome");  

Two objects, one in string constant pool and other in non-pool(heap).

[pic]

90) What is the basic difference between string and stringbuffer object?

String is an immutable object. StringBuffer is a mutable object.

[pic]

91) What is the difference between StringBuffer and StringBuilder ?

StringBuffer is synchronized whereas StringBuilder is not synchronized.

[pic]

92) How can we create immutable class in java ?

We can create immutable class as the String class by defining final class and

[pic]

93) What is the purpose of toString() method in java ?

The toString() method returns the string representation of any object. If you print any object, java compiler internally invokes the toString() method on the object. So overriding the toString() method, returns the desired output, it can be the state of an object etc. depends on your implementation.

[pic]

Core Java : Nested classes and Interfaces Viva Questions

[pic]

94)What is nested class?

A class which is declared inside another class is known as nested class. There are 4 types of nested class member inner class, local inner class, annonymous inner class and static nested class.

[pic]

95) Is there any difference between nested classes and inner classes?

Yes, inner classes are non-static nested classes i.e. inner classes are the part of nested classes.

[pic]

96) Can we access the non-final local variable, inside the local inner class?

No, local variable must be constant if you want to access it in local inner class.

[pic]

97) What is nested interface ?

Any interface i.e. declared inside the interface or class, is known as nested interface. It is static by default.

[pic]

98) Can a class have an interface?

Yes, it is known as nested interface.

[pic]

99) Can an Interface have a class?

Yes, they are static implicitly.

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download