Categories
Core JAVA

Top 40 JAVA Programming Questions

JAVA Programming Questions

JAVA Programming Questions

How to add argument in java

class AddArguments
{
public static void main(String[] args)
{
int sum=0;
for(int i=0;i<args.length;i++)
{
sum=sum+Integer.parseInt(args[i]);
}
System.out.println("after add all args :"+sum);
}
}

Program for Create an Array in JAVA

class Test
{
public static void main(String[] args)
{
int a[]=new int[5]{1,2,3,4,5};          
for (int i=0;i< a.length;i++ ) 
{
    System.out.println(a);  
}

}
}

Factorial Program in JAVA

class factorial
{
public static void main(String[] args)
{
int x=1;
int n=5;
while(n>0)
{
x=x*n;
n--;
}
System.out.println(x);
}
}

Write a Program to find out greatest between float number using Command line.

class Greatest
{
public static void main(String[] args)
{
float no1=Float.parseFloat(args[0]);
float no2=Float.parseFloat(args[1]);
float no3=Float.parseFloat(args[2]);    
float big =no1>no2 ?( no1 >no3 ? no1 : no3):(no2>no3 ?no2:no3);
    System.out.println(big);
}
}

Program for Local Variable in JAVA

class Local
{
void fun(int z)
{
z=700;
System.out.println(z);
}
public static void main(String[] args)
{
int x=50;
int y=100;
System.out.println(x);
System.out.println(y);
Local t=new Local();
t.fun(600);
}
}

Constructor Program in JAVA

class constructor
{
constructor()
{
System.out.println("constructor");
}
static void constructor()
{
System.out.println("method");
}
public static void main(String[] args)
{
constructor();
constructor.constructor();
new constructor();
}
}

Copy Constructor in JAVA

class Student
{
int roll;
String name;
Student (Student k)
{
    roll=k.roll;
    name=k.name;
}
Student()
{

}
public static void main(String[] args) 
{
    Student s1=new Student();
    System.out.println("state of s1:"+s1.roll+"\t\t"+s1.name);
    s1.roll=100;
    s1.name="abc";
    System.out.println("state of s1:"+s1.roll+"\t\t"+s1.name);
    Student s2=new Student(s1);
    System.out.println("state of s2:"+s2.roll+"\t\t"+s2.name);
    s2.roll=101;
    s2.name="abcd";
    System.out.println("state of s2:"+s2.roll+"\t\t"+s2.name);
    Student s3=new Student(s2);
    System.out.println("state of s3:"+s3.roll+"\t\t"+s3.name);
    s3.name="abcde";
    s3.roll=102;
    System.out.println("state of s3:"+s3.roll+"\t\t"+s3.name);
    Student s4=new Student(s3);
    System.out.println("state of s4:"+s4.roll+"\t\t"+s4.name);  
}
}

PrimeFactor Program in JAVA

class PrimeFactor
{
public static void main(String[] args) throws java.io.IOException
{
java.io.BufferedReader br=new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
int num=Integer.parseInt(br.readLine());  
  findFactors(num);
}
static void findFactors(int num)
{
    for (int i=1;i<=num;i++) 
    {
        if(num%i==0)
            checkPrime(i);
    }
}
static void checkPrime(int num)
{
    int count=0;
    for (int i=1;i<=num;i++) 
    {
        if(num%i==0)
        count++;    
    }
    if(count==2)
        System.out.println(num);
}
}

Pyramid Program in JAVA

class Pyramid
{
public static void main(String[] args)
 {
     int row=Integer.parseInt(args[0]);      
    for(int i=0;i<row;i++)  
    {
         for(int j=0;j<row-i;j++) 
         {
             System.out.print(" ");
        }
        for(int k=0;k<=i;k++) 
        {
            System.out.print(" " +k);
        }
        System.out.println();  
    }

}
}

Prime Number Program in JAVA

class Prime
{
public static void main(String args[])
{
java.util.Scanner sc=new java.util.Scanner(System.in);
int num,i,c=0;
System.out.println("Enter a number");
num=sc.nextInt();  
  for (i=2;i<=num/i;i++) 
    {
            if (num%i==0) 
            {
                c=1;
                break;  
            }       
    }   
    if (c==0) 

        System.out.println("It is a prime Number....");
    else
        System.out.println("It's not a prime number");
}
}

Amstrong Number Program in JAVA

class Amstrong
{
public static void main(String[] args)
{
int num1,num2,s;
java.util.Scanner sc=new java.util.Scanner(System.in);
System.out.println("enter a number :");
int a=sc.nextInt();
num2=a;
num1=0;
while(a!=0)
{
s=a%10;
num1+=(sss);
a=a/10;
}
if(num1==num2)
System.out.println("Amstrong Number ..");
else
System.out.println("not an Amstrong Number ..");
}
}

Program to calculate strong number using console class

class Strong
{
static int fact(int num)
{
int f=1;
while(num!=0)
f*=num--;
return f;
}
public static void main(String[] args)
{
int bef,aft;
java.io.Console c=System.console();
System.out.println("enter a number:");
int n=Integer.parseInt(c.readLine());   
 bef=n;
    aft=0;

    while(n!=0)
    {
        aft+=fact(n%10);
        n=n/10;
    }
    if(bef==aft)
        System.out.println("stong number");
    else
        System.out.println("not a Strong number");
}
}

Program to calculate strong numbers between 1-1000 in JAVA.

class Strongrange
{
static int fact(int num)
{
int f=1;
while(num!=0)
f*=num--;   
 return f;
}
public static void main(String[] args) 
{
    int bef,aft,i,n;
    for (i=1;i<=100000;i++) 
    {
        n=bef=i;
        aft=0;
        while(n!=0)
        {
            aft+=fact(n%10);
            n=n/10;
        }
        if (bef==aft)
            System.out.println(bef);
    }
}
}

Claculate reverse of a number program in JAVA

class Reverse
{
public static void main(String[] args)
{
java.util.Scanner sc=new java.util.Scanner(System.in);
int num,rev;
System.out.println("enter a number ..");
num=sc.nextInt();   
 rev=0;
    while(num!=0)
    {
        rev=(rev*10)+(num%10);
        num=num/10;
    }
    System.out.println("reverse of the number :"+rev);
}
}

Check the given number is palindrome program in JAVA.

class Palindrome
{
public static void main(String[] args)
{
java.util.Scanner sc=new java.util.Scanner(System.in);
int num,rev,a;
System.out.println("enter a number :");
num=sc.nextInt();
rev=0;
a=num;
while(num!=0)
{
rev=(rev*10)+(num%10);
num=num/10;
}
if(a==rev)
System.out.println("palindrome");
else
System.out.println("not palindrome");
}
}

How to display all the file in a directoy.

class Display_files
{
public static void main(String[] args)
{
File dir=new File("c:");
String[]children=dis.list();
if(children == null)
{
System.out.println("enter dir dose not exit or is not a directory");
}
else
{
for(int i=0;i>children.length;i++)
{
string filename=children[i];
System.out.println(filename);
}
}
}
}

Swap between two number Program in JAVA

class Swap
{
public static void main(String[] args)
{
int a,b;
java.util.Scanner sc=new java.util.Scanner(System.in);   
 System.out.println("enter two number....");
    a=sc.nextInt();
    b=sc.nextInt();
    System.out.println("Before Swpping....:a="+a+" b="+b);
        a=a+b;
        b=a-b;
        a=a-b;

        System.out.println("after swapping...:a="+a+" b="+b);
}
}

Fibonaci Series number

class Fibonaic
{
public static void main(String[] args)
{
java.util.Scanner sc=new java.util.Scanner(System.in);
int a,b,c,range;
a=0;
b=1;
System.out.println("Enter the range: ");
range=sc.nextInt();
System.out.print(" "+a+" "+b);    
for(int i=1;i<=range;i++)
    {
        c=a+b;
        System.out.println(" "+c);
        a=b;
        b=c;
    }
}
}

Check leapyear or not program in JAVA

class Leapyear
{
public static void main(String[] args)
{
int year=Integer.parseInt(args[0]);
if((year%400==0)||(year%4==0)&&(year%100==0))
System.out.println("leapyear");
else
System.out.println("non-leapyear");
}
}

Scanner Class Program in JAVA

class Test
{
public static void main(String[] args)
{
java.util.Scanner sc=new java.util.Scanner(System.in);System.out.println("enter your name");
    String name=sc.next();

    System.out.println("enter your roll");
    int roll=sc.nextInt();

    System.out.println("enter your cgpa");
    float cgpa=sc.nextFloat();

    System.out.println("enter your fees");
    long fees=sc.nextLong();

    System.out.println("--------------------");
    System.out.println("name:"+name);
    System.out.println("roll:"+roll);
    System.out.println("cgpa:"+cgpa);
    System.out.println("fees:"+fees);   
}
}

Find the letter character number spaces in a String

class Test
{
public static void main(String[] args)
{
char ch=65;
char ch1='A';
if(ch==ch1)
System.out.println("true");
else
System.out.println("flase");
String text="JAVA is simple ,1.8 is the latset version";    
int digit=0,let=0,upper=0 ,lower=0,space=0;
    {
        char letter=text.charAt(i);
        if(Character.isDigit(letter));
            digit++;
        if(Character.isLetter(letter));
            letter++;
        if(Character.isUpperCase(letter));
            upper++;
        if(Character.isLowerCase(letter));
            lower++;
        if(Character.isWhitespace(letter));
            space++;
    }
    System.out.println("text");
    System.out.println("No of Letter :"+let);
    System.out.println("No of UpperCse :"+upper);
    System.out.println("No of LowerCase :"+lower);
    System.out.println("No of space :"+space);

}
}

Method in Java

public class JAVAMETHOD
{
static int y=10;
int z=22;
static void show() //static method
{
System.out.println("i am in show()");
}
void run() //non static method
{
System.out.println("I am in run()");
}
final void A() //final method
{
System.out.println("i am in final()");
}
public static void main(String[] args)
{
int x=5;
System.out.println(+x);
System.out.println(JAVAMETHOD.y);
JAVAMETHOD J=new JAVAMETHOD();
System.out.println(J.z);
J.run();
show();
}
}

Integer to Octal Conversion

class Test
{
public static void main(String[] args)
{
System.out.println(Integer.toOctalString(10));
}
}

INSTANCE OF OPERATOR Program in JAVA

class Demo extends Test
{
public static void main(String[] args)
{
Test t1=new Test();
Demo d1=new Demo();
System.out.println(t1 instanceof Test);
System.out.print(t1 instanceof Demo);
System.out.println(d1 instanceof Test);
System.out.println(d1 instanceof Demo);
}
}

Object Program in JAVA

class Test
{
static int a=100;
int b=30;
public static void main(String[] args)
{
System.out.println(a); //by directly
// System.out.println(b); non-static variable b cannot be referenced from a static context
// by class name
System.out.println(Test.a);
// System.out.println(Test.b); non-static variable b cannot be referenced from a static context
// by object name
Test obj=new Test();
System.out.println(obj.a);
System.out.println(obj.b);
}
}

Variable function block constructor string string buffer string builder

import java.util.*;
public class AllinOne
{
static int w=40;
int x=10;
void fun()
{
int z=3;
System.out.println("i am in fun()");
System.out.println(z);
}
static void test()
{
System.out.println("i am in staic function");
}
final void satya()
{
System.out.println("i am in final()");
}
AllinOne()
{
System.out.println("i am in default ctor");
}
AllinOne(String x)
{
System.out.println("i am in parameterised constructor");
}
public static void main(String[] args)
{
int y=20;
AllinOne a=new AllinOne();
System.out.println(a.x);
a.fun();
test();
a.satya();
System.out.println(w);
new AllinOne();
new AllinOne("as");
}
}

Data Input Stream Program in JAVA

import java.io.DataInputStream;
public class datainputstreamreader
{
public static void main(String[] args) throws java.io.IOException
{
java.io.DataInputStream dis=new java.io.DataInputStream(System.in);
System.out.println("enter your choice");
char ch=(char)dis.read(); 
  if(ch=='Y' || ch=='y')
        System.out.println("success");
    else 
        if(ch=='N' || ch=='n')
            System.out.println("not required");
        else
            System.out.println("entr a valid choice");
}
}

Thread Program in JAVA

public class SingleThread
{
static void fact() throws Exception
{
System.out.println("fact()");
System.out.println(Thread.currentThread().getName());
Thread.sleep(3000);
}
public static void main(String[] args) throws Exception
{
fact();
Thread.sleep(3000);
System.err.println("hi");
reverse();
}
static void reverse() throws Exception
{
System.out.println("reverse()");
System.out.println(Thread.currentThread().getName());
Thread.sleep(3000);
System.out.println("bye");
}
}

Array List Program in JAVA

import java.util.*;
class Employee
{
int id;
String name;
double salary;
public Employee(int id, String name, double salary) {
    super();
    this.id = id;
    this.name = name;
    this.salary = salary;
}
public String toString() {
    return id+"\t"+name+"\t"+salary+"\t"+"\n";
}
}
public class ArrayList_Ex1
{
public static void main(String[] args)
{
ArrayList list=new ArrayList();
System.out.println(list);
list.add(new Employee(100,"raja",5000));
list.add(new Employee(101,"rani",1000));
list.add(new Employee(102,"ajaya",2000));
list.add(new Employee(103,"raja",9000));
System.out.println(list);
}
}

Hashmap and Treemap in JAVA

import java.util.HashMap;
import java.util.TreeMap;
public class HashMap_TreeMap_Ex
{
public static void main(String[] args)
{
HashMap map1=new HashMap();
TreeMap map2=new TreeMap();    
map1.put(1, "Raja");
    map1.put(33, "praja");
    map1.put(33, "praja");
    map1.put(2, "aja");
    System.out.println(map1);

    map2.put(1, "Raja");
    map2.put(33, "praja");
    map2.put(33, "praja");
    map2.put(2, "aja");
    System.out.println(map2);

    System.out.println(map2.size());
    System.out.println(map2.containsKey(33));
    System.out.println(map2.containsValue("Raja"));
    System.out.println(map2.keySet());
    System.out.println(map2.values());
    System.out.println(map2.lastEntry());
    System.out.println(map2.isEmpty());
    System.out.println(map2.entrySet());
}
}

HashSet and TreeSet in JAVA

import java.util.*;
public class HashSet_TreeSet_Ex
{
public static void main(String[] args) {
HashSet set1=new HashSet();
TreeSet set2=new TreeSet();
int arr[]= {11,22,11,33,11,77,99};
for(int a:arr)
{
set1.add(a);
set2.add(a);
}
System.out.println(Arrays.toString(arr));
System.out.println(set1);
System.out.println(set2);
System.out.println(set1.size());
System.out.println(set2.size());
System.out.println(set1.contains(99));
set1.remove(99);
System.out.println(set1);
set2.remove(99);
System.out.println(set2);
}
}

Queue and Deque in Java

import java.util.*;
public class Q_DQ_Ex
{
public static void main(String[] args)
{
Queue q=new LinkedList();
q.add(1);
q.add(3);
q.add(67);
System.out.println(q);
System.out.println(q.peek());//peek-first element
q.remove(1);
System.out.println(q);    
System.out.println("------------------------------");

    Deque<Integer> dq=new LinkedList<Integer>();
    dq.add(1);
    dq.add(3);
    dq.add(67);
    System.out.println(dq);
    System.out.println(dq.peek());//peek-first element
    q.remove(1);
    System.out.println(dq);
    dq.addFirst(22);
    System.out.println(dq);
    dq.addLast(10);
    System.out.println(dq);
    dq.removeLast();
    System.out.println(dq);
    dq.removeFirst();
    System.out.println(dq);
}
}

Stack program in JAVA

import java.util.*;
public class Stack_Ex
{
public static void main(String[] args)
{
Stack st=new Stack();
st.push('A');//insert data into stack
st.push(new Character('A'));
st.push('H');
st.push('C');
st.push('V');
st.push('D');
st.push('M');
st.push('W');
System.out.println(st);
System.out.println(st.isEmpty()); //check empty or not
System.out.println(st.search('D')); //search element in which position
Collections.reverse(st);
System.out.println(st);
st.pop();
System.out.println(st);
System.out.println(st.peek());//first element
st.clear();
System.out.println(st);
}
}

Vector Program in JAVA

import java.util.Enumeration;
import java.util.Vector;
public class Vector_Ex
{
public static void main(String[] args)
{
Vector v=new Vector();
v.addElement(11);
v.addElement(21);
v.addElement(31);    
v.add(1,41);
    v.add(2,51);
    v.add(0, 31);

    System.out.println(v);
    System.out.println(v.size());

    v.removeElement(31);
    System.out.println(v);
    v.remove(0);
    System.out.println(v);

    System.out.println("Traverse by enumeration interface...");
    Enumeration<Integer> e=v.elements();
    while(e.hasMoreElements())
    {
        System.out.println(e.nextElement());
    }
}
}

//Enumeration is a predefined class which is present in java.util package..
//this enumeration is used to traverse the collection like vector
//iterator listiterator enumeration is suitable for which collection….

File Program in JAVA

import java.io.File;
public class File1
{
public static void main(String[] args) throws Exception
{
    File f1=new File("hello.txt");
    File f2=new File("hi.txt");
    System.out.println(f1.createNewFile());
    System.out.println(f1.length());
    System.out.println(f1.getName());
    System.out.println(f1.getAbsolutePath());
    System.out.println(f1.isFile());
    System.out.println(f1.exists());
    System.out.println(f1.delete());
    System.out.println(f1.getAbsolutePath());
    System.out.println(f1.exists());

    System.out.println(f1.renameTo(f2));
    System.out.println(f1.getName());
}
}

Abstract Class and Abstract Method in JAVA

abstract class Test
{
abstract void show();
abstract void fun1();
int x=90;
void display()
{
System.out.println("display()….");
}
}
abstract class Demo extends Test
{
void show()
{
System.out.println("show()….");
}
}
class MyClass extends Demo
{
void fun1()
{
System.out.println("fun1()….");
}
public static void main(String[] args)
{
MyClass mc=new MyClass();
mc.show();
mc.fun1();
System.out.println(mc.x);
mc.display();
}
}

Pattern Program in JAVA

class Pattern
{
public static void main(String[] args)
{
int row=Integer.parseInt(args[0]);
for(int i=1;i<=row;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(" "+j);
}
System.out.println();
}
}
}

Abstraction in JAVA

interface Body
{
abstract void fun();
void show();
}
abstract class Demo implements Body
{
abstract void display();
public void fun()
{
System.out.println("i am in fun…");
}
}
class Abstraction extends Demo
{
public void show()
{
System.out.println(" i am in show");
}
public void display()
{
System.out.println(" i am in display…");
}
public static void main(String[] args)
{
Abstraction ab=new Abstraction();
ab.display();
ab.fun();
ab.show();
}
}

Inheritance in JAVA

class Inheritance
{
void display()
{
System.out.println("i am in display");
}
}
class Test extends Inheritance
{
void fun()
{
System.out.println("i am in fun");
}
}
class MyClass extends Test
{
public static void main(String[] args)
{
MyClass mc=new MyClass();
mc.fun();
mc.display();
}
}
Hierarchical inheritance in JAVA
class A
{
void display()
{
System.out.println("i am in display");
}
}
class B extends A
{
void fun()
{
System.out.println(" a am in fun");
}
}
class C extends A
{
void show()
{
System.out.println("i am in show..");
}
}
class hierarchical extends A
{
void abc()
{
System.out.println("i am in abc..");
}
public static void main(String[] args) {
hierarchical h=new hierarchical();
h.abc();
C c=new C();
c.show();
B b=new B();
b.fun();
A a=new A();
a.display();
}
}

Multiple inheritance in JAVA

interface Abc
{
void fun();
}
interface Acb
{
void fun();
}
public class Multiple implements Abc,Acb
{
public void fun()
{
System.out.println("i am in fun");
}
public static void main(String[] args)
{
Multiple m=new Multiple();
m.fun();
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *