Java Interview Questions - Part II


26) Define super class and subclass?

Super class is a class from which another class inherits.

Subclass is a class that inherits from one or more classes.

27) What is meant by Binding, Static binding, Dynamic binding?

Binding: Binding denotes association of a name with a class.

Static binding: Static binding is a binding in which the class association is made during compile time. This is also called as Early binding.

Dynamic binding: Dynamic binding is a binding in which the class association is not made until the object is created at execution time. It is also called as Late binding.

28) What is meant by Inheritance?

Inheritance is a relationship among classes, wherein one class shares the structure or behavior defined in another class. This is called Single Inheritance. If a class shares the structure or behavior from multiple classes, then it is called Multiple Inheritance. Inheritance defines "is-a" hierarchy among classes in which one subclass inherits from one or more generalised superclasses.

29) What is the use of Inheritance and what are its advantages?

Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses.

30) What is the difference between superclass and subclass?

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

31) Differentiate between a Class and an Object?

The Object class is the highest-level class in the Java class hierarchy. The Class is used to represent the classes and interfaces that are loaded by a Java program. The Class class is used to obtain information about an object's design. A Class is only a definition or prototype of real life object. Whereas an object is an instance or living representation of real life object. Every object belongs to a class and every class contains one or more related objects.

32) What is meant by Polymorphism?

Polymorphism literally means taking more than one form. Polymorphism is a characteristic of being able to assign a different behavior or value in a subclass, to something that was declared in a parent class.

33) Define Dynamic Binding?

Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.

34) What is final modifier?

The final modifier keyword makes that the programmer cannot change the value anymore. The actual meaning depends on whether it is applied to a class, a variable, or a method.

final Classes: A final class cannot have subclasses.

final Variables: A final variable cannot be changed once it is initialized.

final Methods: A final method cannot be overridden by subclasses.

35) What modifiers may be used with top-level class?

Public, abstract and final can be used for top-level class.

36) What is an Abstract Class?

Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods.

37) What are inner class and anonymous class?

Inner class: classes defined in other classes, including those defined in methods are called inner classes. An inner class can have any accessibility including private.

Anonymous class: Anonymous class is a class defined inside a method without a name and is instantiated and declared in the same place and cannot have explicit constructors

38) What is an Interface?

Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, no body).

39) What is an exception?

An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions.

40) Mention the different ways to generate an Exception?

There are two different ways to generate an Exception.

1. Exceptions can be generated by the Java run-time system. Exceptions thrown by Java relate to fundamental errors that violate the rules of the Java language or the constraints of the Java execution environment.

2. Exceptions can be manually generated by the code. Manually generated exceptions are typically used to report some error condition to the caller of a method.

41) What is a base class?

Base class is the most generalised class in a class structure. Most applications have such root classes. In Java, Object is the base class for all classes.

42) What is reflection in java?

Reflection allows Java code to discover information about the fields, methods and constructors of loaded classes and to dynamically invoke them.

43) Define superclass and subclass?

Superclass is a class from which another class inherits.

Subclass is a class that inherits from one or more classes.

44) What is the difference between abstract class and interface?

a) All the methods declared inside an interface are abstract whereas abstract class must have at least one abstract method and others methods can be concrete or abstract.

b) In abstract class, key word abstract must be used for the methods whereas interface we need not use that keyword for the methods.

c) Abstract class must have subclasses whereas interface can't have subclasses.

45) What are interface and its use?

Interface is similar to a class which may contain method's signature only but not bodies and it is a formal set of method and constant declarations that must be defined by the class that implements it. Interfaces are useful for:

a) Declaring methods that one or more classes are expected to implement

b) Capturing similarities between unrelated classes without forcing a class relationship.

c) Determining an object's programming interface without revealing the actual body of the class.

46) What is garbage collection? What is the process that is responsible for doing that in java?

Reclaiming the unused memory by the invalid objects. Garbage collector is responsible for this process.

47) What kind of thread is the Garbage collector thread?

It is a daemon thread.

48) What is a daemon thread?

These are the threads which can run without user intervention. The JVM can exit when there are daemon thread by killing them abruptly.

49) What are the different states of a thread?

The different thread states are ready, running, waiting and dead.

50) What is the difference between process and thread?

Process is a program in execution whereas thread is a separate path of execution in a program.

51) What is the difference between yielding and sleeping?

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.


Java Interview Question - Part II

Java Interview Question - Part III