javainterview.in - Core Java









Search Preview

Core Java interview questions and answers for Freshers and Experienced

javainterview.in
Top 100 core java interview questions and answers for freshers and experienced. This is part 1 in a 3 part series on core java interview questions. In this part, we focus on JVM, Java Platform, Wrapper Classes, String, Object class methods and a lot more. Questions categorised by Experience - 2 to 3 Years, 4 to 6 Years and 7-10 Years.
.in > javainterview.in

SEO audit: Content analysis

Language Error! No language localisation is found.
Title Core Java interview questions and answers for Freshers and Experienced
Text / HTML ratio 70 %
Frame Excellent! The website does not use iFrame solutions.
Flash Excellent! The website does not have any flash contents.
Keywords cloud = Java String class method object int true Client return toString methods classes equals objects variable Integer false Java? code
Keywords consistency
Keyword Content Title Description Headings
= 69
Java 35
String 30
class 24
method 21
object 20
Headings
H1 H2 H3 H4 H5 H6
1 2 19 0 9 0
Images We found 9 images on this web page.

SEO Keywords (Single)

Keyword Occurrence Density
= 69 3.45 %
Java 35 1.75 %
String 30 1.50 %
class 24 1.20 %
method 21 1.05 %
object 20 1.00 %
int 19 0.95 %
true 17 0.85 %
Client 17 0.85 %
return 16 0.80 %
toString 14 0.70 %
methods 13 0.65 %
classes 13 0.65 %
equals 13 0.65 %
objects 13 0.65 %
variable 12 0.60 %
Integer 12 0.60 %
false 12 0.60 %
Java? 11 0.55 %
code 11 0.55 %

SEO Keywords (Two Word)

Keyword Occurrence Density
= new 24 1.20 %
What is 17 0.85 %
of the 11 0.55 %
toString method 10 0.50 %
in Java? 9 0.45 %
are the 8 0.40 %
What are 8 0.40 %
the content 8 0.40 %
SystemoutprintlnZ is 8 0.40 %
in the 8 0.40 %
content of 7 0.35 %
implementation of 7 0.35 %
is a 6 0.30 %
the same 6 0.30 %
Class Loader 6 0.30 %
the important 6 0.30 %
is the 6 0.30 %
equals method 6 0.30 %
of equals 6 0.30 %
method is 6 0.30 %

SEO Keywords (Three Word)

Keyword Occurrence Density Possible Spam
the content of 7 0.35 % No
What are the 6 0.30 % No
What is the 5 0.25 % No
are the important 5 0.25 % No
name String type 4 0.20 % No
in great detail 4 0.20 % No
in Java? What 4 0.20 % No
method in Java? 4 0.20 % No
new Client25 Client 4 0.20 % No
For any reference 4 0.20 % No
= new Client25 4 0.20 % No
different utility methods 4 0.20 % No
of equals method 4 0.20 % No
content of the 4 0.20 % No
toString method is 4 0.20 % No
implementation of equals 4 0.20 % No
Class Loader Loads 3 0.15 % No
the example below 3 0.15 % No
are pointing to 3 0.15 % No
Loader Loads all 3 0.15 % No

SEO Keywords (Four Word)

Keyword Occurrence Density Possible Spam
What are the important 4 0.20 % No
the content of the 4 0.20 % No
= new Client25 Client 4 0.20 % No
in Java? What is 3 0.15 % No
any reference values x 3 0.15 % No
For any reference values 3 0.15 % No
differences between C and 3 0.15 % No
Class Loader Loads all 3 0.15 % No
nested if else? Can 2 0.10 % No
a nested if else? 2 0.10 % No
is a nested if 2 0.10 % No
What is a nested 2 0.10 % No
are variables initialialized in 2 0.10 % No
variables initialialized in Java? 2 0.10 % No
else? Can you explain 2 0.10 % No
How are variables initialialized 2 0.10 % No
What is Explicit Casting? 2 0.10 % No
What is Implicit Casting? 2 0.10 % No
used for in Java? 2 0.10 % No
method used for in 2 0.10 % No

Internal links in - javainterview.in

In28Minutes
Java Interview Questions and Answers
Download Java Interview Pdf Guide
Java Interview PDF Guide with 400+ Questions
Popular Video Courses
20 Video Courses
Buy Java Interview Video Guide - $12.99/₹799
Get our Java Interview Video Guide
Posts (Atom)
Java Interview Questions and Answers For Freshers and Experienced
Core Java
Core Java interview questions and answers for Freshers and Experienced
Advanced Java
Advanced Java Interview Questions for Freshers and Experienced
Spring
Spring Interview Questions
Spring MVC
Spring MVC Interview Questions and Answers
Struts
Struts Interview Questions
Hibernate
Hibernate Interview Questions
Generics
Java Generics Interview Questions
Collections
Java Collections Interview Questions
Java New Features
Java New Features
Design Patterns
Java Design Patterns Interview Questions
Web Services
REST Web Services Interview Questions

Javainterview.in Spined HTML


CadreJava interview questions and answers for Freshers and Experienced Toggle navigation In28Minutes Download Java Interview Pdf Guide Popular Video Courses Buy Java Interview Video Guide - $12.99/₹799 Start learning on Udemy today!CadreJava interview questions and answers for Freshers and ExperiencedCadreJava interview questions are important expressly for freshers and developers who have less experience. We will imbricate all the important cadre java concepts in these interview questions. In wing to reading the questions, we recommend watching these two interview preparation videos tent important cadre java questions. A Freshers Guide - Part 1: https://www.youtube.com/watch?v=njZ48YVkei0 Java Interview : A Freshers Guide - Part 2: https://www.youtube.com/watch?v=xyXuo0y-xoU Why is Java so popular? What is Platform Independence? What are the important differences between C++ and Java? What are Wrapper Classes? What are the variegated utility methods present in wrapper classes? What is Auto Boxing? Are all String’s immutable? Where are String literals stored in memory? Can you requite examples of variegated utility methods in String class? Explain well-nigh toString method in Java? What is the use of equals method in Java? What are the important things to consider when implementing equals method? What is the hashCode method used for in Java? What is Casting? What is Implicit Casting? What is Explicit Casting? How are variables initialialized in Java? What is a nested if else? Can you explain with an example? Why is Java so Popular? Two main reasons for popularity of Java are Platform Independence Object Oriented Language We will squint at these in detail in later sections. What is Platform Independence? This video (https://www.youtube.com/watch?v=lLgcgvIHyAw) explains Platform Independence in unconfined detail. Refer to it for a increasingly detailed answer. Platform Independence is moreover tabbed build once, run anywhere. Java is one of the most popular platform self-sustaining languages. Once we compile a java program and build a matriculation file or a jar file, we can run the jar file or matriculation file (compiled java program) in any Platform - where a JVM is installed. Java achieves Platform Independence in a trappy way. On compiling a java file the output is a matriculation file - which contains an internal java representation tabbed bytecode. JVM converts bytecode to executable instructions. The executable instructions are variegated in variegated operating systems. So, there are variegated JVM's for variegated operating systems. A JVM for windows is variegated from a JVM for mac. However, both the JVM's understand the bytecode and convert it to the executable lawmaking for the respective operating system. What are the important differences between C++ and Java? Few differences between C++ and Java are listed below. Java is platform independent. C++ is not platform independent. Java is a pure Object Oriented Language (except for primitive variables). In C++, one can write structural programs without using classes and objects. C++ has pointers (access to internal memory). Java has no concept tabbed pointers. In C++, programmer has to handle memory management. A programmer has to write lawmaking to remove an object from memory. In Java, JVM takes superintendency of removing objects from memory using a process tabbed Garbage Collection. C++ supports Multiple Inheritance. Java does not support Multiple Inheritance. What is the role for a ClassLoader in Java? A Java program is made up of a number of custom classes (written by programmers like us) and cadre classes (which come pre-packaged with Java). When a program is executed, JVM needs to load the content of all the needed class. JVM uses a ClassLoader to find the classes. ThreeMatriculationLoaders are shown in the picture SystemMatriculationLoader - Loads all classes from CLASSPATH ExtensionMatriculationLoader - Loads all classes from extension directory BootstrapMatriculationLoader - Loads all the Java cadre files When JVM needs to find a class, it starts with SystemMatriculationLoader. If it is not found, it checks with ExtensionMatriculationLoader. If it not found, it goes to the BootstrapMatriculationLoader. If a matriculation is still not found, a ClassNotFoundException is thrown. What are Wrapper Classes? This video(https://www.youtube.com/watch?v=YQbZRw2yIBk) covers the topic in unconfined detail. A unenduring unravelment is provided below. A primitive wrapper matriculation in the Java programming language is one of eight classes provided in the java.lang package to provide object methods for the eight primitive types. All of the primitive wrapper classes in Java are immutable. Wrapper : Boolean,Byte,Character,Double,Float,Integer,Long,Short Primitive: boolean,byte,char,double, float, int , long,short Wrapper classes are final and immutable. Examples of creating wrapper classes are listed below. Integer number = new Integer(55);//int Integer number2 = new Integer("55");//StringBladdernumber3 = new Float(55.0);//double argumentBladdernumber4 = new Float(55.0f);//float argumentBladdernumber5 = new Float("55.0f");//String Character c1 = new Character('C');//Only char constructor //Character c2 = new Character(124);//COMPILER ERROR Boolean b = new Boolean(true); //"true" "True" "tRUe" - all String Values requite True //Anything else gives false Boolean b1 = new Boolean("true");//value stored - true Boolean b2 = new Boolean("True");//value stored - true Boolean b3 = new Boolean("False");//value stored - false Boolean b4 = new Boolean("SomeString");//value stored - false b = false; What are the variegated utility methods present in wrapper classes?A number of utility methods are specified in wrapper classes to create and convert them from primitives. valueOf MethodsProvide flipside way of creating a Wrapper Object Integer seven = Integer.valueOf("111", 2);//binary 111 is converted to 7 Integer hundred = Integer.valueOf("100");//100 is stored in variable YyyValue methodsYyyValue methods help in creating primitives Integer integer = Integer.valueOf(57); int primitive = seven.intValue();//57 bladder primitiveFloat = seven.floatValue();//57.0fBladderfloatWrapper = Float.valueOf(57.0f); int floatToInt = floatWrapper.intValue();//57 bladder floatToFloat = floatWrapper.floatValue();//57.0f parseYyy methodsparseYyy methods are similar to valueOf but they return primitive values int sevenPrimitive = Integer.parseInt("111", 2);//binary 111 is converted to 7 int hundredPrimitive = Integer.parseInt("100");//100 is stored in variable static toString methodLook at the example of the toString static method below. Integer wrapperEight = new Integer(8); System.out.println(Integer. toString(wrapperEight));//String Output: 8 Overloaded static toString method2nd parameter: radix System.out.println(Integer .toString(wrapperEight, 2));//String Output: 1000 static toYyyString methods.Yyy can be Hex,Binary,Octal System.out.println(Integer .toHexString(wrapperEight));//String Output:8 System.out.println(Integer .toBinaryString(wrapperEight));//String Output:1000 System.out.println(Integer .toOctalString(wrapperEight));//String Output:10 What is Auto Boxing? Autoboxing is the will-less conversion that the Java compiler makes between the primitive types and their respective object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is tabbed unboxing. Integer ten = new Integer(10); ten++;//allowed. Java does had work overdue the screen for us Boxing and new instancesAuto Boxing helps in saving memory by reusing once created Wrapper objects. However wrapper classes created using new are not reused. Two wrapper objects created using new are not same object. Integer nineA = new Integer(9); Integer nineB = new Integer(9); System.out.println(nineA == nineB);//false System.out.println(nineA.equals(nineB));//true Two wrapper objects created using boxing are same object. Integer nineC = 9; Integer nineD = 9; System.out.println(nineC == nineD);//true System.out.println(nineC.equals(nineD));//true Are all String’s immutable? This video (https://www.youtube.com/watch?v=wh6L8zO_Hr4 ) covers all the topics related to String’s in unconfined detail. Refer to it for increasingly details. Value of a String Object once created cannot be modified. Any modification on a String object creates a new String object. String str3 = "value1"; str3.concat("value2"); System.out.println(str3); //value1 Note that the value of str3 is not modified in the whilom example. The result should be prescribed to a new reference variable (or same variable can be reused). String concat = str3.concat("value2"); System.out.println(concat); //value1value2 Where are string literals stored in memory? All strings literals are stored in "String unvarying pool". If compiler finds a String literal,it checks if it exists in the pool. If it exists, it is reused.Pursuitstatement creates 1 string object (created on the pool) and 1 reference variable. String str1 = "value"; However, if new operator is used to create string object, the new object is created on the heap.Pursuitpiece of lawmaking create 2 objects. //1. String Literal "value" - created in the "String unvarying pool" //2. String Object - created on the heap String str2 = new String("value"); Can you requite examples of variegated utility methods in String class? String matriculation defines a number of methods to get information well-nigh the string content. String str = "abcdefghijk"; Get information from StringFollowing methods help to get information from a String. //char charAt(int paramInt) System.out.println(str.charAt(2)); //prints a char - c System.out.println("ABCDEFGH".length());//8 System.out.println("abcdefghij".toString()); //abcdefghij System.out.println("ABC".equalsIgnoreCase("abc"));//true //Get All notation from alphabetize paramInt //String substring(int paramInt) System.out.println("abcdefghij".substring(3)); //cdefghij //All notation from alphabetize 3 to 6 System.out.println("abcdefghij".substring(3,7)); //defy Explain well-nigh toString method in Java? This video (https://www.youtube.com/watch?v=k02nM5ukV7w ) covers toString in unconfined detail. toString method is used to print the content of an Object. If the toString method is not overridden in a class, the default toString method from Object matriculation is invoked. This would print some hashcode as shown in the example below. However, if toString method is overridden, the content returned by the toString method is printed.Consider the matriculation given below: matriculationUnprepossessing{ public Animal(String name, String type) { this.name = name; this.type = type; } String name; String type; } Run this piece of code:Unprepossessingunprepossessing = new Animal("Tommy", "Dog"); System.out.println(animal);//[email protected] Output does NOT show the content of unprepossessing (what name? and what type?). To show the content of the unprepossessing object, we can override the default implementation of toString method provided by Object class. Adding toString toUnprepossessingmatriculation matriculationUnprepossessing{ public Animal(String name, String type) { this.name = name; this.type = type; } String name; String type; public String toString() { return "Animal [name=" + name + ", type=" + type + "]"; } } Run this piece of code:Unprepossessingunprepossessing = new Animal("Tommy","Dog"); System.out.println(animal);//Animal [name=Tommy, type=Dog] Output now shows the content of the unprepossessing object. What is the use of equals method in Java?Equals method is used when we compare two objects. Default implementation of equals method is specified in Object class. The implementation is similar to == operator. Two object references are equal only if they are pointing to the same object. We need to override equals method, if we would want to compare the contents of an object. Consider the exampleVendeematriculation provided below. matriculationVendee{ private int id; public Client(int id) { this.id = id; } } == comparison operator checks if the object references are pointing to the same object. It does NOT squint at the content of the object.Vendeeclient1 = new Client(25);Vendeeclient2 = new Client(25);Vendeeclient3 = client1; //client1 and client2 are pointing to variegated vendee objects. System.out.println(client1 == client2);//false //client3 and client1 refer to the same vendee objects. System.out.println(client1 == client3);//true //similar output to == System.out.println(client1.equals(client2));//false System.out.println(client1.equals(client3));//true We can override the equals method in theVendeematriculation to trammels the content of the objects. Consider the example below: The implementation of equals method checks if the id's of both objects are equal. If so, it returns true. Note that this is a vital implementation of equals and increasingly needs to be washed-up to make it fool-proof. matriculationVendee{ private int id; public Client(int id) { this.id = id; } @Override public boolean equals(Object obj) {Vendeeother = (Client) obj; if (id != other.id) return false; return true; } } Consider running the lawmaking below.Unelevatedlawmaking compares the values (id's) of the objects.Vendeeclient1 = new Client(25);Vendeeclient2 = new Client(25);Vendeeclient3 = client1; //both id's are 25 System.out.println(client1.equals(client2));//true //both id's are 25 System.out.println(client1.equals(client3));//true What are the important things to consider when implementing equals method?Any equals implementation should satisfy these properties: Reflexive. For any reference value x, x.equals(x) returns true. Symmetric. For any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. Transitive. For any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) must return true. Consistent. For any reference values x and y, multiple invocations of x.equals(y) unceasingly return true or unceasingly return false, if no information used in equals is modified. For any non-null reference value x, x.equals(null) should return false. Let's now provide an implementation of equals which satisfy these properties: //Client matriculation @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false;Vendeeother = (Client) obj; if (id != other.id) return false; return true; } What is the hashCode method used for in Java? HashCode's are used in hashing to decide which group (or bucket) an object should be placed into. A group of object's might share the same hashcode. The implementation of hash lawmaking decides effectiveness of Hashing. A good hashing function evenly distributes object's into variegated groups (or buckets).A good hashCode method should have the pursuit properties: If obj1.equals(obj2) is true, then obj1.hashCode() should be equal to obj2.hashCode() obj.hashCode() should return the same value when run multiple times, if values of obj used in equals() have not changed. If obj1.equals(obj2) is false, it is NOT required that obj1.hashCode() is not equal to obj2.hashCode(). Two unequal objects MIGHT have the same hashCode. A sample hashcode implementation ofVendeematriculation which meets whilom constraints is given below: //Client matriculation @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; return result; } What is Casting?Casting is used when we want to convert on data type to another. There are two types of Casting: ImplicitTossingExplicitTossingWhat is Implicit Casting?ImplicitTossingis washed-up by the compiler. Good examples of implicit tossing are all the will-less widening conversions i.e. storing smaller values in larger variable types. int value = 100; long number = value; //ImplicitTossingbladder f = 100; //ImplicitTossingWhat is Explicit Casting?ExplicitTossingis washed-up through code. Good examples of explicit tossing are the narrowing conversions - storing larger values into smaller variable types. Explicit tossing would rationalization truncation of value if the value stored is greater than the size of the variable. long number1 = 25678; int number2 = (int)number1;//ExplicitTossing//int x = 35.35;//COMPILER ERROR int x = (int)35.35;//ExplicitTossingint bigValue = 280; byte small = (byte) bigValue; System.out.println(small);//output 24. Only 8 shit remain. How are variables initialialized in Java?Following are the important rules regarding variable initialisation in Java Member and Static variables are unchangingly initialized with default values. Default values for numeric types is 0, floating point types is 0.0, boolean is false, char is '\u0000' and object reference variable is null. Local/block variables are NOT initialized by compiler. If local variables are used surpassing initialization, it would result in a compilation error. package com.rithus.variables; public matriculation VariableInitialization { public static void main(String[] args) { Player player = new Player(); //score is an int member variable - default 0 System.out.println(player.score);//0 - RULE1 //name is a member reference variable - default null System.out.println(player.name);//null - RULE1 int local; //not initialized //System.out.println(local);//COMPILER ERROR! RULE3 String value1;//not initialized //System.out.println(value1);//COMPILER ERROR! RULE3 String value2 = null;//initialized System.out.println(value2);//null - NO PROBLEM. } } What is a nested if else? Can you explain with an example?Look at the example below. The lawmaking in first if condition which is true is executed. If none of the if conditions are true, then lawmaking in else is executed. int z = 15; if(z==10){ System.out.println("Z is 10");//NOT executed } else if(z==12){ System.out.println("Z is 12");//NOT executed } else if(z==15){ System.out.println("Z is 15");//executed. Rest of the if else are skipped. } else { System.out.println("Z is Something Else.");//NOT executed } z = 18; if(z==10){ System.out.println("Z is 10");//NOT executed } else if(z==12){ System.out.println("Z is 12");//NOT executed } else if(z==15){ System.out.println("Z is 15");//NOT executed } else { System.out.println("Z is Something Else.");//executed } If you loved these Questions, you will love our PDF Interview Guide with 400+ Questions. Download it now!. 400+ Interview Questions in 4 Categories: Java :CadreJava, Advanced Java, Generics, Exception Handling, Serialization, Threads, Synchronization, Java New Features Frameworks : Spring, Spring MVC, Struts, Hibernate Design : Design, Design Patterns,LawmakingReview Architecture : Architecture, Performance & Load Testing, Web Services, REST Web Services,Security, Continuous Integration Home Subscribe to: Posts (Atom) Best Selling Java Interview Udemy Course 400+ Java Interview Questions 25 Java Interview VideosCadreJava Advanced Java Spring Spring MVC Struts Hibernate Generics Collections Java New Features Design Patterns Web Services © Java Interview Questions and Answers For Freshers and Experienced 2013 . Powered by Bootstrap and Bootstrap Blogger Templates