0% found this document useful (0 votes)
6 views

Untitled Document

Uploaded by

mruzumaki952
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Untitled Document

Uploaded by

mruzumaki952
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Batthula Manoj

PIET CSE AI

Viva Questions Oops

1. Introduction to Java

1.What is Java, and why is it platform-independent?


Java is a high-level, object-oriented programming language. It's platform-independent because
its code is compiled into bytecode, which can run on any machine with a JVM (Java Virtual
Machine).

2 What is the difference between JVM, JRE, and JDK?


JVM (Java Virtual Machine) runs Java bytecode, JRE (Java Runtime Environment) provides
libraries and JVM, and JDK (Java Development Kit) is a complete development kit, including
JRE and development tools.

3.What is bytecode in Java?


Bytecode is the intermediate code generated after Java source code is compiled. It's
platform-independent and executed by the JVM.

2. Control Statements

4.What are the types of control statements in Java?


Control statements include conditional (if-else, switch), looping (for, while, do-while), and
branching (break, continue, return).

5.What is the difference between while and do-while loops?


In while, the condition is checked before the loop execution, while in do-while, the loop executes
at least once before checking the condition.

6.What does the break statement do?


It terminates the loop or switch statement immediately.

3. Inheritance

7.What is inheritance in Java?


Inheritance allows one class (child) to acquire properties and behaviors (methods) of another
class (parent).

8.What is method overriding?


Method overriding occurs when a subclass provides a specific implementation of a method that
is already defined in its superclass.

9.How is multiple inheritance achieved in Java?


Multiple inheritance is not directly supported with classes but can be achieved using interfaces.

4. Strings

10.How are strings immutable in Java?


Once a String object is created, its value cannot be changed. If modified, a new object is
created instead.

11.What is the difference between String, StringBuilder, and StringBuffer?


String is immutable, while StringBuilder and StringBuffer are mutable. StringBuffer is
thread-safe, whereas StringBuilder is not.

12.How can you compare two strings in Java?


You can compare strings using equals() for content comparison or == for reference comparison.

5. Interface

13.What is an interface in Java?


An interface is a reference type in Java that can contain abstract methods and constants. It's
used to achieve abstraction and multiple inheritance.

14.How is an interface different from an abstract class?


An abstract class can have both abstract and concrete methods, while an interface only
contains abstract methods (Java 8+ allows default methods).

15.Can an interface extend another interface?


Yes, an interface can extend multiple other interfaces.

6. Multithreading

16.What is a thread in Java?


A thread is a lightweight subprocess, and Java supports multithreading to execute multiple tasks
concurrently.

17.How do you create a thread in Java?


A thread can be created by extending the Thread class or by implementing the Runnable
interface.
18.What is synchronization in Java?
Synchronization is used to control the access of multiple threads to shared resources,
preventing data inconsistency.

7. Arrays

19.What is an array in Java?


An array is a collection of elements of the same type, stored in a contiguous memory location.

20.How do you declare and initialize an array?


int[] arr = new int[5]; (declaration and initialization with size) or int[] arr = {1, 2, 3}; (direct
initialization).

21.What is an ArrayIndexOutOfBoundsException?
This exception occurs when you try to access an array element using an invalid index.

8. Collections

22.What is the Java Collections Framework?


It is a set of classes and interfaces that implement commonly reusable collection data structures
like List, Set, and Map.

23.What is the difference between ArrayList and LinkedList?


ArrayList provides fast random access but slow insertions and deletions, while LinkedList
provides faster insertions and deletions but slower random access.

24.What is the difference between HashSet and TreeSet?


HashSet stores elements in an unordered way, while TreeSet stores them in a sorted (natural
order) manner.

25.What is Encapsulation ?
Encapsulation is the bundling of data (variables) and methods (functions) that operate on the
data into a single unit, i.e., a class. It also restricts direct access to some of the object's
components, which is known as data hiding.

Example:
You can make fields private and provide public getter and setter methods to access and update
the value.

class Person {
private String name; // Encapsulation: private variable
public String getName() { // Getter method
return name;
}

public void setName(String name) { // Setter method


this.name = name;
}
}

26. What is Abstraction ?

Definition: Abstraction means hiding complex implementation details and showing only the
necessary functionality. It allows you to focus on what an object does rather than how it does it.

Example:
Using abstract classes and interfaces to define methods without implementing them.

abstract class Animal {


abstract void sound(); // Abstract method, no implementation
}

class Dog extends Animal {


void sound() {
System.out.println("Bark");
}
}

27. What is Inheritance ?

Definition: Inheritance is the mechanism by which one class (child or subclass) inherits fields
and methods from another class (parent or superclass). This allows for code reusability and
establishing relationships between classes.

Example:
A Dog class can inherit properties and behaviors from an Animal class.

class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
}

28. What is Polymorphism ?

Definition: Polymorphism allows one entity (method or object) to take many forms. It is achieved
through method overloading (compile-time) and method overriding (run-time).

Example:
Method Overloading (compile-time polymorphism) – same method name, different parameters.

class Calculator {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}

Method Overriding (run-time polymorphism) – a subclass provides its own implementation of a


method already defined in the parent class.

class Animal {
void sound() {
System.out.println("Animal sound");
}
}

class Cat extends Animal {


void sound() {
System.out.println("Meow");
}
}

29. Explain about the Association, Aggregation, and Composition ?


Association: It defines a relationship between two classes where objects of one class interact
with objects of another class. For example, a Teacher and a Student class have an association,
as a teacher can teach students.

Aggregation: It's a special form of association where one class (whole) contains objects of
another class (part). The part can exist independently of the whole. For example, a Department
and a Professor—a department can have many professors, but professors can exist
independently of the department.

Composition: It's a stronger form of aggregation where the part cannot exist without the whole.
For example, a Car and its Engine—if the car is destroyed, the engine ceases to exist as well.

30. What are the four pillars of oops?


● Encapsulation
● inheritance
● polymorphism
● Abstraction

All the best….👍🙂

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy