OOPJ Sample Questions
OOPJ Sample Questions
OOPJ Sample Questions
LONG QUESTION
1. Demonstrate two Java programs to insert/add items (“Ram”,” Shayam”,” Hari”, “Gopal”, “ravi”) creating
ArrayList and LinkedList object. Iterate and display the items using Iterator class object.
ANS-
2. Differentiate between Byte stream and character stream in Java. Write a Java program to copy the
content of “First.txt” to “Second.txt” using FileInputStream and FileOutputStream.
Character Stream:
4. You are given a text file (registration.txt) that contains lines of pairs of student IDs and course names.
[(2381, Java), ( 1812, ML), (1812, DS), (2381, DB). Each bracket represents a separate instance of a
student registration for a particular course. The course is a String; the student ID is an int. The
delimiter is a comma. You do not know the length of the file before you read all of it. You are to write
a program that: 1. reads the file, 2. counts the number of students registered in each course, and 3.
prints a list of all courses with their enrollment to the console.
ANS-
ANS- A thread's life cycle refers to the various stages it goes through during its execution.
These stages are typically represented by states, and a thread can only be in one state at a
time. Here's a breakdown of the common thread states:
6. CRUD operation
ANS- CRUD stands for Create, Read, Update, and Delete. These are fundamental operations
used in data persistence and management:
CRUD operations form the basis of interacting with various data sources in applications.
They are often implemented using database libraries or APIs provided by the storage system.
7. Constructor overloading
ANS- Constructor overloading allows a class to have multiple constructors with different
parameter lists. This enables you to create objects of the same class in various ways,
depending on the data you want to provide during initialization.
Here's an example:
Java
class Person {
String name;
int age;
// Default constructor
public Person() {
name = "John Doe";
age = 25;
}
In this example, you can create a Person object with the default values using the default
constructor or provide a custom name and age using the constructor with parameters.
8. Java Keywords
ANS- Java keywords are reserved words that have specific meanings within the Java
programming language. You cannot use them as variable names, method names, or class
names. Here are some common Java keywords:
• class: Defines a new class
• public: Access modifier for variables and methods
• private: Access modifier for variables and methods
• static: Keyword to declare class-level variables and methods
• void: Denotes a method that doesn't return a value
• int: Data type for integers
• String: Data type for representing text
• if: Conditional statement
• for: Looping construct
• while: Looping construct
• return: Keyword used to return a value from a method
There are many more Java keywords, and you can find a comprehensive list in the official
Java language documentation.
ANS- Exception handling is a mechanism to manage errors and unexpected situations that
might occur during program execution. It allows you to write more robust and reliable code.
Here's an example program that demonstrates exception handling:
Java
public class ExceptionExample {
In this example:
• The try block attempts to execute the code that might throw an exception.
• The catch block intercepts the exception if it occurs.
• You can handle the exception gracefully within the catch block, such as providing an
error message or taking corrective actions.
This is a simple example, but it illustrates the basic principles of exception handling in Java.
// 2. Concatenation
// 3. Length
// 4. Comparison
if (str1.equals(str2)) {
} else {
// 5. Substring
// 7. IndexOf
// 8. Replace
String replaced = result.replace("World", "Universe");
// 9. Split
System.out.println("Words in sentence:");
System.out.println(word);
// 10. Trim
SHORT QUESTION
1. Justify the problem arises without using “synchronized” keyword for thread synchronization in
Multithreaded program in Java.
2. Illustrate collection framework in Java. List out some collection frame work.
ANS- Marshaling is converting an object into a data format suitable for storage or transmission.
Serialization, a part of marshaling, converts objects into byte streams. Serialization is significant for
saving object states, allowing data to be stored in files or transmitted over networks
.
ANS- toArray(): Converts a list into an array. It returns an array containing all elements of the list.
contains(): Checks if a specific element exists in the list. It returns true if the element is found,
otherwise false.
5. Name some common implementations of the Java Set interface in the Java Collections Framework.
ANS- Common implementations of the Set interface include HashSet, TreeSet, and LinkedHashSet.
ANS - StringBuffer is synchronized and thread-safe, whereas StringBuilder is not synchronized, making it
faster but not thread-safe.
7. Super keyword
ANS- "super" keyword refers to the superclass instance. It's used to access superclass methods and
constructors, especially when overridden in subclasses.
8. Thread Priority
ANS- Thread priority determines the importance of a thread. Higher priority threads are scheduled before
lower ones. Priority is a hint to the scheduler, not a strict rule.
9. Polymorphism
ANS- Polymorphism allows objects to be treated as instances of their superclass or interface. It enables
methods to be defined by their superclass or interface and overridden by subclasses, enhancing code
flexibility and reusability.
ANS- Checked Exceptions: Checked at compile time, like IOException, need handling.Unchecked
Exceptions: Not checked at compile time, like NullPointerException, don't necessarily need handling.
ANS- "super" refers to the superclass object. It's used to access superclass members from subclass
methods or constructors.
ANS- "this" refers to the current object. It's used to differentiate between instance variables
andparameters with the same name, and to call instance methods.