Core Java Interview Questions & Answers for Job Seekers



What are static blocks and static initializers in Java?
Static blocks or static initializers are used to initialize static fields in java. we declare static blocks when we want to initialize static fields in our class. Static blocks get executed exactly once when the class is loaded. Static blocks are executed even before the constructors are executed.
1. How to call one constructor from the other constructor?
If we want to call one constructor from another in the same class, we use this() method. Based on the number of parameters we pass appropriately this() method is called.
Restrictions for using this method :
1) this must be the first statement in the constructor
2) we cannot use two of this() methods in the constructor

2. What is method overriding in java?
If we have methods with the same signature (same name, same signature, same return type) in the superclass and subclass then we say the subclass method is overridden by the superclass.

3. When to use overriding in java
If we want the same method with different behavior in the superclass and subclass then we go for overriding. When we call overridden method with the subclass reference subclass method is called hiding the superclass method.

4. What is a super keyword in java?
Variables and methods of the super class can be overridden in a subclass. In overriding, a subclass object calls its own variables and methods. A subclass cannot access the variables and methods of the superclass because the overridden variables or methods hide the methods and variables of the superclass. But still, java provides a way to access superclass members even if its members are overridden. Super is used to access superclass variables, methods, and constructors.

Super can be used in two forms :
1) First form is for calling the super class constructor.
2) Second one is to call super class variables, and methods. Super if present must be the first statement.

5. Why java is the platform independent?
The most unique feature of java is platform independence. In any programming language soruce code is compiled into executable code. This cannot be run across all platforms. When javac compiles a java program it generates an executable file called a .class file.
A class file contains byte codes. Byte codes are interpreted only by JVM’s . Since these JVM’s are made available across all platforms by Sun Microsystems, we can execute this byte code in any platform. Byte code generated in windows environment can also be executed in linux environment. This makes java platform independent.

6. What is method overloading in java ?
A class having two or more methods with same name but with different arguments then we say that those methods are overloaded. Static polymorphism is achieved in java using method overloading.

Method overloading is used when we want the methods to perform similar tasks but with different inputs or values. When an overloaded method is invoked java first checks the method name, and the number of arguments , type of arguments; based on this compiler executes this method.

Compiler decides which method to call at compile time. By using overloading static polymorphism or static binding can be achieved in java.

Note : Return type is not part of the method signature. we may have methods with different return types but return type alone is not sufficient to call a method in java.

7. What is JIT compiler ?
JIT compiler stands for Just in time compiler. JIT compiler compiles byte code in to executable code . JIT a part of JVM .JIT cannot convert complete java program in to executable code it converts as and when it is needed during execution.

8. What is bytecode in java ?
When a javac compiler compiles a class it generates .class file. This .class file contains a set of instructions called byte code. Byte code is a machine independent language and contains set of instructions that are to be executed only by JVM. JVM can understand these byte codes.

9. Difference between this() and super() in java?
this() is used to access one constructor from another within the same class while super() is used to access the superclass constructor. Either this() or super() exists it must be the first statement in the constructor.

10. What is a class?
Classes are the fundamental or basic units in Object Oriented Programming. A class is a kind of blueprint or template for objects. Class defines variables, and methods. A class tells what type of objects we are creating. For example, taking a Department class tells us we can create department-type objects. We can create any number of department objects.

All programming constructs in java reside in class. When JVM starts running it first looks for the class when we compile. Every Java application must have at least one class and one main method.

Class starts with class keyword. A class definition must be saved in class file that has same as class name. File name must end with .java extension.

public class FirstClass
{public static void main(String[] args)
{System.out.println(“My First class”);
}
}

If we see the above class when we compile JVM loads the FirstClass and generates a .class file(FirstClass.class). When we run the program we are running the class and then execute the main method.

What is an object ?
An Object is an instance of the class. A class defines the type of object. Each object belongs to some class. Every object contains a state and behavior. State is determined by the value of attributes and behavior is called method. Objects are also called an instance.

To instantiate the class we declare with the class type.
public classFirstClass {public static voidmain(String[] args)
{FirstClass f=new FirstClass(); System.out.println(“My First class”);
}
}

To instantiate the FirstClass we use this statement FirstClass f=new FirstClass();
f is used to refer FirstClass object.

11.What is the method in java?
It contains the executable body that can be applied to the specific object of the class. The method includes the method name, parameters or arguments, return type, and a body of executable code.

Syntax : type methodName(Argument List){ }
ex : public float add(int a, int b, int c)

methods can have multiple arguments. Separate with commas when we have multiple arguments.

12. What is encapsulation?
The process of wrapping or putting up data into a single unit class and keeping data safe from misuse is called encapsulation. Through encapsulation we can hide and protect the data stored in java objects.Java supports encapsulation through access control. There are four access control modifiers in java public, private, protected, and default level.

For example, take a car class, In a car, we have many parts which are not required for a driver to know what all it consists of inside. He is required to know only how to start and stop the car. So we can expose what is required and hide the rest by using encapsulation.

13. Why main() method is public, static, and void in java?
public: “public” is an access specifier which can be used outside the class. When the main method is declared public which means it can be used outside class.

static : To call a method we require an object. Sometimes it may be required to call a method without the help of object. Then we declare that method as static. JVM calls the main() method without creating object by declaring the keyword static.

void : the void return type is used when a method doesn’t return any value. main() method doesn't return
any value, so main() is declared as void.

Signature : public static void main(String[] args) {

14. Explain about main() method in java ?
Main() method is the starting point of execution for all java applications.
public static void main(String[] args) {}

String args[] are arrays of string objects we need to pass from command line arguments. Every Java application must have atleast one main method.

15. What is a constructor in java?
A constructor is a special method used to initialize objects in the java. we use constructors to initialize all variables in the class when an object is created. As and when an object is created it is initialized automatically with the help of a constructor in java.

We have two types of constructors Default Constructor

Parameterized Constructor Signature : public classname()
{
}
Signature : public classname(parameters list)
{
}

16. What is difference between length and length() method in java ?
length() : In String class we have length() method which is used to return the number of characters in string.
Ex : String str = “Hello World”;
System.out.println(str.length());
Str.length() will return 11 characters including space.
length : we have length instance variable in arrays which will return the number of values or objects in array.
For example :
String days[]={” Sun”,”Mon”,”wed”,”thu”,”fri”,”sat”};
Will return 6 since the number of values in days array is 6.

17. What is ASCII Code?
ASCII stands for American Standard Code for Information Interchange. ASCII character range is 0 to 255. We can’t add more characters to the ASCII Character set. ASCII character set supports only English. That is the reason, if we see C language we can write c language only in English we can’t write in other languages because it uses ASCII code.

18. What is Unicode ?
Unicode is a character set developed by Unicode Consortium. To support all languages in the world Java supports Unicode values. Unicode characters were represented by 16 bits and its character range is 0 -65,535.
Java uses ASCII code for all input elements except for Strings, identifiers, and comments. 

19. Difference between Character Constant and String Constant in java ?
A character constant is enclosed in single quotes. String constants are enclosed in double-quotes. Character constants are single digits or characters. String Constants are collections of characters.
Ex :’2’, ‘A’
Ex : “Hello World”

20. What are constants and how to create constants in java?
Constants are fixed values whose values cannot be changed during the execution of the program. We create constants in java using final keyword.
Ex : final int number =10;
final String str=”java-interview –questions”

21. Difference between ‘>>’ and ‘>>>’ operators in java?
>> is a right shift operator shifts all of the bits in a value to the right a specified number of times. int a =15;
a= a >> 3;
The above line of code moves 15 three characters right.
>>> is an unsigned shift operator used to shift right. The places which were vacated by shift are filled with zeroes.

22. Difference between method overloading and method overriding in java?

Method Overloading

Method Overriding

1) Method Overloading occurs within the same class

1) Method Overriding occurs between two classes superclass and subclass

2) Since it involves only one class inheritance is not involved.

2) Since method overriding occurs between superclass and subclass inheritance is involved.

3)In overloading return type need not be the same

3) In overriding return type must be the same.

4) Parameters must be different when we do overloading

4) Parameters must be the same.

5) Static polymorphism can be achieved using method overloading

5) Dynamic polymorphism can be achieved using method overriding.

6) In overloading one method can’t hide the other

6) In overriding subclass method hides that of the superclass method.

23. Difference between abstract class and interface ?

Interface

Abstract Class

1) Interface contains only abstract methods

1) Abstract class can contain abstract methods, concrete methods or both

2) Access Specifiers for methods in the interface must be public

2) Except private we can have any access specifier for methods in abstract class.

3) Variables defined must be public , static , final

3) Except private variables can have any access specifiers

4) Multiple Inheritance in java is implemented using interface

4)We cannot achieve multiple inheritance using abstract class.

5) To implement an interface we use implements keyword

5)To implement an interface we use implements keyword


24. What is difference between c++ and Java?

Java

C++

1)

Java is platform independent

C++ is platform dependent.

2)

There are no pointers in java

There are pointers in C++.

3)

There is no operator overloading in java

C ++ has operator overloading.

4)

There is garbage collection in java

There is no garbage collection

5)

Supports multithreading

Doesn’t support multithreading

6)

There are no templates in java

There are templates in java

7)

There are no global variables in java

There are global variables in c++

0 Comments

Please do not spam

Subscribe

Fill in all informations