AEQUALIS Software Solutions – Fresher Interview Questions & Answers – Part 2

1. How to get person’s age below 10 from the array list ?

 // Java program for age calculator import java.io.*;   class GFG {     static void findAge(int current_date, int current_month,                     int current_year, int birth_date,                     int birth_month, int birth_year)     {         int month[] = { 31, 28, 31, 30, 31, 30, 31,                               31, 30, 31, 30, 31 };           // if birth date is greater then current          // birth_month, then donot count this month         // and add 30 to the date so as to subtract          // the date and get the remaining days         if (birth_date > current_date) {             current_month = current_month – 1;             current_date = current_date + month[birth_month – 1];         }           // if birth month exceeds current month,          // then do not count this year and add          // 12 to the month so that we can subtract         // and find out the difference         if (birth_month > current_month) {             current_year = current_year – 1;             current_month = current_month + 12;         }           // calculate date, month, year         int calculated_date = current_date – birth_date;         int calculated_month = current_month – birth_month;         int calculated_year = current_year – birth_year;           // print the present age         System.out.println(“Present Age”);         System.out.println(“Years: ” + calculated_year +                ” Months: ” + calculated_month + ” Days: ” +                calculated_date);     }     public static void main(String[] args)     {         // present date         int current_date = 7;         int current_month = 12;         int current_year = 2017;           // birth dd// mm// yyyy         int birth_date = 16;         int birth_month = 12;         int birth_year = 2009;           // function call to print age         findAge(current_date, current_month, current_year,               birth_date, birth_month, birth_year);     } }

Present Age

Java String Array

• Java String array is basically an array of objects.
• There are two ways to declare string array – declaration without size and declare with size.
• There are two ways to initialize string array – at the time of declaration, populating values after declaration.
• We can do different kind of processing on string array such as iteration, sorting, searching etc.
• Java String Array Declaration

It shows different ways for string array declaration in java.

String[] strArray; //declare without size

String[] strArray1 = new String[3]; //declare with size

Note that we can also write string array as String strArray[] but above shows way is the standard and recommended way. Also in the above code, strArray is null whereas strArray1 value is [null, null, null].

Java String Array Initialization

Let’s look at different ways to initialize string array in java.

//inline initialization

String[] strArray1 = new String[] {“A”,”B”,”C”};

String[] strArray2 = {“A”,”B”,”C”};

//initialization after declaration

String[] strArray3 = new String[3];

strArray3[0] = “A”;

strArray3[1] = “B”;

strArray3[2] = “C”;

All the three string arrays will have same values. However if you will call equals method on them, it will return false.

Copy System.out.println(strArray1.equals(strArray2)); // false

System.out.println(Arrays.toString(strArray1).equals(Arrays.to

String(strArray2)));// true

The reason is that array are Objects and Object class implements equals() method like below.

public boolean equals(Object obj) {

return (this == obj);

}

Second statement is true because when converted to String, their values are same and String class equals() method implementation check for values.