Java Interview Questions - Part III

52) What classes of exceptions may be thrown by a throw statement?

A throw statement may throw any expression that may be assigned to the Throwable type.

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

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic. "==" or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

54) Can we Overload or Override static methods in java?

Overriding: Overriding is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in superclass (or base class) at runtime.

Overloading: Overloading is related to compile time (or static) polymorphism. This feature allows different methods to have same name, but different signatures, especially number of input parameters and type of input paramaters.

55) Can we overload static methods?

The answer is 'Yes'. We can have two ore more static methods with same name, but differences in input parameters.

56) Can we Override static methods in java?

We can declare static methods with same signature in subclass, but it is not considered overriding as there won't be any run-time polymorphism. Hence the answer is 'No'. Static methods cannot be overridden because method overriding only occurs in the context of dynamic (i.e. runtime) lookup of methods. Static methods (by their name) are looked up statically (i.e. at compile-time).

57) Why the main method is static in java?

Java main() method is always static, so that compiler can call it without the creation of an object or before the creation of an object of the class.

Static method of a class can be called by using the class name only without creating an object of a class.

The main() method in Java must be declared public, static and void. If any of these are missing, the Java program will compile but a runtime error will be thrown.

58) What is the scope of variables in Java in following cases?

Member Variables (Class Level Scope): The member variables must be declared inside class (outside any function). They can be directly accessed anywhere in class

Local Variables (Method Level Scope): Variables declared inside a method have method level scope and can't be accessed outside the method.

Loop Variables (Block Scope): A variable declared inside pair of brackets "{" and "}" in a method has scope withing the brackets only.

59) What is "this" keyword in java?

The 'this' keyword refers to the current object in a method or constructor.

The most common use of the 'this' keyword is to eliminate the confusion between class attributes and parameters with the same name

60) Can we overload main() method?

We can overload the main method in Java. But the program doesn't execute the overloaded main method when we run your program, we need to call the overloaded main method from the actual main method only.

61) Why method overloading is not possible by changing the return type in java?

In C++ and Java, functions can not be overloaded if they differ only in the return type. The return type of functions is not a part of the mangled name which is generated by the compiler for uniquely identifying each function. The No of arguments, Type of arguments & Sequence of arguments are the parameters which are used to generate the unique mangled name for each function. It is on the basis of these unique mangled names that compiler can understand which function to call even if the names are same(overloading).

62) Can we override private methods in Java?

No, a private method cannot be overridden since it is not visible from any other class.

63) What is blank final variable?

A final variable in Java can be assigned a value only once, we can assign a value either in declaration or later.

    final int i = 10;
    i = 30; // Error because i is final.

64) Differences between HashMap and HashTable in Java.

1. HashMap is non synchronized. It is not-thread safe and can't be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.

2. HashMap allows one null key and multiple null values whereas Hashtable doesn't allow any null key or value.

3. HashMap is generally preferred over HashTable if thread synchronization is not needed

65) How are Java objects stored in memory?

In Java, all objects are dynamically allocated on Heap. This is different from C++ where objects can be allocated memory either on Stack or on Heap. In C++, when we allocate object using new(), the object is allocated on Heap, otherwise on Stack if not global or static.

In Java, when we only declare a variable of a class type, only a reference is created (memory is not allocated for the object). To allocate memory to an object, we must use new(). So the object is always allocated memory on the heap.

66) Why multiple inheritance is not supported in java?

Java supports multiple inheritance but not through classes, it supports only through its interfaces. The reason for not supporting multiple inheritance is to avoid the conflict and complexity arises due to it and keep Java a Simple Object Oriented Language. If we recall this in C++, there is a special case of multiple inheritance (diamond problem) where you have a multiple inheritance with two classes which have methods in conflicts. So, Java developers decided to avoid such conflicts and didn't allow multiple inheritance through classes at all.

67) Can a top level class be private or protected?

Top level classes in java can't be private or protected, but inner classes in java can. The reason for not making a top level class as private is very obvious, because nobody can see a private class and thus they can not use it. Declaring a class as protected also doesn't make any sense. The only difference between default visibility and protected visibility is that we can use it in any package by inheriting it. Since in java there is no such concept of package inheritance, defining a class as protected is no different from default.

68) Difference between Set and List interface.

Set and List both are child interface of Collection interface. There are following two main differences between them.

List can hold duplicate values but Set doesn't allow this.

In List interface data is present in the order you inserted but in the case of Set insertion order is not preserved.

69) Is it possible to create an object for an interface?

Its not possible to create an object for an interface since it does not contain any implementation. We can create an instance of a class and assign it to interface object variable provides the signature of the methods should be same for the interface and the class.

70) Is it possible to create an object for the abstract class?

We cannot create an object for the abstract class. We can create an instance of a class and assign it to abstract object variable provides the signature of the methods should be same for the interface and the class.

71) What is casting?

There are two types of casting, casting between primitive numeric types and casting between object references. Casting between numeric types is used to convert larger values, such as double values, to smaller values, such as byte values. Casting between object references is used to refer to an object by a compatible class, interface, or array type reference.

Java Interview Question - Part I

Java Interview Question - Part II