Skip to content

50 Java Interview Questions asked in SRM Placements

    1. Table of Contents

      What is Java?

    Java is a popular object-oriented programming language. It is defined as a complete collection of objects. By using Java, we can develop lots of applications such as gaming, mobile apps, and websites.

    1. Explain in brief the history of Java?

    In the year 1991, a small group of engineers called 'Green Team' led by James Gosling, worked a lot and introduced a new programming language called "Java". This language is created in such a way that it is going to revolutionize the world.

    In today's World, Java is not only invading the internet, but also it is an invisible force behind many of the operations, devices, and applications.

    1. What is the difference between C and Java?

    C- Language

    Java

    C language was developed by Dennis M. Ritchie in the year 1972.

    Java was developed by James Gosling in the year 1995.

    C is Procedural-Oriented

    Java is Object-Oriented

    Functions play a major role in the C language.

    Objects play a major role in the Java language.

    It is a middle-level language

    It is a high-level language.

    C language does not support OOPS Concepts.

    Java supports OOPS concepts, in which Inheritance the main property used for code reusability.

    In C, memory is allocated using Malloc 

    In Java, memory is allocated using a new keyword.

    The threading concept is not supported by C

    Java supports Threading 

    It is not portable

    It is portable 

    Default members of C are public

    Default members of Java are private

    Storage classes are supported by C

    Storage classes are not supported in Java

    The differences between C and Java are as follows:

     

    1. What is the reason why Java came into existence?

    Java is the first programming language that is used to write code on a virtual machine, so that is the reason why it is called JVM (Java Virtual Machine). This JVM is a new concept that is introduced in Java. It also provides a new feature called code reusability which is not possible in C.

    Java

    C++

    Java is platform-independent

    C++ is platform dependent

    Goto statement is not supported by Java

    C++ supports the goto statement

    Multiple inheritances are supported in java by using interfaces

    Multiple inheritances are supported in C++

    Java doesn't support structures and unions

    C++ does support unions and structures

    Java has built-in support for threading

    C++ does not provide built-in support for threading

    Java is used for building applications

    C++ is used for system programming

    Java uses both interpreter and compiler

    C++ uses compiler only

    1. Compare both C++ and Java?

    2. What are the prominent features of Java?

    The following are the notable features in Java:

    • Dynamic: Java is more dynamic when compared to C++ and C. Java is designed in such a way that it is adaptable to any the evolving environments.
    • Simple: Java is very easy to learn and code.
    • Object-oriented: In Java, everything is based on objects.
    • Secure: Java provides a secure platform to develop safe and virus-free applications.
    • Platform Independent: Unlike C and C++, when we compile Java code it is compiled into platform-independent bytecode rather than the platform-specific machine code.
    • Portable: Java provides no implementation aspects for the specifications which make Java portable.
    • Multithreaded: By this feature, we can perform multiple tasks simultaneously in Java.
    • High-Performance: With the built-in Just-in-Time compiler, Java provides high performance.
    • Robust: Java makes more efforts to eliminate errors by concentrating more on runtime checking and compile-time check.
    1. What is a Class in Java?

    Class is defined as a template or a blueprint that is used to create objects and also to define objects and methods.

    1. Define Object in Java?

    The instance of a class is called an object. Every object in Java has both state and behavior. The state of the object is stored in fields and the behavior of the objects is defined by methods.

    1. How to write a basic Hello World program in Java?

    class Mindmajix

    {

    public static void main(String args[ ]) {

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

    }

    }

    1. Define JVM?

    JVM is the abbreviation for Java Virtual Machine. It is a virtual machine that provides a runtime environment to write code. JVM is a part of JRE (Java Runtime Environment) and is used to convert the bytecode into machine-level language. This machine is responsible for allocating memory.

    1. Name the memory areas that are allocated by JVM and explain the class loader in JVM?

    There are totally five memory areas that are allocated by the JVM, and they are:

    • Class Area
    • Heap
    • Stack
    • Native Method Stack
    • PC Register

    ClassLoader: class loader is a subschema of JVM which is used to load class files. Whenever we run Java programs, the data will be first loaded from the classloader. There are mainly three in-built classloaders in JVM, they are:

    • Application Classloader
    • Bootstrap Classloader
    • Extension Classloader
    1. What is JDK?

    Java Development Kit is one of the three prominent technology packages used in Java programming. JDK is used as a standalone component to run the Java programs by JVM and JRE. This kit is used to implement Java platform specifications, including class libraries and compiler.

    1. What do you mean by JRE?

    Java Runtime Environment (JRE) is a collection of software tools that are designed for the development of Java applications. This is a part of JDK, but it can be downloaded separately. JRE is mainly responsible for orchestrating the component activities. 

    Become a member and get full access of SRM PLACEMENT Assests.

    1. What is the significant difference between  JVM, JRE, and JDK?

    We can understand the difference between JVM, JDK, and JRE by the following diagram:

    • JVM: Java Virtual Machine is the main part of Java programming, which provides platform independence. JRE and JDK both contain JVM in order to run our Java programs.
    • JDK: This development kit is mainly used for developing programs. 
    • JRE: Java Runtime Environment is mainly used for running Java programs.
    1. Define the JIT compiler in Java?

    Just In Time Compiler is the component of JRE, which is used to compile the bytecodes of the particular method into the native machine code. This compiled code of the method is directly called by JVM without interpreting it

    1. Define variables in Java and explain with example?

    Variables in Java can be defined as a basic storage unit of a program. It is a storage unit that holds the value during the program execution. Always the variable is assigned with a datatype.

    For Example:

    int a = 10;

    1. What are the different types of variables in Java?

    There are mainly three different types of variables available in Java, and they are:

    • Static Variables
    • Local Variables
    • Instance Variables

    Static Variables: A variable that is declared with the static keyword is called a static variable. A static variable cannot be a local variable, and the memory is allocated only once for these variables. 

    Local Variables: A variable that is declared inside the body of the method within the class is called a local variable. A local variable cannot be declared using the static keyword.

    Instance Variables: The variable declared inside the class but outside the body of the method is called the instance variable. This variable cannot be declared as static and its value is instance-specific and cannot be shared among others.

    Example:

    class A{ 

    int num=30;//instance variable 

    static char name=pranaya;//static variable 

    void method(){ 

    int n=90;//local variable 

    }//end of class

    1. What is Typecasting?

    Typecasting in Java is done explicitly by the programmer; this is done to convert one data type into another data type. 

    Widening (automatically) – conversion of a smaller data type to a larger data type size.   

    byte -> short -> char -> int -> long -> float -> double

    Narrowing (manually) – converting a larger type to a smaller size type

    double -> float -> long -> int -> char -> short -> byte

    1. What is Type Conversion?

    Type conversion can be defined as converting one data type to another data type automatically by the compiler.

    There are two types of type conversions, and they are:

    • Implicit type conversion
    • Explicit type conversion.

    [ Related Article:- Clojure Tutorial]

    1. What are the data types in Java?

    Datatypes in Java specify the values and sizes that can be stored in the variables. There are mainly two types of data types; they are:

    • Primitive Data Types
    • Non-primitive Data Types
    1. What are primitive data types?

    Primitive data types in Java are the major constituents of data manipulation. These are the most basic data types that are available in Java. The primitive data types include int, char, byte, float, double, long, short, and boolean.

    1. What are the default values and sizes of primitive data types?

    Data Type

    Default Size

    Default Value

    int

    4 bytes

    0

    char

    2 bytes

    'u0000'

    byte

    1 byte

    0

    byte

    2 bytes

    0

    long

    8 bytes

    0L

    float

    4 bytes

    0.0f

    double

    8 bytes

    0.0d

    boolean

    1 bit

    false

    1. What are non-primitive data types?

    The non-primitive data types are something that is different from primitive data types, and these non-primitive data types include String, arrays, and structures.

    1. Why char uses 2 bytes in Java and what is this 'u0000' notation called?

    This is only due to the reason that Java uses the Unicode system. The notation 'u0000' is the lowest range of the Unicode system, and it is the default value of the char data type.

    1. Write the syntax for object declaration in Java?

    The new keyword is used to create an object.

    For example:

    Mindmajix m1= new Mindmajix();// create an object for Mindmajix.

    1. What is the Unicode system?

    Unicode is a Universal International Standard Character Encoding which is an adequate resource for representing most of the languages written worldwide. 

    1. Why Java uses Unicode System?

    To overcome the problems present in the previous language standards, the Unicode system has been introduced. Java uses the Unicode system because the character default size provided by Unicode is 2 bytes and Java also needs only 2 bytes for the character.

    1. What makes Java 'Run Anywhere' in nature?

    It is because the Java compiler converts the code into byte code which is the main transitional language between machine code and source code. This byte code is not platform-dependent so that it can be compiled and executed on any platform.

    1. Is exit, null, delete, main, and next are the keywords in Java?

    No, they are no such keywords in Java.

    1. What if we write public static void as static public void?

    Both the compilation and execution of the programs are done correctly because in Java specifiers order doesn't matter.

    1. Is there any default value for local variables?

    No, local variables are not initialized by any default values.

    1. How many types of operators are available in Java?

    Eight types of operators are available in java, and they are:

    1. Arithmetic operators
    2. Assignment operators
    3. Logical operators
    4. Relational operators
    5. Bitwise operators
    6. Unary operators
    7. Ternary operators
    8. Shift operators

    Become a member and get full access of SRM PLACEMENT Assests.

    1. What is operator precedence in Java?

    Java provides a set of rules and regulations for particularly specifying the order in which operators are evaluated. If the expression has many numbers of operators then the operator precedence comes into action. This operator precedence evaluates the operators present in the expressions based on the priority. For example, multiplication has the highest priority when compared to addition and subtraction.

     

     

    1. What are the different logical operators in Java?

    Operator

    Description

    Logical NOT

    This operator can be used with logical expressions, boolean type variables, and relational variables, The Logical NOT operator is denoted by the symbol "!"

    Logical OR

    These logical operators operate only on boolean variable types and the symbol for this operator is "||"

    Logical AND

    We can combine many relational operations using this operator and the output will be of boolean type. The "&&" is the symbol for logical AND

    1. What is the role of the unary operator in Java?

    This type of operator has only one operand and is mainly used to perform various operations including negating an expression, either incrementing/ decrementing the value by one, and invention on boolean values.

    An example for the unary operator is given below:

    class UnaryExample{ 

    public static void main(String args[]){ 

    int x=15; 

    System.out.println(x++);//15 (16) 

    System.out.println(++x);//17 

    System.out.println(x–);//12 (16) 

    System.out.println(–x);//15 

    }} 

    1. What is the difference between ++a and a++ increment operators?

    ++a is a prefix increment and a++ is the postfix increment. The prefix increment is used to return the value after incrementing the present value. Whereas in postfix increment, the value is returned before incrementing it.

    1. Define left shift and right shift operators in Java?

    Left Shift: This left shift is a bitwise operator in which bits are moved towards the left side and zeros are placed at the rightmost places.

    Example: 

    public class LeftShiftOperator {

            public static void main(String[] args) {

                                   int a=2;//

                                   int i;

                                   i=a<<1;//4

                                   System.out.println("the value of a before left shift is: " +a);

                                   System.out.println("the value of a after applying left shift is: " +i);

            }

    }

    Output: 4

    Right Shift: It is also of the bitwise operator in which bits are moved towards the right-hand side and zeros are places at the leftmost places.

    Example:

    public class RightShiftOperator {

            public static void main(String[] args) {

                   int a=2;

                   int i;

                   i=a>>1;

                    System.out.println("the value of a before right shfit is: " +a);

                   System.out.println("the value of a after applying right shfit is: " +i);

       }

    }

    Output: 1

    1. What are the bitwise operators in Java?

    Bitwise operators are mainly used to work on bits and these operators continue to work on bit-by-bit operations.

    The following are the bitwise operators in Java, and they are:

    • Bitwise OR (A&B)
    • Bitwise AND (A|B)
    • Bitwise XOR (A^B)
    • Bitwise Complement (~A)
    • Left shift ( A<<2)
    • Right shift (A>>2)
    • Unsigned left shift ( <<<)
    • Unsigned right shift (>>>)
    1. What is a ternary operator?

    Ternary operator in Java is used to replace the if-else statement. The representation or the syntax for the ternary operator is given as:

    variable= (expression) ? expression true : expression false

    1. Is the Empty .java file is a valid source file name?

    Yes, Java allows us to save our Java file by .java only, we need to compile it by javac.java and run it by Java class name.

    For example:

    //save by .java only 

    class A{ 

    public static void main(String args[]){ 

    System.out.println("Hello Mindmajix"); 

    }

    1. What are Java keywords?

    Java keywords are also called "Reserved keywords" that act as a key to a code. Keywords in Java are predefined that cannot be used as an object name or variable. There are many keywords in Java, and some of them are:

    • abstract
    • default
    • new
    • This
    • static
    • Extends and many more
    1. What are various access specifiers present in Java?

    There are four access specifiers present in Java, and they are:

    1. Public: The methods, classes, and variables that are defined as the public can be accessed by any class or method.
    2. Private: The methods or classes which are declared as private can be accessible within the same class only.
    3. Protected: The variables, methods, and classes which are defined as private can be accessed within the same class of the same package or by the subclass of the same class.
    4. Default: By default, all the classes, variables, and methods are of default scope. The default is accessible within the package only.
    1. What are the advantages of packages in Java?

    The following are the advantages of packages in Java, and they are:

    • Name clashes are avoided using packages
    • Packages enable easier access control 
    • Packages provide an effective and easier way to locate the related classes.
    1. What is the output of the following program?

    The program is as follows:

    class Java

        public static void main (String args[])  

        { 

            System.out.println(10 * 50 + "Mindmajix");  

            System.out.println("Mindmajix" + 10 * 50); 

        } 

    Output:

    500Mindmajix

    Mindmajix500

    1. List out the control statements in Java?

    In Java control statements are divided into three types. They are:

    • Selection Statements
    • Iterative/looping Statements
    • Jump Statements.
    1. What are the selection statements in Java?

    A selection statement is mainly used to transfer program control to a specific flow based upon the condition either true or false. These selection statements are also called conditional statements.

    Selection/Conditional statements in Java include:

    • If statement
    • If-else statement
    • Switch statements
    1. What are the different types of iterative statements?

    The iterative statements in Java are also called looping statements, these statements are the set of statements that repeat continuously until the condition for the termination is not met.

    Looping/iterative statements in Java include:

    • For loop
    • While loop
    • Do-while loop
    1. What are the jump statements in Java?

    In Java, jump statements are mainly used to transfer control to another part of our program depending on the condition. Moreover, these statements are used to jump directly to other statements.

    • Break and Continue are the two jump statements present in Java.
    1. Define for each loop in Java?

    For-each is another kind of array traversing technique in Java which is the same as that of for loop, while loop. It is most commonly used to iterate over a collection or an array such as ArrayList. An example for for-each loop is as follows:

    class ForEachPro{ 

    public static void main(String args[]){ 

       //declaring an array 

    int arr[]={12,13,14,44}; 

       //traversing the array with for-each loop 

     for(int i:arr){ 

     System.out.println(i); 

       } 

     }  

    }

    Output:

    12

    12

    14

    44

    1. What is the difference between a while loop and a do-while loop

    In the case of a while loop the condition is tested first and then if the condition is true then the loop continues if not it stops the execution. Whereas in the case of the do-while loop first the condition is executed and at the end of the loop, the condition is tested.

    The syntax for the while loop is as follows:

    while(condition){ 

    //code to be executed 

    Become a member and get full access of SRM PLACEMENT Assests. 

    Leave a Reply