Java FAQ (Frequently Asked Questions and answers) for freshers

This post lists the Java Programming FAQ (Frequently Asked Questions and answers) for freshers for next job interview or Java Programming exam.

James Gosling is the father of the Java language. It was developed as a programming language in 1996 by the Sun Microsystem.

The main target of developing the Java language was "WRITE ONE, RUN ANYWHERE". It means the code you are writing is machine-independent. If you are writing any program that particular program will run on every operating system. There is no need to write a program again for another operating system. In the case of C and C++, the codes are machine-dependent. So to make your program run on every platform you have to write an individual program for every platform.

Features of Java language as follow:

  • Easy to learn: It is a syntax-based language. It is very simple to learn and understand the concepts of Java.
  • Object-Oriented: It is also known as a pure object-oriented programming language. It implements all the concepts of Object-oriented language.
  • Platform Independent: Java is a platform-independent language. During the compilation of java code, it is converted into byte code. The byte code can run on any of the platforms. The platform includes different hardware and software environments.
  • Highly Secured: It is extremely secured because there is no concept of pointers is used in Java.
  • Robust: It is a robust language. Java has a well-built memory management system.
  • Multithreaded: Java uses multi-threading which helps to make multitasking applications.
  • Portable: It is portable because it generates a byte code which is an intermediate code. It does not require any implementation. The byte code can run on any of the platforms.
  • Speedy: It is faster than the other traditional language but slower than the compiled language. It is interpreted Language. It means it checks the code line by line.

For more details, you can link on the below

https://www.tutorialslink.com/Articles/What-is-Java-and-Its-Features/1493

The Java programming model is way different from the traditional programming model. The java programming model is designed to make the language machine-independent.

Java programming Model:

Source Code ----> Bytecode ----> JVM

Source code: It is a basic code you write in java IDE.

Bytecode: It is a machine-independent code. This bytecode file helps to run the code on any operating system.

JVM: It converts the bytecode for the machine in which it is running.

In the traditional approach, the source code is directly converted into the native code. That's why it is machine-dependent.

Reserved words are pre-defined words in the language. These words have an already defined meaning in the language. The programmer can not these words a name of the variable, identifier, or function name. If a programmer tries to do that a program will through an error. 

In general, the packages mean putting a similar thing into one pack. Similarly, package in java means a grouping of related types of classes, interfaces, and other packages. Package help in providing access protection and namespace management.

Conversion means converting the data type into another data type. There are two types of conversion:

  • Widening Conversions: It converts the small byte data types into long byte data types. Examples

byte ----> short, int, long, float.

int ----> long, float, double

float ----> double

  • Narrowing conversions

short ---->byte

int -----> short, byte

double ----> float 

 

Access modifiers are hinder the access of classes, interfaces, and variables. There are four types of access modifier in Java:

  • Public: The classes, interface, and constructors are accessible anywhere.
  • Private: The classes, interface, and variables are accessible only inside the class in which it is defined.
  • Protected: The classes, interfaces, and variables are accessible only inside the classes where it is inherited.
  • Default: It is defined inside the complete package where it is defined.

It refers to the instance of the current object from any method or constructor. It is particularly used to differentiate between a class variable and a local variable. We can provide the same name in the method or function parameters mentioned in class variables.

In simple words, they are class-level members. There is no need to create an object of static class. Static methods are not allowed to access the class-level variables. Only static methods can access the static variables. We can also initialize the value to the static variables in the static blocks.

Typecasting means assigning an object of the superclass to an inherited class or vice-versa. There are two types of typecasting:

  • upcasting: It assigns the object of the subclass to the superclass variable. 
  • downcasting: It assigns the object of the superclass to the subclass variable.

Inheritance means inheriting the properties of one class to another class. The class whose property is inherited is known as parent class and in which property is inherited is known as child class. The types of inheritance Java supports are:

  • Single Inheritance: There are only two classes one is whose property is inheriting into another class.
  • Multilevel Inheritance: Multiple classes exist in this. There is only one superclass. Example: Suppose there are three classes the property of the first class is inherited into the second class and the property of the second class is inherited into the third class.
  • Hierarchical Inheritance: When the property of one class inherited into two child classes. 

One more inheritance exists in the OOPs i.e Multiple Inheritance but Java does not support multiple inheritances.

JDK JVM JRE
JDK stands for Java Development Kit. JVM stands for Java Virtual Machine. JRE stands for Java Runtime Environment.
JDK is used to compile and execute the Java package. It provides the specification for the runtime environment to run the bytecode of the java program. It provides the runtime environment to run the bytecode.
It consists of JRE+development tools. It a superset that contains specification, Implementation, and runtime instances. It is the virtual implementation of the JDK.

Constructor is the method of the class that has the same name as that of the class. It is specially used to initialize the object. There are two types of the constructor:

  • Default Constructor: In the case of the default constructor, no arguments are passed in the constructor definition. If we don't declare the default constructor, it will be created automatically to initialize the instance variables with the default values.
  • Parameterized Constructor: In parameterized constructor, you can pass any number of arguments in the constructor definition. The arguments are defined with arguments. You can initialize the instance variables along with provided values.

No, we can inherit the constructor of one class to another class. Constructor is the part of the class whose name is the same as that of the class.

Java Constructor Java Method
It is used to initialize the instance of the object. It is used to expose the behavior of an object. 
It does not have any return type. It does have a return type.
The default constructor is available to initialize the value. There is no default method is available.
The constructor's name is the same name as that of the class. You can or cannot have the same name as that of the class

Local Variable is declared inside the methods, constructor, and inside any other blocks. The scope of these variables is only inside the block where they are declared. You can not access these variables outside that particular block.

Instance variables are declared inside the class, but not inside the method. These variables are bounded to its object itself. Each object is creating its individual copy. It won't affect any other variable if we are making changes in one instance.

The final keyword has a very special meaning in java. It has a different meaning in a different concept:

final variable: The value of the variable can not be modified once it is assigned. When a final keyword is used with the class variable then only the constructor can assign value to the final variable.

final method: You cannot override the method if it is declared with the final keyword.

final class: The class cannot be extended to any other class but it can extend other classes.

The Multiple inheritance increase the complexity in the Java language. In multiple inheritance, the properties of two classes are inherited in one child class. For example, there are three classes A, B, and C. If A and B have the same methods and you are creating an object of the child class, then there will be a problem when you are calling methods of class A and class B. 

Java shows a compile-time error if two classes are inherited. It does not matter whether the methods are different or the same it will give a compile-time error. 

Pointers make the language a bit complex and unsafe too. But the java language is known for the simplicity. JVM looks after the implicit memory location. It will avoid direct access to the memory location by the user is more encouraged by Java. 

break Continue
It is used with conditional(if, if-else, switch) and loop statements(for, while, do-while). It is used only with the loop statements.
Once the statement is executed it terminated the moment and come out of the block.  Once the statement is executed it will jump to the next iteration.
It terminates the innermost loop immediately. It skips the next set of statements after continue and jump to the next iteration. 

When a method is static, there is no need to create an object to call the method. It will be called automatically. That's why the main method of Java is static. If we create a main method without static, then JVM needs to create an object to call the method. This procedure will acquire extra memory space. 

Java does not support a copy constructor like C++. Moreover, there are some concepts that work like copy constructor in java. We can copy the value of one object to another object. The ways you can do:

  • Through constructor

Example: We have a class of employees.

employees(int i, String name)

{

this.i =i;

this.name=name;

}

  • Assigning the values of one object to another.

Example: employee(employee e)

{

i = e.i;

name = e.name;

}

this() super()
It belongs to the current instance of the class. It belongs to the current instance of the parent class.
It calls the default constructor of the same class It calls the default constructor of the parent class.
It accesses the method of the current class. It accesses the method of the parent class.
It points to the instance of the current class. It points to the instance of the base class.

One constructor can call another constructor of the class with respect to the current class object. Constructor chaining can be performed inside the same with the help of this keyword.  

Example:

public class student

{

int id;

String name;

public student(int id)

{

this.id=id;

}

public student(int id, String name)

{

this(id);

this.name=name;

}

public static void main(String[] args)

{

student s = new student(3, " Ram");

System.out.print("The student id" + id + "and name is" + name); 

}

}

Method overloading is a type of polymorphism. Polymorphism allows you to create methods with the same name. There is two way through which you can implement the method overloading:

  • change the number of arguments.
  • change the data type of parameter.

 

 

Method overriding comes under run time polymorphism. There are some conditions in method overriding:

  • Two classes have the method with the same name. 
  • The number of parameters should be the same.
  • The return type of the parameter should be the same.

If these conditions are satisfied then run-time will occur polymorphism. 

String String Builder String Buffer
String values are stored in a constant string pool. String Builder is stored in the stack. String Buffer is also stored in the stack.
The values can not be changes We can change the value We can change the value.
  It is not thread-safe so it is not synchronized.  It is thread-safe so it is synchronized. 
  It is faster in working. It is slower in working.

It is used to achieve abstraction. The template of the Interface has only method declaration. It does not have method implementation. Few points are needed to remember about the interface:

  • all the methods declared inside the interface are by default public abstract void.
  • all the variables inside the interface are by default public static final.
  • It can only be implemented in classes, not extended.
  • The methods will be implemented in the classes where the interface is implemented. 

Abstract classes are used to achieve abstraction. It is declared by using the "abstract" keyword. It can have both abstract and non-abstract methods. Abstract classes have declaration and implementation of non-abstract methods but the implementation of abstract methods is inside the class where it is extended. 

ArrayList is present in java.util package and also the part of the collection framework. 

Features of ArrayList:

  • They are dynamic in nature. 
  • We can add, remove, update, and iterating the elements of ArrayList.
  • It allows to randomly access the elements.
  • It implements a List interface. 
  • It can not be used for primitive data types. 
Abstract Class Interface
It has a default constructor.  It does not have any default constructor
It has both abstract and non-abstract methods. It has only an abstract method
It contains instance variables. It contains only constants
It is extended in classes. It is implemented in classes and methods defined in interfaces are implemented in the class.

A group of objects of classes and interfaces is called collections. It comes under java.util package. It is designed to store objects and manipulate the design to store objects. 

Operations that you can perform on collections:

  • Insertion
  • Deletion
  • Updation
  • Iteration
  • Searching
  • Sorting

Ordered: The values stored in the collection are the way in which we inserted the values.  We can iterate the collections in any specific order.

Sorted: It can be done internally and externally. It is a mechanism that can sort the collections in any particular order.

Set believes in solitary. It does not allow duplicate values. The existing value inserted again will display an error. There are three types of sets that exist in Java: 

  • Hash set: It is unsorted and unordered. It makes use of hash code to insert the values. 
  • Linked Hash set: It is similar to the hash set but with ordered property. It keeps the doubly linked list of the elements.
  • Tree set: It contains elements either in ascending and descending order. 

Encapsulation means combining the properties of class and methods in a single unit. It helps the programmer to follow the modular approach for software development. Each object has its own property and methods and works independently of other objects. It also serves the purpose of data hiding. 

A class that has one instance is called the singleton class. All its variables and methods belong to that one instance of the class only. The concept is useful where there is a need to limit the instance of the class. 

For achieving abstraction both abstract and the interface is used. The abstract class helps in partial abstraction and interface achieves full abstraction. Let us study the difference between the two in the table given below.

Abstract class

Interface

A class that is abstract can have both abstract and non-abstract methods.

The interface on the other hand can have only abstract methods. However, after Java 8, it can have default and static methods also.

It does not support multiple inheritances.

It supports multiple inheritances.

Abstract class contains final, non-final, static and non-static variables.

The interface can contain only static and final variables.

An abstract class can provide an implementation of the interface.

It cannot implement the abstract class.

If we want to declare a class as abstract we need to use the abstract keyword.

To declare interface we use the interface keyword.

It can extend another Java class and multiple java interfaces can be implemented.

It can only extend another Java interface.

The “extends” keyword is used to extend an abstract class.

“implements “keyword is used.

Class members can be private, protected etc.

Class members are by default public.

Example

Public abstract class painting{

Public abstract void draw();}

Example

Public interface Creation{

Void draw();}