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

Viva Oopj

Viva questions for object oriented programming with java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
16 views

Viva Oopj

Viva questions for object oriented programming with java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
Batthula Manoj PIET CSE Al Viva Questions Oops 4. 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 (iFelse, 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? ‘Muttiple 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 of 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. Multi i 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. Aways 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 deciare and initialize an array? int{] arr = new int{5}; (declaration and initialization with size) or int] arr initialization) 1, 2, 3}: (direct 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, ie., 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 getNameQ) { // 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(); I! Abstract method, no implementation } class Dog extends Animal { void sound() { ‘System.out printin("Bark’); } } 27. What is inheritance 7 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 printin("Animal is eating’); } } class Dog extends Animal { void bark() { System out printin(’Dog is barking"); } } 28. What is Polymorphism ? Definition: Polymorphism allows one entity (method or object) to take many forms. itis achieved through method overloading (compile-time) and method overriding (run-time) Example ‘Method Overioading (compile-time polymorphism) ~ same method name, different parameters. class Caloulator { int add(int a, int b) { return a +b; } int add(int a, int b, int c) { retuna +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 printin("Animal sound”); } } class Cat extends Animal { void sound() { ‘System.out printin("Meow’); } } 29, Explain about the Association, Aggregation, and Composition ? Association’ |t 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: \t'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 Allthe best....dp@ OOPJ Java Quick Revision 1, Basic Concepts of OOP: - Class: Blueprint for objects, defines attributes and methods. - Object: Instance of a class. - Inheritance: Acquiring properties of one class by another. - Polymorphism: Ability to take many forms (method overloading, method overriding). - Abstraction: Hiding internal details and showing functionality. - Encapsulation: Wrapping data (variables) and methods together as a single unit (class). 2. Principles of OOP in Java: - Encapsulation: Using ‘private’ access specifiers with getter and setter methods. - Inheritance: Using ‘extends’ keyword. A child class inherits from a parent class. - Polymorphism: - Compile-time (Method Overloading): Methods with the same name but different parameter lists - Runtime (Method Overriding): Subclass provides a specific implementation of a method already defined in the parent class - Abstraction: Using abstract classes or interfaces. 3. Key Java Concepts: - Constructor: A special method to initialize objects. No return type, same name as the class. - Static: A keyword for class-level variables or methods that can be accessed without an object - Final: Used to restrict inheritance (class), method overriding, or changing variables - Interface vs Abstract Class: - Interface: All methods are abstract (Java 7). Multiple inheritance possible. - Abstract Class: Can have both abstract and concrete methods. Single inheritance. 4, Access Specitiers: - private: Only within the class. - default: Accessible within the package. - protected: Within the package and by subclasses. - public: Accessible everywhere. 5, Exception Handling: = try-catch block: Used to handle runtime errors (exceptions). - finally block: Executes regardless of an exception, - throw: Used to throw exceptions explicitly - throws: Used in method declarations to specify exceptions that the method can throw. 6. Java Collections Framework: - List: An ordered collection (can contain duplicates). Examples: ArrayList, LinkedList - Set: A collection that cannot contain duplicate elements. Examples: HashSet, TreeSet. - Map: A collection that maps keys to values (no duplicate keys). Examples: HashMap, TreeMap. 7. Threads: - Implemented through: - Extending Thread Class - Implementing Runnable Interface - Use ‘start()' to begin a thread and ‘run()' for its task. - Synchronization: Used to handle concurrency issues. 8. File Handling: - FileReader/FileWriter: For reading/writing text files. - BufferedReader/BufferedWriter: For efficient reading/writing. - Serialization: Converting an object into a byte stream for storage or transfer. 9, JVM (Java Virtual Machine): - Responsible for running Java bytecode. Comprises: - Class Loader: Loads class files. - Memory Areas: Heap, Stack, Method Area, etc. - Execution Engine: Executes bytecode 10. Garbage Collection: - Automatic memory management. The garbage collector removes objects that are no longer referenced - You can suggest garbage collection using ‘System.gc()’, but it's not guaranteed to run immediately.

You might also like