20 Basic Core Java Interview Questions & Answers- TCS, Accenture, Cognizant, Infosys, Wipro, HCL
Summary
TLDRIn this informative video, Nishant from CareerRight delves into essential Java interview questions tailored for freshers. He covers Java's origins, its versatility across platforms, and its robust community support. The video explains Java's execution process, from source code compilation to bytecode interpretation by the JVM. Nishant also discusses access specifiers, the significance of the main method, and the nuances of static methods, constructors, and inheritance. He touches on method overloading, the final keyword's role in preventing overrides, and the organization of classes within packages. The video is a comprehensive guide for beginners looking to solidify their Java foundations.
Takeaways
- π Java is a high-level, general-purpose programming language created by James Gosling in 1995 and is now owned by Oracle.
- π Java supports multiple paradigms such as object-oriented, functional, and imperative, and it has a vast community support.
- π± Java is used for developing mobile apps, desktop applications, web applications, web servers, games, database connections, and more.
- π» A Java program is executed by being compiled into bytecode, which is then translated into machine code by the Java Virtual Machine (JVM).
- π Java is platform-independent because it compiles source code into platform-independent bytecode that can run on any platform with a JVM.
- π Java has four access specifiers: public, private, protected, and default, which define the accessibility of classes, interfaces, and variables.
- π οΈ A method in Java is a block of code that performs a specific task and can take input data in the form of parameters.
- π The main method in Java is the entry point of a Java program, and it must be declared with the 'public static void' signature.
- π String[] args is a parameter in the main method that stores command-line arguments supplied when executing a Java program.
- π A static method can be called without creating an object of the class, as it belongs to the class itself and not to any object.
- βοΈ Method overloading allows multiple methods with the same name but different parameters within the same class.
- π Constructors in Java are special methods used to initialize objects and must have the same name as the class with no return type.
- π Inheritance in Java allows a class (subclass) to acquire attributes and methods of another class (superclass), promoting code reusability.
- π The 'final' keyword in Java is used to prevent overriding and modifying variables or methods.
- π¦ Packages in Java are used to group related classes and avoid name conflicts, consisting of built-in packages and user-defined packages.
- π An abstract class in Java is a restricted class that cannot be instantiated and can have both regular and abstract methods.
Q & A
What is Java?
-Java is a high-level, general-purpose programming language created in 1995 by James Gosling and is now owned by Oracle. It is popular for being secure, fast, and powerful, supporting multiple paradigms such as object-oriented, functional, and imperative. Java is used to develop mobile applications, desktop applications, web applications, web servers, games, and database connections, and it runs on over 3 billion devices worldwide.
How is a Java program executed?
-A Java program is written in a Java file as source code, then compiled into bytecode by the Java compiler. This bytecode is not directly executable by the machine; it is translated into machine code by the Java Virtual Machine (JVM), which is platform-specific, allowing the program to run on any platform that has a JVM.
How is Java platform-independent?
-Java's platform independence is achieved through the use of the Java compiler, which converts source code into bytecode. This bytecode is platform-independent and can be executed on any platform using the appropriate JVM. For example, bytecode compiled on Windows can be translated and executed on Linux or macOS through their respective JVMs.
What are the various access specifiers in Java?
-In Java, access specifiers define the scope or accessibility of classes, interfaces, variables, etc. There are four access specifiers: public, private, protected, and default. Public allows access from anywhere, private is accessible only within the class, protected is accessible within the same package, subclass, and within the class, and default (no specifier) is accessible within the same package.
What is a method in Java?
-A method in Java is a block of code that performs a specific task and can also be called a function. Methods can take input data in the form of parameters and must be declared within a class. They are defined by a name followed by parentheses. Methods can be predefined or user-defined.
What is the significance of the main method in a Java program?
-The main method is the entry point of a Java program, where execution starts. It is one of the most important methods in Java. The Java compiler or JVM looks for the main method when executing a program.
What are string arguments in the Java main method?
-String arguments in the Java main method are used to handle command-line arguments supplied when executing a Java program. They are stored as an array of String objects and are necessary for the main function to be treated as the entry point of the program.
What is a static method in Java?
-A static method in Java is a method that can be accessed directly by specifying its name without creating an object of the class. It belongs to the class, not to the object, and does not require any object state to be accessed.
What is method overloading in Java?
-Method overloading in Java allows multiple methods to have the same name but different parameters, achieved by changing the number or data type of arguments. Overloading the main method is also possible, and the JVM will recognize the entry point by checking for the string argument parameters.
What is a constructor in Java?
-A constructor in Java is a special method used to initialize an object. It must have the same name as the class and cannot have a return type. The default constructor is called every time an object is created using the 'new' keyword.
How many types of constructors are there in Java?
-There are three types of constructors in Java: no-argument constructor, parameterized constructor, and default constructor. No-argument constructors have no parameters, parameterized constructors contain arguments, and default constructors are created by the compiler when no other constructor is defined.
What is the difference between a Java constructor and a Java method?
-A Java constructor is a special method used to initialize an object and must have the same name as the class, with no return type. It is invoked implicitly. In contrast, a method is a block of code that performs a task, can have a return type, and may or may not have the same name as the class. Methods are invoked explicitly.
What is inheritance in Java?
-Inheritance in Java is a process where a class acquires attributes and methods of another class, promoting code reusability. A subclass inherits from a superclass, gaining access to its attributes and methods.
How is inheritance implemented in a Java program?
-In Java, inheritance is implemented using the 'extends' keyword, allowing a class to inherit attributes and methods from another class.
What is a final variable in Java?
-A final variable in Java is a variable that cannot be changed once it has been assigned a value. It is used to prevent overriding and modification. If a method is declared as final, it cannot be overridden by any subclasses.
What is a package in Java?
-A package in Java is used to group related classes and is a collection of related classes. It helps avoid name conflicts and allows for better maintainable code.
What are the different types of packages in Java?
-Packages in Java are divided into built-in packages, which are pre-written and stored in the Java API, and user-defined packages, which are created by the user.
What is an abstract class in Java?
-An abstract class in Java is a restricted class from which objects cannot be created directly. It can have both regular methods and abstract methods without bodies. To access its members, it must be inherited by a subclass.
Outlines
π Java Basics and Execution
This paragraph introduces Java as a high-level, general-purpose programming language created by James Gosling in 1995 and owned by Oracle. It highlights Java's popularity, security, speed, and power, along with its support for multiple paradigms like object-oriented, functional, and imperative. Java is extensively used for developing mobile, desktop, web applications, web servers, games, and database connections, running on over 3 billion devices globally. The execution process of a Java program involves writing source code, compiling it into bytecode, and then translating this bytecode into machine code by the Java Virtual Machine (JVM), which is platform-specific. The paragraph also explains Java's platform independence, achieved through the JVM, allowing the same bytecode to run on any platform.
π Access Specifiers and Java Main Method
This section discusses the concept of access specifiers in Java, which determine the scope or accessibility of classes, interfaces, and variables within a program. There are four access specifiers: public, private, protected, and default. Public allows access from anywhere, private is accessible only within the class, protected is accessible within the same package, subclass, and the class itself, and default allows access within the same package. The paragraph also explains the structure and importance of the main method in a Java program, which serves as the entry point and must be declared with the 'public static void' signature. Additionally, it touches upon the use of the 'String[] args' parameter in the main method for handling command-line arguments.
π Understanding Java Methods and Constructors
This paragraph delves into the concept of methods in Java, which are blocks of code that perform specific tasks and can take input parameters. Methods must be declared within a class and can be predefined or user-defined. The main method is highlighted as the starting point of a Java program. The discussion then shifts to static methods, which can be called without creating an object of the class and belong to the class rather than an instance. Method overloading is introduced, allowing multiple methods with the same name but different parameters. The paragraph also covers constructors, which are special methods used to initialize objects, and their types: no-argument, parameterized, and default constructors created by the compiler when none are defined.
π Inheritance, Final Variables, Packages, and Abstract Classes
This section covers inheritance in Java, where a class acquires attributes and methods from another class, promoting code reusability. It explains the concepts of subclasses and superclasses, with subclasses inheriting properties from superclasses. The use of the 'extends' keyword to implement inheritance is discussed, along with the idea of final variables and methods, which prevent modification or overriding. The paragraph introduces packages in Java as a way to group related classes and avoid name conflicts, distinguishing between built-in packages provided by the Java API and user-defined packages. Lastly, it touches upon abstract classes, which cannot be instantiated and can contain both regular and abstract methods, requiring inheritance to a subclass to access their members.
Mindmap
Keywords
π‘Java
π‘Java Virtual Machine (JVM)
π‘Bytecode
π‘Access Specifiers
π‘Method
π‘Static Method
π‘Method Overloading
π‘Constructor
π‘Inheritance
π‘Final Keyword
Highlights
Java is a high-level, general-purpose programming language created in 1995 by James Gosling and is now owned by Oracle.
Java supports multiple paradigms such as object-oriented, functional, and imperative programming, and has huge community support.
Java programs are compiled into bytecode, which is then translated into machine code by the Java Virtual Machine (JVM) for execution.
The JVM is platform-specific, enabling Java's 'write once, run anywhere' capability by running bytecode on any platform.
There are four access specifiers in Java: public, private, protected, and default, which control the visibility and scope of classes, variables, and methods.
A method in Java is a block of code that performs a specific task, and can accept parameters for flexibility.
The Java main method serves as the entry point for a Java program's execution.
Static methods in Java can be called without creating an object of the class, whereas non-static methods require an instance of the class.
Method overloading allows multiple methods with the same name but different parameters to coexist in Java, enhancing code flexibility.
A constructor in Java is a special method used to initialize objects. It must have the same name as the class and doesn't return a value.
Java supports three types of constructors: no-argument, parameterized, and default constructors.
Inheritance in Java allows a class (subclass) to acquire attributes and methods from another class (superclass), promoting code reusability.
Final variables in Java cannot be modified once assigned, and final methods cannot be overridden by subclasses.
A package in Java is used to group related classes, helping in avoiding naming conflicts and enhancing maintainability.
Abstract classes in Java cannot be instantiated directly; they must be inherited by a subclass to access their members.
Transcripts
hello everyone it's nishant from career
right here and in today's video i'm
gonna talk about new topic
and the topic is basic code java
interview questions and answers for
freshers
so without wasting time let's come to
the concrete stuff now
alright so let's begin with the first
one what is java java is high level
general purpose programming language
created in 1995 by james gosling and is
now owned by oracle
java is one of the most popular language
in the world
and is secured fast and powerful
it supports multi paradise such as
object oriented functional imperative
etc
and has a huge community support behind
it
it is used to develop mobile application
desktop applications web applications
web servers games database connection
and many more
and runs on more than 3 billion devices
around the world
now coming to the next one how is java
program executed
a java program is written and saved in a
java file known as a source code
this source code is then passed to the
compiler
where it compiles into byte code
now this bytecode cannot be directly
executed by the machine
the bytecode is translated into machine
code first by the java virtual machine
that is jbm
and then executed by the machine
now what is this jbm
jbm is platform specific which means
every platform has its own java virtual
machine
okay so moving on to the next one how is
java platform independent
the java programming language is
platform independent which means the
code can be written once and run
anywhere
and this is achieved because of java
compiler that converts source code into
byte code
and this byte code is platform
independent and can be executed on any
platform through java virtual machine
for example if a program is written and
compiled on a windows platform
the program's byte code is first be
translated into machine code
and then can be executed on any platform
such as windows linux mac os through
their java virtual machine
now coming to the next one what are the
various access specifiers in java
excess specifiers define the scope or
accessibility of the classes
interfaces variables etc
it defines how they can be accessed from
other parts of the program
and there are four access specifiers in
java
they are public private protected and
default
now coming to public
now when defined as public the data
items and functions are accessible from
anywhere in the program
next is private
when defined as private
they are only accessible from the class
where they are defined
now next is protected
when defined as protected they are
accessible from the classes that belong
to the same package
the subclass of the class and from
within the class where they are defined
now next is default
and if you do not mention any specifier
then by default the default access
specifier is active which means they are
accessible from all the classes that
belong to the same package
okay so coming to the next one what is a
method in java
a method is a block of code that
performs a specific task
methods are also called functions
methods can also take input of the data
in the form of parameters
methods must be declared within a class
and they are defined by the name of the
method followed by a parenthesis
there are methods which are already
predefined such as
system.out.printing
and we can also define our own methods
now here is an example of our own method
now in this example you can see forward
slashes which are comments
which are ignored by the compiler but it
is very useful for the user for better
reliability of the code
okay so let's try to understand what is
java main method
in a java program the main method is a
method where the execution starts
or it is the entry point of a java
program
hence it is one of the most important
methods of java
the java compiler or java virtual
machine looks for the main method
when it starts executing a java program
now let's have an example of main method
in a java program
okay so the next one is what is string
arguments
that always appear on the java main
method
string array is used to declare a simple
string array
and argument
is the name of the string array
argument stores command line arguments
that are supplied when executing a java
program
as an area of string objects
and if we run our program from the
command line and supply arguments such
as
then argument will contain string 1 and
2.
hence it is necessary parameter for the
main function
and these parameters are absolutely
necessary in main method
in order for it to be treated as entry
point of the java program
alright so moving on to the next one
what is a static method
a method which is declared as static can
be accessed directly by specifying its
name it can be called without creating
an object of class
whereas a method declared as public
without static
can be accessed only by the creation of
an object of the class
static method belongs to the class and
not to the object
a static method doesn't require any
object state
so when you do not want to access
instance variable you can call static
methods without instantiating the object
now let's have an example of static
method
so in this example we have a class
called test
and the test class we have two methods
one is static method and other is normal
method
now come to the main method
in the main method you can see we have
called static method without creating
object of the class test
but in order to call a normal method
we have created an object of class test
and then called
a normal method
alright so moving on to the next one
what is method overloading
in method overloading multiple methods
can have same name with different
parameters
method overloading can be achieved in
two ways by changing the number of
arguments and by changing
the data type of arguments
now let's have an example over here
so in this example you can see we have
two methods with same name
but different type of arguments
now the next question is
can we overload the main method
yes we can overload the main method
we can have any number of main method
now the question arises how does the
compiler know which is the entry point
into the program if there are multiple
main methods
the compiler will check for the
parameters string argument in the main
method and will recognize it as the
entry point into the program
jbm always calls the original main
method it doesn't call the overloaded
main method
alright so coming to the next one what
is a constructor in java
a constructor in java is a special
method that is used to initialize an
object
a constructor must have the same name as
that of the class
and doesn't have a return type
and every time an object is created
using the new keyword the default
constructor is called
now let's have an example of a
constructor
so in this example you can see the
moment we create an object
it will automatically call the
constructor
now coming to the next one how many
types of constructors are used in java
now based on arguments accepted by the
constructor there are three types of
constructors
and the first one is no argument
constructor
a no argument constructor is a
constructor that doesn't have any
arguments or parameters the values are
defined within the constructor itself
next is parameterized constructor
a parameterized constructor is a
constructor that contains arguments or
parameters
next one is default constructor
a default constructor is a constructor
that is created by the compiler
when we do not define any constructor in
the program
a default constructor must not be
confused with a no argument constructor
they are not the same in java
okay so the next question is explain the
difference between java constructor and
java method
a java constructor is a special method
used to initialize an object
whereas a method is a block of code that
performs a certain task
a constructor must not have a return
type whereas a method can have a return
type
the constructor name must be same as
that of class
whereas the method name may or may not
be same as that of class
the constructor is invoked implicitly
whereas a method is invoked explicitly
now coming to the next one what is
inheritance in java
inheritance is a process where a class
acquires attributes and methods of
another class
when we inherit the attributes and
methods of an existing class we can
access all the attributes and methods of
that class in the program
and this promotes code reasonability
now the next question is what is
subclass and superclass
subclass is a class that inherits from
another class
and is also known as a child
it is derived from super class and also
inherits the properties of super class
whereas superclass is a class that is
being inherited from
and it is also known as a paret
it is a class from which many subclasses
can be created
the subclasses also inherit the
properties of a super class
let's have an example of these classes
in this example bus car truck
are all subclasses of the superclass
vehicle
now the next question is how is
inheritance implemented in a java
program
in java to inherit from a class we use
the extend keyword
let's have an example of inheritance
now in this example the class main can
use all attributes and methods of the
arithmetic class as if they were their
own
okay so coming to the next one what is a
final variable
a variable declared with the final
keyword is a final variable and is used
to prevent from overriding and modifying
so we can say a final variable once
assigned a value can never be changed
so if we initialize a variable with the
final keyword then we cannot modify its
value
and if we declare a method as final
then it cannot be overridden by any
subclasses
now the next one is what is a package in
java
a package in java is used to group
related classes or it is a collection of
related classes
packages are used to avoid name
conflicts between classes
and it allows to write a better
maintainable code
the next one is what are the different
types of packages
packages are divided into two categories
built-in packages and user-defined
packages
now built-in packages are pre-written
and are stored in the java api that are
free to use
they consist of packages for managing
input output data programming and many
more
and here are some of the commonly used
built-in packages
next is user defined packages and these
packages are created by the user
now coming to the last question in this
series what are abstract class an
abstract class is a restricted class
where we cannot create objects of the
class
to access the members of the abstract
class
it must be inherited to a subclass where
we can then access the members of the
abstract class through the object of the
subclass
an abstract class can have both regular
methods and abstract methods without its
body
you
5.0 / 5 (0 votes)