20 Basic Core Java Interview Questions & Answers- TCS, Accenture, Cognizant, Infosys, Wipro, HCL

CareerRide
18 Jul 202215:21

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

00:00

📘 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.

05:02

🔑 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.

10:04

🔍 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.

15:05

🚀 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 is a high-level, general-purpose programming language developed in 1995 by James Gosling and is now owned by Oracle. It is designed for versatility, running on over 3 billion devices globally. Java is central to the video as it forms the basis of the discussion on programming concepts, including its applications in mobile, web, and desktop development.

💡Java Virtual Machine (JVM)

The Java Virtual Machine (JVM) is a critical component that enables Java's platform independence. It translates bytecode (the compiled Java source code) into machine code that can run on any operating system. The JVM ensures that Java code can 'write once, run anywhere,' which is a key theme in the video when explaining Java's cross-platform capabilities.

💡Bytecode

Bytecode is the intermediate code generated after compiling Java source code. It is platform-independent and can be executed on any machine with the appropriate JVM. In the video, bytecode's role is explained as an essential step that allows Java programs to run on different operating systems without modification.

💡Access Specifiers

Access specifiers define the visibility and accessibility of classes, variables, and methods in Java. The video outlines four types—public, private, protected, and default—each determining how parts of the code can be accessed within a program or across different packages. Understanding access specifiers is key for managing data encapsulation and security in Java.

💡Method

A method in Java is a block of code that performs a specific task, similar to a function in other programming languages. The video explains that methods can take parameters, return values, and must be defined within a class. Java’s 'main' method is particularly important, as it serves as the entry point for program execution.

💡Static Method

A static method is a method that belongs to the class, rather than any particular object of the class. It can be called directly without creating an instance of the class. In the video, this is highlighted as a way to avoid creating unnecessary objects when a method does not rely on instance variables.

💡Method Overloading

Method overloading allows multiple methods to have the same name but differ in the number or type of parameters. In the video, it is explained as a feature that improves code readability and reusability, showing an example where two methods share the same name but have different parameters.

💡Constructor

A constructor is a special type of method used to initialize objects. It has the same name as the class and does not return any value. The video discusses different types of constructors (no-argument, parameterized, and default), emphasizing their role in object creation and how they differ from regular methods.

💡Inheritance

Inheritance is a fundamental object-oriented programming concept where a subclass inherits properties and methods from a superclass. The video explains how inheritance promotes code reusability by allowing subclasses to extend the functionality of existing classes, such as in the example of a 'vehicle' superclass being extended by 'bus' and 'car' subclasses.

💡Final Keyword

The final keyword in Java is used to restrict the modification of variables, methods, and classes. A final variable cannot be changed once assigned, a final method cannot be overridden, and a final class cannot be inherited. This concept is introduced in the video to explain how Java ensures immutability and prevents unwanted changes to code.

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

play00:00

hello everyone it's nishant from career

play00:02

right here and in today's video i'm

play00:04

gonna talk about new topic

play00:06

and the topic is basic code java

play00:08

interview questions and answers for

play00:10

freshers

play00:11

so without wasting time let's come to

play00:13

the concrete stuff now

play00:17

alright so let's begin with the first

play00:18

one what is java java is high level

play00:21

general purpose programming language

play00:23

created in 1995 by james gosling and is

play00:27

now owned by oracle

play00:29

java is one of the most popular language

play00:32

in the world

play00:33

and is secured fast and powerful

play00:36

it supports multi paradise such as

play00:38

object oriented functional imperative

play00:41

etc

play00:42

and has a huge community support behind

play00:44

it

play00:46

it is used to develop mobile application

play00:48

desktop applications web applications

play00:51

web servers games database connection

play00:53

and many more

play00:55

and runs on more than 3 billion devices

play00:58

around the world

play01:02

now coming to the next one how is java

play01:04

program executed

play01:06

a java program is written and saved in a

play01:09

java file known as a source code

play01:12

this source code is then passed to the

play01:14

compiler

play01:16

where it compiles into byte code

play01:18

now this bytecode cannot be directly

play01:20

executed by the machine

play01:22

the bytecode is translated into machine

play01:24

code first by the java virtual machine

play01:28

that is jbm

play01:29

and then executed by the machine

play01:32

now what is this jbm

play01:35

jbm is platform specific which means

play01:38

every platform has its own java virtual

play01:40

machine

play01:45

okay so moving on to the next one how is

play01:47

java platform independent

play01:50

the java programming language is

play01:51

platform independent which means the

play01:54

code can be written once and run

play01:56

anywhere

play01:58

and this is achieved because of java

play01:59

compiler that converts source code into

play02:02

byte code

play02:04

and this byte code is platform

play02:06

independent and can be executed on any

play02:09

platform through java virtual machine

play02:13

for example if a program is written and

play02:15

compiled on a windows platform

play02:18

the program's byte code is first be

play02:20

translated into machine code

play02:23

and then can be executed on any platform

play02:26

such as windows linux mac os through

play02:29

their java virtual machine

play02:35

now coming to the next one what are the

play02:37

various access specifiers in java

play02:40

excess specifiers define the scope or

play02:43

accessibility of the classes

play02:45

interfaces variables etc

play02:48

it defines how they can be accessed from

play02:50

other parts of the program

play02:52

and there are four access specifiers in

play02:54

java

play02:55

they are public private protected and

play02:58

default

play02:59

now coming to public

play03:01

now when defined as public the data

play03:04

items and functions are accessible from

play03:06

anywhere in the program

play03:09

next is private

play03:11

when defined as private

play03:13

they are only accessible from the class

play03:15

where they are defined

play03:18

now next is protected

play03:20

when defined as protected they are

play03:22

accessible from the classes that belong

play03:24

to the same package

play03:26

the subclass of the class and from

play03:28

within the class where they are defined

play03:32

now next is default

play03:34

and if you do not mention any specifier

play03:37

then by default the default access

play03:39

specifier is active which means they are

play03:42

accessible from all the classes that

play03:44

belong to the same package

play03:50

okay so coming to the next one what is a

play03:52

method in java

play03:54

a method is a block of code that

play03:56

performs a specific task

play03:59

methods are also called functions

play04:02

methods can also take input of the data

play04:04

in the form of parameters

play04:07

methods must be declared within a class

play04:10

and they are defined by the name of the

play04:12

method followed by a parenthesis

play04:16

there are methods which are already

play04:18

predefined such as

play04:20

system.out.printing

play04:22

and we can also define our own methods

play04:26

now here is an example of our own method

play04:29

now in this example you can see forward

play04:31

slashes which are comments

play04:34

which are ignored by the compiler but it

play04:36

is very useful for the user for better

play04:38

reliability of the code

play04:44

okay so let's try to understand what is

play04:46

java main method

play04:48

in a java program the main method is a

play04:50

method where the execution starts

play04:53

or it is the entry point of a java

play04:55

program

play04:57

hence it is one of the most important

play04:59

methods of java

play05:02

the java compiler or java virtual

play05:04

machine looks for the main method

play05:07

when it starts executing a java program

play05:11

now let's have an example of main method

play05:13

in a java program

play05:19

okay so the next one is what is string

play05:21

arguments

play05:22

that always appear on the java main

play05:24

method

play05:26

string array is used to declare a simple

play05:29

string array

play05:30

and argument

play05:32

is the name of the string array

play05:35

argument stores command line arguments

play05:37

that are supplied when executing a java

play05:39

program

play05:40

as an area of string objects

play05:44

and if we run our program from the

play05:45

command line and supply arguments such

play05:48

as

play05:49

then argument will contain string 1 and

play05:52

2.

play05:53

hence it is necessary parameter for the

play05:55

main function

play05:57

and these parameters are absolutely

play05:59

necessary in main method

play06:02

in order for it to be treated as entry

play06:04

point of the java program

play06:09

alright so moving on to the next one

play06:11

what is a static method

play06:14

a method which is declared as static can

play06:16

be accessed directly by specifying its

play06:18

name it can be called without creating

play06:22

an object of class

play06:24

whereas a method declared as public

play06:27

without static

play06:28

can be accessed only by the creation of

play06:31

an object of the class

play06:35

static method belongs to the class and

play06:37

not to the object

play06:39

a static method doesn't require any

play06:41

object state

play06:44

so when you do not want to access

play06:46

instance variable you can call static

play06:49

methods without instantiating the object

play06:53

now let's have an example of static

play06:55

method

play06:56

so in this example we have a class

play06:58

called test

play07:00

and the test class we have two methods

play07:02

one is static method and other is normal

play07:05

method

play07:08

now come to the main method

play07:10

in the main method you can see we have

play07:13

called static method without creating

play07:16

object of the class test

play07:19

but in order to call a normal method

play07:22

we have created an object of class test

play07:25

and then called

play07:27

a normal method

play07:32

alright so moving on to the next one

play07:34

what is method overloading

play07:36

in method overloading multiple methods

play07:38

can have same name with different

play07:40

parameters

play07:42

method overloading can be achieved in

play07:44

two ways by changing the number of

play07:47

arguments and by changing

play07:49

the data type of arguments

play07:53

now let's have an example over here

play07:56

so in this example you can see we have

play07:58

two methods with same name

play08:01

but different type of arguments

play08:08

now the next question is

play08:10

can we overload the main method

play08:13

yes we can overload the main method

play08:16

we can have any number of main method

play08:19

now the question arises how does the

play08:21

compiler know which is the entry point

play08:23

into the program if there are multiple

play08:25

main methods

play08:28

the compiler will check for the

play08:29

parameters string argument in the main

play08:32

method and will recognize it as the

play08:34

entry point into the program

play08:38

jbm always calls the original main

play08:40

method it doesn't call the overloaded

play08:43

main method

play08:48

alright so coming to the next one what

play08:50

is a constructor in java

play08:53

a constructor in java is a special

play08:55

method that is used to initialize an

play08:57

object

play08:59

a constructor must have the same name as

play09:01

that of the class

play09:03

and doesn't have a return type

play09:06

and every time an object is created

play09:09

using the new keyword the default

play09:12

constructor is called

play09:14

now let's have an example of a

play09:16

constructor

play09:18

so in this example you can see the

play09:20

moment we create an object

play09:22

it will automatically call the

play09:24

constructor

play09:29

now coming to the next one how many

play09:31

types of constructors are used in java

play09:34

now based on arguments accepted by the

play09:36

constructor there are three types of

play09:38

constructors

play09:40

and the first one is no argument

play09:41

constructor

play09:43

a no argument constructor is a

play09:45

constructor that doesn't have any

play09:47

arguments or parameters the values are

play09:51

defined within the constructor itself

play09:54

next is parameterized constructor

play09:57

a parameterized constructor is a

play09:59

constructor that contains arguments or

play10:01

parameters

play10:03

next one is default constructor

play10:07

a default constructor is a constructor

play10:09

that is created by the compiler

play10:12

when we do not define any constructor in

play10:14

the program

play10:16

a default constructor must not be

play10:18

confused with a no argument constructor

play10:21

they are not the same in java

play10:26

okay so the next question is explain the

play10:28

difference between java constructor and

play10:30

java method

play10:33

a java constructor is a special method

play10:35

used to initialize an object

play10:38

whereas a method is a block of code that

play10:40

performs a certain task

play10:44

a constructor must not have a return

play10:46

type whereas a method can have a return

play10:49

type

play10:50

the constructor name must be same as

play10:52

that of class

play10:54

whereas the method name may or may not

play10:56

be same as that of class

play10:59

the constructor is invoked implicitly

play11:02

whereas a method is invoked explicitly

play11:09

now coming to the next one what is

play11:11

inheritance in java

play11:14

inheritance is a process where a class

play11:16

acquires attributes and methods of

play11:18

another class

play11:20

when we inherit the attributes and

play11:22

methods of an existing class we can

play11:24

access all the attributes and methods of

play11:26

that class in the program

play11:29

and this promotes code reasonability

play11:36

now the next question is what is

play11:38

subclass and superclass

play11:40

subclass is a class that inherits from

play11:42

another class

play11:44

and is also known as a child

play11:47

it is derived from super class and also

play11:50

inherits the properties of super class

play11:54

whereas superclass is a class that is

play11:56

being inherited from

play11:58

and it is also known as a paret

play12:03

it is a class from which many subclasses

play12:05

can be created

play12:08

the subclasses also inherit the

play12:10

properties of a super class

play12:13

let's have an example of these classes

play12:16

in this example bus car truck

play12:19

are all subclasses of the superclass

play12:22

vehicle

play12:27

now the next question is how is

play12:29

inheritance implemented in a java

play12:32

program

play12:33

in java to inherit from a class we use

play12:36

the extend keyword

play12:38

let's have an example of inheritance

play12:45

now in this example the class main can

play12:48

use all attributes and methods of the

play12:50

arithmetic class as if they were their

play12:53

own

play12:58

okay so coming to the next one what is a

play13:00

final variable

play13:01

a variable declared with the final

play13:03

keyword is a final variable and is used

play13:06

to prevent from overriding and modifying

play13:10

so we can say a final variable once

play13:12

assigned a value can never be changed

play13:15

so if we initialize a variable with the

play13:17

final keyword then we cannot modify its

play13:21

value

play13:23

and if we declare a method as final

play13:25

then it cannot be overridden by any

play13:27

subclasses

play13:32

now the next one is what is a package in

play13:34

java

play13:36

a package in java is used to group

play13:38

related classes or it is a collection of

play13:40

related classes

play13:42

packages are used to avoid name

play13:44

conflicts between classes

play13:47

and it allows to write a better

play13:49

maintainable code

play13:54

the next one is what are the different

play13:57

types of packages

play13:59

packages are divided into two categories

play14:03

built-in packages and user-defined

play14:05

packages

play14:07

now built-in packages are pre-written

play14:09

and are stored in the java api that are

play14:12

free to use

play14:14

they consist of packages for managing

play14:16

input output data programming and many

play14:19

more

play14:20

and here are some of the commonly used

play14:22

built-in packages

play14:32

next is user defined packages and these

play14:34

packages are created by the user

play14:40

now coming to the last question in this

play14:42

series what are abstract class an

play14:45

abstract class is a restricted class

play14:47

where we cannot create objects of the

play14:49

class

play14:50

to access the members of the abstract

play14:52

class

play14:53

it must be inherited to a subclass where

play14:56

we can then access the members of the

play14:58

abstract class through the object of the

play15:01

subclass

play15:03

an abstract class can have both regular

play15:05

methods and abstract methods without its

play15:08

body

play15:20

you

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Java BasicsProgrammingInterview PrepBeginner GuideCoding TutorialSoftware DevelopmentTech EducationCareer AdviceJava PlatformDeveloper Tips
¿Necesitas un resumen en inglés?