Java is high-level programming and independent language specifically designed to work on the Internet. It has a feature that provides you better “look and feels” then the C++ language, and pretty easier to use than C++. It works efficiently on the concept of object-oriented programming. Here is a list of important questions related to Java.
- What are the main features of Java?
The primary goal of Java programming language development was to create the language of programming portable, easy and safe.Besides that, there are also some amazing features that play an significant part in this language’s popularityJava’s characteristics are also called java buzzwords.
- Simple: Java can be learned very easily and its syntax is simple.
- Object-based: Java works purely on the concept of Object based programming . Everything in java is object. all programs and data reside inside objects and classes.
- Independent platform: Java compiler generates an platform independent code called bytecode. It is a self regulating platform as compared to other programming languages such as C, C ++ etc.
- Security: Java’s safety is best known. We can create virus-free systems with Java.
- Portable: Java is portable because it makes the Java bytecode easier to perform to any platform. It does not involve any kind of execution.
2. What is an interface in Java?
Interfaces are a way to implement dynamic bonding or polymorphism. The interface defines a set of methodologies as contracts. The class that is implementing this interface must implement these functionalities in a concrete class. When you are building a large system or library, the interface adds value.
Using references to interfaces instead of classes helps minimize future changes to the user interface.
3. Is JVM’s is also Platform Independent like Java?
JVM’S are not platform Independent. JVM’S are platform Specific.
4. What can you do if the main method asserts as private?
No, it will compile fine but in run time it will give error like the main method should be in public and cannot find the main method.
5. Why Java is platform-independent?
Platform-independent practically means “write once run anywhere”. Java is called so because of its byte codes which can run on any system irrespective of its underlying operating system.
Read more about Java String Interview Questions
6. Why java is not 100% Object-oriented?
No, Java is not really 100% object-based language platform since it uses eight primitive data types which are not really objects. These 8 primitive Data Types are
7. What are wrapper classes?
Wrapper classes convert Java primitives to reference types (objects). Each primitive data type has a class that dedicates it. These classes are known because they “wrap” the primitive data type into the object of that class.
8. What is a singleton class and how can we create a class singleton?
Singleton is a class whose only one instance can be created at any time, in a JVM. A class can be made by making its constructor private.
9. What an I/O filter?
An I/O filter is an object that reads from one stream and writes to another, usually altering the data in some way as it is passed from one stream to another.
10. How to Create an Object without using the keyword “new” in java?
Without new, the Factory methods are used to create objects for a class. For example
here Calendar is a class and the method getInstance() is a Factory method which can create an object for Calendar class.
11. Explain the jspDestroy() method.
jspDestry() method is invoked from java.servlet.jsp.JspPage interface whenever a JSP page is about to be destroyed. Servlets destroy methods can be easily overridden to perform cleanup, like when closing a database connection.
12. What feature made Java different from all other programming languages?
Java is such a Programming Language through which One can Develop any type of Applications like Desktop Application, Web Application, and Android Application.
Java is a Platform Independent Programming Language. That means If Once the Java Program is written in any Operating System, then you can run that Program in any Operating System. So Java is also known as WORA type of Programming Language. Means Write Once and Run Anywhere (WORA).
Java supports the Object-Oriented Programming Features.
Due to all the above Features, Java is a Different from Other programming languages.
13. How is JSP better than Servlet technology?
JSP is a technology on the server’s side to make content generation simple. They are document-centric, whereas servlets are programs. A Java server page can contain fragments of Java program, which execute and instantiate Java classes. However, they occur inside an HTML template file.
14. What do you understand by a Runnable interface in Java?
Runnable is an interface designed to encapsulate some independent unit of work that can run and do something useful and isn’t specifically expected to return a value at the end.
15. Why was the String class made inviolable in Java?
For performance & thread-safety.
16. How can a constant variable be defined in Java?
The variable should be declared to be static and final. So there’s only one copy of the variable for all category value instances that can’t change either.
17. What is the return type of the main method?
The main method does not return anything, so we can declare it void.
18. What’s a Package?
The package is mainly a compilation of interactive classes and interfaces. Package declaration should be the first statement in a very java class.
19. What happens if we implement Serializable Interface in Java
When we need to store a copy of the object. Send them to another process on the same system or network. Because we want to store or send an object. It makes storing and sending the object easily. It doesn’t have anything to do with Security.
20. how constructors are used in Java?
Mainly constructors are used when you want to perform any actions on the start of the application(When some display need to be shown or some music need to be played or to perform some swing operations ).
- With the help of a constructor (along with parameters), you can ‘ask’ the user of that class for required dependencies.
- It is used to initialize instance variables
- and to pass up arguments to the constructor of a superclass (super(…)), which does the same
You need to write those operations in the constructor.
Constructors don’t have return types not even void and must be of the same name as that of the class.
21. What do you know about a constructor in Java?
A constructor is a method which must be declared public and should not have any return type- even void is not allowed! Also, it is called automatically during object creation- you cannot call it like other functions- remember it is a very special and unique function. Unique doesn’t mean it cannot be overloaded. You are allowed to have more than one constructors for a single class.
22. Why is it required in java to overload the constructors?
Constructor overloading is very helpful way to replicate the default values, or to creat an object from an already existing example. When we have more than one member variables and multiple users are using our API. Sometimes calling API does not have all the variables defined in our class.
So instead of providing null values to we can provide multiple constructors and set the reaming member variables as per out requirement.
23. What is the base class of all the classes?
24. What is javac?
It produces the java byte code from *.java file.
It is the intermediate representation of your source code that contains instructions.
25. Differentiation between String, StringBuffer, StringBuilder in Java?
· the difference between StringBuffer and StringBuilder are:-
1)every method present in StringBuffer is synchronized but no method present in StringBuilder is synchronized.
2)Only one thread is allowed to operate on the StringBuffer object at a time, therefore the StringBuffer object is thread-safe.multiple threads are allowed to operate on StringBuilder object thus StringBuilder object is not thread-safe.
3) Since only one thread is allowed in StringBuffer, the thread wait time increases and therefore the performance is low. StringBuilder is comparatively high on performance.
4)StringBuffer was introduced in 1.0 version while StringBuilder was introduced in 1.5 version.
26. Differentiation between JDK, JRE and JVM?
In simply difference between JDK, JRE and JVM is
· JDK (Java Development Kit) provides an environment to develop and run Java applications.
· JRE (Java Runtime Environment) provides the environment only to run java applications. For example, you installed java application in the client machine, then the client is responsible for run java application, not responsible for developing java application. So here JRE is required.
· JVM (Java Virtual Machine) is responsible to run our java application line by line. So here JVM is an interpreter.
27. What are “Java OOPs concepts”?
OOPs, concept forms the basis of Java Programming Language, that’s it is called an object-oriented language.
OOPs stands for Object-Oriented Programming. An approach to do programming which is based on Objects (or real-life objects). There are mainly six OOPs concepts in general.
28. How can you find duplicate characters in a string in java?
Write a java program to find duplicate characters and their count in a given string. For example, in a string “Better Butter”, duplicate characters and their count is t: 4, e : 3, r: 2 and B: 2.
29. What do you understand by Exception handling in java?
Exception handling a mechanism to handle runtime errors in java and compile time so that the normal flow of the application can be maintained. It upkeep the errors such as ClassNotFound, IO, SQL, Remote, etc.
Exception handling is used in java to minimize the loss or in novice, term to stop the abnormal termination of the program .jsut consider the possibility that there is a file on once the server and at run time this file is not available then some other alternative should be there to provide file because the program is running all the resources is running parallelly running .
30. Which class is the super class of all classes?
The java.lang.An object is the root class for all classes and we do not need to extend it.
30. Which class is the superclass of all classes?
java.lang.An object is the root class for all the java classes and we don’t need to extend it.
31. Is it necessary that each block be followed by a gripping block?
It is not necessary that each block is followed by a catch block. This should be followed by a block catch or a final block. And whatever exceptions are likely to occur, it should be declared in the throws section of the method.
32. What is JDBC Connection Interface?
The connection interface maintains a database session. It can be used to manage transactions. It provides factory methods for the return of statements, readjustments, callable statements, and databasemetata.
33. Differentiation between preemptive scheduling and time slicing?
Preemptive scheduling is a general topic of scheduling algorithms. Preemptive Scheduling includes many of algorithms(Round Robin-Fixed Priority Preemtive Scheduling …). Time slicing schedulling is one of the preemptive based scheduling algorithms. But it has a few differences. Time slicing scheduling has a time quantum (tq) value to CPU switches another context (process). Tq value determines how long process work in the CPU uninterruptly. If you have CPU intesive processes, you will set your tq value high to avoid cpu trashing. (CPU counts time until tq value over.
34. Differentiation between a process and a thread?
There are several differences between a thread and a process:
Each process has its code, data and kernel context (VM structures, descriptor table, etc). The threads of the process share the same code, data and kernel context.
Processes usually form a tree hierarchy. P0 spawning off P1 and so on. While all the threads of a process are peers.
The process context switch is much heavier than a thread context switch for the OS kernel because there is much more state to change.
35. How one can throw an exception in Java?
Exceptions originate when a piece of Java code finds some sort of problem during execution and throws an exception object. This is done by using the throw keyword followed by an instance of the exception type to be thrown. It is often convenient to instantiate an exception object at the time the exception has to be thrown. Thus, a throw statement is typically written as follows:
throw new exception type(parameters);
where exception type is the type of the exception and the parameters are sent to that type’s constructor; most exception types offer a version of a constructor that accepts an error message string as a parameter.
36. what is the term ‘collection’ mean in java?
Java collections are kind of Data Structure. Collections are a group of Objects that means it’s like a container that has a set of elements in a single Unit. It is used to store and process the data effectively. This framework is provided in java. util package
37. What is encapsulation in Java?
In any object oriented programming (OOP) language, be it Java, encapsulation is a property in which the elements of a program – data members (variables) and member functions (functions) – are wrapped together in one unit. This is done through creation of “Classes” in any program. It is mainly a mechanism of wrapping the variables and
Java encapsulation is a system for wrapping information (variables) and code that acts as a single unit on information (methods).
38. What is polymorphism in java?
Polymorphism means that the same thing exists in many forms to perform various actions. It is generally used for function overloading. In real life, scenario supposes that there is a START function this START function can either start a bicycle, motorcycle or a car depending on the input.
39. Can we save and run a Java program with other names than the class name?
Yes, you can do it very easily. You just have to give a default access specifier for the class that contains the main method i.e. do not provide any access specifier not even public and you are good to go. Remember you can compile the program with any name but the compiler will automatically create the .class file with the name of the class that contains the main method so, the program will run by the class name only.
40 Why is Java not supporting various inheritances in Java?
Java doesn’t support multiple inheritances in classes because of “Diamond Problem”. To know more about the diamond problem with an example, read Multiple Inheritance in Java. H In interfaces, though, multiple inheritance is strongly supported. An interface can stretch various interfaces as they simply declare the techniques and there will be applied in the execution class. So there is no question of interfaces having a diamond problem.