Java Last Year Question Paper
Java Last Year Question Paper
Java Last Year Question Paper
->>JDK is an acronym for Java Development Kit. The Java Development Kit (JDK) is
a software development environment which is used to develop java applications
and applets. It physically exists. It contains JRE + development tools.
Syntax:
throw Instance
Example:
throw new ArithmeticException("/ by zero");
*Define Polymorphism?
-->The word polymorphism means having many forms. In simple words, we can
polymorphism as the ability of a message to be displayed in more than one form.
*Explain features of java.?
1) Simple-:
Java is easy to learn and its syntax is quite simple, clean and easy to understand.
2) Object Oriented:-
In java, everything is an object which has some data and behavior. Java can be
easily extended as it is based on Object Model.
3) Robust:-
Java makes an effort to eliminate error prone codes by emphasizing mainly on
compile time error checking and runtime checking.
4) Platform Independent:-
Unlike other programming languages such as C, C++ etc. which are compiled into
platform specific machines.
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.
6) Multi Threading:-
Java multithreading feature makes it possible to write program that can do many
tasks simultaneously.
7) Portable:-
Java Byte code can be carried to any platform. No implementation dependent
features.
8) High Performance:-
Java is an interpreted language, so it will never be as fast as a compiled language
like C or C++.
*Explain the concept of exception and exception handling?
-->An exception is an event, which occurs during the execution of a program that
disrupts the normal flow of the program's instructions. When an error occurs
within a method, the method creates an object and hands it off to the runtime
system.
Exception Handling in Java is one of the effective means to handle the runtime
errors so that the regular flow of the application can be preserved. Java Exception
Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, SQLException, RemoteException, etc.
**Types of Exceptions:-
1) Built-in Exceptions:
Built-in exceptions are the exceptions that are available in Java library.
2) User-Defined Exceptions:
Sometimes, the built-in exceptions in Java are not able to describe a certain
situation. In such cases, users can also create exceptions, which are called ‘user-
defined Exceptions’.
*Propagation of Errors
catch(Exception e)
{
System.out.println("Can't divided by zero");
}
}
}
2) CATCH :-
Java catch block is used to handle the Exception by declaring the type of
exception within the parameter. The declared exception must be the parent class
exception ( i.e., Exception) or the generated exception type. However, the good
approach is to declare the generated type of exception.
EXAMPLE.
public class TryCatchExample {
}
}
*Write a java program to display all the perfect numbers between 1 to n?
import java.util.Scanner;
public class Perfect
{
static boolean perfect(int num)
{
int sum = 0;
for(int i=1; i<num; i++)
{
if(num%i==0)
{
sum = sum+i;
}
}
if(sum==num)
return true;
else
return false;
}
public static void main(String[] args)
{
Scanner obj = new Scanner (System.in);
System.out.println("enter the value for n");
int n = obj.nextInt();
for(int i=1; i<=n; i++)
{
if(perfect(i))
System.out.println(i);
}
}
}
*Java Program to Find Area of Square, Rectangle and Circle. (using Method
Overloading).?
class OverloadDemo
{
void area(float x)
{
System.out.println("the area of the square is "+Math.pow(x, 2)+" sq units");
}
void area(float x, float y)
{
System.out.println("the area of the rectangle is "+x*y+" sq units");
}
void area(double x)
{
double z = 3.14 * x * x;
System.out.println("the area of the circle is "+z+" sq units");
}
}
class Overload
{
public static void main(String args[])
{
OverloadDemo ob = new OverloadDemo();
ob.area(5);
ob.area(11,12);
ob.area(2.5);
}
}
Output:
$ javac OverloadDemo.java
$ java OverloadDemo
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++)
{
String elmt=sc.next();
alist.add(elmt);
}
Collections.reverse(alist);
import java.util.*;
class prac3 A
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
System.out.print("Enter A String: ");
String str=input.nextLine();
int letter=0,space=0,digit=0,other=0;
char ch[]=str.toCharArray();
for(int i=0;i<str.length();i++)
{
if(Character.isLetter(ch[i]))
{
letter++;
}
else if(Character.isDigit(ch[i]))
{
digit++;
}
else if(Character.isSpaceChar(ch[i]))
{
space++;
}
else{
other++;
}
}
System.out.println("Letter are: "+letter);
System.out.println("Space are: "+space);
System.out.println("Digit are: "+digit);
System.out.println("Other: "+other);
}
}
*Write a program that displays the x and y position of the cursor movement
using Keyboard..?
-->
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
*/
public class Simplekey extends Applet implements KeyListener {
String msg = ” “;
int X = 10, Y = 20;
public void init() {
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke) {
showStatus(“Key Down”);
}
public void keyReleased(KeyEvent ke) {
showStatus(“Key Up”);
}
public void keyTyped (KeyEvent ke) {
msg += ke.getKeyChar();
repaint();
}
public void paint(Graphics g) {
g.drawString(msg, X, Y);
}
}
*what is package. Write down all the steps for package creation.?
Java Package
A java package is a group of similar types of classes, interfaces and sub-packages.
Package in java 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.
Write the package name at the top of every source file (classes, interface,
enumeration, and annotations).
Remember that there must be only one package statement in each source file.
Package name must be in lower case that avoids conflict with the name of classes
and interfaces.
Organizations used their internet domain name to define their package names.
For example, com.javatpoint.mypackage.
Sometimes, the organization also uses the region after the company name to
name the package.
For example, com.javatpoint.region.mypackage.
We use underscore in the package name if the domain name contains hyphen or
other special characters or package names begin with a digit or reserved keyword
*DEFINE OBJECT?
-->It is a basic unit of Object-Oriented Programming and represents real life
entities. A typical Java program creates many objects, which as you know, interact
by invoking methods. An object consists of :
3) Identity: - It gives a name to an object and enables one object to interact with
other objects
An applet is a Java program that can be embedded into a web page. It runs
inside the web browser and works at client side. An applet is embedded in an
HTML page using the APPLET or OBJECT tag and hosted on a web server.
Applets are used to make the website more dynamic and entertaining.
TYPES OF APPLETS.
1)Local Applet--: is written on our own, and then we will embed it into web pages.
Local Applet is developed locally and stored in the local system. A web page
doesn't need the get the information from the internet when it finds the local
Applet in the system.
--> Java finally block is a block used to execute important code such as closing the
connection, etc.
Java finally block is always executed whether an exception is handled or not.
Therefore, it contains all the necessary statements that need to be printed
regardless of the exception occur or not.
1) Abstract class can have abstract and non- Interface can have only abstract methods. Since
abstract methods. Java 8, it can have default and static methods also.
3) Abstract class can have final, non-final, Interface has only static and final variables.
static and non-static variables.
4) Abstract class can provide the Interface can't provide the implementation of
implementation of interface. abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare interface.
abstract class.
6) An abstract class can extend another Java An interface can extend another Java interface only.
class and implement multiple Java interfaces.
7) An abstract class can be extended using An interface can be implemented using keyword
keyword "extends". "implements".
8) A Java abstract class can have class Members of a Java interface are public by default.
members like private, protected, etc.
9)Example: Example:
public abstract class Shape{ public interface Drawable{
public abstract void draw(); void draw();
}
*Java Program to Count the Number of Lines, Words, Characters from a given File
import java.io.*;
throws IOException
{
File file = new File("C:\\Users\\hp\\Desktop\\TextReader.txt");
String line;
int wordCount = 0;
int characterCount = 0;
int paraCount = 0;
int whiteSpaceCount = 0;
int sentenceCount = 0;
if (line.equals("")) {
paraCount += 1;
}
else {
characterCount += line.length();
wordCount += words.length;
whiteSpaceCount += wordCount - 1;
sentenceCount += sentence.length;
if (sentenceCount >= 1) {
paraCount++;
}
}