Java Practical Question Bank
1) Write a program to check number is even or odd using if else.
import java.util.*;
public class Pr1_even_odd {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a valid number");
int a = sc.nextInt();
if(a%2==0){
System.out.println(a+" is a Even number");
}
else{
System.out.println(a+" is a Odd number");
}
}
}
2) Write a program to check switch case statement using character datatype.
public class Pr2_switch {
public static void main(String[] args) {
char a = 'a';
switch (a) {
case 'a':
System.out.println("case a");
break;
case 'b':
System.out.println("case b");
break;
case 'c':
System.out.println("case c");
break;
default:
System.out.println("");
}
}
}
case a
3) Develop a program to print command line argument using for loop.
public class Pr3_command {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("Output = " + args[i]);
}
}
}
PS E:\Shubham\Java\Practical Java> javac Pr3_command.java
PS E:\Shubham\Java\Practical Java> java Pr3_command Viva College of Diploma Engineering
and Technology
Output = Viva
Output = College
Output = of
Output = Diploma
Output = Engineering
Output = and
Output = Technology
4) Develop a program to show the use of implicit typecasting.
public class Pr4_implicit {
public static void main(String[] args) {
int i = 100;
long l = i;
float f = l;
System.out.println("Int Value = "+i);
System.out.println("Long Value = "+l);
System.out.println("Float Value = "+f);
}
}
Int Value = 100
Long Value = 100
Float Value = 100.0
5) Write a program to implement different types of constructors to perform addition of
complex numbers.
import java.util.*;
class complex {
int real, img;
complex() {
}
complex(int r, int i) {
real = r;
img = i;
}
complex add(complex c1, complex c2) {
complex c = new complex();
c.real = c1.real + c2.real;
c.img = c1.img + c2.img;
return c;
}
void disp() {
System.out.println(real + "+i" + img);
}
}
class Pr5_program {
public static void main(String args[]) {
complex c1 = new complex(1, 2);
complex c2 = new complex(2, 3);
System.out.println("1st Complex number");
c1.disp();
System.out.println("2nd Complex number");
c2.disp();
complex c3 = new complex();
c3 = c3.add(c1, c2);
System.out.println("Addition of Complex number is = " + c3.real + "+i" +
c3.img);
}
}
1st Complex number
1+i2
2nd Complex number
2+i3
Addition of Complex number is = 3+i5
6) Write a program to show the use of all methods of String class.
class Pr6_String
{
public static void main(String args[]) {
String s1= new String ("Java");
String s2 = new String ("Java");
String s3 = new String ("JAVA");
String s4 = new String ();
System.out.println("Character at 1st index = "+s1.charAt(1));
if(s1.compareTo(s2)==0)
System.out.println("Both string are equal");
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s3));
System.out.println("Length of String s1 = "+s1.length());
s4 = s3.replace('A', 'E');
System.out.println("String s4 = "+s4);
System.out.println(s2.startsWith("Ja"));
System.out.println(s2.startsWith("A"));
System.out.println(s2.endsWith("va"));
System.out.println(s2.endsWith("v"));
System.out.println(s1.indexOf('a'));
s4=s3.substring(2);
System.out.println("s4 = "+s4);
System.out.println(s1.lastIndexOf('a'));
}
}
Character at 1st index = a
Both string are equal
true
true
Length of String s1 = 4
String s4 = JEVE
true
false
true
false
1
s4 = VA
3
7) Write a program to implement all methods of StringBuffer class.
public class Pr7_StringBuffer {
public static void main(String args[]) {
StringBuffer s1 = new StringBuffer("Java");
StringBuffer s2 = new StringBuffer("Language");
s1.append(s2);
System.out.println(s1);
s2.insert(3, "Hello");
System.out.println(s2);
s1.setLength(25);
System.out.println(s1.length());
s2.setCharAt(1, 'p');
System.out.println(s2);
System.out.println(s1.reverse());
}
}
JavaLanguage
LanHelloguage
25
LpnHelloguage
egaugnaLavaJ
8) Write a program to implement single inheritance.
class one {
int a = 10;
void show() {
System.out.println("The value of the first number is " + a);
}
}
class two extends one {
int b = 20;
void display() {
System.out.println("The value of the second number is " + b);
}
}
public class Pr8_single {
public static void main(String[] args) {
two obj = new two();
obj.show();
obj.display();
}
}
The value of the first number is 10
The value of the second number is 20
9) Write a program to implement multilevel inheritance.
class one {
int a = 10;
void show() {
System.out.println("The value of the first number is " + a);
}
}
class two extends one {
int b = 20;
void display() {
System.out.println("The value of the second number is " + b);
}
}
class three extends two {
int c = 30;
void disp() {
System.out.println("The value of the third number is " + c);
}
}
public class Pr9_multilevel {
public static void main(String[] args) {
three obj = new three();
obj.show();
obj.display();
obj.disp();
}
}
The value of the first number is 10
The value of the second number is 20
The value of the third number is 30
10) Develop a program to find area of rectangle and circle using interface.
interface one {
final int l = 10;
final int b = 20;
final double pi = 3.14;
final int r = 10;
}
class two implements one {
double circle = pi * r * r;
int rectangle = l * b;
void display() {
System.out.println("The area of circle is " + circle);
System.out.println("The area of rectangle is " + rectangle);
}
}
public class Pr10_area {
public static void main(String[] args) {
two obj = new two();
obj.display();
}
}
The area of circle is 314.0
The area of rectangle is 200
11) Write a program to implement user defined packages in terms of creating a new
package and importing the same.
Add.java
package Calc;
public class Add {
public void dispAdd() {
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
System.out.println("Sum of these numbers: " + sum);
}
}
Sub.java
package Calc;
public class Sub {
public void dispSub() {
int num1 = 50, num2 = 15, subtract;
subtract = num1 - num2;
System.out.println("Subtraction of these numbers: " + subtract);
}
}
import Calc.*;
public class Pr11_Calculator {
public static void main(String args[]) {
Add a = new Add();
a.dispAdd();
Sub s = new Sub();
s.dispSub();
}
}
Sum of these numbers: 20
Subtraction of these numbers: 35
12) Write a program to implement two thread use sleep method.
import java.lang.Thread;
class one extends Thread {
public void run() {
try {
for (int i = 1; i < 5; i++) {
Thread.sleep(1000);
System.out.println("Java");
}
} catch (Exception expn) {
System.out.println(expn);
}
}
}
class two extends Thread {
public void run() {
try {
for (int j = 1; j < 5; j++) {
Thread.sleep(1000);
System.out.println("Python");
}
} catch (Exception expn) {
System.out.println();
}
}
}
public class Pr12_sleep {
public static void main(String args[]) {
one t1 = new one();
two t2 = new two();
t1.start();
t2.start();
}
}
Java
Python
Python
Java
Python
Java
Python
Java
13) Develop a program to accept a password from the user and throw
“Authentication Failure” exception if the password is incorrect.
import java.util.Scanner;
class PasswordException extends Exception {
PasswordException(String msg) {
super(msg);
}
class Pr13_PassCheck {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter Password : ");
if (sc.nextLine().equals("Shubham")) {
System.out.println("Authenticated ");
} else {
throw new PasswordException("Authentication failure");
}
} catch (PasswordException e) {
System.out.println(e);
}
}
}
Enter Password :
Hello
PasswordException: Authentication failure
Enter Password :
Shubham
Authenticated
14) Develop a program to draw a polygon using applet.
import java.awt.*;
import java.applet.*;
public class Pr14_Polygon extends Applet {
int xCoords[] = { 50, 200, 300, 150, 50, 50 };
int yCoords[] = { 100, 0, 50, 300, 200, 100 };
public void paint(Graphics g) {
g.drawPolygon(xCoords, yCoords, 6);
}
}
// <applet code= "Pr14_Polygon.class" width=300 height=300> </applet>
15) Develop an applet for drawing a human face.
import java.awt.*;
import java.applet.*;
public class Pr15_HumanFace extends Applet {
public void paint(Graphics g) {
g.drawOval(80, 70, 150, 150);
g.drawOval(120, 120, 20, 20);
g.drawOval(170, 120, 20, 20);
g.drawArc(130, 180, 50, 20, 180, 180);
}
}
// <applet code= "Pr15_HumanFace.class" width=500 height=500> </applet>
16) Develop a program to draw square inside a circle using applet.
import java.awt.*;
import java.applet.*;
public class Pr16_Square extends Applet {
public void paint(Graphics g) {
g.drawOval(180, 10, 80, 80);
g.drawRect(192, 22, 55, 55);
}
}
// <applet code = "Pr16_Square.class" width = 400 height=400> </applet>
17) Develop a program to copy characters from one file to another.
import java.io.*;
public class Pr17_CopyCharacter {
public static void main(String[] args) throws IOException {
File source = new File("C:\\Users\\HP\\Desktop\\input.txt");
File destination = new File("C:\\Users\\HP\\Desktop\\output.txt");
FileInputStream inputStream = new FileInputStream(source);
FileOutputStream outputStream = new FileOutputStream(destination);
int length = (int) source.length();
byte[] buffer = new byte[length];
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
System.out.println("File copied successfully.......");
}
}
File copied successfully.......