JAVA

Java is a general-purpose computer-programming language







Java Questions Series 1

JVM- JVM is Java Virtual Machine, it is an abstract machine which provides the runtime environment in which java bytecode can be executed. It is a specification. JVMs are available for many hardware and software platforms (so JVM is platform dependent). JRE- JRE is Java Runtime Environment. It is the implementation of JVM. JDK- JDK is Java Development Kit. It contains JRE and development tools.

Just-In-Time(JIT) compiler is used to improve the performance. JIT compiles parts of the byte code that have similar functionality at the same time, and hence reduces the amount of time needed for compilation.

Serialization is used when data needs to be transmitted over the network. Using serialization, object’s state is saved and converted into byte stream .The byte stream is transferred over the network and the object is re-created at destination.

The classloader is a subsystem of JVM that is used to load classes and interfaces. There are many types of classloaders e.g. Bootstrap classloader, Extension classloader, System classloader, Plugin classloader etc.

The local variables are not initialized to any default value.

In Java, constructor is a block of codes similar to method. It is called when an instance of object is created and memory is allocated for the object. It is a special type of method which is used to initialize the object.

The default constructor provides the default values to the objects.

A static variable is common to all the instances (or objects) of the class because it is a class level variable. In other words you can say that only a single copy of static variable is created and shared among all the instances of the class. Memory allocation for such variables only happens once when the class is loaded in the memory.

A static method belongs to the class rather than object of a class. A static method can be invoked without the need for creating an instance of a class. Static method can access static data member and can change the value of it.

Java main method is static because object is not required to call static methods if it were non-static method, JVM would create the object first then call main() method that will lead the problem of extra memory allocation.

Yes, one of the way is static block. With static block we can execute a program without main() method.

this is a reference variable that refers to the current object.

Though both are used to pause currently running thread, sleep() is actually meant for short pause because it doesn't release lock, while wait() is meant for conditional wait and that's why it release lock which can then be acquired by another thread to change the condition on which it is waiting.

These are variables declared with in a class, outside any method, with the static keyword.

Singleton class control object creation. A singleton is a class that allows only a single instance of itself to be created and gives access to that created instance.

Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded.

Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class, the java compiler builds a default constructor for that class itself.

Default value of byte datatype is 0.

Java provides access modifiers to set access levels for classes, variables, methods and constructors. A member has package or default accessibility when no accessibility modifier is specified.

Java does not support multiple inheritance instead it supports multi-level inheritance which means in java any class cannot inherit to multiple classes.
There is no concept of pointers in java.
Java does not support unions and structures and destructors.
Java includes built-in support of memory management via garbage collections where as in C, developer has to take care of it.

finalize( ) will be called just before an object's final destruction by the garbage collector and it can be used to ensure that an object terminates cleanly.

The finally keyword is used to create a block of code that follows a try block. A finally block of code always executes, whether or not an exception has occurred.

Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object

It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.

An interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.

Thread can be created by: implementing Runnable interface, extending the Thread class.

It is used to free the memory. By cleaning those objects that are no longer reference in the program.

An immutable object can’t be changed once it is created.

Holding the reference of the other class within some other class is known as composition.

A NullPointerException is thrown when calling the instance method of a null object, accessing or modifying the field of a null object etc.

When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.

A transient variable is a variable that may not be serialized during Serialization and which is initialized by its default value during de-serialization,

Synchronization is the capability to control the access of multiple threads to shared resources. synchronized keyword in java provides locking which ensures mutual exclusive access of shared resource and prevent data race.

A dead thread cannot be restarted.

No, a top level class can not be private or protected. It can have either "public" or no modifier.

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run-time.

If you need to frequently add and remove elements from the middle of the list and only access the list elements sequentially, then LinkedList should be used. If you need to support random access, without inserting or removing elements from any place other than the end, then ArrayList should be used.

The dot operator(.) is used to access the instance variables and methods of class objects.It is also used to access classes and sub-packages from a package.

A thread is an execution in a program. The life cycle of a thread include −
Newborn state
Runnable state
Running state
Blocked state
Dead state

Import statement is allowed at the beginning of the program file after package statement.

The main thread is created automatically and it begins to execute immediately when a program starts. It is the thread from which all other child threads originate.

It is a simple drawing surface which are used for painting images or to perform other graphical operations.

Sockets provide the communication mechanism between two computers using TCP. A client program creates a socket on its end of the communication and attempts to connect that socket to a server.

It refers to writing programs that execute across multiple devices (computers), in which the devices are all connected to each other using a network.

Daemon thread is a low priority thread, which runs intermittently in the back ground doing the garbage collection operation for the java runtime system.

All tasks must implement the run() method.

Path and Classpath are operating system level environment variables. Path is defines where the system can find the executables(.exe) files and classpath is used to specify the location of .class files.

An object's lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object's lock.

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.

A class that is declared without any access modifiers is said to have package access. This means that the class can only be accessed by other classes and interfaces that are defined within the same package.

Overriding is a method with the same name and arguments as in a parent, whereas overloading is the same method name but different arguments

Yes it's possible. This is basically to avoid instance creation of the class

No not possible. Doing so will result in compilation error. public and abstract are the only applicable modifiers for method declaration in an interface.

A final variable's value can't be changed. final variables should be initialized before using them.

Main() method doesn't return anything hence declared void.

main() method is called by the JVM even before the instantiation of the class hence it is declared as static.

The read() method returns -1 when it has reached the end of a file.

Operator overloading makes the code very difficult to read and maintain. To maintain code simplicity, Java doesn't support operator overloading.

In java, to convert an object into byte stream by serialization, an interface with the name Serializable is implemented by the class. All objects of a class implementing serializable interface get serialized and their state is saved in byte stream.

The bytecode. Java is compiled to form a byte code which is the intermediate language between source code and machine code. This byte code is not platform specific.

Yes, a class can have multiple constructors with different parameters. Which constructor gets used for object creation depends on the arguments passed while creating the objects.

String is not a primitive data type in java. When a string is created in java, it’s actually an object of Java.Lang.String class that gets created. After creation of this string object, all built-in methods of String class can be used on the string object.

Multi threading is a programming concept to run multiple tasks in a concurrent manner within a single program. Threads share same process stack and running in parallel. It helps in performance improvement of any program.

If we want to execute any statements before even creation of objects at load time of class, we can use a static block of code in the class. Any statements inside this static block of code will get executed once at the time of loading the class even before creation of objects in the main method.

Constructor is called automatically when we create an object using new keyword. It’s called only once for an object at the time of object creation and hence, we can’t invoke the constructor again for an object after its creation.

Checked exceptions can be caught at the time of program compilation. Checked exceptions must be handled by using try catch block in the code in order to successfully compile the code.

In Java, there is not goto keyword and java doesn’t support this feature of going to a particular labeled line.

Constructor in Java must have same name as the class name and if the name is different, it doesn’t act as a constructor and compiler thinks of it as a normal method.

No, Java class main method can have only void return type for the program to get successfully executed.

In Java, if we define a new class inside a particular block, it’s called a local class. Such a class has local scope and isn’t usable outside the block where it’s defined.

In Java, Java.lang.Throwable is the super class of all exception classes and all exception classes are derived from this base class.

Phantom memory is false memory. Memory that does not exist in reality.

Java file will not compile as we can have only one public class in Java file. Rest all classes have to be non-public. Also the name of java file has to be same as that of public class.

No, Java is not pure objected oriented programming language because it does support 8 primitive data types.

No, you cannot store a double value into a long variable without casting because the range of double is more that long and you we need to type cast.

An Integer object will take more memory than an int. Integer is an object and it store meta data overhead about the object and int is primitive type so its takes less space.

The size of an int variable is constant in Java, it's always 32-bit irrespective of platform.

Yes, two unequal objects can have same hashcode that's why collision happen in a hashmap.

Java Annotations is a tag which symbolizes metadata associated with class, interface, methods, fields, etc. The additional information carried by annotations are utilized by java compiler and JVM.

The Vector class provides the ability to execute a growable array of objects. Vector proves to be very useful if you don’t know the size of the array in advance, or we need one that can change sizes over the lifetime of a program.

This method returns a hash code value (an integer number).

Platform independence means that we can write and compile the java code in one platform (eg Windows) and can execute the class in any other supported platform eg (Linux,Solaris,etc).

java.lang.Object

A package is a collection of related classes and interfaces providing access protection and namespace management.

A super class is a class that is inherited whereas sub class is a class that does the inheriting.

We can set the security in applets using setSecurityManager() method.

JDBC is a set of Java API for executing SQL statements. This API consists of a set of classes and interfaces to enable programs to write pure Java Database applications.

Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions.

The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a Collection.

A memory leak is where an unreferenced object that will never be used again still hangs around in memory and doesnt get garbage collected.

An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception.

There are four principle concepts of OOPS are:
Abstraction
Polymorphism
Inheritance
Encapsulation

No, because main is a static method. A static method can't be overridden in Java.

super is a keyword which is used to access the method or member variables from the superclass.

Yes, it is always necessary to create an object implementation for an interface. Interfaces cannot be instantiated in their own and we must write a class that implements the interface and fulfill all the methods defined in it.

Yes, other nonabstract methods can access a method that you declare as abstract.

Set contain only unique elements while List can contain duplicate elements. Set is unordered while List is ordered. List maintains the order in which the objects are added.

Map object has unique keys each containing some value, while Set contain only unique values.

Queue is a data structure which is based on FIFO (first in first out) property. An example of Queue in real world is buying tickets in a stadium.
Stack is a data structure which is based on LIFO (last in first out) property. An example of Stack in real world is insertion or removal of CD from the CD case.

HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values.
HashMap is not synchronized or thread-safe while Hashtable is synchronized or thread-safe.



codenody