Java Cse-Cic-Csd-20apc0512-20apc3609-20apc3207 II II Vivavoce Ay 2023 24 TSR
Java Cse-Cic-Csd-20apc0512-20apc3609-20apc3207 II II Vivavoce Ay 2023 24 TSR
AUTONOMOUS
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Java is called platform independent because of its byte codes which can run on any system
irrespective of its underlying operating system.
Java is not 100% Object-oriented because it makes use of eight primitive data types such as
boolean, byte, char, int, float, double, long, short which are not objects.
Wrapper classes convert the Java primitives into the reference types (objects). Every primitive
data type has a class dedicated to it. These are known as wrapper classes because they “wrap” the
primitive data type into an object of that class. Refer to the below image which displays different
primitive type, wrapper class and constructor argument.
In Java, constructor refers to a block of code which is used to initialize an object. It must have
the same name as that of the class. Also, it has no return type and it is automatically called when
an object is created.
1. Default Constructor: In Java, a default constructor is the one which does not take any inputs. In
other words, default constructors are the no argument constructors which will be created by
default in case you no other constructor is defined by the user. Its main purpose is to initialize the
instance variables with the default values. Also, it is majorly used for object creation.
2. Parameterized Constructor: The parameterized constructor in Java, is the constructor which is
capable of initializing the instance variables with the provided values. In other words, the
constructors which take the arguments are called parameterized constructors
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Equals() method is defined in Object class in Java and used for checking equality of two objects
defined by business logic.
“==” or equality operator in Java is a binary operator provided by Java programming language
and used to compare primitives and objects. public boolean equals(Object o) is the method
provided by the Object class. The default implementation uses == operator to compare two
objects. For example: method can be overridden like String class. equals() method is used to
compare the values of two objects.
In Java, the super keyword is a reference variable that refers to an immediate parent class object.
When you create a subclass instance, you’re also creating an instance of the parent class, which is
referenced to by the super reference variable.
Java doesn’t use pointers because they are unsafe and increases the complexity of the program.
Since, Java is known for its simplicity of code, adding the concept of pointers will be
contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order
to avoid direct access to memory by the user, pointers are discouraged in Java
In Java, access modifiers are special keywords which are used to restrict the access of a class,
constructor, data member and method in another class. Java supports four types of access
modifiers:
1. Default
2. Private
3. Protected
4. Public
A class in Java is a blueprint which includes all your data. A class contains fields (variables) and
methods to describe the behavior of an object. Let’s have a look at the syntax of a class.
1classAbc {
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
3methods}
An object is a real-world entity that has a state and behavior. An object has three characteristics:
1. State
2. Behavior
3. Identity
1. Inheritance: Inheritance is a process where one class acquires the properties of another.
2. Encapsulation: Encapsulation in Java is a mechanism of wrapping up the data and code
together as a single unit.
3. Abstraction: Abstraction is the methodology of hiding the implementation details from
the user and only providing the functionality to the users.
4. Polymorphism: Polymorphism is the ability of a variable, function or object to take
multiple forms.
Methods Constructors
1. Used to represent the behavior of an object 1. Used to initialize the state of an object
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
4. No default method is provided by the 4. A default constructor is provided by the compiler if the
compiler class has none
5. Method name may or may not be same as 5. Constructor name must always be the same as the class
class name name
final is a special keyword in Java that is used as a non-access modifier. A final variable can be
used in different contexts such as:
● final variable
When the final keyword is used with a variable then its value can’t be changed once assigned. In
case the no value has been assigned to the final variable then using only the class constructor a
value can be assigned to it.
● final method
When a method is declared final then it can’t be overridden by the inheriting class.
● final class
When a class is declared as final in Java, it can’t be extended by any subclass class but it can
extend other class.
Java String pool refers to a collection of Strings which are stored in heap memory. In this,
whenever a new object is created, String pool first checks whether the object is already present in
the pool or not. If it is present, then the same reference is returned to the variable else new object
will be created in the String pool and the respective reference will be returned.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
In Java, string objects are immutable in nature which simply means once the String object is
created its state cannot be modified. Whenever you try to update the value of that object instead
of updating the values of that particular object, Java creates a new string object. Java String
objects are immutable as String objects are generally cached in the String pool. Since String
literals are usually shared between multiple clients, action from one client might affect the rest. It
enhances security, caching, synchronization, and performance of the application.
Array ArrayList
Cannot contain values of different data types Can contain values of different data types.
Size must be defined at the time of
Size can be dynamically changed
declaration
Need to specify the index in order to add
No need to specify the index
data
Arrays can contain primitive data types as Arraylists can contain only objects, no primitive
well as objects data types are allowed
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with
hiding the details and showing the essential things to the user. Thus you can say that abstraction
in Java is the process of hiding the implementation details from the user and revealing only the
functionality to them. Abstraction can be achieved in two ways:
An interface in Java is a blueprint of a class or you can say it is a collection of abstract methods
and static constants. In an interface, each method is public and abstract but it does not contain
any constructor. Thus, interface basically is a group of related methods with empty bodies
Inheritance in Java is the concept where the properties of one class can be inherited by the other.
It helps to reuse the code and establish a relationship between different classes. Inheritance is
performed between two types of classes:
A class which inherits the properties is known as Child Class whereas a class whose properties
are inherited is known as Parent class
1. Single Inheritance: In single inheritance, one class inherits the properties of another i.e there will
be only one parent as well as one child class.
2. Multilevel Inheritance: When a class is derived from a class which is also derived from
another class, i.e. a class having more than one parent class but at different levels, such
type of inheritance is called Multilevel Inheritance.
3. Hierarchical Inheritance: When a class has more than one child classes (subclasses) or
in other words, more than one child classes have the same parent class, then such kind of
inheritance is known as hierarchical.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Method Overloading :
● In Method Overloading, Methods of the same class shares the same name but each method must
have a different number of parameters or parameters having different types and order.
● Method Overloading is to “add” or “extend” more to the method’s behavior.
● It is a compile-time polymorphism.
● The methods must have a different signature.
● It may or may not need inheritance in Method Overloading.
Method Overriding:
● In Method Overriding, the subclass has the same method with the same name and exactly the
same number and type of parameters and same return type as a superclass.
● Method Overriding is to “Change” existing behavior of the method.
● It is a run time polymorphism.
● The methods must have the same signature.
● It always requires inheritance in Method Overriding
If a child class inherits the property from multiple classes is known as multiple inheritance. Java
does not allow to extend multiple classes.
The problem with multiple inheritance is that if multiple parent classes have the same method
name, then at runtime it becomes difficult for the compiler to decide which method to execute
from the child class.
Therefore, Java doesn’t support multiple inheritance. The problem is commonly referred to as
Diamond Problem
Encapsulation is a mechanism where you bind your data(variables) and code(methods) together
as a single unit. Here, the data is hidden from the outer world and can be accessed only via
current class methods. This helps in protecting the data from any unnecessary modification. We
can achieve encapsulation in Java by:
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Q 27) What is meant by the Local variable and the Instance variable?
Answer:
Local variables are defined in the method and scope of the variables that exist inside the method
itself.
Instance variable is defined inside the class and outside the method and the scope of the variables
exists throughout the class
Q 30) What are all the Classes and Interfaces that are available in the collections?
Answer: Given below are the Classes and Interfaces that are available in Collections:
Interfaces:
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
● Collection
● List
● Set
● Map
● Sorted Set
● Sorted Map
● Queue
Classes:
● Lists:
● Array List
● Vector
● Linked List
Sets:
● Hash set
● Linked Hash Set
● Tree Set
Maps:
● Hash Map
● Hash Table
● TreeMap
● Linked Hashed Map
Queue:
● Priority Queue
Checked Exceptions must either declare the exception using throws keyword (or) surrounded by
appropriate try/catch.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
These exceptions are not checked during the compile time by the compiler. The compiler doesn’t
force to handle these exceptions. It includes:
● Arithmetic Exception
● ArrayIndexOutOfBounds Exception
At the end of the method, we can declare the exception using throws keyword.
b) catch:
This is followed by a try block. Exceptions are caught here.
c) finally:
This is followed either by try block (or) catch block. This block gets executed regardless of an
exception. So generally clean up codes are provided here
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Wait() and the other above-mentioned methods do not give the lock on the object immediately until
the currently executing thread completes the synchronized code. It is mostly used in synchronization.
This method is used to send a signal to wake up a single This method sends the signal to wake up all the t
thread in the waiting pool. in a waiting spool.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
● Running
● Non-runnable (Blocked)
● Terminated
Ans: If an exception is not handled in a program using try catch blocks, program gets
aborted and no statement executes after the statement which caused exception
throwing.
Ans: An anonymous class is a class defined without any name in a single line of code
using new keyword.
Ans: In Java, if we define a new class inside a particular block, it’s called a local
class. Such a class has local scope and isn’t usable outside the block where its defined.
The main method is always static because static members are those methods that belong to the
classes, not to an individual object. So if the main method will not be static then for every object,
It is available. And that is not acceptable by JVM. JVM calls the main method based on the class
name itself. Not by creating the object.
Because there must be only 1 main method in the java program as the execution starts from the
main method. So for this reason the main method is static.
The main objective of this process is to free up the memory space occupied by the unnecessary
and unreachable objects during the Java program execution by deleting those unreachable
objects.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
● This ensures that the memory resource is used efficiently, but it provides no guarantee
that there would be sufficient memory for the program execution
Heap
52. What is the difference between the ‘throw’ and ‘throws’ keyword in java?
● The ‘throw’ keyword is used to manually throw the exception to the calling method.
● And the ‘throws’ keyword is used in the function definition to inform the calling method
that this method throws the exception. So if you are calling, then you have to handle the
exception.
No, it is not necessary for a catch block to be present after a try block. - A try block should be
followed either by a catch block or by a finally block. If the exceptions likelihood is more, then
they should be declared using the throws clause of the method
This is why it releases lock, which can then be developed by a different thread to alter
the condition of which it is waiting.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Finally is a keyword which is used in exception handling, along with try and catch. The
finally block is always implemented regardless of whether an exception is thrown from
try block or not
Q57. What is a finally block? Is there a case when finally will not execute?
Finally block is a block which always executes a set of statements. It is always associated with a
try block regardless of any exception that occurs or not.
Yes, finally will not be executed if the program exits either by calling System.exit() or by
causing a fatal error that causes the process to abort.
Q59. Can we write multiple catch blocks under single try block?
Yes we can have multiple catch blocks under single try block but the approach should be from
specific to general.
A thread is the smallest piece of programmed instructions which can be executed independently
by a scheduler. In Java, all the programs will have at least one thread which is known as
the main thread. This main thread is created by the JVM when the program starts its execution.
The main thread is used to invoke the main() of the program.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
When we assign a value of one data type to the different data type then these two data
types may not be compatible and needs a conversion. If the data types are compatible (for
example assigning int value to long) then java does automatic conversion and does not
require casting. However if the data types are not compatible then they need to be casted
for conversion.
Yes. Java is a platform independent language. We can write java code on one platform
and run it on another platform. For e.g. we can write and compile the code on windows
and can run the generated bytecode on Linux or any other supported platform. This is one
of the main features of java.
Classloader, Class area, Heap, Stack, Program Counter Register and Native Method
Stack
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Here public is an access modifier, which means that this method is accessible by any
class.
static – static keyword tells that this method can be accessed without creating the
instance of the class. Refer: Static keyword in java
String args[] – The args is an array of String type. This contains the command line
arguments that we can pass while running the program.
The javac is a compiler that compiles the source code of your program and generates
bytecode. In simple words javac produces the java byte code from the source code
written *.java file. JVM executes the bytecode to run the program.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
A class is a blueprint or template or prototype from which you can create the object of
that class. A class has set of properties and methods that are common to its objects.
A wrapper class converts the primitive data type such as int, byte, char, boolean etc. to
the objects of their respective classes such as Integer, Byte, Character, Boolean etc.
Refer: Wrapper class in Java
Path specifies the location of .exe files. Classpath specifies the location of bytecode
(.class files).
● byte – 8 bit
● short – 16 bit
● char – 16 bit Unicode
● int – 32 bit (whole number)
● float – 32 bit (real number)
● long – 64 bit (Single precision)
● double – 64 bit (double precision)
Java uses Unicode to represent the characters. Unicode defines a fully international
character set that can represent all of the characters found in human languages.
Any constant value that is assigned to a variable is called literal in Java. For example –
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
When we assign a value of one data type to the different data type then these two data
types may not be compatible and needs a conversion. If the data types are compatible (for
example assigning int value to long) then java does automatic conversion and does not
require casting. However if the data types are not compatible then they need to be casted
for conversion.
For example:
● break statement is generally used with switch case data structure to come out
of the statement once a case is executed.
● It can be used to come out of the loop in Java
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
● Inheritance
● Polymorphism
● Data Encapsulation
● Abstraction
The process by which one class acquires the properties and functionalities of another
class is called inheritance. Inheritance brings reusability of code in a java
application. Refer: Guide to Inheritance in Java.
When a class extends more than one classes then it is called multiple inheritance. Java
doesn’t support multiple inheritance whereas C++ supports it, this is one of the difference
between java and C++. Refer: Why java doesn’t support multiple inheritance?
Polymorphism is the ability of an object to take many forms. The most common use of
polymorphism in OOPs is to have more than one method with the same name in a single
class. There are two types of polymorphism: static polymorphism and dynamic
polymorphism. Refer these guides to understand the polymorphism concept in detail:
1) Java Polymorphism 2) Types of Polymorphism
This is a special class defined by java; all other classes are subclasses of object class.
Object class is superclass of all other classes. Object class has the following methods
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions
Error: Mostly a system issue. It always occur at run time and must be resolved in order to
proceed further.
Exception: Mostly an input data issue or wrong logic in code. Can occur at compile time
or run time.
If a method does not handle a checked exception, the method must declare it using the
throwskeyword. The throws keyword appears at the end of a method’s signature.
Q89) Can we handle more than one exception in a single catch block?
Yes we can do that using if-else statement but it is not considered as a good practice. We
should have one catch block for one exception.
if you use join() ,it makes sure that as soon as a thread calls join,the current
thread(yes,currently running thread) will not execute unless the thread you have called
join is finished.
Deadlock describes a situation where two or more threads are blocked forever, waiting
for each other.
Regulation: Subject Code: Subject Name : Object Oriented Programming AY: 2023-2024
AK20 20APC0512 Through JAVA
CSE-CIC-CSD (20APC0512/20APC3609/20APC3207)Topic Wise Interview Questions