0% found this document useful (0 votes)
21 views6 pages

OOPS Answer

Uploaded by

Rajparno Dhar
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)
21 views6 pages

OOPS Answer

Uploaded by

Rajparno Dhar
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/ 6

Group A - Answer any five (5x1=5)

1. Which of these is an incorrect array declaration?

o a) int arr[] = new int[5]

o b) int [] arr = new int[5]

o c) int arr[] = new int[5]

o d) int arr[5]

Answer: d) int arr[5]


In Java, array size cannot be declared at the time of array variable declaration like in option (d).
Arrays must be instantiated using new.

2. Which part of code gets executed whether an exception is caught or not?

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

o d) none of the mentioned

Answer: d) none of the mentioned


In Java, there is no direct method to destroy an object, but the finalize() method can be used to clean
up resources before the object is garbage collected. However, it’s not guaranteed to be called.

6. What is the value returned by function compareTo() if the invoking string is greater than
the string compared?

o a) Zero

o b) Value less than zero

o c) Value greater than zero

o d) None

Answer: c) Value greater than zero


The compareTo() method returns a value greater than zero when the invoking string is
lexicographically greater than the string it is compared with.

Group B - Answer any four (4x5=20)

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

final class FinalClass {

final int value = 10; // final variable

final void display() { // final method

System.out.println("This is a final method.");

class SubClass extends FinalClass { // Error, cannot extend a final class

// Cannot override display() method since it is final

}
Difference between abstract and final:

o Abstract: A method or class declared as abstract must be implemented in a subclass.


Abstract classes cannot be instantiated.

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 Single inheritance: One class inherits from one parent class.

o Multilevel inheritance: A class is derived from a class that is also derived from
another class.

o Hierarchical inheritance: Multiple classes inherit from one base class.

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

Example of multiple inheritance using interfaces:

java

Copy code

interface A {

void methodA();

interface B {

void methodB();

class C implements A, B {

public void methodA() {

System.out.println("Method A");

public void methodB() {

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.

Difference between equals() and ==:

o equals() checks the content of two strings.

o == checks if two references point to the same memory location.

10. What is a constructor? Describe constructor chaining with example.


Answer: A constructor in Java is a block of code that initializes new objects. It has the same
name as the class and does not have a return type.

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() {

this(10); // Constructor chaining within the same class

System.out.println("Default constructor of class A");

A(int x) {

System.out.println("Parameterized constructor of class A: " + x);

class B extends A {

B() {

super(); // Constructor chaining from child to parent class


System.out.println("Constructor of class 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 StringBuffer: Mutable, meaning its content can be modified after creation.


Synchronized (thread-safe).

o StringBuilder: Also mutable like StringBuffer, but not synchronized (faster in single-
threaded environments).

Immutable String Example:

java

Copy code

String s1 = "Hello";

s1.concat(" World");

System.out.println(s1); // Output: Hello (not changed)

12. What are the different access modifiers used in Java? Show with example.
Answer: Java provides four access modifiers:

o Private: Only accessible within the same class.

o Default (no modifier): Accessible within the same package.

o Protected: Accessible within the same package and by subclasses.

o Public: Accessible from everywhere.

Example:

java

Copy code

class Example {

private int privateVar;

int defaultVar;

protected int protectedVar;

public int publicVar;


}

You might also like