0% found this document useful (0 votes)
6 views11 pages

Java Interview Programs and Solutions

The document contains various Java programs demonstrating fundamental programming concepts such as summing array elements, multiplication without the multiply operator, finding the maximum difference between adjacent array elements, comparing arrays, sorting arrays in both ascending and descending order, swapping numbers without a temporary variable, printing patterns, generating Fibonacci series, and checking for prime numbers.

Uploaded by

karanbir1991
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

Java Interview Programs and Solutions

The document contains various Java programs demonstrating fundamental programming concepts such as summing array elements, multiplication without the multiply operator, finding the maximum difference between adjacent array elements, comparing arrays, sorting arrays in both ascending and descending order, swapping numbers without a temporary variable, printing patterns, generating Fibonacci series, and checking for prime numbers.

Uploaded by

karanbir1991
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Interview java programs

// Create a method which accepts Array and returns sum of all the elements in
the array

class Sumofelements {

public static int sumarray(int[] a)

int sum=0;

for(int i=0; i<[Link];i++){

sum=sum+a[i];

return sum;

public static void main(String[] args) {

int[] a={1,2,3,4,5};

int sum=sumarray(a);

[Link]("sum= "+sum);

}}

// print 5 Multiplication table without using Multiply operator


class Multiplywithoutmultioperator {

public static int multiply(int i,int j)

int k=1;

int result=0;

while(k<=j){

result=result+i;

k++;

return result;

public static void main(String[] args) {

int result=multiply(5,10);

[Link]("result= "+result);

}}

// Max difference between any adjacent index in array

class AdjacentArray {

public static void main(String[] args) {


int[] a={1,4,8,15,17};

int diff=0;

for(int i=1;i<[Link];i++){

if(a[i]-a[i-1]>diff){

diff=a[i]-a[i-1];

[Link]("diff= "+diff);

}}

//compare same indexes of 2 different arrays and create another array for
matching values

import [Link];

import [Link];

public class Main {

public static void main(String[] args) {

int[] a= {9,7,6,2,3,4};

int[] b={4,7,3,2,5,4};

ArrayList <Integer> c=new ArrayList<>();

for(int i=0; i<[Link];i++){

if(a[i]==b[i]){
[Link](a[i]);

int[] d= new int[[Link]()];

for(int j=0;j<[Link]();j++){

d[j]=[Link](j);

[Link]([Link](d));

}}

//sort the array in ascending order

import [Link];

import [Link];

public class Main {

public static void main(String[] args) {

int[] a= {9,7,6,2,3,4};

int temp;

for(int i=0; i<[Link];i++){

for(int j=0; j<[Link];j++){

if(a[i]<a[j]){

temp=a[i];

a[i]=a[j];
a[j]=temp;

[Link]([Link](a));

//sort the array in ascending order without temp variable

import [Link];

import [Link];

public class Main {

public static void main(String[] args) {

int[] a= {9,7,6,2,3,4};

for(int i=0; i<[Link];i++){

for(int j=0; j<[Link];j++){

if(a[i]<a[j]){

a[i]=a[j]+a[i];

a[j]=a[i]-a[j];

a[i]=a[i]-a[j];

}
}

[Link]([Link](a));

//sort the array in Descending order

import [Link];

import [Link];

public class Main {

public static void main(String[] args) {

int[] a= {9,7,6,2,3,4};

int temp;

for(int i=0; i<[Link];i++){

for(int j=0; j<[Link];j++){

if(a[i]>a[j]){

temp=a[i];

a[i]=a[j];

a[j]=temp;

[Link]([Link](a));
}

//sort the array in Descending order without temp variable

import [Link];

import [Link];

public class Main {

public static void main(String[] args) {

int[] a= {9,7,6,2,3,4};

for(int i=0; i<[Link];i++){

for(int j=0; j<[Link];j++){

if(a[i]>a[j]){

a[i]=a[j]+a[i];

a[j]=a[i]-a[j];

a[i]=a[i]-a[j];

[Link]([Link](a));

// swap 2 numbers without temp variable

public class Main {

public static void main(String[] args) {


int a=6;

int b=7;

a=a+b;

b=a-b;

a=a-b;

[Link]("a = " +a);

[Link]("b = " +b);

}}

// print pattern in java

**

***

****

*****

public class Main {

public static void main(String[] args) {

for(int i=1;i<=5;i++)

for(int j=1;j<=i;j++){

[Link]("* ");

}
[Link]();

}}

//fibonacci series

public class Main {

public static void main(String[] args) {

int n=0;

int n1=1;

[Link](n);

[Link](n1);

int n2=0;

for(int i=1;i<=10;i++)

n2=n1+n;

n=n1;

n1=n2;

[Link](n2);

}
}}

// Prime number

public class Main {

public static void main(String[] args) {

int num=1647;

boolean isPrime=true;

if(num<=1){

isPrime=false;

else{

for(int i=2;i<=num/2;i++)

if(num%i==0){

isPrime=false;

break;

[Link]("number is prime "+isPrime);


}}

You might also like