Java Practical Question Bank
1) Write a program to check number is even or odd using if else.
import [Link].*;
public class Pr1_even_odd {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a valid number");
int a = [Link]();
if(a%2==0){
[Link](a+" is a Even number");
}
else{
[Link](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':
[Link]("case a");
break;
case 'b':
[Link]("case b");
break;
case 'c':
[Link]("case c");
break;
default:
[Link]("");
}
}
}
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 < [Link]; i++) {
[Link]("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;
[Link]("Int Value = "+i);
[Link]("Long Value = "+l);
[Link]("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 [Link].*;
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();
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return c;
}
void disp() {
[Link](real + "+i" + img);
}
}
class Pr5_program {
public static void main(String args[]) {
complex c1 = new complex(1, 2);
complex c2 = new complex(2, 3);
[Link]("1st Complex number");
[Link]();
[Link]("2nd Complex number");
[Link]();
complex c3 = new complex();
c3 = [Link](c1, c2);
[Link]("Addition of Complex number is = " + [Link] + "+i" +
[Link]);
}
}
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 ();
[Link]("Character at 1st index = "+[Link](1));
if([Link](s2)==0)
[Link]("Both string are equal");
[Link]([Link](s2));
[Link]([Link](s3));
[Link]("Length of String s1 = "+[Link]());
s4 = [Link]('A', 'E');
[Link]("String s4 = "+s4);
[Link]([Link]("Ja"));
[Link]([Link]("A"));
[Link]([Link]("va"));
[Link]([Link]("v"));
[Link]([Link]('a'));
s4=[Link](2);
[Link]("s4 = "+s4);
[Link]([Link]('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");
[Link](s2);
[Link](s1);
[Link](3, "Hello");
[Link](s2);
[Link](25);
[Link]([Link]());
[Link](1, 'p');
[Link](s2);
[Link]([Link]());
}
}
JavaLanguage
LanHelloguage
25
LpnHelloguage
egaugnaLavaJ
8) Write a program to implement single inheritance.
class one {
int a = 10;
void show() {
[Link]("The value of the first number is " + a);
}
}
class two extends one {
int b = 20;
void display() {
[Link]("The value of the second number is " + b);
}
}
public class Pr8_single {
public static void main(String[] args) {
two obj = new two();
[Link]();
[Link]();
}
}
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() {
[Link]("The value of the first number is " + a);
}
}
class two extends one {
int b = 20;
void display() {
[Link]("The value of the second number is " + b);
}
}
class three extends two {
int c = 30;
void disp() {
[Link]("The value of the third number is " + c);
}
}
public class Pr9_multilevel {
public static void main(String[] args) {
three obj = new three();
[Link]();
[Link]();
[Link]();
}
}
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() {
[Link]("The area of circle is " + circle);
[Link]("The area of rectangle is " + rectangle);
}
}
public class Pr10_area {
public static void main(String[] args) {
two obj = new two();
[Link]();
}
}
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.
[Link]
package Calc;
public class Add {
public void dispAdd() {
int num1 = 5, num2 = 15, sum;
sum = num1 + num2;
[Link]("Sum of these numbers: " + sum);
}
}
[Link]
package Calc;
public class Sub {
public void dispSub() {
int num1 = 50, num2 = 15, subtract;
subtract = num1 - num2;
[Link]("Subtraction of these numbers: " + subtract);
}
}
import Calc.*;
public class Pr11_Calculator {
public static void main(String args[]) {
Add a = new Add();
[Link]();
Sub s = new Sub();
[Link]();
}
}
Sum of these numbers: 20
Subtraction of these numbers: 35
12) Write a program to implement two thread use sleep method.
import [Link];
class one extends Thread {
public void run() {
try {
for (int i = 1; i < 5; i++) {
[Link](1000);
[Link]("Java");
}
} catch (Exception expn) {
[Link](expn);
}
}
}
class two extends Thread {
public void run() {
try {
for (int j = 1; j < 5; j++) {
[Link](1000);
[Link]("Python");
}
} catch (Exception expn) {
[Link]();
}
}
}
public class Pr12_sleep {
public static void main(String args[]) {
one t1 = new one();
two t2 = new two();
[Link]();
[Link]();
}
}
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 [Link];
class PasswordException extends Exception {
PasswordException(String msg) {
super(msg);
}
class Pr13_PassCheck {
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
try {
[Link]("Enter Password : ");
if ([Link]().equals("Shubham")) {
[Link]("Authenticated ");
} else {
throw new PasswordException("Authentication failure");
}
} catch (PasswordException e) {
[Link](e);
}
}
}
Enter Password :
Hello
PasswordException: Authentication failure
Enter Password :
Shubham
Authenticated
14) Develop a program to draw a polygon using applet.
import [Link].*;
import [Link].*;
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) {
[Link](xCoords, yCoords, 6);
}
}
// <applet code= "Pr14_Polygon.class" width=300 height=300> </applet>
15) Develop an applet for drawing a human face.
import [Link].*;
import [Link].*;
public class Pr15_HumanFace extends Applet {
public void paint(Graphics g) {
[Link](80, 70, 150, 150);
[Link](120, 120, 20, 20);
[Link](170, 120, 20, 20);
[Link](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 [Link].*;
import [Link].*;
public class Pr16_Square extends Applet {
public void paint(Graphics g) {
[Link](180, 10, 80, 80);
[Link](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 [Link].*;
public class Pr17_CopyCharacter {
public static void main(String[] args) throws IOException {
File source = new File("C:\\Users\\HP\\Desktop\\[Link]");
File destination = new File("C:\\Users\\HP\\Desktop\\[Link]");
FileInputStream inputStream = new FileInputStream(source);
FileOutputStream outputStream = new FileOutputStream(destination);
int length = (int) [Link]();
byte[] buffer = new byte[length];
while ((length = [Link](buffer)) > 0) {
[Link](buffer, 0, length);
}
[Link]();
[Link]();
[Link]("File copied successfully.......");
}
}
File copied successfully.......