Java Basic Questions

Category : Java | Sub Category : Java Interview questions | By Runner Dev Last updated: 2020-10-01 15:55:19 Viewed : 211

Java Basic Questions

 1.           Q: What do you know about Java?

A: Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

2.           Q: What are the supported platforms by Java Programming Language?

A: Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Redhat Linux, Ubuntu, CentOS, etc.

3.           Q: List any five features of Java?

A: Some features include Object Oriented, Platform Independent, Robust, Interpreted, Multi-threaded

4.           Q: What is Java Virtual Machine and how it is considered in context of Java’s platform independent feature?

A: When Java is compiled, it is not compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over the web and interpreted by virtual Machine (JVM) on whichever platform it is being run. 

5.           Q: What do you mean by Object?

A: Object is a runtime entity and it’s state is stored in fields and behavior is shown via methods. 

6. Q: List two   Java IDE’s?

A: Netbeans, Eclipse, etc

7.           Q: Define class?

A: A class is a blue print from which individual objects are created. A class can contain fields and methods to describe the behavior of an object.

8.           Q: What kind of variables a class can consist of?

A: A class consist of Local variable, instance variables and class variables.

9.           Q: What is a Local Variable

A: Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and it will be destroyed when the method has completed.

10.           Q: What is a Instance Variable

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

 11.           Q: What is a Class Variable

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

12.           Q: What do you mean by Constructor?

A: 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.

13.           Q: List the three steps for creating an Object for a class?

A: An Object is first declared, then instantiated and then it is initialized.

14.           Q: What is the default value of byte datatype in Java? A: Default value of byte datatype is 0.

15.           Q: What is the default value of float and double datatype in Java?

A: Default value of float and double datatype in different as compared to C/C++. For float its 0.0f and for double it’s 0.0d

16.           Q: When a byte datatype is used?

A: This data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.

17.           Q: What is a static variable?

A: Class variables also known as static variables are declared with the static keyword in a class, but outside a method, constructor or a block.

18.           Q: What do you mean by Access Modifier?

A: 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.

19.           Q: According to Java Operator precedence, which operator is considered to be with highest precedence?
A: Postfix operators i.e () [] . is at the highest precedence.

20.        Q: When parseInt() method can be used?

A: This method is used to get the primitive data type of a certain String.

21.           Q: Why is String class considered immutable?

A: The String class is immutable, so that once it is created a String object cannot be changed. Since String is immutable it can safely be shared between many threads ,which is considered very important for multithreaded programming.

22.           Q: Why is StringBuffer called mutable?

A: The String class is considered as immutable, so that once it is created a String object cannot be changed. If there is a necessity to make alot of modifications to Strings of characters then StringBuffer should be used.

23.           Q: What is the difference between StringBuffer and StringBuilder class?

A: Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.

24.           Q: Which package is used for pattern  matching with regular expressions?
 A: java.util.regex package is used for this purpose.

25.          Q: java.util.regex consists of which classes?

A: java.util.regex consists of three classes: Pattern class, Matcher class and PatternSyntaxException class.

26.           Q: What is finalize() method?

A: It is possible to define a method that will be  called just before  an objects  final destruction by the garbage collector. This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.

27.           Q: What is an Exception?

A: An exception is a problem that arises during the execution of a program. Exceptions are caught by handlers positioned along the threads method invocation stack.

28.           Q: What do you mean by Checked Exceptions?

A: It is an exception that is typically a user error or a problem that cannot be foreseen by the programmer. For example, if a file is to be opened, but the file cannot be found, an exception occurs. These exceptions cannot simply be ignored at the time of compilation.

29.           Q: Explain Runtime Exceptions?

A: It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compliation.

30.           Q: Which are the two subclasses under Exception class?

A: The Exception class has two main subclasses : IOException class and RuntimeException Class.

31.           Q: When throws keyword is used?

A: If a method does not handle a checked exception, the method must declare it using the throwskeyword. The throws keyword appears at the end of a methods signature.

32.           Q: When throw keyword is used?

A: An exception can be thrown, either a newly instantiated one or an exception that you just caught, by using throw keyword.

33.           Q: How finally used under Exception Handling?

A: 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.

34.           Q: Define Inheritance?

A: It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.

35.           Q: When super keyword is used?

A: If the method overrides one of its superclass methods, overridden method can be invoked through the use of the keyword super. It can be also used to refer to a hidden field

36.           Q: What is Polymorphism?

A: 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.

37.           Q: What is Abstraction?

A: It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.

38.           Q: What is Abstract class

A: These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.

39.           Q: When Abstract methods are used?

A: If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.

40.        Q: What is Encapsulation?

A: It is the techn

41.           Q: What is the primary benefit of Encapsulation?

A: The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this Encapsulation gives maintainability, flexibility and extensibility to our code.

42.           Q: What is an Interface?

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

43.           Q: Give some   features of Interface?
A: It includes: 
  •    Interface  cannot be instantiated
  •   An interface does not contain any constructors.
  •   All of the methods in an interface are abstract.

44.           Q: Define Packages in Java?

A: A Package can be defined as a grouping of related types(classes, interfaces, enumerations and annotations ) providing access protection and name space management.

45.           Q: Why Packages are used?

A: Packages are used in Java in-order to prevent naming conflicts, to control access, to make searching/locating and usage of classes, interfaces, enumerations and annotations, etc., easier.

46.           Q: What do you mean by Multithreaded program?

A: A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is called a thread, and each thread defines a separate path of execution.

47.           Q: What are the two ways in which Thread can be created?

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

48.           Q: What is an applet?

A: An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.

49.           Q: An applet extend which class?

A: An applet extends java.applet.Applet class.

50.        Q: Explain garbage collection in Java?

A: It uses garbage collection to free the memory. By cleaning those objects that is no longer reference by any of the program.ique 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. 

51.            Q: Explain garbage collection in Java?

A: It uses garbage collection to free the memory. By cleaning those objects that is no longer reference by any of the program.

52.            Q: Explain the usage of this() with constructors?

A: It is used with variables or methods and used to call constructer of same class.

53.            Q: Explain Set Interface?

A: It is a collection of element which cannot contain duplicate elements. The Set interface contains only methods inherited from Collection and adds the restriction that duplicate elements are prohibited.

54.            Q: Explain TreeSet?

A: It is a Set implemented when we want elements in a sorted order.

55.            Q: What is Comparable Interface?

A: It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.

56.            Q: Difference between throw   and  throws? 
A: It includes:

Throw is used to trigger an exception where as throws is used in declaration of exception.

 Without throws, Checked exception cannot be handled where as checked exception can be propagated with throws.

  62.  Q: Explain the following line used under Java Program: public static void main (String args[ ])

A: The following shows the explanation individually:


public: it is the access specifier.


static: it allows main() to be called without instantiating a particular instance of a class.

void: it affirns the compiler that no value is returned by main(). main(): this method is called at the beginning of a Java program. String args[ ]: args parameter is an instance array of class String

63.            Q: Define JRE i.e. Java Runtime Environment?

A: Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java programs. It provides the minimum requirements for executing a Java application;

64.            Q: What is JAR file?

A: JAR files is Java Archive fles and it aggregates many files into one. It holds Java classes in a library. JAR files are built on ZIP file format and have .jar file extension.

65.            Q: What is a WAR file?

A: This is Web Archive File and used to store XML, java classes, and JavaServer pages. which is used to distribute a collection of JavaServer Pages, Java Servlets, Java classes, XML files, static Web pages etc.

66.            Q: Define JIT compiler?

A: It improves the runtime performance of computer programs based on bytecode.

67.            Q: What is the difference between object oriented programming language and object based programming language?

A: Object based programming languages follow all the features of OOPs except Inheritance. JavaScript is an example of object based programming languages

68.            Q: What is the purpose of default constructor?

A: The java compiler creates a default constructor only if there is no constructor in the class.

4.            Q: Can a constructor be made final? A: No, this is not possible.

69.            Q: What is static block?

A: It is used to initialize the static data member, It is excuted before main method at the time of classloading.

70.            Q: Define composition?

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

71.            Q: What is function overloading?

A: If a class has multiple functions by same name but different parameters, it is known as Method Overloading.

72.            Q: What is function overriding?

A: If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.

73.            Q: Difference between Overloading and Overriding?

A: Method overloading increases the readability of the program. Method overriding provides the specific implementation of the method that is already provided by its super class parameter must be different in case of overloading, parameter must be same in case of overriding.

74.            Q: What is final class?

A: Final classes are created so the methods implemented by that class cannot be overridden. It can’t be inherited.

75.            Q: What is NullPointerException?

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

76.            Q: What are the ways in which a thread can enter the waiting state?

A: A thread can enter the waiting state by invoking its sleep() method, by blocking on IO, by unsuccessfully attempting to acquire an objects lock, or by invoking an objects wait() method.  It can also enter the waiting state by invoking its (deprecated) suspend() method.

77.            Q: How does multi-threading take place on a computer with a single CPU?

A: The operating systems task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.

78.            Q: What invokes a threads run() method?

A: After a thread is started, via its start() method of the Thread class, the JVM invokes the threads run() method when the thread is initially executed.

79.            Q: Does it matter in what order catch statements for FileNotFoundException and IOException are written?

A: Yes, it does. The FileNoFoundException is inherited from the IOException. Exceptions subclasses have to be caught first.

80.            Q: What is the difference between yielding and sleeping?

A: 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.

81.            Q: Why Vector class is used?

A: The Vector class provides the capability to implement a growable array of objects. Vector proves to be very useful if you dont know  the size of the array in advance, or you just need one that can change sizes over the lifetime of a program.

82.            Q: How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters?

A: Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns.

83.            Q: What are Wrapper classes?

A: These are classes that allow primitive types to be accessed as objects. Example: Integer, Character, Double, Boolean etc.

84.            Q: What is the difference between a Window and a Frame?

A: The Frame class extends Window to define a main application window that can have a menu bar.

85.            Q: Which package has light weight components?

A: javax.Swing package. All components in Swing, except JApplet, JDialog, JFrame and JWindow are lightweight components.

84.            Q: What is the difference between the paint() and repaint() methods?

A: The paint() method supports painting via a Graphics object. The repaint() method is used to cause paint() to be invoked by the AWT painting thread.

85.            Q: What is the purpose of File class?

A: It is used to create objects that provide access to the files and directories of a local file system.

9.            Q: What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy? A: The Reader/Writer class hierarchy is character-oriented, and the InputStream/OutputStream class hierarchy is byte-oriented.

86.          Q: Which class should you use to obtain design information about an object?

A: The Class class is used to obtain information about an objects design and java.lang.Class class instance represent classes, interfaces in a running Java application.

87.          Q: What is the difference between static and non-static variables?

A: A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.

88.          Q: What is Serialization and deserialization?

A: Serialization is the process of writing the state of an object to a byte stream. Deserialization is the process of restoring these objects.

13.          Q: Can you write a Java class that could be used both as an applet as well as an application? A: Yes, just add a main() method to the applet.

89.          Q: What is the difference between Swing and AWT components?

A: AWT components are heavy-weight, whereas Swing components are lightweight. Heavy weight components depend on the local windowing toolkit. For example, java.awt.Button is a heavy weight component, when it is running on the Java platform for Unix platform, it maps to a real Motif button.

90.          Q: Whats the difference between constructors and other methods?
A: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times.

91.          Q: Is there any limitation of using Inheritance?

A: Yes, since inheritance inherits everything from the super class and interface, it may make the subclass too clustering and sometimes error-prone when dynamic overriding or dynamic overloading in some situation.

92.          Q: Whats the difference between the methods sleep() and wait()?

A: The code sleep(2000); puts thread aside for exactly two seconds. The code wait(2000), causes a wait of up to two second. A thread could stop waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread.

93.          Q: When ArithmeticException is thrown?

A: The ArithmeticException is thrown when integer is divided by zero or taking the remainder of a number by zero. It is never thrown in floating-point operations.

94.          Q: What is the Collections API?

A: The Collections API is a set of classes and interfaces that support operations on collections of objects.

95.          Q: Does garbage collection guarantee that a program will not run out of memory?

A: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection.

96.          Q: What is the difference between a break statement and a continue statement?

A: A break statement results in the termination of the statement to which it applies (switch, for, do, or while). A continue statement is used to end the current loop iteration and return control to the loop statement.

97.       Q: If a variable is declared as private, where may the variable be accessed?

A: A private variable may only be accessed within the class in which it is declared.

98.   What is JVM ? Why is Java called the “Platform Independent Programming Language” ? A Java virtual machine (JVM) is a process virtual machine that can execute Java bytecode. Each Java source file is compiled into a bytecode file, which is executed by the JVM. Java was designed to allow application programs to be built that could be run on any platform, without having to be rewritten or recompiled by the programmer for each separate platform. A Java virtual machine makes this possible, because it is aware of the specific instruction lengths and other particularities of the underlying hardware platform.

99.           What is the Difference between JDK and JRE ? The Java Runtime Environment (JRE) is basically the Java Virtual Machine (JVM) where your Java programs are being executed. It also includes browser plugins for applet execution. The Java Development Kit (JDK) is the full featured Software Development Kit for Java, including the JRE, the compilers and tools (like JavaDoc, and Java Debugger), in order for a user to develop, compile and execute Java applications.

100.           Explain the available thread states in a high-level. During its execution, a thread can reside in one of the following states:

Runnable: A thread becomes ready to run, but does not necessarily start running immediately.

Running: The processor is actively executing the thread code.

Waiting: A thread is in a blocked state waiting for some external processing to finish.

Sleeping: The thread is forced to sleep.

Blocked on I/O: Waiting for an I/O operation to complete. Blocked on Synchronization: Waiting to acquire a lock. Dead: The thread has finished its execution.

Related Articles

Leave a Comment: