Oops Viva Questions
Oops Viva Questions
Here are some simple and basic viva questions and answers for Stack and Queue in Java:
1. What is a Stack?
Answer:
A stack is a linear data structure that follows the Last In, First Out (LIFO) principle. The last
element added is the first one removed.
2. What is a Queue?
Answer:
A queue is a linear data structure that follows the First In, First Out (FIFO) principle. The first
element added is the first one removed.
Answer:
Push: Add an element to the top of the stack.
Answer:
Answer:
Stack Overflow occurs when trying to add (push) an element to a full stack.
Answer:
Stack Underflow occurs when trying to remove (pop) an element from an empty stack.
Answer:
Answer:
The Peek operation returns the top element of the stack without removing it.
Answer:
The Front operation returns the first element of the queue without removing it.
Answer:
Yes, both Stack and Queue can be implemented using arrays or linked lists in Java.
Here are some basic viva questions and answers related to a program for generating a payslip
using inheritance:
Answer:
Inheritance is a mechanism in Java where one class (child) can inherit the properties and
methods of another class (parent)
Answer:
We use inheritance to achieve code reusability. It allows the child class to reuse the fields and
methods of the parent class, reducing code duplication.
Answer:
A base or parent class is the class from which other classes (child classes) inherit properties
and methods
Answer:
A derived or child class is the class that inherits the properties and methods from another
class (the parent class).
Answer:
The super keyword is used to refer to the parent class’s constructor or methods in a child
class.
Answer:
Answer:
Inheritance allows us to store common employee details in a base class and then calculate
specific salary components in a derived class. This avoids redundancy and promotes cleaner
code.
Answer:
Method overriding occurs when a child class provides its own implementation of a method
that is already defined in its parent class.
Answer:
No, Java does not support multiple inheritance with classes. However, it can be achieved
using interfaces.
Answer:
Here are some simplest basic viva questions and their answers related to using abstract
classes and interfaces in a Java program to calculate the area:
Abstract Class:
Answer: An abstract class is a class that cannot be instantiated and may contain abstract
methods (without a body) as well as concrete methods (with a body). It serves as a blueprint
for other classes.
2. How do you declare an abstract class in Java?
Answer: You declare an abstract class using the abstract keyword, like this:
// methods
Answer: Yes, an abstract class can have both abstract methods and concrete methods.
Answer: The purpose of an abstract class is to provide a common base for subclasses to
inherit from and to define common methods that subclasses can implement.
Interface:
Answer: An interface is a reference type in Java that can contain only constants, method
signatures, default methods, static methods, and nested types. Interfaces cannot contain
instance fields or constructors.
Answer: You declare an interface using the interface keyword, like this:
double calculateArea();
Answer: Yes, starting from Java 8, interfaces can have default methods with implementations.
Answer: An abstract class can provide method implementations and maintain state, while an
interface cannot maintain state and primarily defines method signatures. A class can
implement multiple interfaces but can extend only one abstract class.
General Questions:
Answer: You would use an abstract class when you want to provide a common base with
some shared implementation for subclasses, while using an interface is best when you want
to define a contract that multiple classes can implement without forcing a specific class
hierarchy.
Answer: Yes, an abstract class can implement an interface and can also provide
implementations for some of the interface methods.
Answer: You implement an interface in a class using the implements keyword, like this:
// implementation
Here are some simplest basic viva questions and their answers related to user-defined
exception handling in Java:
User-Defined Exception:
Answer: You create a user-defined exception by defining a new class that extends the
Exception class. For example:
super(message);
Answer: You throw a user-defined exception using the throw statement. For example:
Answer: You handle a user-defined exception using a try-catch block. For example:
try {
} catch (InvalidAgeException e) {
System.out.println(e.getMessage());
Answer: Checked exceptions are exceptions that are checked at compile-time (e.g.,
IOException), while unchecked exceptions are not checked at compile-time and occur during
runtime (e.g., NullPointerException, ArithmeticException).
Answer: You would use a user-defined exception when the built-in exceptions do not
adequately represent the specific error conditions in your application, allowing for clearer
error handling and messaging.
Answer: Yes, a user-defined exception can have constructors to allow for custom messages or
additional information to be passed when the exception is created.
Answer: Yes, you can catch multiple exceptions in a single catch block using the pipe (|)
operator. For example:
System.out.println(e.getMessage())