OOPS Answer
OOPS Answer
o d) int arr[5]
o a) try
o b) catch
o c) finally
o d) none
Answer: c) finally
The finally block is always executed after the try-catch blocks, regardless of whether an exception
was caught or not.
3. Which of these method of class String is used to remove leading and trailing whitespace?
o a) startsWith()
o b) endsWith()
o c) trim()
o d) doTrim()
Answer: c) trim()
The trim() method removes both leading and trailing spaces from a string.
4. Which of the following is true about a method having the same name as that of its class?
o a) finalize
o b) class
o c) constructor
o d) none
Answer: c) constructor
A constructor in Java has the same name as the class and is used to initialize objects of the class.
5. Which function is used to perform some action when the object is to be destroyed?
o a) finally
o b) catch
o c) main()
6. What is the value returned by function compareTo() if the invoking string is greater than
the string compared?
o a) Zero
o d) None
7. Write a program to show how final keyword is used with class, method, and variable.
Write the difference between abstract and final keyword.
Program:
java
Copy code
}
Difference between abstract and final:
o Final: A method declared as final cannot be overridden, and a final class cannot be
inherited. Final variables cannot be changed once initialized.
8. What is inheritance in Java? Write about different types of inheritance in Java. Show one
example of implementation of multiple inheritance in Java.
Answer: Inheritance allows one class (child) to inherit the properties and methods of
another class (parent). The types of inheritance in Java are:
o Multilevel inheritance: A class is derived from a class that is also derived from
another class.
Java does not support multiple inheritance with classes, but it can be achieved using interfaces.
java
Copy code
interface A {
void methodA();
interface B {
void methodB();
class C implements A, B {
System.out.println("Method A");
System.out.println("Method B");
}
}
9. Write about static and final keyword in Java. What is the difference between equals()
method and == operator in Java String?
Answer:
o Static: When a method or variable is marked as static, it belongs to the class rather
than instances of the class. Static methods and variables can be accessed without
creating an object.
o Final: The final keyword is used to declare constants. A final method cannot be
overridden, and a final class cannot be inherited.
Constructor chaining is the process of calling one constructor from another in the same or different
class using this() or super().
Example:
java
Copy code
class A {
A() {
A(int x) {
class B extends A {
B() {
11. Write the difference between String and StringBuffer class. Write the difference between
StringBuffer and StringBuilder class. "String object is immutable" — justify this statement
with example.
Answer:
o String: Immutable, meaning once a String object is created, its content cannot be
changed.
o StringBuilder: Also mutable like StringBuffer, but not synchronized (faster in single-
threaded environments).
java
Copy code
String s1 = "Hello";
s1.concat(" World");
12. What are the different access modifiers used in Java? Show with example.
Answer: Java provides four access modifiers:
Example:
java
Copy code
class Example {
int defaultVar;