Java Interview Programs 1
Java Interview Programs 1
input str1="Water,str2="Bottle"
o/p-WatBottleer
public class StringManipulation1 {
public static void main(String[] args) {
String str1="Water";
String str2="Bottle";
//str2.replaceFirst("", str1.substring(0, 3));
//o/p WatBottle
//str1.substring(str1.length()-2, str1.length());
//o/p er
System.out.println(str2.replaceFirst("",
str1.substring(0, 3))+ str1.substring(str1.length()-2,
str1.length()));
}
}
Write a Program to print average of the integer array
elements and also to print the mean base on odd or even
number of elements in the array
public class ArrayAverage {
public static void main(String[] args) {
int[] numbers = new int[]{10,20,15,25,16,60,100,5,7};
//to print the average of array elements
int sum = 0;
for(int i=0; i < numbers.length ; i++)
sum = sum + numbers[i];
double average = sum / numbers.length;
System.out.println("Average value of array
elements is : " + average);
//to give you the mean based on odd or even elements
// in the array
if (numbers.length % 2==0)
{
int num1pos=numbers.length/2;
int num2pos=num1pos +1;
double mean=(numbers[num1pos-
1]+numbers[num2pos-1])/2;
System.out.println(mean);
}
else
{
int num1pos=numbers.length/2;
System.out.println(numbers[num1pos]);
}
}
}
Write a program to divide a number without using / operator
public class DivideWithOutOperator {
public static void main(String[] args) {
int number = 26;
int divisor = 5;
int result = 0;
while((number-divisor)>=0){
result++;
number = number - divisor;
}
System.out.println(result);
}
}
Write a program to multiply 2 numbers without using number without using *
multiplication operator
public class MultiplyWithoutOperator {
public static void main(String[] args) {
int number1 = 10;
int number2 = 5;
int result = 0;
for(int i=1;i<=number2;i++)
{
result=result + number1;
}
System.out.println(result);
}
}
Write a program to sort numbers and digits in a given String
public class SortingNumberAndDigits {
public static void main(String[] args) {
String str="abcd123efgh456";
char[] charArray = str.toCharArray();
StringBuffer str1=new StringBuffer();
StringBuffer str2=new StringBuffer();
for(char ch: charArray)
{
if (Character.isDigit(ch))
{
str1=str1.append(ch);
}
else
{
str2=str2.append(ch);
}
}
System.out.println(str1);
System.out.println(str2);
}
}
Write a program to print A-Z and a-z
public class PrintA2Z {
public static void main(String[] args) {
for(char ch='a';ch<='z';ch++){
System.out.print(ch+" ");
}
System.out.println();
for(char ch='A';ch<='Z';ch++){
System.out.print(ch+" ");
}
}
}
Write a program to reverse a String and also
Sort the string characters alphabetically.
public class ReverseAndSort {
public static void main(String[] args) {
String str="Hello Chennai";
StringBuffer str1 = new StringBuffer(str);
System.out.println(str1.reverse());
//to put it in a string
str=str1.reverse().toString();
System.out.println(str);
//code to sort
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
str=new String(charArray);
System.out.println(str);
}
}
Write a program to print a the following Triangle
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
public class TriangleOne {
public static void main(String[] args) throws IOException {
System.out.println("Enter the number of rows");
Scanner in = new Scanner(System.in);
int numRow = in.nextInt();
for (int i = 1; i <= numRow; i++) {
// Prints the blank spaces
for (int j = 1; j <= numRow - i; j++) {
System.out.print(" ");
}
// Prints the value of the number
for (int k = 1; k <= i; k++) {
System.out.print("1 ");
}
System.out.println();
}
} }
Write a program to print a the following Triangle
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
public class RowNumberIncrementTriangle {
public static void main(String[] args) throws IOException
{
System.out.println("Enter the number of rows");
Scanner in = new Scanner(System.in);
int numRow = in.nextInt();
for (int i = 1; i <= numRow; i++) {
// Prints the blank spaces
for (int j = 1; j <= numRow - i; j++) {
System.out.print(" ");
}
// Prints the value of the number
for (int k = 1; k <= i; k++) {
System.out.print(i +" ");
}
System.out.println();
}
}
}
Write a program to print a the following Triangle
1
32
654
10987
public class FlippedTriangle
{
public static void main(String[] args)
{
int rows=4;
int cntr=1;
int start;
int val;
for(int i=1;i<=rows;i++)
{
for(int k=rows-i;k>=1;k--)
{
System.out.print(" ");
}
start=cntr + i-1;
val=start;
for(int j=1;j<=i;j++)
{
System.out.print(start);
start--;
cntr++;
}
System.out.println();
}
}
}
Write a program to print the next characters in a given String
Ex:
String s1=Selenium
o/p should be- Tfmfojvn
public class SetNextCharForString {
public static void main(String[] args) {
String str="Selenium";
StringBuffer str1=new StringBuffer();
char arr[]=str.toCharArray();
for(int i=0;i<=arr.length-1;i++)
{
char ch=arr[i];
str1=str1.append(++ch);
}
System.out.println(str1);
}
}
Write a program to print the perfect numbers b/w 1-500
Ex:
The number 6 is said to be a perfect number because it is equal to the sum of all its exact
divisors (other than itself).
6 = 1 + 2 + 3
public class PerfectNumber{
public static void main(String[]args){
int sum=0, x=0;
for(int num=1;num<500;num++)
{
for(int i=1;i<num;i++)
{
x=num%i;
if(x==0)
sum=sum+i;
}
if(sum==num)
{
System.out.println("Perfect Number is: "+num);
System.out.println("Factors are: ");
for(int i=1;i<num;i++)
{
x=num%i;
if(x==0)
System.out.println(i);
}
}
sum=0;
}
}
}
Write a program to print the adams number
If the reverse square root of the reverse of square of a number is the number itself then it is Adam
Number.
12 and 21
Take 12
square of 12 = 144
reverse of square of 12 = 441
square root of the reverse of square of 12 = 21
The reverse square root of the reverse of square of 12 = 12, then number itself.
Such number is called Adam Number.
class AdamsNumber
{
public static void main(String[] args)
{
AdamsNumber an = new AdamsNumber();
int i, n, rn;
int sn, rsn, rrsn;
System.out.println("List of Adam Numbers under 1000");
for (i = 10; i < 1000; i++)
{
n = i;
rn = an.ReverseNumber(i);
if (n == rn)
continue;
sn = n * n;
rsn = rn * rn;
rrsn = an.ReverseNumber(rsn);
if (rrsn == sn)
{
System.out.println(n);
}
}
}
int CountNumberOfDigits(int n)
{
int numdgits = 0;
do
{
n = n / 10;
numdgits++;
}
while (n > 0);
return numdgits;
}
int ReverseNumber(int n)
{
int i = 0, result = 0;
int numdigits = CountNumberOfDigits(n);
for (i = 0; i < numdigits; i++)
{
result *= 10;
result += n % 10;
n = n / 10;
}
return result;
}
}