6
Sponsored Links


Ad by Google
In our previous post we have listed 10 Core Java basic interview questions and here we are going to list down another 20 Core Java basic interview questions for freshers. Yes answers of these questions are designed for fresher level only.

1. What is constructor in Java and How to create constructor?
Constructor is a special type of method with class name, and used to initialize the class before methods are invoked or fields are accessed. Constructor is invoked when we use new operator to create an object. Constructor are defined by following the class name () brackets for example if class name is Book then constructor to be Book(){}

2. What is the difference between constructor and method?
Constructors are a special type of method they don't have any return type and constructor name must be same as class name. Constructors used to create and initialize objects that don't already exist. Whereas methods are used to perform operations on object that are already exist, and methods name can be anything and also has return type and it can be void also.
Constructor can't be call directly they can call using new operator whereas method can be called directly.

3. Can you write a program to find the input number provided by user is even or odd?
Number.java
package com.javamakeuse.poc;

import java.util.Scanner;

public class Number {
	public static void main(String[] args) {
		System.out.println("Enter integer number");
		Scanner input = new Scanner(System.in);
		int number = input.nextInt();
		if (number % 2 == 0) {
			System.out.println(number + " Is an even number");
		} else {
			System.out.println(number + " Is an odd number");
		}
	}
}

4. What is JVM?
JVM(Java Virtual Machine) that understand byte code only and run that byte codes. JVM who makes java as platform independent because JVM doesn't understand our .java program that's why we need to compile .java file to .class files that contains the byte code.

5. How many types of class loaders in java?
Three class loaders in java
  1. Bootstrap Class Loader
  2. Extension Class Loader
  3. System Class Loader

6. What is the difference between Interface and Abstract class?
  • Abstract class can have both abstract as well none abstract methods whereas interface must have only abstract methods.
  • extends keyword is used to extend abstract class whereas implements keyword is used to implements an interface.
  • A class can extends only one abstract class at a time whereas A class can implements any number of interface at a time.
  • Abstract class has method with any access specifiers such as public, protected etc. Whereas interface has only public abstract method.

7. Write a program to check given number is prime or not.
Number.java
package com.javamakeuse.poc;

public class Number {

	public static boolean isPrimeNum(int num) {
		for (int i = 2; i <= num / 2; i++) {
			if (num % i == 0) {
				return false;
			}
		}
		return true;
	}

	public static void main(String[] args) {
		if (isPrimeNum(3)) {
			System.out.println("Given number is prime number");
		} else {
			System.out.println("Given number is not a prime number");
		}
	}
}

8. Is it possible to run a java program without using main method
Yes, It's possible to run a java program without using main method. You can use static block to run a java program.

9. What is "this" and "super" keywords in Java
super keyword is used to access the member of base class whereas this keyword is used to access the member of current class. In short super keyword refer to constructor of base class whereas this keyword refer to current constructor.

10. What is the difference between local variables and instance variables?
Local variables has limited scope, they are visible within the declared block only and they can't access outside of that block and also local variable has not any default value. Whereas instance variables are visible every where in the class, based on their access level and instance variables also has default values. For example for reference type default value is null.

11. What is static method in Java?
  • A static method is a method which belongs to class not to the object.
  • A static method can not call none static field/methods inside the static method.
  • A static method directly access by class name instead of object.

12. What is static variable in Java?
A static variables directly belong to class instead of an object, that's why static variable also known as class variable. All the objects share the same copy of the class variables and also class variable access directly by following class name.

13. What is difference between overloading and overriding?
  • Overloading happens at compile time whereas Overriding happens at runtime.
  • Overloading possible only in the same class whereas Overriding only done in child class.
  • Static method can be Overloaded but static method can not be Override.
  • Method parameters are same while Overloading, whereas In Overriding method parameters can be different.

14. Is it possible to define interface as final?
No, Interface can not be final, it will gives you a compile time error.
15. What are the popular methods of java.lang.Object class?
Well java.lang.Object class has various methods but few are very popular and interviewer expecting from you to here about those which are listed below.
You can read more from here What are the methods of Object class?

16. What is marker interface in Java?
Marker interface in java is an empty interface and used to define the type of the class. Serializable is a marker interface in java. Whenever a class implements a marker interface that class got a special treatment via special methods of java library. You can read more from here What is marker interface in java?

17. What is Serialization in Java
Serialization is a mechanism to convert object's state into sequence of byte stream, so that you can easily send the object byte stream via network or write them in you hard disk. Here is very nice Tutorial on Serialization

18. What is a transient keyword?
The field(s) which are marked as transient can not be serialized and at the time of deserialization they will assigned with the default value of the declared data types. Here you can read more

19. What is package in Java?
In java package is a namespace that organizes a set of related classes and interfaces.

20. Is it possible to create an object of abstract class?
No, You can not create an instance of abstract class.
Sponsored Links

6 comments:

  1. Wow very nice collections of Java interview questions... please post more sir :)

    ReplyDelete
    Replies
    1. Glad to hear that you liked it. Sure will post few more question in our next series.

      Delete
  2. That is really useful piece of information, thanks for sharing it. Do keep sharing such good stuff.
    Core Java Courses

    ReplyDelete