Java Programming Exercises With Solutions PDF
Java Programming Exercises With Solutions PDF
Java Programming Exercises With Solutions PDF
in/
Index
Q .no. Questions Page no. Remark
1 Write a program that implement the concept of Encapsulation. 03-04
Write a program to demonstrate the concept of function overloading of
2
Polymorphism.
05-06
Write a program the use Boolean data type and print the prime number
4
series up to 50.
09
13 Write a program to check that the given String is palindrome or not. 23-24
14 Write a program to arrange the String in alphabetical order. 25-26
Write a program for StringBuffer class which perform the all methods
15 27-28
of that class.
16 Write a program to calculate Simple interest using the Wrapper class. 29
Write a program to calculate Area of various geometrical figures using
17 30-31
the abstract class.
Write a program where Single class implements more than one
18 interfaces and with help of interface reference variable user call the 32-33
methods.
WAP that use the multiple catch statements within the try-catch
19
mechanism.
34
WAP where user will create a self- Exception using the “throw”
20 35
keyword.
Write a program for multithread using is Alive(), join() and
21
synchronized() methods of thread class
36-37
Write a program for JDBC to insert the values into the existing table by
23 40-41
using prepared statement.
WAP for JDBC to display the records from the existing table.
24 42-43
25 WAP for demonstrate of switch statement ,continue and break. 44-45
1
https://masterprogramming.in/
2
https://masterprogramming.in/
Figure 1
3
https://masterprogramming.in/
Ans :-
4
https://masterprogramming.in/
Output:
5
https://masterprogramming.in/
Ans :-
6
https://masterprogramming.in/
Output:-
7
https://masterprogramming.in/
Q4) Write a program the use Boolean data type and print the prime
number series up to 50.
Ans :-
Output:-
8
https://masterprogramming.in/
Ans :-
do
{
sum=b+c;
System.out.print(" "+sum);
// Swap
b=c;
c=sum;
i++;
}
// Iterate till i is N
while(i<=N);
System.out.println();
}
}
Output:-
9
https://
Ans :-
1
https://
Output:-
1
https://
Ans :-
// Driver method
public static void main(String[] args)
{
Scanner input=new Scanner(System.in); System.out.print("\
nEnter a number to find factorial : "); int
num=input.nextInt();
Output:-
1
https://
Output:-
1
https://
if(r!=c)
{
System.out.println("\nSorry! matrix multiplication cannot
be performed..!");
System.exit(0);
}
else
{ int m1[][]=new int[r][c];
int m2[][]=new int[r][c];
int m3[][]=new int[r][c];
int sum;
for(int i=0;i<r;i++)
{
1
https://
for(int j=0;j<c;j++)
{ sum=0;
for(int k=0;k<r;k++)
{ sum=sum + m1[i][k]*m2[k][j];
}
m3[i][j]=sum;
}
}
}
Output:-
1
https://
Ans :-
for(int i=0;i<r;i++)
{ for(int j=0;j<c;j++)
{
m2[i][j]=input.nextInt();
}
}
for(int i=0;i<r;i++)
{ for(int j=0;j<c;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
}
}
1
https://
for(int i=0;i<r;i++)
{ for(int j=0;j<c;j++)
{
System.out.print(" " +m3[i][j]);
}
System.out.println();
}
input.close();
}
}
Output: -
1
https://
Ans :-
// Java Program for matrix transpose using input/output stream class.
import java.util.Scanner;
public class MatrixTranspose{
public static void main(String[] args)
{ Scanner input = new
Scanner(System.in); int original[][]=new
int[3][3] ;
System.out.println("Enter the elements of matrix: ");
for(int row = 0; row<3; row++)
{
for(int col = 0; col<3; col++)
{ original[row][col] = input.nextInt();
}
}
int transpose[ ][ ] = new int[3][3];
for(int row = 0; row<3; row++)
{
for(int col = 0; col<3; col++)
{
transpose[row][col] = original[col][row];
}
}
System.out.println("Transpose of matrix : \n");
for(int row = 0; row<3; row++)
{ for(int col = 0; col<3; col++)
{
System.out.print(" "+ transpose[row][col] );
}
System.out.print("\n");
}
}
}
1
https://
Output:-
1
https://
Ans :-
2
https://
}
catch(Exception e)
{ System.out.println("\nProgram ended...............");
System.out.println("Exception : "+e.getMessage());
}
}
}
Output:-
2
https://
Q13) write a program to check that the given String is palindrome or not.
Ans :-
if (isPalindrome(str))
System.out.println(str + " is a palindrome.");
else
System.out.println(str + " is not a
palindrome.");
}
}
2
https://
Output:-
2
https://
Ans :-
2
https://
for (int i = 0; i <= count - 1; i++)
{
System.out.print(str[i] + ", ");
}
System.out.println();
}
}
Output:-
2
https://
Q15) write a program for StringBuffer class which perform the all methods
of that class.
Ans :-
// Java Program for StringBuffer class which perform the all methods
of that class
import java.util.*;
public class StringBufferExample{
2
https://
Output:-
2
https://
Q16) write a program to calculate Simple interest using the Wrapper class.
Ans :-
,si;
Scanner input = new Scanner(System.in);
principleAmount = Integer.parseInt(p);
rate = Integer.parseInt(r);
time = Integer.parseInt(t);
Output:-
2
https://
Ans :-
2
https://
}
}
Output:-
3
https://
Q18) write a program where Single class implements more than one
interfaces and with help of interface reference variable user call the
methods.
Ans :-
}
class Interface{
rect = Area;
System.out.println("Area of Rectangle : " +
rect.calculate(5,20));
}
}
3
https://
Output:-
3
https://
Q19) write a program that use the multiple catch statements within the try-
catch mechanism.
Ans :-
try
{ int a[]=new int[5];
a[5]=30/0;
}
//Arithmetic Exception occurs
catch(ArithmeticException e)
{
System.out.println("\nArithmetic Exception
occurs");
}
//Array Index Out Of Bounds Exception occurs
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("ArrayIndexOutOfBounds Exception
occurs");
}
//Other Exception occurs
catch(Exception e)
{
System.out.println("Parent Exception occurs");
}
System.out.println("rest of the code");
}
}
Output:-
3
https://
Q20) write a program where user will create a self- Exception using the
“throw” keyword.
Ans :-
//Java Program for user will create a self- Exception using the
“throw” keyword
import java.lang.Exception;
class Throw{
public static void main(String args[])
{ int balance=5000;
int withdrawlAmount=6000;
try
{ if(balance< withdrawlAmount)
{ throw new
ArithmeticException("Insufficient balance");
}
balance=balance-withdrawlAmount;
System.out.println("Transaction successfully
completed");
}
catch(ArithmeticException e)
{ System.out.println("\nException: "+ e.getMessage());
}
finally
{ System.out.println("Program continue...........");
}
}
}
Output:-
3
https://
Ans :-
//Synchronized function
synchronized void bookseat(int seats)
{
if(total_seats>=seats)
{ System.out.println("\nSeat Book Successfull...!!");
total_seats=total_seats-seats;
System.out.println("Total seat Left : "+total_seats);
}
else
{ System.out.println("\nSeat cannot be Booked");
System.out.println("Only "+total_seats + "
available");
}
}
}
catch(NullPointerException e)
{ e.printStackTrace();
}
}
}
3
https://
{
cutomer C1=new cutomer();
cutomer C2=new cutomer();
C2.seat=19;
C1.seat=8;
C1.start();
C2.start();
//isAlive() method
System.out.println(C1.isAlive());
try
{
//Thread sleep() and join method
C2.sleep(2000);
C1.join();
}
catch (Exception e)
{
System.out.println("Exception : "+e.getMessage());
}
}
}
Output:-
3
https://
Q22) write a program to create a package using command and one package
will import another package.
Ans :-
3
https://
//Main class and method
//Save by ExamplePackage.java
import Pack1.ArithmeticOperations; //Importing Pack1.
ArithmeticOperations class
class ExamplePackage
{ public static void main(String args[])
{ ArithmeticOperations obj = new ArithmeticOperations();
System.out.print("\nAddition : Enter Two Number - ");
float result = obj.Addition();
System.out.println("Result is :"+result);
Output:-
3
https://
Q23) write a program for JDBC to insert the values into the existing table
by using prepared statement.
Ans :-
//Java Program to insert the values into the existing table by using
prepared statement.
import java.io.*;
import java.sql.*;
import java.util.*;
public class Database{
public static void main(String args[])
{ int roll;
String name,city, str;
try{ //step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
3
https://
System.out.print("Enter Address : " );
city = buffer.readLine();
if (count>0)
System.out.println("\n<-----Data inserted
successfully-------->\n");
else
System.out.println("Data insertion failed!!!!!!!");
Output:-
4
https://
Q24) write a program for JDBC to display the records from the existing
table.
Ans :-
//Java Program for JDBC to display the records from the existing
table.
import java.lang.*;
import java.sql.*;
public class OracleCon{
public static void main(String args[])
{ String sname,city;
int id;
try{
//step1 load the driver class
Class.forName("oracle.jdbc.driver.OracleDriver");
4
https://
catch(Exception e)
{ System.out.println("Exception occur : ");
System.out.println(e);
}
}
}
Output:-
4
https://
Ans :-
4
https://
}
System.out.println();
}
}
Output:-