Java Q & A

What is Java?

Java is a high-level, object-oriented programming language known for its portability, platform independence, and robustness. It was developed by Sun Microsystems (now owned by Oracle Corporation) and is widely used for building various types of applications.

 

What are the main features of Java?

Java has several key features, including platform independence, strong typing, automatic memory management (garbage collection), multi-threading support, and a vast standard library.

 

Explain the difference between JDK, JRE, and JVM.

JDK (Java Development Kit): It includes tools like the Java compiler (javac) and libraries needed for Java development.

JRE (Java Runtime Environment): It provides the runtime environment required to run Java applications.

JVM (Java Virtual Machine): It is an integral part of the JRE and executes Java bytecode.

 

What is the difference between == and .equals() in Java?

== compares object references, checking if they point to the same memory location.

.equals() is a method used to compare the content or values of objects. It is often overridden in classes to provide custom comparison logic.

 

What is an Object in Java?

An object in Java is an instance of a class. It represents a real-world entity and encapsulates data (attributes) and behavior (methods).

 

Explain the concept of Inheritance in Java.

Inheritance is a fundamental OOP concept in Java that allows a subclass to inherit properties and behaviors from a superclass. It promotes code reuse and supports the “is-a” relationship.

 

What is the final keyword in Java?

The final keyword can be used to restrict further modification of classes, methods, or variables. For example, a final variable cannot be reassigned, and a final method cannot be overridden.

 

 

What is the purpose of the static keyword in Java?

The static keyword is used to declare members (variables and methods) that belong to the class itself rather than instances of the class. It allows you to access them without creating an object of the class.

 

What is the difference between an abstract class and an interface in Java?

An abstract class can have both abstract (unimplemented) and concrete (implemented) methods, while an interface can only have abstract methods (prior to Java 8).

A class can implement multiple interfaces, but it can inherit from only one abstract class.

 

Explain the concept of Exception Handling in Java.

Exception handling in Java is the mechanism to handle runtime errors and abnormal situations. It uses try-catch blocks to catch and handle exceptions, ensuring that the program does not terminate unexpectedly.

 

The Java Collections Framework provides a set of classes and interfaces for working with collections of objects. It includes data structures like lists, sets, and maps, along with algorithms for common operations.

 

What is the difference between ArrayList and LinkedList in Java?

ArrayList is implemented as a dynamic array, while LinkedList is implemented as a doubly-linked list.

ArrayList is generally more efficient for random access and searching, while LinkedList is better for frequent insertions and deletions in the middle of the list.

 

What is the purpose of the synchronized keyword in Java?

The synchronized keyword is used to create synchronized blocks or methods, ensuring that only one thread can access the synchronized code at a time. It helps in achieving thread safety in multithreaded applications.

 

Explain the concept of Java Streams.

Java Streams provide a functional programming approach for processing sequences of elements (e.g., collections). They enable operations like map, filter, and reduce to be applied to data in a concise and declarative manner.

 

How do you handle exceptions in a multi-catch block in Java?

A multi-catch block allows you to catch multiple exceptions in a single catch block. For example:

try {

// Code that may throw exceptions

} catch (IOException | SQLException e) {

// Handle IOException or SQLException

}

 

What are the differences between C++ and Java?

C++ is not platform-independent; the principle behind C++ programming is “write once, compile anywhere.”

In contrast, because the byte code generated by the Java compiler is platform-independent, it can run on any machine, Java programs are written once and run everywhere.

Languages Compatibility.

C++ is a programming language that is based on the C programming language. Most other high-level languages are compatible with C++.

Most of the languages of Java are incompatible. Java is comparable to those of C and C++.

Interaction with the library.

It can access the native system libraries directly in C++. As a result, it’s better for programming at the system level.

Java’s native libraries do not provide direct call support. You can use Java Native Interface or access the libraries.

Characteristics.

C++ distinguishes itself by having features that are similar to procedural and object-oriented languages. The characteristic that sets Java apart is automatic garbage collection. Java doesn’t support destructors at the moment.

The semantics of the type.

Primitive and object types in C++ have the same kind of semantics. The primitive and object and classes of Java, on the other hand, are not consistent.

In the context of Compiler and Interpreter.

Java refers to a compiled and interpreted language. In contrast, C++ is only a compiled language.

In Java, the source code is the compiled output is a platform-independent byte code.

In C++, the source program is compiled into an object code that is further executed to produce an output.

 

List the features of the Java Programming language?

A few of the significant features of Java Programming Language are:

Easy: Java is a language that is considered easy to learn. One fundamental concept of OOP Java has a catch to understand.

Secured Feature: Java has a secured feature that helps develop a virus-free and tamper-free system for the users.

OOP: OOP stands for Object-Oriented Programming language. OOP signifies that, in Java, everything is considered an object.

Independent Platform: Java is not compiled into a platform-specific machine; instead, it is compiled into platform-independent bytecode. This code is interpreted by the Virtual Machine on which the platform runs.

 

Define package in Java.

The package is a collective bundle of classes and interfaces and the necessary libraries and JAR files. The use of packages helps in code reusability.

 

What is a ClassLoader?

A classloader in Java is a subsystem of Java Virtual Machine, dedicated to loading class files when a program is executed; ClassLoader is the first to load the executable file.

Java has Bootstrap, Extension, and Application classloaders.

 

 

What are the Memory Allocations available in JavaJava?

Java has five significant types of memory allocations.

Class Memory

Heap Memory

Stack Memory

Program Counter-Memory

Native Method Stack Memory

 

What are the differences between Heap and Stack Memory in Java?

Stack memory in data structures is the amount of memory allocated to each individual programme. It is a fixed memory space. Heap memory, in contrast, is the portion that was not assigned to the Java code but will be available for use by the Java code when it is required, which is generally during the program’s runtime.

 

Will the program run if we write static public void main?

Yes, the program will successfully execute if written so. Because, in Java, there is no specific rule for the order of specifiers.

 

What is the default value stored in Local Variables?

Neither the Local Variables nor any primitives and Object references have any default value stored in them.

 

Explain the expected output of the following code segment?

public class Simplilearn

{

public static void main (String args[])

{

System.out.println(100 + 100 +“Simplilearn”);

System.out.println(“E-Learning Company” + 100 + 100);

}

}

The answers for the two print statements are as follows.

200Simplilearn

E-Learning Company100100

 

What is an Association?

An Association can be defined as a relationship that has no ownership over another. For example, a person can be associated with multiple banks, and a bank can be related to various people, but no one can own the other.

 

What do you mean by aggregation?

The term aggregation refers to the relationship between two classes best described as a “whole/part” and “has-a” relationship. This kind is the most specialized version of an association relationship. It contains the reference to another class and is said to have ownership of that class.

 

Define Copy Constructor in Java

A Copy Constructor in Java is a constructor that initializes an object through another object of the same class.

 

What is a Marker Interface?

An empty interface in Java is referred to as a Marker interface. Serializable and Cloneable are some famous examples of Marker Interface.

 

What is Object Cloning?

An ability to recreate an object entirely similar to an existing object is known as Object Cloning in Java. Java provides a clone() method to clone a current object offering the same functionality as the original object.

 

What is an object-oriented paradigm?

A Paradigm that is based on the concepts of “Objects.” It contains data and code. Data that is in the form of fields, and regulation, that is in the form of procedures. The exciting feature of this paradigm is that the object’s procedures can access and often modify the data fields themselves.

 

Define Wrapper Classes in Java.

In Java, when you declare primitive datatypes, then Wrapper classes are responsible for converting them into objects(Reference types).

 

What is a singleton class in Java? And How to implement a singleton class?

A class that can possess only one object at a time is called a singleton class. To implement a singleton class given steps are to be followed:

  • Make sure that the class has only one object
  • Give global access to that object

 

Define package in Java.

The package is a collective bundle of classes and interfaces and the necessary libraries and JAR files. The use of packages helps in code reusability.

 

Can you implement pointers in a Java Program?

Java Virtual Machine takes care of memory management implicitly. Java’s primary motto was to keep programming simple. So, accessing memory directly through pointers is not a recommended action. Hence, pointers are eliminated in Java.

 

Differentiate between instance and local variables.

For instance, variables are declared inside a class, and the scope of variables in javascript is limited to only a specific object.

A local variable can be anywhere inside a method or a specific block of code. Also, the scope is limited to the code segment where the variable is declared.

 

Explain Java String Pool.

A collection of strings in Java’s Heap memory is referred to as Java String Pool. In case you try to create a new string object, JVM first checks for the presence of the object in the pool. If available, the same object reference is shared with the variable, else a new object is created.

 

What is an Exception?

An Exception handling in Java is considered an unexpected event that can disrupt the program’s normal flow. These events can be fixed through the process of Exception Handling.

 

What is the final keyword in Java?

The term final is a predefined word in Java that is used while declaring values to variables. When a value is declared using the final keyword, then the variable’s value remains constant throughout the program’s execution.

 

What happens when the main() isn’t declared as static?

When the main method is not declared as static, then the program may be compiled correctly but ends up with a severe ambiguity and throws a run time error that reads “NoSuchMethodError.”

 

Why is Java a platform independent language?

One of the most well-known and widely used programming languages is Java. It is a programming language that is independent of platforms. Java doesn’t demand that the complete programme be rewritten for every possible platform. The Java Virtual Machine and Java Bytecode are used to support platform independence. Any JVM operating system can run this platform-neutral byte code. The application is run after JVM translates the byte code into machine code. Because Java programmes can operate on numerous systems without having to be individually rewritten for each platform, the language is referred to as “Write Once, Run Anywhere” (WORA).

 

Why is the main method static in Java?

Java’s main() function is static by default, allowing the compiler to call it either before or after creating a class object. The main () function is where the compiler begins programme execution in every Java programme. Thus, the main () method needs to be called by the compiler. If the main () method is permitted to be non-static, the JVM must instantiate its class when calling the function.

 

What part of memory – Stack or Heap – is cleaned in the garbage collection process?

On Heap memory, garbage collection is employed to release the memory used by objects with no references. Every object created in the Heap space has access to the entire application and may be referred to from anywhere.

 

What is the difference between the program and the process?

A programme is a non-active entity that includes the collection of codes necessary to carry out a specific operation. When a programme is run, an active instance of the programme called a process is launched. A process is begun by a programme once it has been run. The process carries out the program’s specified instructions.

 

What are the differences between constructor and method of a class in Java?

Initializing the state of the object is done by constructors. A function Object () { [native code] }, like methods, contains a group of statements (or instructions) that are carried out when an object is created. A method is a group of statements that work together to complete a certain task and return the outcome to the caller. A method has the option of working without returning anything.

 

Which among String or String Buffer should be preferred when there are a lot of updates required to be done in the data?

Because StringBuilder is quicker than String Buffer, it is advised to utilize it wherever possible. However, String Buffer objects are the best choice if thread safety is required.