Code Robo
Virtual Service
Tester / Comparator
Converter
Utility
Java Code Complience
Validator
Formatter
EncoderDecoder

Java Interview Question And Answer
Talk to EasyAssistant

Java Interview Question And Answer. Java interview question and answer related to core java. . .


Questions:
How do we compare two String objects?
We use equal() method of String class to compare two String objects. We must not use '==' operator to compare the string values of two String objects as its check the reference only- whether both are pointig to the same object or not.
String str1="ABC";
String str1="XYZ";
if(str1.equals(str2)){
System.out.println("Both are equals");
}

What is Class?
Class is the defination of an object. It's the set of property and operations(methods) to describe an object. Example:
public class Column {
	private String size = "";
	private String dataType = "";
	private String name = "";
	private String constraint = "";
	public String getDataType() {
		return dataType;
	}
	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

What is Object?
It's just instance of a class. Object belongings to same class will have similar properties and will support same operations. Example: Here col1 and col2 are two objects of Column class. Both will have same properties e.g. size, name, dataType
public class Table {
	
	Column col1 = new Column();
	Column col2 = new Column();
}

What is static in Java?
'static' is a keyword in java. staic means its value does not changes with instances. All instance share the same value. When we declare a class variable is static, it means that variable is associated with the Class not with the instance of the class. All instance of that class will have the same value for that variable (property).
1). A method can be static 2). A class can be static 3). A class variable can be static
Example: In the GrandI10Car class we have defined noOfWheels and wheelsDiameter property as static becuase all the GrandI10Car object(car) will have 4 wheel with diameter 16 inch. So noOfWheel and wheelsDiameter property does not change object(car) to object(car) where as color and modelNo changes object(car) to object(car)
public class GrandI10Car {
	private String color = "";
	private String modeNo = "";
	private static double noOfWheels;
	private static double wheelsDiameter;

	public void setColor(String color) {
		this.color = color;
	}

	public void setModeNo(String modeNo) {
		this.modeNo = modeNo;
	}

	public static void setNoOfWheels(double noOfWheels) {
		GrandI10.noOfWheels = noOfWheels;
	}

	public static void setWheelsDiameter(double wheelsDiameter) {
		GrandI10.wheelsDiameter = wheelsDiameter;
	}

	public double getPrice() {
		if ("Sportz".equals(modeNo))
			return 800000;
		else
			return 850000;
	}

}
Why main method is static and public?
Main method is the entry point for execution of any java program / application. When we try to run a java program (java <Class Name>), JVM invokes the main method of the class. So main method of the class has to be static. Once execution starts, JVM will be able to create object.
It's public (or has to be public) as its JVM invoked it from outside of the java program(class)
Signature of the main method is given below. All three are valid.
public static void main(String []args) {}
public static void main(String[] args) {}
public static void main(String args[]) {}
What is JVM?
JVM is Java Virtual Machine. Its get installed in your machine, Once you install Java or JRE in your laptop, It executes the Java Program(.class file). It can execute any .class file. A JVM installed in Linux machine can execute a .class file generated (complied) in windows.

What do you mean by "Java is a machine independent language"?
Java is a machine independent languagage becuase any .class file generated in one operating system (e.g. windows) can be run in other operation system(e.g. Linux). Example:- Many time we download jar file (e.g. log4j.jar) and start using it in our windows laptop without any change/modification. This jar files may not necessarily built(developed) in windows. May be its developed in linux. This is possible as Java is a machine independent language. But JVM is not machine independent. For Linux m/c you need to install linux version of JVM (JDK / JRE).

What are the access specifier in Java?
  • Public - It can be accessed from any other class either by extending or creating objects.
  • Protected - It can be accessed by the class of the same package, or by the sub-class of this class, or within the same class.
  • Default - It is accessible within the package only.
  • Private - It can be accessed by same class only.It can not be inherited or accesing by creating objects.
We can specifiy access specifier for Java Class, method, and class variables. Private and Public are understood by everybody easily. But People get confused with Protected and Default. Let me expain it.

What is enum, Enum and Enumeration in Java?
enum: enum is a data type in Java. It can store a set of values. Normally any other datatype (e.g int) store a single value where as enum is used to store a series of values. enum is very similar to a static variable. In can be accessed from both static and not-static methods;

Enum: It's a java class (java.lang.Enum). This is the common base class of all Java language enumeration types.

Enumeration: It's a java interface. An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series. StringTokenizer has implemented this interface.

Example:
import java.util.Enumeration;
import java.util.StringTokenizer;

public class T {
	public enum DAY {
		SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
	}

	private enum SIGLE_DGIT_EVEN_NO {
		TWO, FOUR, SIX
	}

	private static Enum abc;

	static Enumeration enumerationObj = null;

	public static void main(String args[]) throws Exception {
		System.out.println("day.sunday=" + DAY.SUNDAY);
		System.out.println("DAY.valueOf(\"MONDAY\")=" + DAY.valueOf("MONDAY"));

		Object arrObj[] = DAY.values();
		Enum arrEnum[] = DAY.values();
		abc = arrEnum[6];
		System.out.println("abc=" + abc);

		for (Object obj : arrObj) {
			System.out.println("First value in the array:" + obj);

			if (obj instanceof String) {
				System.out.println("It is instance Of : " + "String");
			}
			if (obj instanceof Enum) {
				System.out.println("It is instance Of : " + "Enum");
			}
			if (obj instanceof Object) {
				System.out.println("It is instance Of : " + "Object");
			}

			break;
		}

		enumerationObj = new StringTokenizer("abc, def", ",");
		if (enumerationObj.hasMoreElements()) {
			System.out.println("First Token Value is: "+enumerationObj.nextElement());
		}

	}

	public void getName() {
		System.out.println(DAY.MONDAY);
		DAY.valueOf("MONDAY");
	}
}

Output:
day.sunday=SUNDAY
DAY.valueOf("MONDAY")=MONDAY
abc=SATURDAY
First value in the array:SUNDAY
It is instance Of : Enum
It is instance Of : Object
First Token Value is: abc


What is instanceof
In Java instanceof is an operator. It is used to check if an object is the instance of a specified class or interface.
import java.io.File;
import java.io.IOException;

public class T {

	public void main(String[] args) {

		try {
			File.createTempFile("abc", "txt");
		} catch (Exception ex) {
			if (ex instanceof IOException) {
				System.out.println("It's and IO Exception");
			}
		}
	}

}


What is the output of System.out.println("Value="+"56789".valueOf(3));;
This question is tricky. Output will be 'Value=3'. Try to understand how it will print Value=3.
valueOf is a method of String class. It's return type is String. It returns the String representation of the argument we passed. "56789" is nothing but a String object. we are just invokeing the valueOf() static method of the String class.
So 'System.out.println("Value="+String.valueOf(3))' will also print 'Value=3'.




Please provide your feedback here
It has been explained in this video (https://youtu.be/bxkNb0GuELY). Please watch it.
 


User Comments:
Post Your Comment:
Name :
Email ( Optional*) :
Comments / Suggestion (* Required) It is required: :
: