[go: up one dir, main page]

0% found this document useful (0 votes)
18 views11 pages

Oops Viva Questions

Uploaded by

nithish2062005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

Oops Viva Questions

Uploaded by

nithish2062005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Here are some of the simplest

basic viva questions for


sequential search, binary search,
and quadratic sorting algorithms:

Sequential (Linear) Search:


1. What is linear search?
Linear search is a simple search
technique where we go through
each element one by one to find
the target.
2. What is the time complexity of
linear search?
The time complexity is O(n), where
n is the number of elements in the
array.
Binary Search:
1. What is binary search?
Binary search is a search
technique that works on sorted
arrays, repeatedly dividing the
search interval in half to find the
target element.
2. Does binary search require the
array to be sorted?
Yes, binary search only works on
sorted arrays.
3. What is the time complexity of
binary search?
The time complexity of binary
search is O(log n).
Bubble Sort:
1. What is bubble sort?
Bubble sort is a sorting algorithm
that repeatedly swaps adjacent
elements if they are in the wrong
order.
2. What is the time complexity of
bubble sort?
The time complexity is O(n²) in the
worst case.
Insertion Sort:
1. What is insertion sort?
Insertion sort is a sorting
algorithm that builds the sorted
array one element at a time by
inserting each element into its
correct position.
2. What is the time complexity of
insertion sort?
The time complexity is O(n²) in the
worst case.

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.

3. What are the main operations of a Stack?

Answer:
Push: Add an element to the top of the stack.

Pop: Remove and return the top element.

Peek: Check the top element without removing it.

4. What are the main operations of a Queue?

Answer:

Enqueue: Add an element to the back (rear) of the queue.

Dequeue: Remove and return the front element.

5. What is Stack Overflow?

Answer:

Stack Overflow occurs when trying to add (push) an element to a full stack.

6. What is Stack Underflow?

Answer:

Stack Underflow occurs when trying to remove (pop) an element from an empty stack.

7. What is the difference between Stack and Queue?

Answer:

Stack uses LIFO (Last In, First Out).

Queue uses FIFO (First In, First Out).

8. What is the use of the Peek operation in Stack?

Answer:

The Peek operation returns the top element of the stack without removing it.

9. What is the Front operation in Queue?

Answer:

The Front operation returns the first element of the queue without removing it.

10. Is it possible to implement a Stack or Queue using arrays in Java?

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:

1. What is Inheritance in Java?

Answer:

Inheritance is a mechanism in Java where one class (child) can inherit the properties and
methods of another class (parent)

2. Why do we use Inheritance?

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.

3. What is a base (parent) class?

Answer:

A base or parent class is the class from which other classes (child classes) inherit properties
and methods

4. What is a derived (child) class?

Answer:

A derived or child class is the class that inherits the properties and methods from another
class (the parent class).

5. What is the super keyword used for?

Answer:

The super keyword is used to refer to the parent class’s constructor or methods in a child
class.

6. How do you define inheritance in Java?

Answer:

In Java, inheritance is defined using the extends keyword. For example:

class Child extends Parent {


// code

7. How is inheritance useful for generating a payslip?

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.

8. What is method overriding?

Answer:

Method overriding occurs when a child class provides its own implementation of a method
that is already defined in its parent class.

9. Can you have multiple inheritance in Java?

Answer:

No, Java does not support multiple inheritance with classes. However, it can be achieved
using interfaces.

10. What is the difference between inheritance and composition?

Answer:

Inheritance represents an "is-a" relationship (e.g., a Manager is an Employee).

Composition represents a "has-a" relationship (e.g., a Car has an Engine).

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:

1. What is an 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:

public abstract class Shape {

// methods

3. Can an abstract class have concrete methods?

Answer: Yes, an abstract class can have both abstract methods and concrete methods.

4. What is the purpose of an abstract class?

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:

1. What is an interface in Java?

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.

2. How do you declare an interface in Java?

Answer: You declare an interface using the interface keyword, like this:

public interface Shape {

double calculateArea();

3. Can an interface have method implementations?

Answer: Yes, starting from Java 8, interfaces can have default methods with implementations.

4. What is the difference between an abstract class and an interface?

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:

1. Why would you use an abstract class instead of an interface?

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.

2. Can an abstract class implement an interface?

Answer: Yes, an abstract class can implement an interface and can also provide
implementations for some of the interface methods.

3. How do you implement an interface in a class?

Answer: You implement an interface in a class using the implements keyword, like this:

public class Circle implements Shape {

public double calculateArea() {

// implementation

Here are some simplest basic viva questions and their answers related to user-defined
exception handling in Java:

User-Defined Exception:

1. What is a user-defined exception in Java?

Answer: A user-defined exception is a custom exception class created by the programmer


that extends the Exception class or its subclasses. It allows for more specific error handling
based on application needs.

2. How do you create a user-defined exception in Java?

Answer: You create a user-defined exception by defining a new class that extends the
Exception class. For example:

public class InvalidAgeException extends Exception {


public InvalidAgeException(String message) {

super(message);

3. How do you throw a user-defined exception?

Answer: You throw a user-defined exception using the throw statement. For example:

if (age < 18) {

throw new InvalidAgeException("Age must be 18 or older.");

4. How do you handle a user-defined exception?

Answer: You handle a user-defined exception using a try-catch block. For example:

try {

// code that may throw the exception

} catch (InvalidAgeException e) {

System.out.println(e.getMessage());

General Exception Handling:

1. What is the difference between checked and unchecked exceptions?

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

2. When would you use a user-defined exception?

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.

3. Can a user-defined exception have constructors?

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.

4. Can you catch multiple exceptions in a single catch block?

Answer: Yes, you can catch multiple exceptions in a single catch block using the pipe (|)
operator. For example:

catch (IOException | InvalidAgeException e) {

System.out.println(e.getMessage())

You might also like