Answer---Object Oriented Programming-2022
Answer---Object Oriented Programming-2022
Group-A
(Very Short Answer Type Question; 1-Mark Question)
(I) In which memory a String is stored, when we create a string using new operator?
Heap memory
(III) Which of the following keywords are used to control access to a class Member?
A) new
B) abstract
C) public
D) interface
C) public
(IV) An abstract class, which declared with the “abstract” keyword, cannot be instantiated. True
or False?
True
(IX) Which method is used to set the graphics current color to the specified color in the graphics
class?
setColor()
(X) Java is robust because ________________ . Fill in the blank.
A) it is object oriented
B) garbage collection is present
C) inheritance is present
D) exception handling
B) garbage collection is present
(XII) What is the length of the application box made by the following Java program?
import java.awt.*;
import java.applet.*;
public class myapplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A Simple Applet", 20, 20);
}
}
A) 20
B) 50
C) 100
D) System dependent
D) System dependent
Group-B
(Short Answer Type Question; 5-Mark Question)
ButtonExample() {
b = new Button("Click Me");
b.setBounds(30, 50, 80, 30);
b.addActionListener(this);
add(b);
setSize(200, 200);
setLayout(null);
setVisible(true);
}
8(ii) Thread Life-Cycle: The Thread Life Cycle defines the states a thread goes through in its life.
It includes the following states:
i. New: A thread is in this state when it is created but has not yet started.
ii. Runnable: A thread is in this state when it is ready to run but may be waiting for CPU time.
iii. Blocked: A thread is in this state when it is waiting for a resource that is currently
unavailable.
iv. Waiting: A thread is in this state when it is waiting indefinitely for another thread to perform
a specific action.
v. Timed Waiting: A thread is in this state when it is waiting for a specific amount of time.
vi. Terminated: A thread is in this state when it has completed execution.
8(iii) Abstraction: It is one of the core concepts in object-oriented programming (OOP), where only
the essential details of an object are shown to the user, and the implementation details are hidden.
The goal of abstraction is to reduce complexity by hiding unnecessary implementation details and
showing only the functionality to the user.
Types of Abstraction in Java:
1. Abstract Classes:
o An abstract class is a class that cannot be instantiated directly. It may contain both
abstract methods (methods without implementation) and concrete methods (methods
with implementation).
o It provides a way to define a common structure or behavior that can be shared across
multiple derived classes.
o Subclasses of the abstract class must provide implementations for the abstract
methods
2. Interfaces:
o An interface in Java is similar to an abstract class, but all methods in an interface are by default
abstract (before Java 8). Interfaces can be implemented by classes, and a class can
implement multiple interfaces.
o Unlike abstract classes, interfaces cannot contain instance fields (only constants) and
cannot have constructor methods.
o Interfaces allow you to define methods without specifying their behavior. The
behavior must be implemented by the classes that implement the interface.
Example: Using an Abstract Class
abstract class Shape {
abstract void draw();
void area() { System.out.println("Area calculated."); }
}
9(ii) Dynamic Binding: Dynamic Binding is the process of resolving method calls at runtime. It
occurs when a method is called on an object, but the specific method to be executed is determined
based on the object's actual class at runtime. This supports polymorphism and method overriding.
9(iii) Encapsulation: Encapsulation is a fundamental concept in object-oriented programming
where the internal details of an object are hidden and only accessible through public methods. This
helps in restricting direct access to an object's state and ensures that it is modified only in controlled
ways.
Example:
class Person {
private String name; // private field
System-Defined Exceptions: These are exceptions that are predefined in Java, like
NullPointerException, ArrayIndexOutOfBoundsException, etc.
11. (b) Briefly explain the use of “this” and “super” keywords.
this: Refers to the current object. It is used to differentiate between instance variables and
local variables or to call a constructor of the current class.
o Example:
class Person {
String name;
Person(String name) {
this.name = name; // this refers to the instance variable
}
}
super: Refers to the superclass of the current object. It can be used to access superclass
methods or constructors.
o Example:
class Animal {
void sound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
void sound() {
super.sound(); // Calls the superclass method
System.out.println("Dog barks");
}
}
11. (c) Difference between == operator and equals() method in the context of string objects.
== operator: The == operator compares the references (memory addresses) of two string
objects, not their actual contents. It returns true if both references point to the same object.
Example:
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2);
// Output: false (