Open In App

Java Core Concepts Interview Questions

Last Updated : 27 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, Input/Output (I/O) and related concepts like streams, classloaders, packages, and keywords play a key role in building robust applications. These fundamentals help developers handle files, user inputs, outputs, memory, and object serialization effectively.

1. What is a classloader?

Classloader is the part of JRE(Java Runtime Environment), during the execution of the bytecode or created .class file classloader is responsible for dynamically loading the java classes and interfaces to JVM(Java Virtual Machine). Because of classloaders Java run time system does not need to know about files and file systems.

To know more about the topic refer to ClassLoader in Java.

2. What is Java String Pool?

A Java String Pool is a place in heap memory where all the strings defined in the program are stored. A separate place in a stack is there where the variable storing the string is stored. Whenever we create a new string object, JVM checks for the presence of the object in the String pool, If String is available in the pool, the same object reference is shared with the variable, else a new object is created.

Java-String-Pool-768
Java String Pool

Example:

String str1="Hello";
// "Hello" will be stored in String Pool
// str1 will be stored in stack memory

3. What is the difference between System.out, System.err, and System.in?

FeatureSystem.out (Output)System.err (Error)System.in (Input)
TypeStandard Output StreamStandard Error StreamStandard Input Stream
Default DeviceConsole (screen)Console (screen)Keyboard
PurposeDisplays normal messages/resultsDisplays error/debug messagesReads user input
Stream ClassPrintStreamPrintStreamInputStream
Byte/CharacterCharacter-based outputCharacter-based outputByte-based input (usually wrapped)
RedirectableYes (can redirect to file/other)Yes (can redirect separately)Yes (input can come from file/pipe)

Example Using All Three:

Java
import java.util.Scanner;

public class SystemStreamsExample {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Using System.out
        System.out.println("=== Welcome to the Program ===");

        // Using System.in
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        System.out.println("You entered: " + number);

        // Using System.err
        if (number < 0) {
            System.err.println("Error: Negative numbers are not allowed!");
        } else {
            System.out.println("Square of the number: " + (number * number));
        }
    }
}

4. What do you understand by an IO stream?

2-768
Java Application

Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all types of objects, data types, characters, files, etc to fully execute the I/O operations.

5. What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

FeatureReader/Writer HierarchyInputStream/OutputStream Hierarchy
Type of DataCharacter-oriented (text data)Byte-oriented (binary data)
Base ClassesReader (abstract) and Writer (abstract)InputStream (abstract) and OutputStream (abstract)
Data UnitWorks with 16-bit Unicode charactersWorks with 8-bit bytes
Use CaseReading/writing text files, strings, charactersReading/writing binary files, images, audio, etc.
Encoding HandlingSupports character encoding (Unicode, UTF-8, etc.)No encoding/decoding, raw bytes only

Example:

Java
// Java Program to demonstrate Reading Writing Binary Data
// with InputStream/OutputStream
import java.io.*;

class GFG {
    public static void main(String[] args) {
        try {
            // Writing binary data to a file using OutputStream
            byte[] data = {(byte) 0xe0, 0x4f, (byte) 0xd0, 0x20, (byte) 0xea};
            OutputStream os = new FileOutputStream("data.bin");
            os.write(data);
            os.close();

            // Reading binary data from a file using InputStream
            InputStream is = new FileInputStream("data.bin");
            byte[] newData = new byte[5];

            is.read(newData);
            is.close();

            // Printing the read data
            for (byte b : newData) {
                System.out.print(b+" ");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output
-32 79 -48 32 -22 

6. What are the super most classes for all the streams?

In Java’s I/O system, the topmost (super) classes for all streams are:

  • InputStream - Superclass of all byte-oriented input streams.
  • OutputStream - Superclass of all byte-oriented output streams.
  • Reader - Superclass of all character-oriented input streams.
  • Writer - Superclass of all character-oriented output streams

7. What are the FileInputStream and FileOutputStream?

To read and write data, Java offers I/O Streams. A Stream represents an input source or an output destination, which could be a file, an i/o device, another program, etc. FileInputStream in Java is used to read data from a file as a stream of bytes. It is mostly used for reading binary data such as images, audio files, or serialized objects. 

Example:

File file = new File("path_of_the_file");
FileInputStream inputStream = new FileInputStream(file);

In Java, the FileOutputStream function is used to write data byte by byte into a given file or file descriptor. Usually, raw byte data, such as pictures, is written into a file using FileOutputStream.

Example:

File file = new File("path_of_the_file");
FileOutputStream outputStream = new FileOutputStream(file);

8. What is the purpose of using BufferedInputStream and BufferedOutputStream classes?

When we are working with the files or stream then to increase the Input/Output performance of the program we need to use the BufferedInputStream and BufferedOutputStream classes. These both classes provide the capability of buffering which means that the data will be stored in a buffer before writing to a file or reading it from a stream. It also reduces the number of times our OS needs to interact with the network or the disk. Buffering allows programs to write a big amount of data instead of writing it in small chunks. This also reduces the overhead of accessing the network or the disk. 

BufferedInputStream(InputStream inp);
// used to create the bufferinput stream and save the arguments.

BufferedOutputStream(OutputStream output);
// used to create a new buffer with the default size.

9. What are FilterStreams?

Stream filter or Filter Streams returns a stream consisting of the elements of this stream that match the given predicate. While working filter() it doesn't actually perform filtering but instead creates a new stream that, when traversed, contains the elements of initial streams that match the given predicate.

Example:

FileInputStream fis =new FileInoutStream("file_path");
FilterInputStream = new BufferedInputStream(fis);

10. What is an I/O filter?

An I/O filter also defined as an Input Output filter is an object that reads from one stream and writes data to input and output sources. It used java.io package to use this filter.

11. How many ways you can take input from the console?

There are two methods to take input from the console in Java mentioned below:

  1. Using Command line argument
  2. Using Buffered Reader Class
  3. Using Console Class
  4. Using Scanner Class

The program demonstrating the use of each method is given below.

Example 1:

Java
import java.io.*;
import java.util.*;

public class ConsoleInputExample {
    public static void main(String[] args) throws Exception {
        // 1. Using Scanner
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter your name (Scanner): ");
        String name1 = sc.nextLine();
        
        // 2. Using BufferedReader
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your age (BufferedReader): ");
        int age = Integer.parseInt(br.readLine());
        
        // 3. Using Console (may return null in IDEs, works in terminal)
        Console console = System.console();
        String name2 = "N/A";
        if (console != null) {
            name2 = console.readLine("Enter your city (Console): ");
        }
        
        // 4. Using DataInputStream (deprecated style, just for example)
        DataInputStream dis = new DataInputStream(System.in);
        System.out.print("Enter your country (DataInputStream): ");
        String country = dis.readLine(); // readLine() is deprecated
        
        // Displaying collected input
        System.out.println("\n--- User Input Summary ---");
        System.out.println("Name (Scanner): " + name1);
        System.out.println("Age (BufferedReader): " + age);
        System.out.println("City (Console): " + name2);
        System.out.println("Country (DataInputStream): " + country);
    }
}
  • Scanner: Easy and widely used.
  • BufferedReader: Faster for reading large text input.
  • Console: Useful for secure input (like passwords).
  • DataInputStream: Outdated, but still possible.

12. Difference in the use of print, println, and printf.

MethodDescriptionLine BreakUse Case
print()Prints the text/data as it is.No newline (cursor stays on the same line).When you want to continue output on the same line.
println()Prints the text/data and then moves cursor to the next line.Yes, adds newline.When you want each output on a new line.
printf()Prints formatted text (like C’s printf). Supports format specifiers (%d, %s, %.2f, etc.).No automatic newline unless specified (%n).When you need formatted output (numbers, strings, decimals).

Example Code:

Java
public class PrintExample {
    public static void main(String[] args) {
        // Using print()
        System.out.print("Hello ");
        System.out.print("World");


        System.out.println(); // just adds newline

        // Using println()
        System.out.println("Hello");
        System.out.println("World");

        // Using printf()
        int age = 22;
        double marks = 85.6789;
        String name = "abcd";

        System.out.printf("Name: %s, Age: %d, Marks: %.2f%n", name, age, marks);

    }
}

13. What is the transient keyword?

The transient keyword is used at the time of serialization if we don’t want to save the value of a particular variable in a file. When JVM comes across a transient keyword, it ignores the original value of the variable and saves the default value of that variable data type


Explore