What is Java?
Java is a programming language. It is an object-oriented language derived from C++ with strong support for networking, multithreading, and component-oriented development. It is commonly used to refer to the Java platform, a set of tools allowing for easy cross-platform application development, as well as the Java programming language, which is a general-purpose programming language often used to develop programs for this platform.
Java files are stored in files suffixed with .java. These files are then compiled into Java byte code using the Java compiler, and the byte code is then executed using the Java Virtual Machine (JVM). The Java compiler and the JVM are part of the Java Development Kit.
What are the features of Java?
Simple: Java is a simple language because of its various features, Java Doesn’t Support Pointers, Operator Overloading etc. Java provides bug free system due to the strong memory management.
Object-Oriented: Object-Oriented Programming Language (OOPs) is the methodology which provide software development and maintenance by using object state, behavior, and properties.
Secure: Java is Secure Language because of its many features it enables to develop virus-free, tamper-free systems. Authentication techniques are based on public-key encryption. Java does not support pointer explicitly for the memory. All Program Run under the sandbox.
Robust: Java was created as a strongly typed language. Data type issues and problems are resolved at compile-time, and implicit casts of a variable from one type to another are not allowed.
Platform-independent: Java Language is platform-independent due to its hardware and software environment. Java code can be run on multiple platforms ex: windows, Linux, sun Solaris, Mac/Os etc. Java code is compiled by the compiler and converted into byte code. This byte code is a platform independent code because it can be run on multiple platforms i.e. Write Once and Run Anywhere (WORA).
Architecture neutral: It is not easy to write an application that can be used on Windows, UNIX and a Macintosh. And it’s getting more complicated with the move of windows to non-Intel CPU architectures.
Portable: Java code is portable. It was an important design goal of Java that it be portable
so that as new architectures (due to hardware, operating system, or both) are developed, the java environment could be ported to them.
Dynamic: Because it is interpreted, Java is an extremely dynamic language, at runtime, the java environment can extend itself by linking in classes that may be located on remote
Servers on a network.
Interpreted: We all know that Java is an interpreted language as well. With an interpreted language such as Java, programs run directly from the source code. The interpreter program reads the source code and translates it on the fly into computations. Thus, Java as an interpreted language depends on an interpreter program.
High performance: For all but the simplest or most infrequently used applications, performance is always a consideration for most applications, including graphics-intensive ones such as are commonly found on the World Wide Web, the performance of java is more than adequate.
Multithreaded and Interactive: Writing a computer program that only does a single thing at a time is an artificial constraint that we’ve lived with in most programming languages. With java, we no longer have to live with this limitation. Support for multiple, synchronized threads is built directly into the Java language and runtime environment.
Distributed: Java facilitates the building of distributed application by a collection of classes for use in networked applications. By using java?s URL (Uniform Resource Locator) class, an application can easily access a remote server. Classes also are provided for establishing socket-level connections.
Simple Small and Familiar: It is a simple Language because it contains many features of other Languages like c and C++ and Java Removes Complexity because it doesn’t use pointers, Storage Classes and Go to Statements and java doesn’t support Multiple Inheritance
What are the features of JAVA SE8?
Java Platform, Standard Edition (Java SE) Java SE 8 is one of the most feature packed release in the Java history. Following list is a highlight of important features of Java 8.
- Lambda Expressions
- Parallel operations
- Pipelines and Streams
- Concurrent Accumulator
- Date and Time API
- Default Methods
- Type Annotations
- Nashorn JavaScript Engine
- PermGen Space Removed
- TLS SNI
What is difference between JDK vs JRE vs JVM?
Some of the important difference between JDK, JRE and JVM.
JDK is for development purpose whereas JRE is for running the java programs.
JDK and JRE both contains JVM so that we can run our java program.
JVM is the heart of java programming language and provides platform independence.
What is JIT compiler?
The Just-In-Time (JIT) compiler is a component of the JRE (Java Runtime Environment) that improves the performance of Java applications at run time. It helps improve the performance of Java programs by compiling bytecodes into native machine code at runtime.
What is JDBC Driver?
JDBC (Java Data Base Connectivity) Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers:
- JDBC-ODBC (Open Data Base Connectivity) bridge driver (ODBC is used to connect to the data base. It contains data sources.)
- Native-API driver (partially java driver)
- Network Protocol driver (fully java driver)
- Thin driver (fully java driver)
What is Constructor in java?
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
There are two types of constructors:
Default constructor (no-arg constructor): A constructor having no parameter is known as default constructor and no-arg constructor.
Parameterized constructor: A constructor having an argument list is known as a parameterized constructor. Parameterized constructors are used to supply dissimilar values to the distinct objects.
What is the role of JDBC DriverManager class?
The JDBC DriverManager class manages the registered drivers. It can be used to register and unregister drivers. It provides factory method that returns the instance of Connection.
What is ClassLoader in Java? And Types of ClassLoader?
Java Class Loader loads a java class file into java virtual machine. It is as simple as that. It is not a huge complicated concept to learn and every java developer must know about the java class loaders and how it works.
Java class loaders can be broadly classified into 3 types.
Bootstrap Class Loader: Bootstrap class loader loads java’s core classes like java.lang, java.util etc. These are classes that are part of java runtime environment. Bootstrap class loader is native implementation and so they may differ across different JVMs.
Extensions Class Loader: Extensions Class Loader is JAVA_HOME/jre/lib/ext contains jar packages that are extensions of standard core java classes. Extensions class loader loads classes from this ext folder. Using the system environment propery java.ext.dirs you can add ‘ext’ folders and jar files to be loaded using extensions class loader.
System Class Loader: System Class Loader Java classes that are available in the java classpath are loaded using System class loader.
What is a servlet in java?
Java Servlet is server-side technologies to extend the capability of web servers by providing support for dynamic response and data persistence. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets.
What is the number of bits used to represent ASCII, Unicode, the UTF-16, and the UTF-8 characters?
Unicode requires 16 bits.
ASCII requires 7 bits – however, it is usually represented as 8 bits.
UTF-8 presents characters in 8, 16, and 18 digit patterns.
UTF-16 requires 16 bit or larger patterns.
What is the advantage of using a string literal in Java?
Using a string literal to create strings makes java memory more efficient as no new objects are created if they already exist in the string constant pool.
What is a pointer and does Java support pointers?
Pointer is a reference handle to a memory location. Improper handling of pointers leads to memory leaks and reliability issues hence Java doesn’t support the usage of pointers.
What is the difference between a class variable and an instance variable in Java?
Class Variable: Class Variables only have a single copy of the variable and are declared with static modifiers. Every object of the class shares a single copy of the class variable, so if any changes are made to a class variable they will be seen by all the objects of a class. A class variable is allocated memory when the class is loaded first time.
Instance variable: Instance variables belong to an object and are specified without a static modifier. Every object of the class will have its own personal copy of the instance variable unlike class variables where single copy of the variable is shared by different objects of the class.
What is the difference between sleep () and wait () methods in Java?
Sleep (): Sleep () is a static method. It is a blocking operation that keeps a hold on the monitor / lock of the shared object for the specified number of milliseconds. Also,It is most commonly used for polling, or to check for certain results, at a regular interval. It is defined in java.lang.Thread class.
Wait (): It is an instance method. wait (), on the other hand, simply pauses the thread until either the specified number of milliseconds have elapsed or it receives a desired notification from another thread (whichever is first), without keeping a hold on the monitor/lock of the shared object. It is generally used in multithreaded applications, in conjunction with notify () / notifyAll (), to achieve synchronization and avoid race conditions. It is defined in java.lang.Object class.
What is the difference between System.Out, System.Errand System.in?
System.out and System. Err represent the monitor by default and thus can be used to send data or results to the monitor.
System.out is used to display normal messages and results.
System. Err is used to display error messages.
System.in represents Input Stream object which by default represents standard input device, i.e. keyboard.
What is the difference between ArrayList and Vector in Java?
ArrayList and Vector both use Array as a data structure internally. However, there are few differences in the way they store and process the data.
ArrayList: ArrayList is non-synchronized which means multiple threads can work on ArrayList at the same time. ArrayList grow by half of its size when resized. As ArrayList is unsynchronized, it operates faster than Vector. ArrayList uses Iterator interface to traverse the objects stored in ArrayList.
Vector: Vector is synchronized. This means if one thread is working on Vector, no other thread can get a hold of it. Vector doubles the size of itself by default when grows. As Vector is synchronized, it operates slower than ArrayList. Vector uses Enumeration as well as Iterator interface to traverse the objects stored in Vectors.
What is the difference between throw and throws in Java?
Throw keyword is used to explicitly throw an exception. It is followed by an instance. Throw is used in method implementation
Throws keyword is used to declare an exception.it is followed by class. Throws is used in method signature.
What is the difference between JAR and WAR files?
JAR files (Java ARchive) allows aggregating many files into one, it is usually used to hold Java classes in a library.
WAR files (Web Application ARchive) stores XML, java classes, and Java Server pages for Web Application purposes.
What is the difference between checked and unchecked exceptions?
Checked exceptions represent invalid conditions in areas outside the immediate control of the program.
Unchecked exceptions represent defects in the program (bugs), which are normally Runtime exceptions.
Can you explain fail-fast and fail-safe iterators? Give examples?
Fail-Fast Iterators
Fail-Fast iterators doesn’t allow modifications of a collection while iterating over it.These iterators throw ConcurrentModificationException if a collection is modified while iterating over it.They use original collection to traverse over the elements of the collection. These iterators don’t require extra memory.
Ex: Iterators returned by ArrayList, Vector, HashMap.
Fail-Safe Iterators
Fail-Safe iterators allow modifications of a collection while iterating over it.These iterators
don’t throw any exceptions if a collection is modified while iterating over it.They use copy of the original collection to traverse over the elements of the collection. These iterators require extra memory to clone the collection.
Ex: Iterator returned by ConcurrentHashMap.
Can we override private method in Java?
No, we cannot override private methods in Java as if we declare any variable, method as private that variable or method will be visible for that class only and also if we declare any method as private than they are bonded with class at compile time not in run time so we can’t reference those method using any object.
What is the return type of main () method in Java?
Main () method in java does not return anything and hence is always declared as void.
How to disable session in JSP?
Use this syntax <%@ page session=“false” %>.
What is an immutable object? Can you write one?
Immutable classes are Java classes whose objects cannot be modified once created. Any modification in an immutable object results in a new object. For example, ‘is String immutable in Java?’
What is wrapper classes?
Wrapper class wraps (encloses) around a data type and gives it an object appearance. They are used to convert any primitive type into an object. The primitive data types are not objects, they do not belong to any class and they are defined in the language itself. Sometimes, it is required to convert data types into objects in Java language.
Explain dot Operator?
It is used to access the instance variables and methods of class objects. Also, we can use it to access classes and sub-packages from a package.
How will you implement multiple inheritance in Java?
Java does directly support multiple inheritance unlike C++ but multiple inheritance can be implemented in Java through interfaces.
What is singleton class and how can we make a class singleton?
Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.
What is Type Erasure?
Type Erasure is the technique using which the Java compiler translates generic / parameterized type to raw type in Java generics. It is applicable in the context of generics. It can be used as an easy way to define typing over implicitly typed languages. The reverse operation is called type inference.
What is Applets?
A small program based on Java that can be transformed from one computer to another using the Applet Viewer or web browser.
What is an object's lock in Java and which objects have locks?
Object’s Locking is a mechanism used to synchronize a non-static content of code, so that only one object can hold the monitor and can access the non-static content. Developers can use this lock when two or more threads are sharing the same object of the class that implements Runnable or extends Thread. All objects and classes have locks.
What is the difference between abstraction and encapsulation?
The most important difference between Abstraction and Encapsulation is
Abstraction:
- It solves the problem at design level.
- Abstraction focus on outer lookout.
- Abstraction is supported using interface and abstract class.
Encapsulation
- It focuses on internal working or inner lookout
- It solves it implementation level.
- Encapsulation is supported using access modifiers.
What is a Spring?
Wikipedia defines the spring framework as “an application framework and inversion of control container for the Java platform. The framework’s core features can be used by any Java application, but there are extensions for building web applications on top of the Java EE platform.” Spring is essentially a lightweight, integrated framework that can be used for developing enterprise applications in java.
What is Streams in Java? And what is difference between Byte Streams and Character Streams?
A stream is a way of sequentially accessing a file. In Streams you can process the data one
at a time as, bulk operations are unavailable with them. But, streams support a huge range
of source and destinations including disk file, arrays, other devices, other programs etc. In Java, a byte is not the same thing as a char. Therefore, a byte stream is different from a character stream.
Java defines two types of streams: Byte Streams and Character Streams.
Byte Streams: A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, however not quite appropriate for text files.
Character Streams: A character stream will read a file character by character. Character Stream is a higher-level concept than Byte Stream. A Character Stream is, effectively, a Byte Stream that has been wrapped with logic that allows it to output characters from a specific encoding.
hat means, a character stream needs to be given the file’s encoding in order to work properly. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc. All character stream classes are descended from Reader and Writer.
What are some of the important spring annotations?
Some of the spring annotations are:
@Controller: controller classes in Spring MVC project.
@Request Mapping: This annotation can be used configuring URI mapping in controller handler methods. This is a very important annotation, so you should go through Spring MVC RequestMapping Annotation Examples
@Qualifier: This annotation can be used along with @Autowired to do autowiring by name. You can specify the name of the bean as @Qualifier(“beanName”).
@Primary: It allows to give preference to a bean when multiple beans match for autowiring.
@ResponseBody: sending Object as response, usually for sending XML or JSON data as response.
@PathVariable: mapping dynamic values from the URI to handler method arguments.
@Auto wired: autowiring dependencies in spring beans.
@Scope: configuring scope of the spring bean.
@Service: specialization of the component annotation.
@Configuration, @ComponentScan and @Bean: This are java based configurations.
AspectJ annotations for configuring aspects and advices, @Aspect, @Before, @After, @Around, @Pointcut etc.
Spring Core annotations
@Component: It is used over a class to tell spring to consider it as a bean component.
@ComponentScan: This annotation can be used to specify the packages that need to be scanned for components and is usually used along with @Configuration classes. You may also specify the same behaviour using <context: component-scan in spring xml configuration.
@Repository, @Service and @Controller: This annotation is used instead of @Component based on actual application layer you are developing. However technically all behave the same. If in doubt, use @Component.
@Profile: It allows to specify a profile for your bean.
@ActiveProfiles: It can be used from a JUnit test to specify the active profiles
How to delete a Cookie in a JSP?
The following code explain how to delete a Cookie in a JSP:
Cookie mycook = new Cookie ("name1","value1");
response.addCookie (mycook1);
Cookie killmycook = new Cookie ("mycook1","value1");
killmycook. set MaxAge (0);
killmycook . set Path ("/");
killmycook . addCookie (killmycook 1);
What is exception handling?
Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc. The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.
Why am i getting a NoClassDefFoundError in Java?
NoClassDefFoundError means that the class is present in the class path at Compile time, but it doesn’t exist in the class path at Runtime. After you compile your code, you end up with .class files for each class in your program. These binary files are the bytecode that Java interprets to execute your program.
The NoClassDefFoundError indicates that the class loader, which is responsible for dynamically loading classes, cannot find the .class file for the class that you’re trying to use.
The following are the three-reason cause for this error:
- ClassNotFoundException- .class not found for that referenced class irrespective of whether it is available at compile time or not (i.e. base/child class).
- Class file located, but Exception raised while initializing static variables
- Class file located, Exception raised while initializing static blocks.
What is the difference between web server and app server?
A Web server exclusively handles HTTP requests, whereas an application server serves business logic to application programs through any number of protocols.
What is difference between ArrayList and LinkedList in Java?
Array List:-Implemented with the concept of dynamic array.
ArrayList<Type> arrL = new ArrayList<Type> ();
Linked List:-Implemented with the concept of doubly linked list.
LinkedList<Type> linkL = new LinkedList<Type> ();
Comparison between Array List and Linked List:-
- Insertions are easy and fast in Linked List as compared to Array List because there is no risk of resizing array and copying content to new array if array gets full which makes adding into Array List of O (n) in worst case, while adding is O (1) operation in Linked List in Java. Array List also needs to be update its index if you insert something anywhere except at the end of array.
- Removal also better in Linked List than Array List due to same reasons as insertion.
- Linked List has more memory overhead than Array List because in Array List each index only holds actual object (data) but in case of Linked List each node holds both data and address Of next and previous node.
- Both Linked List and Array List require O (n) time to find if an element is present or not. However we can do Binary Search on Array List if it is sorted and therefore can search in O (Log n) time.
Some most asked basic java Interview Questions?
- What are Java New Features?
- What is the diffrence between New Version snad Previous version?
- what is comparator and comparae with program?
- Explain Hash Map?
- What is Auowriting Concept?