Java Questions
Java Questions
Java Questions
Based On
Programmig
Language
Prepared By-
100 Java Programs
Contents
Java Programs ............................................................................................................................................................. 1
1. Simple Java Program .......................................................................................................................................... 4
2. Print Integer in java ............................................................................................................................................. 4
3. Command Line Argument................................................................................................................................... 4
4. How to get Using input using Scanner Program in java ................................................................................ 5
5. How to convert Fahrenheit to Celsius Program in java ............................................................................... 5
6. How to swap 2 no using 3rd variable Program in java .................................................................................... 6
7. How to swap 2 no without using 3rd variable Program in java ...................................................................... 6
8. How to add two number Program in java ........................................................................................................ 7
9. Find Largest no in java Program ....................................................................................................................... 8
10. If Else clause in java ....................................................................................................................................... 8
11. If Else clause in java- Program 2 ................................................................................................................... 9
12. Nested If Else clause in java .......................................................................................................................... 9
13. How to check Odd and Even Number in java ............................................................................................. 10
14. Find factorial for given no Program in Java ............................................................................................... 10
15. How to complete 2 string in Java program ................................................................................................ 11
16. Simple For Loop Program in Java .............................................................................................................. 12
17. Print Star console using Loop ..................................................................................................................... 12
18. Print Star console using Loop ..................................................................................................................... 13
19. While loop Program in java .......................................................................................................................... 13
20. Print Reverse number in java program ...................................................................................................... 14
21. While loop using break Program in java .................................................................................................... 14
22. While loop using break and continue Program in java .............................................................................. 15
23. Print all alphabet using for loop Program in java....................................................................................... 15
24. Enhance loop in java Program .................................................................................................................... 16
25. Print Multiplication table Program in java.................................................................................................. 16
26. Print prime no Program in java ................................................................................................................... 17
27. Check no is Armstrong or not in java Program ........................................................................................ 18
28. Print Floyd’s Triangle in java Program ................................................................................................................ 19
29. Find All substring of string in java Program .............................................................................................. 19
30. Print reverse string in java Program .......................................................................................................... 20
31. Check Given No is palindrome or Not in java Program ............................................................................ 21
32. How to add two matrix in java Program ...................................................................................................... 22
33. How to multiply two matrix in java Program ............................................................................................... 23
34. How to get transpose of matrix in java Program ....................................................................................... 24
35. How to compare 2 string in java Program ................................................................................................. 25
36. How to string width with specific char in java Program ........................................................................... 25
37. How to use indesOf() in java Program ....................................................................................................... 25
38. How to replace string with another string in java Program ...................................................................... 26
class GetInputFromUser
{
public static void main(String args[])
{
int a;
float b;
String s;
System.out.println("Enter a string");
s = in.nextLine();
System.out.println("You entered string "+s);
System.out.println("Enter an integer");
a = in.nextInt();
System.out.println("You entered integer "+a);
System.out.println("Enter a float");
b = in.nextFloat();
System.out.println("You entered float "+b);
}
}
class FahrenheitToCelsius {
public static void main(String[] args) {
float temperatue;
Scanner in = new Scanner(System.in);
class SwapNumbers
{
public static void main(String args[])
{
int x, y, temp;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
temp = x;
x = y;
y = temp;
class SwapNumbers
{
public static void main(String args[])
{
int x, y;
System.out.println("Enter x and y");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
class AddNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = x + y;
System.out.println("Sum of entered integers = "+z);
}
}
//For Large Number
import java.util.Scanner;
import java.math.BigInteger;
class AddingLargeNumbers {
public static void main(String[] args) {
String number1, number2;
Scanner in = new Scanner(System.in);
sum = first.add(second);
class LargestOfThreeNumbers
{
public static void main(String args[])
{
int x, y, z;
System.out.println("Enter three integers ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
z = in.nextInt();
if (learning) {
System.out.println("Java programmer");
}
else {
System.out.println("What are you doing here?");
}
}
}
11. If Else clause in java- Program 2
// If else in Java code
import java.util.Scanner;
class IfElse {
public static void main(String[] args) {
int marksObtained, passingMarks;
passingMarks = 40;
marksObtained = input.nextInt();
class NestedIfElse {
public static void main(String[] args) {
int marksObtained, passingMarks;
char grade;
passingMarks = 40;
marksObtained = input.nextInt();
class OddOrEven
{
public static void main(String args[])
{
int x;
System.out.println("Enter an integer to check if it is odd or even ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
if ( x % 2 == 0 )
System.out.println("You entered an even number.");
else
System.out.println("You entered an odd number.");
}
}
class Factorial
{
public static void main(String args[])
{
int n, c, fact = 1;
n = in.nextInt();
if ( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for ( c = 1 ; c <= n ; c++ )
fact = fact*c;
class BigFactorial
{
public static void main(String args[])
{
int n, c;
BigInteger inc = new BigInteger("1");
BigInteger fact = new BigInteger("1");
System.out.println("Input an integer");
n = input.nextInt();
class CompareStrings
{
public static void main(String args[])
{
String s1, s2;
Scanner in = new Scanner(System.in);
if ( s1.compareTo(s2) > 0 )
System.out.println("First string is greater than second.");
else if ( s1.compareTo(s2) < 0 )
System.out.println("First string is smaller than second.");
else
System.out.println("Both strings are equal.");
}
}
class WhileLoop {
public static void main(String[] args) {
int n;
System.out.println("Out of loop");
}
}
class ReverseNumber
{
public static void main(String args[])
{
int n, reverse = 0;
while( n != 0 )
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
class BreakWhileLoop {
public static void main(String[] args) {
int n;
while (true) {
System.out.println("Input an integer");
n = input.nextInt();
if (n == 0) {
break;
}
System.out.println("You entered " + n);
}
}
}
22. While loop using break and continue
Program in java
import java.util.Scanner;
class BreakContinueWhileLoop {
public static void main(String[] args) {
int n;
while (true) {
System.out.println("Input an integer");
n = input.nextInt();
if (n != 0) {
System.out.println("You entered " + n);
continue;
}
else {
break;
}
}
}
}
class MultiplicationTable
{
public static void main(String args[])
{
int n, c;
System.out.println("Enter an integer to print it's multiplication
table");
Scanner in = new Scanner(System.in);
n = in.nextInt();
System.out.println("Multiplication table of "+n+" is :-");
class Tables
{
public static void main(String args[])
{
int a, b, c, d;
System.out.println("Enter range of numbers to print their multiplication
table");
Scanner in = new Scanner(System.in);
a = in.nextInt();
b = in.nextInt();
class PrimeNumbers
{
public static void main(String args[])
{
int n, status = 1, num = 3;
if (n >= 1)
{
System.out.println("First "+n+" prime numbers are :-");
System.out.println(2);
}
class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;
temp = n;
while (temp != 0) {
digits++;
temp = temp/10;
}
temp = n;
while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}
if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}
return p;
}
}
class FloydTriangle
{
public static void main(String args[])
{
int n, num = 1, c, d;
Scanner in = new Scanner(System.in);
System.out.println();
}
}
}
class SubstringsOfAString
{
public static void main(String args[])
{
String string, sub;
int i, c, length;
length = string.length();
System.out.println("Substrings of \""+string+"\" are :-");
class ReverseString
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
class Palindrome
{
public static void main(String args[])
{
String original, reverse = "";
Scanner in = new Scanner(System.in);
if (original.equals(reverse))
System.out.println("Entered string is a palindrome.");
else
System.out.println("Entered string is not a palindrome.");
}
}
//Another Method
import java.util.*;
class Palindrome
{
public static void main(String args[])
{
String inputString;
Scanner in = new Scanner(System.in);
System.out.println("Input a string");
inputString = in.nextLine();
begin = 0;
end = length - 1;
middle = (begin + end)/2;
class AddTwoMatrix
{
public static void main(String args[])
{
int m, n, c, d;
Scanner in = new Scanner(System.in);
System.out.println();
}
}
}
class MatrixMultiplication
{
public static void main(String args[])
{
int m, n, p, q, sum = 0, c, d, k;
if ( n != p )
System.out.println("Matrices with entered orders can't be multiplied
with each other.");
else
{
int second[][] = new int[p][q];
int multiply[][] = new int[m][q];
multiply[c][d] = sum;
sum = 0;
}
}
System.out.print("\n");
}
}
}
}
class TransposeAMatrix
{
public static void main(String args[])
{
int m, n, c, d;
System.out.print("\n");
}
}
}
// Constructor method
Methods() {
System.out.println("Constructor method is called when an object of it's
class is created");
}
// Static method
void nonStaticMethod() {
System.out.println("Non static method must be called by creating an
object");
}
}
System.out.println(s);
t = s.replace("Java", "C++");
System.out.println(s);
System.out.println(t);
u = s.concat(" is fun");
System.out.println(s);
System.out.println(u);
}
}
static {
System.out.println("Static block is executed before main method.");
}
}
//Static Block Application …. We need to open Program in speciif window
class StaticBlock {
public static void main(String[] args) {
System.out.println("You are using Windows_NT operating system.");
}
static {
String os = System.getenv("OS");
if (os.equals("Windows_NT") != true) {
System.exit(1);
}
}
}
void show(){
System.out.println("Java is awesome.");
}
}
void computer_method() {
System.out.println("Power gone! Shut down your PC soon...");
}
my.computer_method();
your.laptop_method();
}
}
class Laptop {
Laptop() {
System.out.println("Constructor of Laptop class.");
}
void laptop_method() {
System.out.println("99% Battery available.");
}
}
Language() {
System.out.println("Constructor method called.");
}
Language(String t) {
name = t;
}
cpp.setName("C++");
java.getName();
cpp.getName();
}
void setName(String t) {
name = t;
}
void getName() {
System.out.println("Language name: " + name);
}
}
int a, b, result;
// try block
try {
result = a / b;
System.out.println("Result = " + result);
}
// catch block
catch (ArithmeticException e) {
System.out.println("Exception caught: Division by zero.");
}
}
}
try {
long data[] = new long[1000000000];
}
catch (Exception e) {
System.out.println(e);
}
finally {
System.out.println("finally block will execute always.");
}
}
}
52. How to create Interface in java Program
interface Info {
static final String language = "Java";
public void display();
}
class GetCurrentDateAndTime
{
public static void main(String args[])
{
int day, month, year;
int second, minute, hour;
GregorianCalendar date = new GregorianCalendar();
day = date.get(Calendar.DAY_OF_MONTH);
month = date.get(Calendar.MONTH);
year = date.get(Calendar.YEAR);
second = date.get(Calendar.SECOND);
minute = date.get(Calendar.MINUTE);
hour = date.get(Calendar.HOUR);
System.out.println("Current date is "+day+"/"+(month+1)+"/"+year);
System.out.println("Current time is "+hour+" : "+minute+" : "+second);
}
}
class RandomNumbers {
public static void main(String[] args) {
int c;
Random t = new Random();
class GarbageCollection
{
public static void main(String s[]) throws Exception
{
Runtime rs = Runtime.getRuntime();
System.out.println("Free memory in JVM before Garbage Collection =
"+rs.freeMemory());
rs.gc();
System.out.println("Free memory in JVM after Garbage Collection =
"+rs.freeMemory());
}
}
class IPAddress
{
public static void main(String args[]) throws Exception
{
System.out.println(InetAddress.getLocalHost());
}
}
59. How to open notepad in java Program
import java.util.*;
import java.io.*;
class Notepad {
public static void main(String[] args) {
Runtime rs = Runtime.getRuntime();
try {
rs.exec("notepad");
}
catch (IOException e) {
System.out.println(e);
}
}
}
class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];
class BinarySearch
{
public static void main(String args[])
{
int c, first, last, middle, n, search, array[];
first = 0;
last = n - 1;
middle = (first + last)/2;
class BubbleSort {
public static void main(String []args) {
int n, c, d, swap;
Scanner in = new Scanner(System.in);
}
}
}
}
t2.start();
t3.start();
}
}
fin.close();
}catch(Exception e){System.out.println(e);}
}
}
System.out.println("Protocol: "+url.getProtocol());
System.out.println("Host Name: "+url.getHost());
System.out.println("Port Number: "+url.getPort());
System.out.println("File Name: "+url.getFile());
}catch(Exception e){System.out.println(e);}
}
}
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
}
al.add("Rajendra");
al.add("Raja");
al.add("Ravi");
al.add("Technolamror");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
out itr
}
}
}
hm.put(100,"Rajendra");
hm.put(101,"Vijay");
hm.put(102,"Technolamror");
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
hm.put(100,"Rajendra");
hm.put(102,"Praveen");
hm.put(101,"Bipin");
hm.put(103,"Pankaj");
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
}
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
class Test2{
public static void main(String args[]){
SBI s=new SBI();
ICICI i=new ICICI();
AXIS a=new AXIS();
System.out.println("SBI Rate of Interest: "+s.getRateOfInterest());
System.out.println("ICICI Rate of Interest: "+i.getRateOfInterest());
System.out.println("AXIS Rate of Interest: "+a.getRateOfInterest());
} }