0% found this document useful (0 votes)
2 views19 pages

Core Java Basics of Java Interview Questions

The document provides a comprehensive overview of Java, including its definition, features, and differences from C++. It also explains the Java Virtual Machine (JVM), the distinctions between JDK, JRE, and JVM, and various aspects of Java programming such as memory management and class loading. Additionally, it addresses common interview questions related to Java syntax and behavior.

Uploaded by

amitvani2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views19 pages

Core Java Basics of Java Interview Questions

The document provides a comprehensive overview of Java, including its definition, features, and differences from C++. It also explains the Java Virtual Machine (JVM), the distinctions between JDK, JRE, and JVM, and various aspects of Java programming such as memory management and class loading. Additionally, it addresses common interview questions related to Java syntax and behavior.

Uploaded by

amitvani2024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Core Java: Basics of Java Interview Questions

1) What is Java?
Java is a high-level, o

bject-oriented, robust, secure programming language, platform-independent,


high-performance, Multithreaded, and portable programming language. It was
developed by James Gosling and his team at Sun Microsystems in June 1991.
It can also be known as the platform, as it provides its own JRE and API. Java
is designed to be versatile, secure, and portable, allowing developers to write
code that can run on various platforms without modification.

To read more: Java Tutorial

2) What are the differences between C++ and Java?


The differences between C++ and Java are given in the following table.

Aspect C++ Java

Platform-independent C++ is Java is


platform-dependent. platform-independent.

Used for C++ is mainly used for Java is mainly used for
system programming. application
programming. It is widely
used in Windows,
web-based, enterprise,
and mobile applications.
Design Goal C++ was designed for Java was designed and
systems and created as an interpreter
applications for printing systems, but
programming. It was an later extended as a
extension of the C support network for
programming language. computing. It was
designed with a goal of
being easy to use and
accessible to a broader
audience.

Goto Statement C++ supports the goto Java does not support
statement. the goto statement.

Multiple inheritance C++ supports multiple Java does not support


inheritance. multiple inheritance
through a class.
Interfaces in Java can
achieve it.

Operator Overloading C++ supports operator Java does not support


overloading. operator overloading.

Pointers C++ supports pointers. Java supports pointers


We can write a pointer internally. However, we
program in C++. cannot write the pointer
program in Java. It
means Java has
restricted pointer
support in Java.
Compiler and Interpreter C++ uses a compiler Java uses a compiler
only. C++ is compiled and an interpreter. Java
and run using the source code is converted
compiler, which converts into bytecode at
source code into compilation time. The
machine code, so C++ is interpreter executes this
platform-dependent. bytecode at runtime and
produces output. Java is
interpreted, which is why
it is platform
independent.

Call by Value and Call by C++ supports both call Java supports call by
Reference by value and call by value only. There is no
reference. call by reference in Java.

Structure and Union C++ supports structures Java does not support
and unions. structures and unions.

Thread Support C++ does not have Java has built-in thread
built-in support for support.
threads. It relies on
third-party libraries for
thread support.

Documentation C++ does not support Java supports


comment documentation documentation
comments. comments (/** ... */) to
create documentation
for Java source code.

virtual Keyword C++ supports the virtual Java has no virtual


keyword so that we can keyword. We can
decide whether or not to override all non-static
override a function. methods by default. In
other words, non-static
methods are virtual by
default.

Unsigned Right shift >>> C++ does not support Java supports the
the >>> operator. unsigned right shift >>>
operator that fills zero at
the top for the negative
numbers. For positive
numbers, it works the
same as the>> operator.

Inheritance Tree C++ creates a new Java uses a single


inheritance tree every inheritance tree always
time. because all classes are
the child of the Object
class in Java. The object
class is the root of the
inheritance tree in Java.

Hardware C++ is nearer to Java is not so interactive


hardware. with hardware.

Object-Oriented C++ is an object-oriented Java is also an


language. However, in object-oriented
the C language, a single language. However,
root hierarchy is not everything (except
possible. fundamental types) is an
object in Java. It is a
single root hierarchy as
everything gets derived
from java.lang.Object.

Memory Management Manual memory Automatic memory


management using new management through
and delete keywords. garbage collection.
Exception Handling Supports try, throw, and Exception handling using
catch for exception try, throw, and catch as
handling. well.

Standard Library Standard Template Java Standard Edition


Library (STL) for data (SE) library for various
structures and utilities and functionality.
algorithms.

App Development Commonly used for Widely used for web,


desktop applications. enterprise, and mobile
applications.

Runtime Environment Closer to the machine Java Virtual Machine


may have better (JVM) provides a layer of
performance. abstraction, sacrificing
some performance for
portability.

Garbage Collection Manual memory Automatic garbage


management, no built-in collection to manage
garbage collection. memory.

To read more: Difference between C++ and Java

3) List the features of Java Programming language.


○​ There are the following features in the Java Programming Language.
○​ Simple: Java is easy to learn. The syntax of Java is based on C++, which
makes it easier to write programs in it.
○​ Object-Oriented Programming (OOP): Java follows the object-oriented
paradigm, which allows us to maintain our code as a combination of different
types of objects that incorporate both data and behavior. Everything in Java is
treated as an object, promoting modularity and code reusability.
○​ Portable: Java supports a read-once-write-anywhere approach. We can
execute the Java program on every machine. A Java program (.java) is
converted to bytecode (.class), which can be easily run on every machine.
○​ Platform Independent: Java is a platform-independent programming
language. It is different from other programming languages like C and C++,
which need a platform to be executed. Java comes with its own platform on
which its code is executed. Java doesn't depend upon the operating system to
be executed.
○​ Secured: Java is secured because it does not use explicit pointers. Java also
provides the concept of ByteCode and Exception handling, which makes it
more secured. The Java Runtime Environment (JRE) includes a security
manager and a set of APIs to control access to resources and prevent
malicious activities.
○​ Robust: Java is a strong programming language as it uses strong memory
management. The concepts like Automatic garbage collection, Exception
handling, etc., make it more robust.
○​ Architecture Neutral: Java is architecture-neutral as it is not dependent on the
architecture. In C, the size of data types may vary according to the
architecture (32-bit or 64-bit), which does not exist in Java.
○​ Interpreted: Java uses the Just-in-time (JIT) interpreter along with the
compiler for the program execution.
○​ High Performance: Java is faster than other traditional interpreted
programming languages because Java bytecode is "close" to native code. It is
still a little bit slower than a compiled language (for example, C++). While
interpreted, Java's Just-In-Time (JIT) compilation translates bytecode into
machine code for improved performance.
○​ Multithreaded: We can write Java programs that deal with many tasks at once
by defining multiple threads. The main advantage of multi-threading is that it
does not occupy memory for each thread. It shares a common memory area.
Threads are important for multi-media, Web applications, etc. Java's bytecode
can be executed on any device with a JVM, ensuring portability across
different platforms.
○​ Distributed: Java is distributed because it facilitates users to create
distributed applications in Java. RMI and EJB are used for creating distributed
applications. This feature of Java makes us able to access files by calling the
methods from any machine on the internet.
○​ Dynamic: Java is a dynamic language. It supports dynamic loading of classes.
It means classes are loaded on demand. It also supports functions from its
native languages, i.e., C and C++.

To read more: Features of Java


4) What do you understand by Java Virtual Machine (JVM)?
Java Virtual Machine is a virtual machine that enables a computer to run Java
programs. It is a virtualized execution environment that allows Java programs
to run on any device with a compatible JVM. JVM is the specification that
must be implemented in the computer system. It interprets Java bytecode and
translates it into machine code for the underlying hardware.

To read more: Java Virtual Machine (JVM)


5) What are the differences between JDK, JRE, and JVM?
Feature JDK JRE JVM

Definition Java Java Runtime Java Virtual


Development Kit Environment Machine (JVM) is
(JDK) is a (JRE) is a an abstract
software software package machine that
development kit that provides provides an
used to develop Java Virtual environment for
Java Machine (JVM), the execution of
applications. class libraries Java ByteCodes.
and other
components to
run applications
in Java.
Functions Provides tools for It provides an The virtual
developing Java environment for machine that
applications. It executing Java runs the bytecode
includes compiler applications. It generated by the
(javac), JRE, and includes JVM and compiler. It's a
debugging tools. libraries required part of JRE and is
to run Java platform-depende
programs, but nt but provides a
does not include platform-indepen
development dent execution
tools. environment.

Use Cases Used by Used by Converts


developers to end-users to bytecode into
write, compile, execute Java machine code
and debug Java applications and executes it
programs. without needing on the underlying
development hardware.
tools.

Platform It is platform It is platform It is platform


Dependency dependent. dependent. dependent.

Implementation JDK = JRE + JRE = JVM + JVM = provides a


Development Class libraries runtime
tools environment.

To read more: Difference between JDK, JRE, and JVM

6) How many types of memory areas are allocated by the


JVM?
Class (Method) Area: Class Area stores per-class structures such as the
runtime constant pool, field, method data, and the code for methods.

Heap: It is the runtime data area in which memory is allocated to the objects
Stack: Java Stack stores frames. Holds local variables and partial results,
supporting method execution. Each thread has a private JVM stack, created at
the same time as the thread. A new frame is created each time a method is
invoked. A frame is destroyed when its method invocation completes.

PC Register (Non-Heap): Keeps track of the current execution position for


each thread.

Native Method Stack (Non-Heap): It contains native method information and


supports native code execution.

To read more: Memory Management in Java

7) What is JIT compiler?


Just-In-Time (JIT) Compiler: It is used to improve the performance. The
compiler is part of the Java Runtime Environment (JRE).

It translates Java bytecode into native machine code at runtime. Therefore,


reduces the amount of time needed for compilation.

Here, the term "compiler" refers to a translator from the instruction set of a
Java Virtual Machine (JVM) to the instruction set of a specific CPU.
To read more: JIT in Java

8) What is the platform?


A platform refers to the combination of hardware and software on which
programs run. In the context of Java, the platform includes the operating
system and the Java Virtual Machine (JVM).

There are two types of platforms:

1.​ Software-Based
2.​ Hardware-Based

9) What are the main differences between the Java


platform and other platforms?
There are the following differences between the Java platform and other
platforms.

1.​ Java is platform-independent due to its "Write Once, Run Anywhere"


nature.
2.​ Other platforms may require platform-specific code and compilation for
each target environment.
10) What gives Java its 'write once and run anywhere'
nature?
Java compiler converts the Java programs into a class file (bytecode), which
is the intermediate language between source code and machine code. Code is
compiled to bytecode, which is platform-independent, and the JVM executes
this bytecode on any device with a compatible JVM.

To read more: Java Compiler

11) What is a ClassLoader?


Classloader is a subsystem of the JVM that is used to load class files. It loads
Java classes into memory during runtime. Responsible for finding and loading
class files needed by a Java program.

There are three built-in classloaders in Java.

1.​ Bootstrap ClassLoader:This is the first classloader, which is the


superclass of Extension classloader. It loads the jar file, which contains
all class files of Java Standard Edition, like java.lang package classes,
java.net package classes, java.util package classes, java.io package
classes, java.sql package classes, etc.
2.​ Extension ClassLoader:This is the child classloader of Bootstrap and
the parent classloader of the System classloader. It loads the jar files
located inside $JAVA_HOME/jre/lib/ext
3.​ System/Application ClassLoader:This is the child classloader of the
Extension classloader. It loads the class files from the classpath. By
default, the classpath is set to the current directory. You can change the
classpath using the "-cp" or "-classpath" switch. It is also known as the
Application classloader.
To read more: ClassLoader in Java

12) Is an empty .java file name a valid source file name?


Yes, in Java, the source file can be saved with the .java extension, and then it
can be compiled using the javac command followed by the file name
(including the .java extension). Finally, the program can be executed using the
Java command followed by the name of the class that contains the main()
method.

Let's take a simple example:

​ //save by .java only


​ class Main {
​ public static void main(String args[]){
​ System.out.println("Hello Java");
​ }
​ }
Compile the code using the javac command:​
javac Main.java

Run the program using the java command:​


java Main

13) Is delete, next, main, exit, or null keyword in Java?


No, in Java, delete, next, main, exit, and null are not keywords.
14) If I do not provide any arguments on the command
line, then what will the value stored in the String array
passed into the main() method, empty or NULL?
It is empty, but not null. If we do not provide any command-line arguments
when running a Java program, the String array passed into the main() method
will be an empty array, not null. The args parameter of the main() method is an
array of strings, and it is always initialized by Java, even if no command-line
arguments are provided.

15) What if we write static public void instead of public


static void?
The program compiles and runs correctly because the order of specifiers does
not matter in Java.

In Java, the placement of access modifiers (public, private, protected) and


other specifiers (static, final, etc.) before the return type (void in this case) is
flexible. Both public static void and static public void are accepted and
considered correct syntax.

Here are the two equivalent valid declarations for the main() method:

​ public static void main(String[] args) {


​ // Code goes here
​ }
​ static public void main(String[] args) {
​ // Code goes here
​ }
To read more Java main() Method

16) What is the default value of the local variables?


In Java, local variables are not assigned default values by the compiler. They
must be explicitly initialized before they are used, or a compilation error will
occur.

​ public class Main {


​ public static void main(String[] args) {
​ int x;
​ System.out.println(x); // Compilation error: The local variable x may not
have been initialized
​ }
​ }
To read more: Java Variables

17) What are the various access specifiers in Java?


In Java, access specifiers are the keywords that are used to define the access
scope of a method, class, or variable. There are two types of modifiers in Java:
access modifiers and non-access modifiers.

Access Specifiers

public: public classes, methods, or variables that are defined as public can be
accessed by any class or method.

protected: The protected classes, methods, or variables can be accessed by


the class of the same package, or by a subclass of this class, or within the
same class.

private: The private class, methods, or variables defined as private can be


accessed within the class only.

default (package-private): If no access specifier is used, it is considered


package-private. Members with default access are accessible only within the
same package.

Non-Access Specifier

It includes static, synchronized, abstract, native, volatile, transient.


To read more: Java Access Specifiers

18) What is the purpose of static methods and variables?


The methods or variables defined as static are shared among all the objects
of the class. Static is the part of the class and not of the object. Static
methods and variables in Java are associated with the class rather than with
an instance of the class. Therefore, static is used in the case where we need
to define variables or methods that are common to all the objects of the class.

For example, in the class simulating the collection of the students in a college,
the name of the college is a common attribute to all the students. Therefore,
the college name will be defined as static.

Class-level Access: Static members belong to the class itself rather than to
instances of the class. It means they can be accessed using the class name
without creating an instance of the class.

Constants: Static variables are commonly used to define constants in a class.


These values are shared among all instances of the class and can be
accessed using the class name.

To read more: Static Method in Java

19) What are the advantages of packages in Java?


Packages in Java provide a way to organize and structure classes and
interfaces into a hierarchical structure. They offer several advantages:

○​ Packages avoid the name clashes.


○​ The Package provides easier access control.
○​ We can also have the hidden classes that are not visible outside and used by
the package.
○​ It is easier to locate the related classes.

To read more: Advantages of Packages in Java

20) What is the output of the following Java program?


Example
​ class Test
​ {
​ public static void main (String args[])
​ {
​ System.out.println(10 + 20 + "Tpointtech");
​ System.out.println("Tpointtech" + 10 + 20);
​ }
​ }
Compile and Run
Output:

30Tpointtech
Tpointtech1020

Explanation

In the first case, 10 and 20 are treated as numbers and added to be 30. Now,
their sum 30 is treated as a string and concatenated with the string
Tpointtech. Therefore, the output will be 30Tpointtech.

In the second case, the string Tpointtech is concatenated with 10 to be the


string Tpointtech10, which will then be concatenated with 20 to be
Tpointtech1020.

To read more: Java Programs

21) What is the output of the following Java program?


Example
​ class Test
​ {
​ public static void main (String args[])
​ {
​ System.out.println(10 * 20 + "Tpointtech");
​ System.out.println("Tpointtech" + 10 * 20);
​ }
​ }
Compile and Run
Output:

200Tpointtech
Tpointtech200

Explanation

In the first case, the numbers 10 and 20 will be multiplied first, and then the
result 200 is treated as a string and concatenated with the string Tpointtech to
produce the output 200Tpointtech.

In the second case, the numbers 10 and 20 will be multiplied first to be 200
because the precedence of multiplication is higher than addition. The result
200 will be treated as a string and concatenated with the string Tpointtech to
produce the output as Tpointtech200.

22) What is the output of the following Java program?


​ class Test
​ {
​ public static void main (String args[])
​ {
​ for(int i=0; 0; i++)
​ {
​ System.out.println("Hello Tpointtech");
​ }
​ }
​ }
The above code will result in a compilation error due to the non-boolean
expression in the second part of the for loop, and given is an integer value, i.e.,
0.

You might also like