OOPS AND JAVA VIVA AND INTERVIEW QUESTIONS
Top 100 OOPs and Java viva/interview questions
A. Object-Oriented Programming (OOPs) Concepts
- What are the four main principles of OOPs?
- Define encapsulation. How is it implemented in Java?
- Explain inheritance and its types in Java.
- What is polymorphism? Differentiate between compile-time and runtime polymorphism.
- What is abstraction? How is it implemented in Java?
- What is the difference between an abstract class and an interface?
- Can a class extend multiple classes in Java? Why or why not?
- What are access modifiers? Explain their scope.
- What is method overloading? Provide an example.
- What is method overriding? How does it support polymorphism?
- What are constructors? What are their types?
- Can a constructor be overridden? Why?
- What is the
super
keyword? When is it used? - Explain the
this
keyword in Java. - Can you instantiate an abstract class? Why or why not?
- How does Java implement multiple inheritance?
- What is a static method? Can it be overridden?
- What is the difference between a class and an object?
- What is the difference between shallow copy and deep copy?
- Explain the concept of "is-a" and "has-a" relationships in OOP.
B. Java Core Concepts
- What is the difference between JDK, JRE, and JVM?
- What is bytecode? How is it executed in Java?
- What are the features of Java that make it platform-independent?
- What is the difference between
==
and.equals()
in Java? - What is a
final
keyword? Explain its use in variables, methods, and classes. - Can we have multiple
main()
methods in Java? - What is the difference between a local variable and an instance variable?
- What is the significance of the
String
class in Java? - Why is
String
immutable in Java? - What is a wrapper class? Why are they needed?
- Differentiate between
String
,StringBuffer
, andStringBuilder
. - What is garbage collection? How does it work in Java?
- What are
try
,catch
, andfinally
blocks in exception handling? - What is the difference between
throw
andthrows
? - Can a
finally
block be skipped? When? - Explain the concept of auto-boxing and unboxing.
- What is the difference between
ArrayList
andLinkedList
? - How are
HashMap
andHashtable
different? - What is the difference between an array and a collection in Java?
- What are functional interfaces in Java?
C. Java Advanced Topics
- What is a thread in Java? How is it created?
- What is the difference between
Thread
class andRunnable
interface? - What is synchronization? How is it achieved in Java?
- Explain the difference between
notify()
andnotifyAll()
. - What is a daemon thread? How is it created?
- What is the purpose of
volatile
keyword in Java? - What is the
transient
keyword in Java? - What is serialization? How is it implemented in Java?
- What is the difference between
Serializable
andExternalizable
? - What are annotations in Java? Provide examples.
- Explain the concept of
Lambda
expressions in Java. - What is the Stream API in Java?
- How does the
Optional
class help avoidNullPointerException
? - What is the difference between
Comparable
andComparator
? - What is the difference between
HashSet
andTreeSet
? - What is a
ConcurrentHashMap
? How is it different fromHashMap
? - What are generics in Java? Why are they used?
- What is a
Callable
interface? How is it different fromRunnable
? - What is the difference between
Fork/Join
framework and regular threads? - Explain the concept of immutability in Java.
D. Java Applets and Graphics
- What is a Java applet? How is it different from an application?
- What are the lifecycle methods of an applet?
- What is the difference between
Applet
andJApplet
? - How do you embed an applet into an HTML file?
- What is the
paint()
method in applets? When is it called? - What are the restrictions imposed on applets?
- What is AWT in Java? How is it different from Swing?
- Explain the difference between lightweight and heavyweight components.
- What is the
Graphics
class in Java? How is it used? - How do you draw shapes (e.g., circles, rectangles) in Java?
E. Database and File Handling
- What is JDBC? How is it used in Java?
- What are the different types of JDBC drivers?
- Explain the steps to connect a Java application to a database.
- What is the purpose of the
ResultSet
interface in JDBC? - What is a prepared statement? How is it different from a statement?
- Explain the difference between
commit()
androllback()
in JDBC. - How is data stored and read from a file in Java?
- What is the difference between
FileReader
andBufferedReader
? - What is the purpose of the
FileInputStream
andFileOutputStream
classes? - What are the advantages of using NIO (New I/O) over traditional I/O in Java?
F. Miscellaneous
- What is reflection in Java? Provide an example.
- What are class loaders in Java?
- What is the difference between
checked
andunchecked
exceptions? - What is the
java.lang
package? Why is it imported by default? - What are singleton classes in Java? How are they implemented?
- What is the
enum
keyword in Java? Provide an example. - Explain the concept of design patterns in Java.
- What is the Factory design pattern? Provide an example.
- What is the Singleton design pattern? Why is it used?
- What is the Observer design pattern in Java?
- How does the
System.gc()
method work? - What is the difference between
Object.clone()
andObject.equals()
? - What is the role of the
hashCode()
method in Java? - What is the purpose of the
instanceof
keyword? - Can an interface have a constructor? Why or why not?
- What is the difference between
ArrayList
andVector
? - What is a deadlock? How can it be avoided in Java?
- What is the difference between
final
,finally
, andfinalize()
? - Explain the difference between
AbstractMap
andHashMap
. - What are the benefits of using Java over other programming languages?
KEYS
A. Object-Oriented Programming (OOPs) Concepts
-
What are the four main principles of OOPs?
Encapsulation, Inheritance, Polymorphism, and Abstraction. -
Define encapsulation. How is it implemented in Java?
Wrapping data and methods together using private fields and public getters/setters. -
Explain inheritance and its types in Java.
Mechanism where one class acquires properties of another; types: single, multilevel, hierarchical. -
What is polymorphism? Differentiate between compile-time and runtime polymorphism.
Ability to take multiple forms; compile-time achieved through method overloading, runtime through method overriding. -
What is abstraction? How is it implemented in Java?
Hiding implementation details and showing only essential features using abstract classes or interfaces. -
What is the difference between an abstract class and an interface?
Abstract class can have concrete methods, while interfaces have only abstract (or default) methods (till Java 8). -
Can a class extend multiple classes in Java? Why or why not?
No, because Java doesn’t support multiple inheritance to avoid ambiguity. -
What are access modifiers? Explain their scope.
Keywords controlling visibility:public
,protected
,default
,private
. -
What is method overloading? Provide an example.
Defining multiple methods with the same name but different parameters. -
What is method overriding? How does it support polymorphism?
Redefining a method in a subclass; enables runtime polymorphism. -
What are constructors? What are their types?
Special methods to initialize objects; types: default, parameterized, copy. -
Can a constructor be overridden? Why?
No, because constructors are not inherited. -
What is the
super
keyword? When is it used?
Refers to the immediate parent class object; used for constructor chaining and method access. -
Explain the
this
keyword in Java.
Refers to the current class instance. -
Can you instantiate an abstract class? Why or why not?
No, because abstract classes are incomplete by definition. -
How does Java implement multiple inheritance?
Through interfaces. -
What is a static method? Can it be overridden?
Belongs to the class, not objects; cannot be overridden, only hidden. -
What is the difference between a class and an object?
Class is a blueprint; an object is an instance of a class. -
What is the difference between shallow copy and deep copy?
Shallow copy copies references; deep copy duplicates entire objects. -
Explain the concept of "is-a" and "has-a" relationships in OOP.
"Is-a" denotes inheritance, "has-a" denotes composition.
B. Java Core Concepts
-
What is the difference between JDK, JRE, and JVM?
JDK is a development kit, JRE runs Java programs, JVM executes bytecode. -
What is bytecode? How is it executed in Java?
Intermediate code executed by JVM. -
What are the features of Java that make it platform-independent?
Bytecode and JVM. -
What is the difference between
==
and.equals()
in Java?
==
compares references;.equals()
compares content. -
What is a
final
keyword? Explain its use in variables, methods, and classes.
Makes variables constant, prevents method overriding, and disallows inheritance. -
Can we have multiple
main()
methods in Java?
Yes, but only one with the signaturepublic static void main(String[] args)
is the entry point. -
What is the difference between a local variable and an instance variable?
Local variables are declared inside methods; instance variables are class-level fields. -
What is the significance of the
String
class in Java?
Immutable and widely used for text manipulation. -
Why is
String
immutable in Java?
For thread safety and caching. -
What is a wrapper class? Why are they needed?
Converts primitives to objects and vice versa. -
Differentiate between
String
,StringBuffer
, andStringBuilder
.
String
is immutable;StringBuffer
is thread-safe;StringBuilder
is faster but not thread-safe. -
What is garbage collection? How does it work in Java?
Automatic memory management to reclaim unused objects. -
What are
try
,catch
, andfinally
blocks in exception handling?
Handle exceptions with guaranteed execution offinally
. -
What is the difference between
throw
andthrows
?
throw
is used to explicitly throw an exception;throws
declares exceptions. -
Can a
finally
block be skipped? When?
Yes, if the JVM exits during execution. -
Explain the concept of auto-boxing and unboxing.
Automatic conversion between primitives and wrapper classes. -
What is the difference between
ArrayList
andLinkedList
?
ArrayList
uses dynamic arrays;LinkedList
uses doubly linked lists. -
How are
HashMap
andHashtable
different?
HashMap
is non-synchronized;Hashtable
is synchronized. -
What is the difference between an array and a collection in Java?
Arrays are fixed-size, collections are dynamic. -
What are functional interfaces in Java?
Interfaces with a single abstract method (e.g.,Runnable
).
C. Java Advanced Topics
-
What is a thread in Java? How is it created?
Lightweight process created viaThread
class orRunnable
interface. -
What is the difference between
Thread
class andRunnable
interface?
Runnable
allows sharing threads;Thread
directly represents a thread. -
What is synchronization? How is it achieved in Java?
Prevents thread interference using thesynchronized
keyword. -
Explain the difference between
notify()
andnotifyAll()
.
notify()
wakes one thread;notifyAll()
wakes all waiting threads. -
What is a daemon thread? How is it created?
Background thread usingsetDaemon(true)
. -
What is the purpose of
volatile
keyword in Java?
Ensures visibility of changes to variables across threads. -
What is the
transient
keyword in Java?
Excludes variables from serialization. -
What is serialization? How is it implemented in Java?
Converting an object into bytes usingSerializable
interface. -
What is the difference between
Serializable
andExternalizable
?
Serializable
uses default serialization;Externalizable
requires custom implementation. -
What are annotations in Java? Provide examples.
Metadata for code; e.g.,@Override
.
D. Java Applets and Graphics
-
What is a Java applet? How is it different from an application?
A Java applet is a small program embedded in web pages; it runs inside a browser. -
What are the lifecycle methods of an applet?
init()
,start()
,stop()
, anddestroy()
. -
What is the difference between
Applet
andJApplet
?
JApplet
is a Swing-based applet;Applet
is part of AWT. -
How do you embed an applet into an HTML file?
Using the<applet>
tag withcode
andwidth
/height
attributes. -
What is the
paint()
method in applets? When is it called?
Used to draw graphics; called by the AWT system. -
What are the restrictions imposed on applets?
Restricted from accessing local file systems and executing arbitrary code. -
What is AWT in Java? How is it different from Swing?
AWT is heavyweight and platform-dependent; Swing is lightweight and platform-independent. -
Explain the difference between lightweight and heavyweight components.
Lightweight components are drawn in Java; heavyweight rely on native OS components. -
What is the
Graphics
class in Java? How is it used?
Used for drawing shapes and images in AWT/Swing. -
How do you draw shapes (e.g., circles, rectangles) in Java?
Using methods likedrawRect()
anddrawOval()
of theGraphics
class.
E. Database and File Handling
-
What is JDBC? How is it used in Java?
JDBC is a Java API to connect and interact with databases. -
What are the different types of JDBC drivers?
Type 1 (JDBC-ODBC Bridge), Type 2 (Native-API), Type 3 (Network Protocol), Type 4 (Thin Driver). -
Explain the steps to connect a Java application to a database.
Load driver, establish connection, create statement, execute query, process results, close connection. -
What is the purpose of the
ResultSet
interface in JDBC?
Used to retrieve and manipulate database query results. -
What is a prepared statement? How is it different from a statement?
Precompiled SQL query; reduces SQL injection risk and improves performance. -
Explain the difference between
commit()
androllback()
in JDBC.
commit()
saves changes,rollback()
undoes them. -
How is data stored and read from a file in Java?
Using classes likeFileReader
,FileWriter
,BufferedReader
, andBufferedWriter
. -
What is the difference between
FileReader
andBufferedReader
?
FileReader
reads characters;BufferedReader
reads text efficiently using buffering. -
What is the purpose of the
FileInputStream
andFileOutputStream
classes?
For reading/writing raw byte data. -
What are the advantages of using NIO (New I/O) over traditional I/O in Java?
Non-blocking I/O, improved performance, and scalability.
F. Miscellaneous
-
What is reflection in Java? Provide an example.
Reflection allows runtime inspection of classes, methods, and fields. -
What are class loaders in Java?
Responsible for loading classes into JVM at runtime. -
What is the difference between
checked
andunchecked
exceptions?
Checked exceptions are handled during compilation; unchecked are at runtime. -
What is the
java.lang
package? Why is it imported by default?
It contains core classes likeString
,Math
; imported for convenience. -
What are singleton classes in Java? How are they implemented?
Classes ensuring only one instance exists; implemented using private constructors and static methods. -
What is the
enum
keyword in Java? Provide an example.
Used to define constants; e.g.,enum Day { MONDAY, TUESDAY }
. -
Explain the concept of design patterns in Java.
Standard solutions to common software design problems. -
What is the Factory design pattern? Provide an example.
Creates objects without exposing the instantiation logic. -
What is the Singleton design pattern? Why is it used?
Ensures only one instance of a class exists; used for shared resources. -
What is the Observer design pattern in Java?
Defines a dependency where one object notifies others of changes. -
How does the
System.gc()
method work?
Suggests garbage collection but doesn't guarantee it. -
What is the difference between
Object.clone()
andObject.equals()
?
clone()
creates a copy;equals()
compares objects. -
What is the role of the
hashCode()
method in Java?
Provides a hash value for an object; used in hashing-based collections. -
What is the purpose of the
instanceof
keyword?
Checks if an object is of a specific type. -
Can an interface have a constructor? Why or why not?
No, because interfaces are not meant to be instantiated. -
What is the difference between
ArrayList
andVector
?
ArrayList
is non-synchronized;Vector
is synchronized. -
What is a deadlock? How can it be avoided in Java?
A state where threads block each other; avoided by proper lock ordering and timeout mechanisms. -
What is the difference between
final
,finally
, andfinalize()
?
final
prevents modification,finally
ensures execution,finalize()
is for cleanup. -
Explain the difference between
AbstractMap
andHashMap
.
AbstractMap
is a base class;HashMap
provides a concrete implementation. -
What are the benefits of using Java over other programming languages?
Platform independence, robust libraries, scalability, and ease of use.
Comments
Post a Comment