July 9 2015

Core java interview questions and answers for freshers

Core java interview questions and answers for freshers

  1. Legal identifier?

Names used for classes, methods, interfaces and variables are called Identifiers. The first character must start with a letter or currency character ($) or underscore (_). It cannot start with number. After first character, identifier can contain letters, numbers, currency symbol ($) and underscore (_) in any combination. Read more

  1. Class?

A class can be defined as a template that describes the state and behaviors that object of its type support.

public class Student {

// variables and methods

}

  1. Object?

An object is an instance of the class. Every object will have their copy of variables and access to the methods of the class.

Student student1=new Student();

  1. Different access modifiers a top level class can have?

A top level class can have only two access modifier

public – visible to all the packages

default- visible within the same package.

  1. Different access modifiers class members can have?

There are four access specifiers for class members

private – visible within the class

default – visible within the same package

protected – visible within the same package + sub classes in other package

public – visible to all the packages.  Read more

  1. Abstract method?

Abstract method is a method with only declaration and without implementation. Abstract method ends with a semi colon instead of curly braces. Abstract methods are created when there is different behavior for subclasses and to enforce the subclasses to provide the implementation according to specific type.   Read more

  1. Abstract class?

An abstract class cannot be instantiated. If a class contains one or more abstract methods, the class has to be declared as abstract. The first concrete (non-abstract) class which is extending the abstract class must implement all the abstract methods. An abstract class can have both abstract and non-abstract methods.  Read more

  1. Interface?

An interface is 100 % abstract class. It cannot be instantiated.  Interface is created when all the methods in the class have different behavior for subclasses and to enforce the subclasses to provide the implementation according to specific type.   Read more

  1. Local variable?

Variables which are declared inside the methods are called local variables. They are visible only inside the method in which they are declared.   Read more

  1. Instance variable?

Instance variables are the variables defined inside the class and outside the methods. Instance variables are accessible using object and every object will have its own copy of the class’s instance variables.  Read more

  1. Static variable?

Static variables will have only one copy in memory and that is shared by all the objects, any modification to it will also affect other objects. Static variables are also called as class variables.   Read more

  1. Static method?

A static method can access only static variables and cannot access instance variables. A static method belongs to the class rather than object of a class. A static method can be invoked with class name as well as instance.

  1. Final keyword?

Final keyword can be used in three ways

final variable – To create a constant

final method – To prevent method from overriding

final class – To prevent inheritance    Read more

  1. Static block?

Static init blocks run once, when the class is first loaded. JVM executes the static blocks on high priority. JVM executes the static blocks before all other methods and blocks. Read more

  1. Strictfp keyword?

Marking a class as strictfp means that any method code in the class will conform to the IEEE 754 standard rules for floating points. Without adding strictfp modifier, floating points used in the methods might behave in a platform-dependent wayRead more

  1. Constructor?

Constructors is a special method in Java which is used to initialize the instance variables of the class. A constructor has the same name of the class and does not have any return type (not even void) .A constructor may or may not have parameters. Read more

  1. Constructor Overloading?

Two or more constructors with difference in the parameters is called constructor overloading. Each constructor is used to perform different task.  Read more

  1. instanceof operator?

The instanceof operator compares an object to a specific type. It returns true if the object is an instance of the named class or class’s super classes otherwise it returns false.  Read more

  1. Relational operators?

Relational Operators are used for the purpose of comparing. They compare two quantities to determine if they are equal or greater than other. There are six types of relational operators

= = equal to operator

!= not equal to operator

> greater than operator

< less than operator

>=  great than or equal operator

<=  less than or equal to operator    Read more

  1. Increment and decrement operators? Difference between prefix and postfix?

Increment (++) and decrement (–) operators are used to increment and decrement a variable by one. Increment and decrement operators can be placed before (prefix) and after (postfix) the variable.  Read more

  1. Difference between && and &?

The && and & operators return true only if all the operands are true. & operator always evaluate all operands whereas && does not evaluate the right operand if left operand is false.

  1. Difference between || and |?

The || and | operators return true if any of the operand is true. The | operator always evaluate all operands whereas || does not evaluate the right operand if left operand is true.

  1. While statement? Difference between while and do while?

While loop repeats group of statements as long as condition is true. Once the condition is false the loop is terminated. The while loop will enter into the body of the loop only if test condition is satisfied. Do while loop will enter into the body at least once if test condition is not satisfied.  Read more

  1. Multilevel inheritance?

Multilevel inheritance in java is a mechanism where one class inherits features from a derived class. Multilevel inheritance can go up to any number of level.  Read more

  1. toString method?

toString method is part of Object class. When you pass an object reference to the System.out.println() method, the object’s toString method is called. Read more

  1. equals method?

equals method is inherited from the Object class and it is overridden in each of the wrapper classes. This methods compares the references of two objects if they are equal, it returns true otherwise false.   Read more

  1. finally block?

A try and catch block can have optional finally block which is always executed whether an exception is thrown or not. Finally block is generally used to release the memory, close the files and connections regardless of what happens in the try block.  Read more

  1. throw keyword?

Throw is a special form of the return statement that only returns exception. Exception can be throwed by creating a new instance of it. When JVM encounters throw statement it stops execution and returns the exception to previous method in call stack. Read more

  1. LinkedHashSet?

Class LinkedHashSet extends HashSet. LinkedHashSet maintains a linked list of the elements in the set. LinkedHashSet maintains the order of the elements in which the elements were inserted.  Read more

  1. TreeSet?

TreeSet provides an implementation of the Set interface. It uses tree for storage of Objects. Objects in TreeSet are stored in sorted Order. TreeSet is a good choice when objects need to be stored in sorted order.   Read more

  1. LinkedHashMap?

Linkedhashmap is a collection that stores elements in the form of key and value pairs. Value can be retrieved using the key. LinkedHashMap is combination of Hashtable and linkedlist implementation of the Map interface. Linkedhashmap maintains the insertion order.    Read more

  1. TreeMap?

A TreeMap is a Map that maintains its entries in ascending order, sorted according to the key’s natural ordering(if they implement java.lang.Comparable), or according to a Comparator provided at the time of the SortedMap creation.  Read more 

  1. Comparable interface?

Comparable is interface which contains one method compareTo where the sorting mechanism need to be placed. The return type of this method is int.  Read more

  1. Listiterator?

ListIterator is an interface that contains methods to retrieve the elements from collection from both forward and backward direction. Read more 

  1. Difference between ArrayList and Vector?

Vector is synchronized version of ArrayList. Vector is synchronized whereas ArrayList is not synchronized.

  1. Difference between HashSet and LinkedHashSet?

LinkedHashSet is an ordered version of HashSet. HashSet does not maintain any insertion order whereas LinkedHashSet maintains the order of insertion.

  1. Difference between HashMap and Hashtable?

Hashtable is synchronized version of HashMap. HashMap allows null values and one null key. Hashtable does not allow null (values and keys).

  1. Difference between HashMap and LinkedHashMap?

LinkedHashMap is an ordered version of HashMap. LinkedHashMap maintains the insertion order whereas HashMap does not maintain any insertion order.


Copyright © 2014. All rights reserved.

Posted July 9, 2015 by answersz in category Interview Questions

Leave a Comment

Your email address will not be published. Required fields are marked *

*