Beginner Level Java Viva or Interview Questions

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Ahmad Javed

Beginner Level Java


interview or viva questions.

Java Basics
1. What is Java?
Java is a high-level, object-oriented programming language
developed by Sun Microsystems (now Oracle) in 1995. It is
platform-independent due to the Java Virtual Machine (JVM),
which allows compiled Java code to run on any system.

2. What is the JVM?


The Java Virtual Machine (JVM) is a part of the Java Runtime
Environment (JRE) that runs Java bytecode. It converts bytecode
into machine code for the underlying system, ensuring Java’s
platform independence.

3. What is the JRE?


The Java Runtime Environment (JRE) provides libraries, Java
APIs, and the JVM to run Java applications. It doesn't include
development tools like compilers, making it distinct from the Java
Development Kit (JDK).

4. What is the JDK?


The Java Development Kit (JDK) is a software development
environment used for developing Java applications. It includes the
JRE, a compiler, a debugger, and other tools for writing, compiling,
and running Java code.

Follow Ahmad Javed


5. What is bytecode in Java?
Bytecode is the intermediate code generated after Java source
code is compiled. It is platform-independent and runs on the JVM,
which converts it into machine-specific instructions.

Object-Oriented Programming

6. What are the four pillars of OOP in Java?


The four pillars of Object-Oriented Programming are
Encapsulation, Abstraction, Inheritance, and Polymorphism. These
principles guide how objects interact, encapsulate behaviour, and
reuse code.

7. What is inheritance in Java?


Inheritance allows a class to inherit properties and behaviour
(methods) from another class, enabling code reuse. The class that
inherits is called a subclass, and the class being inherited from is
called a superclass.

8. What is polymorphism in Java?


Polymorphism allows one interface to be used for a general class
of actions. In Java, polymorphism is achieved through method
overloading (compile-time) and method overriding (runtime).

9. What is encapsulation in Java?


Encapsulation involves wrapping data (variables) and code
(methods) inside a class, and restricting direct access to some of
the object's components. This is typically done using private
variables with public getter and setter methods.

10.What is abstraction in Java?

Follow Ahmad Javed


Abstraction is the concept of hiding the implementation details of a class
and showing only the essential features. Abstract classes and interfaces
are used to achieve abstraction in Java.

Java Classes and Objects

11. What is a class in Java?


A class in Java is a blueprint for creating objects. It defines
properties (attributes) and behaviours (methods) that the objects
created from the class will have.

12. What is an object in Java?


An object is an instance of a class. It contains state (fields) and
behaviour (methods), and it is created using the new keyword.

13. What is the difference between a class and


an object?
A class is a blueprint for creating objects, while an object is an
instance of a class. The class defines the properties and
behaviours, and objects represent the real-world entities created
from that blueprint.

14. What is a constructor in Java?


A constructor is a special method in a class that initialises new
objects. It has the same name as the class and does not return
any value, not even void.

15. What is the difference between a


constructor and a method?

Follow Ahmad Javed


A constructor is used to initialise an object and has no return type,
while a method performs a specific operation and may or may not
return a value. Constructors are called implicitly when creating
objects, while methods are explicitly called.

Java Methods
16. What is method overloading in Java?
Method overloading allows multiple methods in the same class to
have the same name but different parameter lists. It enables
methods to perform similar tasks with different types or numbers of
arguments

17. What is method overriding in Java?


Method overriding occurs when a subclass provides a specific
implementation for a method that is already defined in its
superclass. The overridden method must have the same name,
return type, and parameters.

18. What is the difference between overloading


and overriding?
Overloading occurs within the same class, with different
parameter lists, while overriding happens between a superclass
and a subclass, where the method signatures are identical.
Note:Overloading is compile-time polymorphism, while overriding
is runtime polymorphism.

19. What is a static method in Java?


A static method belongs to the class rather than any instance of
the class. It can be called without creating an object of the class.
Static methods can access only static variables and cannot refer to
this or super.

Follow Ahmad Javed


20. What is a final method in Java?

A final method cannot be overridden by subclasses. It is used


to lock down the method implementation to prevent modification,
ensuring the behaviour defined by the method remains consistent.

Java Interfaces and Abstract Classes

21. What is an interface in Java?


An interface is a reference type in Java that contains abstract
methods. It is used to achieve abstraction and multiple inheritance.
Classes that implement an interface must provide implementations
for all its methods.

22. What is an abstract class in Java?


An abstract class is a class that cannot be instantiated and is
meant to be subclassed. It can contain both abstract methods
(without implementation) and concrete methods (with
implementation).

23. What is the difference between an abstract


class and an interface?
An abstract class can have both abstract and non-abstract
methods, while an interface can only have abstract methods
(before Java 8). Abstract classes can have fields, but interfaces
cannot (before Java 8).

24. What is multiple inheritance and how does


Java handle it?
Multiple inheritance occurs when a class inherits from more than
one superclass. Java does not support multiple inheritance with

Follow Ahmad Javed


classes but allows it through interfaces by using the
implements keyword.

25. What is the default method in Java 8


interfaces?
Java 8 introduced default methods in interfaces, which allow
methods to have a default implementation. This enables interface
evolution without breaking existing implementations.

Exception Handling in Java

26. What is an exception in Java?


An exception is an event that occurs during the execution of a
program, disrupting its normal flow. Exceptions in Java are objects
representing errors and are handled using try-catch blocks.

Using Keywords:try,catch and finally.

27. What is the difference between checked and


unchecked exceptions?
Checked exceptions are checked at compile time and must be
either caught or declared in the method signature, while
unchecked exceptions (RuntimeException and its subclasses) are
not checked at compile time.

28. What is a try-catch block in Java?


A try-catch block is used to handle exceptions in Java. The code
that may throw an exception is placed inside the try block, and
the exception is caught in the catch block, where it can be
handled.

Follow Ahmad Javed


29. What is the finally block in Java?
The finally block is used to execute code after a try block,
regardless of whether an exception was thrown. It is typically used
for cleanup actions like closing file streams or releasing resources.

30. What is the throw keyword in Java?


The throw keyword is used to explicitly throw an exception from
a method or block of code. This allows you to signal error
conditions manually rather than relying on the system to throw
exceptions.

Java Collections Framework

31. What is the Java Collections Framework?


The Java Collections Framework provides a set of classes and
interfaces for storing and manipulating groups of data as
collections. It includes classes like ArrayList, HashMap, and
interfaces like List, Set, and Map.

32. What is the difference between a List and a


Set in Java?
A List allows duplicate elements and maintains insertion order,
while a Set does not allow duplicates and may or may not
maintain order (e.g., HashSet doesn’t, while LinkedHashSet
does).

33. What is the difference between an ArrayList


and a LinkedList in Java?

Follow Ahmad Javed


An ArrayList is a dynamic array that allows random access to
elements but has slower insertions and deletions in the middle. A
LinkedList uses nodes and provides faster insertions and
deletions but slower random access.

34. What is a HashMap in Java?


A HashMap is a collection class that stores key-value pairs. It
allows null values and one null key. It does not maintain order but
provides efficient retrieval using the key’s hashcode.

35. What is the difference between a HashMap and


a TreeMap?
A HashMap does not maintain order, while a TreeMap stores
elements in sorted order based on the keys. Additionally, TreeMap
implements the NavigableMap interface, providing methods for
navigating the map.

Java Streams and I/O

36. What is the Stream API in Java?


The Stream API, introduced in Java 8, allows processing
sequences of elements in a functional style. It supports operations
like filtering, mapping, and reducing data and can be performed
sequentially or in parallel.

37. What is the difference between InputStream and


OutputStream in Java?

InputStream reads data from a source, while OutputStream


writes data to a destination. They are part of Java’s I/O package
and provide methods for handling binary data.

Follow Ahmad Javed


38. What is serialization in Java?
Serialization is the process of converting an object into a byte
stream for storage or transmission. Deserialization is the

Unlocking Java: Essential Keywords


Every Developer Should Know

class: Defines a new class.

public: Specifies the access level as visible to all


other classes.

static: Indicates that a method or variable belongs to


the class, not instances.

void: Specifies that a method does not return any


value.

int: Declares an integer variable.

boolean: Defines a variable as having two possible


values: true or false.
if: Executes a block of code conditionally based on a
boolean expression.

Follow Ahmad Javed


else: Provides an alternative block if the if condition
is false.

for: Starts a loop that repeats a block of code a


specific number of times.

while: Repeats a block of code while a condition is


true.

do: Executes a block of code once and then repeats it


while a condition is true.

return: Exits a method and optionally returns a value.

new: Creates new instances of objects or arrays.

try: Starts a block of code to test for exceptions.

catch: Handles exceptions thrown in a try block.

finally: Executes a block of code after try/catch,


regardless of exceptions.

throw: Manually throws an exception.

throws: Declares exceptions that a method might


throw.
extends: Indicates that a class inherits from a parent
class.

Follow Ahmad Javed


implements: Specifies that a class implements an
interface.

this: Refers to the current instance of the class.

super: Refers to the parent class or constructor.

final: Defines constants, prevents method


overriding, or prohibits inheritance.

abstract: Declares a method or class without a


complete implementation.

interface: Declares a collection of abstract methods


that a class can implement.

package: Defines the namespace for a class or


interface.

import: Imports other classes or entire packages for


use in a class.

switch: Selects one of many possible code blocks to


execute.

case: Defines a branch in a switch statement.

Follow Ahmad Javed


default: Specifies the default case in a switch if no
other cases match.

break: Exits a loop or switch statement.

continue: Skips the current iteration of a loop and


moves to the next.

synchronized: Locks a method or block of code for


exclusive access by a single thread.

volatile: Marks a variable to indicate that its value


might change asynchronously.

transient: Excludes a variable from serialization.

assert: Enables test conditions during runtime.

enum: Defines a set of named constants


(enumeration).

instanceof: Tests whether an object is an instance


of a specific class or interface.

native: Indicates that a method is implemented in


platform-dependent code.

strictfp: Restricts floating-point calculations to


ensure portability across platforms.

Follow Ahmad Javed


Thank You To All
Follow @Ahmad Javed for more tech related news,
Full stack Developing and Interview or Viva
Questions.

You might also like