Home /
Arsip Untuk April 2016
Core java Interview Questions
- Ternary operator also known as conditional operator in java.
- Ternary operator used to evaluate boolean expression and it consists of three operands.
Ternary operator in java syntax:- variable x = (expression) ? value if true : value if false
- Value of x will be decided based on the condition. if condition is true then it assigns first value to the variable
- If Expression is false then it assigns second value to the variable.
Java simple example program on ternary operator / conditional operator in java- package operatorsinjava;
- public class TernaryOperator {
-
- /**
- * @ www.instanceofjava.com
- *
- */
- public static void main(String[] args) {
-
- // checking condition 2>3 and if true assign 3 else 3
- int x= (2>3)?3:2;
-
- System.out.println(x);
-
- // checking condition 5<4 if true assign 4 else 3
- int y= (5<4)?4:3;
-
- System.out.println(y);
-
- }
-
- }
Output:Ternary operator in java for boolean: Java simple example program on ternary operator / conditional operator for boolean- package operatorsinjava;
- public class TernaryOperator {
-
- /**
- * @ www.instanceofjava.com
- *
- */
- public static void main(String[] args) {
-
-
- boolean x= true ? true: false;
-
- System.out.println(x);
-
- boolean y= false ? true : false;
-
- System.out.println(y);
-
- boolean z= false ? false : true;
-
- System.out.println(z);
-
- }
-
- }
Output:Ternary operator in java for Strings: Java simple example program on ternary operator / conditional operator for String- package operatorsinjava;
- public class TernaryOperatorForStrings{
-
- /**
- * @ www.instanceofjava.com
- *
- */
- public static void main(String[] args) {
-
- String str= "java".equals("java") ? "Java Questions" : "Java Programs";
-
- System.out.println(str);
-
- String str1= (1==3)? "its true" : "its false";
-
- System.out.println(str1);
-
- }
-
- }
Output:Ternary operator in java for null; Java simple example program on ternary operator / conditional operator for null check- package operatorsinjava;
- public class TernaryOperatorForStrings{
-
- /**
- * @ www.instanceofjava.com
- *
- */
- public static void main(String[] args) {
-
- String str= null;
-
- String str1= (str==null) ? "its true" : "its false";
-
- System.out.println(str1);
-
- String str2= (str1!=null) ? "its true" : "its false";
-
- System.out.println(str2);
- }
-
- }
Output:
exception handling interview questions
Is there any chance of getting multiple exception?- Lets see a java example programs which can raise multiple exceptions.
- package exceptions;
- public class MultipleCatchBlocks {
-
- /**
- * @www.instanceofjava.com
- */
-
- public static void main(String[] args) {
-
-
- int x=10;
- int y=0;
-
- try{
-
- int i=x/y;
- int a[]=new int[2];
- a[3]=12;
-
- } catch(ArithmeticException e){
- e.printStackTrace();
- }
-
- }
-
- }
Output:- java.lang.ArithmeticException: / by zero
at exceptions.MultipleCatchBlocks.main(MultipleCatchBlocks.java:15)
- In the 1st program there is a chance of getting two types of exceptions and we kept only one catch block with ArithmeticException e .
- But there will be chance of getting ArrayIndexOutOfBoundsException too.
- So we need to keep multiple catch blocks in order to handle all those exceptions
- Or else we can keep single catch block with super class Exception class object.
Multiple catch blocks for single try block:
- One try can have multiple catch blocks
- Every try should and must be associated with at least one catch block.( try without catch)
- Whenever an exception object is identified in try block and if there are multiple catch blocks then the priority for the catch block would be given based on order in which catch blocks are have been defined.
- Highest priority would be always given to first catch block . If the first catch block can not handle the identified exception object then it considers the immediate next catch block.
Disadvantages of multiple catch blocks: - We have to know the names of every corresponding exception class names.
- We have to understand which statement is generating which exception class object.
Solution: - Define a single catch block so that it should be able to handle any kind of exceptions identified in the try block.
- Exception class is the super class for all the exception classes.
- To the reference of super class object we can assign any sub class object.
exception handling interview questions
- It is possible to have try block without catch block by using finally block
- Java supports try with finally block
- As we know finally block will always executes even there is an exception occurred in try block, Except System.exit() it will executes always.
- We can place logic like connections closing or cleaning data in finally.
Java Program to write try without catch block | try with finally block- package exceptionsInterviewQuestions;
- public class TryWithoutCatch {
-
-
- public static void main(String[] args) {
-
-
- try {
-
- System.out.println("inside try block");
-
- } finally{
-
- System.out.println("inside finally block");
- }
-
- }
-
- }
Output:- inside try block
- inside finally block
- Finally block executes Even though the method have return type and try block returns something
Java Program to write try with finally block and try block returns some value- package exceptionsInterviewQuestions;
-
- public class TryWithFinally {
-
- public static int method(){
-
-
- try {
-
- System.out.println("inside try block");
-
- return 10;
- } finally{
-
- System.out.println("inside finally block");
- }
-
- }
-
- public static void main(String[] args) {
-
- System.out.println(method());
-
- }
-
- }
Output:- inside try block
- inside finally block
- 10
What happens if exception raised in try block?- Even though exception raised in try block finally block executes.
java Collections interview Questions
1.Write a Java program to remove duplicate elements from an arraylist without using collections (without using set)- package arrayListRemoveduplicateElements;
- import java.util.ArrayList;
-
- public class RemoveDuplicates {
- public static void main(String[] args){
-
- ArrayList<Object> al = new ArrayList<Object>();
-
- al.add("java");
- al.add('a');
- al.add('b');
- al.add('a');
- al.add("java");
- al.add(10.3);
- al.add('c');
- al.add(14);
- al.add("java");
- al.add(12);
-
- System.out.println("Before Remove Duplicate elements:"+al);
-
- for(int i=0;i<al.size();i++){
-
- for(int j=i+1;j<al.size();j++){
- if(al.get(i).equals(al.get(j))){
- al.remove(j);
- j--;
- }
- }
-
- }
-
- System.out.println("After Removing duplicate elements:"+al);
-
- }
-
- }
Output:- Before Remove Duplicate elements:[java, a, b, a, java, 10.3, c, 14, java, 12]
- After Removing duplicate elements:[java, a, b, 10.3, c, 14, 12]
2. Write a Java program to remove duplicate elements from an array using Collections (Linkedhashset)- package arrayListRemoveduplicateElements;
-
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
-
- public class RemoveDuplicates {
-
- public static void main(String[] args){
-
- List<String> arraylist = new ArrayList<String>();
-
- arraylist.add("instanceofjava");
- arraylist.add("Interview Questions");
- arraylist.add("Interview Programs");
- arraylist.add("java");
- arraylist.add("Collections Interview Questions");
- arraylist.add("instanceofjava");
- arraylist.add("Java Experience Interview Questions");
-
-
- System.out.println("Before Removing duplicate elements:"+arraylist);
-
- HashSet<String> hashset = new HashSet<String>();
-
- /* Adding ArrayList elements to the HashSet
- * in order to remove the duplicate elements and
- * to preserve the insertion order.
- */
- hashset.addAll(arraylist);
-
- // Removing ArrayList elements
- arraylist.clear();
-
- // Adding LinkedHashSet elements to the ArrayList
- arraylist.addAll(hashset );
-
- System.out.println("After Removing duplicate elements:"+arraylist);
-
- }
-
- }
Output: - Before Removing duplicate elements:[instanceofjava, Interview Questions, Interview
- Programs, java, Collections Interview Questions, instanceofjava, Java Experience Interview
- Questions]
- After Removing duplicate elements:[java, Collections Interview Questions, Java Experience
- Interview Questions, Interview Questions, instanceofjava, Interview Programs]
Core java Interview Questions
3. Enum Iteration in for-each loop:
- You can obtain an array of all the possible values of a Java enum type by calling its static values() method.
- All enum types get a static values() method automatically by the Java compiler. Here is an example of iterating all values of an enum:
- for (Day day : Day.values()) {
- System.out.println(day);
- }
- If you run this code, then you will get the following output:
- SUNDAY
- MONDAY
- TUESDAY
- WEDNESDAY
- THURSDAY
- FRIDAY
- SATURDAY
4. Enum fields:- It is possible to add fields to Java Enum. Each constant enum value gets these fields. The field values must be supplied to the constructor of the enum when defining the constants.
- Here is an example:
- public enum Day {
- SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5),
- FRIDAY(6), SATURDAY(7);
-
- private final int dayCode;
- private Day(int dayCode) {
- this.dayCode = dayCode;
- }
-
- }
- The example above has a constructor which takes an int. The enum constructor sets the int field. When the constant enum values are defined, an int value is passed to the enum constructor.
- The enum constructor must be either private or package scope (default). You cannot use public or protected constructors for a Java enum.
5. Enum Methods:
- It is possible to add methods to Java Enum. Here I am showing an example that how to do it.
- public enum Day {
-
- SUNDAY(1), MONDAY(2), TUESDAY(3), WEDNESDAY(4), THURSDAY(5),
- FRIDAY(6), SATURDAY(7);
-
- private final int dayCode;
-
- private Day(int dayCode) {
- this.dayCode = dayCode;
- }
- public int getDayCode() {
- return this.dayCode;
- }
-
- }
- It is possible to call an Enum method through a reference to one of the constant values. The following code will show you how to call Java Enum method.
- Day day = Day.MONDAY;
- System.out.println(day.getDayCode()); //which gives 2.Because the dayCode field for the Enum constant MONDAY is 2.
Some details to know about enum:- Java enums extend the java.lang.Enum class implicitly, so your enum types cannot extend another class. Please refer the following code. This code is from Java API.
- public abstract class Enum<E extends Enum<E>> implements Comparable<E>,Serializable {
- // Enum class code in Java API
- }
- If a Java enum contains fields and methods, the definition of fields and methods must always come after the list of constants in the enum. Additionally, the list of enum constants must be terminated by a semicolon;
- Java does not support multiple inheritance, and enum also does not support for multiple inheritance.
- Enums are type-safe. Enum has their own name-space. It means your enum will have a type for example “Day” in below example and you cannot assign any value other than specified in Enum Constants.
- Enum constants are implicitly static and final and cannot be changed once created.
- Enum can be safely compare using:
- Switch-Case Statement
- == Operator
- .equals() method
- You cannot create instance of enums by using new operator in Java because constructor of Enum in Java can only be private and Enums constants can only be created inside Enums itself.
- Instance of Enum in Java is created when any Enum constants are first called or referenced in code
- An enum can be declared outside or inside a class, but NOT in a method.
- An enum declared outside a class must NOT be marked static, final , abstract, protected , or private
- Enums can contain constructors, methods, variables, and constant class bodies.
- Enum constructors can have arguments, and can be overloaded.
- Enum constructors can have arguments, and can be overloaded.
- The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself
- package com.enum.exampleprogram;
-
- /**This is an Enum which indicates possible days in a week **/
-
- public enum Day {
- SUNDAY, MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY;
- }
- package com.pamu.test;
- /** This class is an example of Enum **/
- public class EnumTest {
- Day day;
- public EnumTest(Day day) {
- this.day = day;
- }
- //method to get descriptions of the respected days in a switch-statement.
-
- public void getDayDescription() {
-
- switch (day) {
- case MONDAY:
- System.out.println("Monday is First day in a week.");
- break;
- case FRIDAY:
- System.out.println("Friday is a partial working day.");
- break;
- case SATURDAY:
- System.out.println("Saturday is a Weekend.");
- break;
- case SUNDAY:
- System.out.println("Sunday is a Weekend.");
- break;
- default:
- System.out.println("Midweek days are working days.");
- break;
- }
- }
- public static void main(String[] args) {
-
- EnumTest day1 = new EnumTest(Day.MONDAY);
- day1.getDayDescription();
-
- EnumTest day2 = new EnumTest(Day.TUESDAY);
- day2.getDayDescription();
-
- EnumTest day3 = new EnumTest(Day.WEDNESDAY);
- day3.getDayDescription();
-
- EnumTest day5 = new EnumTest(Day.FRIDAY);
- day5.getDayDescription();
-
- EnumTest day6 = new EnumTest(Day.SATURDAY);
- day6.getDayDescription();
-
- EnumTest day7 = new EnumTest(Day.SUNDAY);
- day7.getDayDescription();
-
- }
- }
Output:- Monday is First day in a week.
- Midweek days are working days.
- Midweek days are working days.
- Friday is a partial working day.
- Saturday is a Weekend.
- Sunday is a Weekend.
Core java Interview Questions
Java Enum:
- In this tutorial I will explain what is enum, how to use enum in different areas of a Java program and an example program on it.
- An Enum type is a special data type which is added in Java 1.5 version. It is an abstract class in Java API which implements Cloneable and Serializable interfaces. It is used to define collection of constants. When you need a predefined list of values which do not represent some kind of numeric or textual data, at this moment, you have to use an enum.
- Common examples include days of week (values of SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY), directions, and months in a year. Etc.
- You can declare simple Java enum type with a syntax that is similar to the Java class declaration syntax. Let’s look at several short Java enum examples to get you started.
Enum declaration Example 1:- public enum Day {
- SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
- }
Enum declaration Example 2:- public enum Month{
- JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY, AUGUST, SEPTEMBER,
- OCTOBER, NOVEMBER, DECEMBER
- }
- Enums are constants; they are by default static and final. That’s why the names of an enum type's fields are in uppercase letters.
- Make a note that the enum keyword which is used in place of class or interface. The Java enum keyword signals to the Java compiler that this type definition is an enum.
You can refer to the constants in the enum above like this:
- Here the ‘day’ variable is of the type ‘Day’ which is the Java enum type defined in the example above. The ‘day’ variable can take one of the ‘Day’ enum constants as value (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY). In this case ‘day’ is set to MONDAY.
- If you use enum instead of integers or String codes, you increase compile-time checking and avoid errors from passing in invalid constants, and you document which values are legal to use.
- How to use a Java enum type in various areas in a Java Program:
- We have seen how to declare simple Java enum types, let’s take a look at how to use them in various areas. We have to use a Java enum type in a variety of situations, including in a Java 5 for loop, in a switch statement, in an if/else statement, and more. For simple, Enum comparison, there are 3 ways to do it. They are, Switch-Case Statement, == Operator, .equals() method. Like that there are other places where you have to use Enum.
- Let's take a look at how to use our simple enum types with each of these Java constructs.
- Enum in IF statement
- Enum in Switch statement
- Enum Iteration in for-each loop
- Enum Fields
Enum Methods
1. Enum in IF statements:
- We know Java Enums are constants. Sometimes, we have a requirement to compare a variable of Enum constant against the possible other constants in the Enum type. At this moment, we have to use IF statement as follows.
- Day day = ----- //assign some Day constants to it.
- If(day ==Day.MONDAY){
- ….//your code
- } else if (day == Day.TUESDAY){
- ….//your code
- } else if (day == Day.WEDNESDAY){
- ….//your code
- } else if (day == Day. THURSDAY){
- ….//your code
- } else if (day == Day.FRIDAY){
- ….//your code
- } else if (day == Day.SATURDAY){
- ….//your code
- } else if (day == Day.SUNDAY){
- ….//your code
- }
- Here, you can use “.equals()” instead of “==”. But, my preference is to use “==” because, it will never throws NullPointerException, safer at compile-time, runtime and faster.
- The code snippet compares the ‘day’ variable against each of the possible other Enum constants in the ‘Day’ Enum.
2. Enums in Switch Statements:
- Just assume that your Enum have lot of constants and you need to check a variable against the other values in Enum. For a small Enum IF Statement will be OK. If you use same IF statement for lot of Enum constants then our program will be increased and it is not a standard way to write a Java program. At this Situation, use Switch Statement instead of IF statement.
- You can use enums in switch statements as follows:
- Day day = ... //assign some Day constant to it
- switch (day) {
-
- case SUNDAY : //your code; break;
- case MONDAY //your code; break;
- case TUESDAY : //your code; break;
- case WEDNESDAY : //your code; break;
- case THURSDAY: //your code; break;
- case FRIDAY : //your code; break;
- case SATURDAY : //your code; break;
-
- }
- Here, give your code in the comments, “//your code”. The code should be a simple Java operation or a method call..etc
3.Enum Iteration in for-each loop 4.Enum Fields5.Enum MethodsPlease click here
Enum in java part 2