Java Coding Practice

SQL INTERVIEW QUESTION PART-2

Java Coding Practice

Below is the list of questions for java coding practice, it will be helpful for your next interview.

 

1. To find out the duplicate entry in an array

In order to find out the duplicate element in the array, we will use HashSet Class. Now HashSet class follows a mechanism to store the element known as hashing. It’s the property of HashSet that it only stores unique elements and we will use this property of HashSet to find ou the duplicate element in the array.

package javaprograns;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Duplicates_Elements_in_Array {
public static void main(String[] args) {
String name[]= {"java","dotnet","C","C++","java"};
Set storenew HashSet();
for(String namesname)
{
if(store.add(names)==false)
{
System.out.println("duplicate element is:  "names);
}
}
}
}

 

2. Java program to check the given number is prime or not

The numbers which are divisible by 1 and itself are known as Prime Numbers. Like: 2,3,5,7,11. 2 is the smallest prime number. 

package javaprograns;
public class Check_Prime {
public static boolean isPrime(int num)
{
if(num<=1)
{
return false;
}
for(int i=2;i < num ; i++)
{
if(num%i == 0)
{
return false;
}
}
return true;
}
        public static void main(String[] args) {
System.out.println("Is 3 is prime number? "+isPrime(3));
System.out.println("Is 2 is prime number? "+isPrime(2));
System.out.println("Is 1 is prime number? "+isPrime(1));
System.out.println("Is 17 is prime number? "+isPrime(17));
}
}

 

3. Java program to find out the factorial of a given number.

The factorial of a number is simply the product of all the integer from 1 to that number. For example factorial of 7 is (1*2*3*4*5*6*7) = 5040

Denoted as 7!

So how will you find out the factorial of a given?

package javaprograns;
public class Factorial {
public static void fact(int num)
{
System.out.println("Given Number is: "num);
int fact=1;
for(int i=1; i<=numi++)
{
fact=fact*i;
}
System.out.println("Factorial is: "fact);
}
public static void main(String[] args) {
fact(5);
fact(1);
fact(0);
        }
}

 

4. Java program to check the given number is a palindrome number or not.

Palindrome Numbers are the numbers whose reverse is the same number. Like: 151, 454, 34543, 161, 78987. So how to check whether the number is a palindrome number or not? For that, we will create logic in a method and will call that method in the main method.

package javaprograns;
public class Palindrome_Number {
public static void palindrome_number1(int num)
{System.out.println("Given Number is: "+num);
int rev=0;
int tnum;
while(num!=0)
{
rev=rev*10+num%10;
num=num/10;
}
if(rev== t)
{
System.out.println("  is a palindrome number");
}
else
{
System.out.println("  is not a palindrome number");
}
}
public static void main(String[] args) {
palindrome_number1(121);
palindrome_number1(030);
palindrome_number1(123);
palindrome_number1(90912);
}
}

 

5. Java program to check the given number is Armstrong number or not.

The Armstrong number is a special number that is equal to the sum of cubes of its digits. Like for example 0, 1, 153, 370, 371 and 407 are the Armstrong numbers.

153 = (1*1*1) + (5*5*5) + (3*3*3)

407 = (4*4*4)+(0*0*0)+(7*7*7)

So how will you find out the given number is Armstrong number.

package javaprograns;
public class Armstrong_Number {
public static void arms_num(int num)
{
System.out.println("Given number is: "+num);
int t;
int cube=0;
int r=0;
t=num;
while(num>0)
{
r=num%10;
num=num/10;
cube=cube+(r*r*r);
}
if (cube == t)
{
System.out.println("This is armstrong number");
}
else
{
System.out.println("This is not a armstrong number");
}
}
public static void main(String[] args) {
arms_num(153);
arms_num(0);
arms_num(407);
arms_num(40);
}
}

Check Java Interview Questions

Wikipedia Java