Oop 2m 4
Oop 2m 4
Define object: | A class that inherits properties and methods A stream in Java is a sequence of data elements
An object is an instance of a class that from another class. | A class whose properties that can be processed in a functional style. Java
encapsulates data (attributes) and methods and methods are inherited by another class. | has two types of streams: Byte Stream (handles
(functions) that operate on the data. Objects | Example: class Dog extends Animal {} | I/O of bytes) and Character Stream (handles I/O
are the basic building blocks of object-oriented Example: class Animal {} | of characters).
programming and represent real-world
entities. a. What is byte code? j. | BufferedReader | Scanner
Bytecode is an intermediate, | Used for reading text from files or input
b. Define data abstraction: platformindependent code generated by the streams. | Used for reading input from the
Data abstraction is the process of hiding the Java compiler after compiling the source code. It console or files. | | It is faster for reading large
implementation details of a class and exposing is executed by the Java Virtual Machine (JVM), amounts of data. | It provides more flexibility
only the essential features or functionalities. It which makes Java platform-independent. for parsing different types of data. | | It reads
is achieved using abstract classes or interfaces data line by line. | It reads input token
in object-oriented programming. b. What are the different types of
by token (e.g., words or numbers). |
variables in Java?
c. Define constructor: c. | Widening Type Casting |
Java has the following types of variables: Narrowing Type Casting | | Converts a
A constructor is a special method in a class that smaller data type to a larger data type (e.g., int
is automatically called when an object is 1. Local Variables: Declared inside
to double). | Converts a larger data type to a
instantiated. It is used to initialize the methods and accessible only within that
smaller data type (e.g., double to int). | |
attributes of the object. method.
Happens automatically (implicit). | Must
2. Instance Variables: Declared inside a be explicitly done using casting. | | No data
d. Define polymorphism:
class but outside methods, associated with an loss occurs. | Data loss may occur.
Polymorphism allows the same method or
operator to behave differently based on the object.
d. What are the different access
context. It is achieved through method modifiers in Java?
overloading (compile-time polymorphism) or 3. Class (Static) Variables: Declared
method overriding (runtime polymorphism). with the static keyword, associated with the
Java provides four access modifiers:
class itself rather than an object.
1. Public: Accessible from any class.
e. Define API:
c. Explain the usage of try and catch
2. Private: Accessible only within the
An Application Programming Interface (API) is clause.
declared class.
a set of predefined functions, protocols, and The try block is used to write code that might
tools that allow different software applications throw exceptions, and the catch block is used
3. Protected: Accessible within the same
to communicate and interact with each other. to handle those exceptions. If an exception
package and by subclasses.
occurs inside the try block, it is caught by the
f. Define package: catch block, preventing the program from 4. Default (Package-private): Accessible
A package is a collection of related classes and crashing. only within the same package.
interfaces grouped together to organize and
manage large codebases. It helps prevent d. How will you find out the length of a e. Given:
naming conflicts and improves code string in Java? String s = "OBJECT ORIENTED METHODOLOGY";
modularity.
The length of a string in Java can be obtained System.out.println(s.length());
g.| Actual Parameters | Formal using the length() method of the String class. Output:
Parameters | These are the values or Example:
arguments passed to a method when it is The length of the string "OBJECT ORIENTED
called. | These are the variables defined in the String str = "Hello"; int length = METHODOLOGY" is 27.
method signature to accept the values from
str.length(); // Returns 5 f. | Final | Finally
actual parameters. | | Example: sum(5, 10)
| A keyword used to declare constants, prevent
where 5 and 10 are actual parameters. |
f. How Java supports platform independency? inheritance, or method overriding. | A block
Example: void sum(int a, int b) where a and b
used to execute code after a try block, whether
are formal parameters. | Java achieves platform independence through or not an exception occurs. | | Example: final int
the use of bytecode and the Java Virtual x = 10; | Example: try { ... } finally { ... }
h.| Local Variables | Global
Machine (JVM). Java programs are compiled |
Variables | Declared within a
into bytecode, which can be run on any system
method or block and accessible only within
that has a compatible JVM, making Java h. Write any two methods of InputStream:
that scope. | Declared outside of all methods 1. read(): Reads a single byte or an
applications platform-independent.
and accessible throughout the program. | | array of bytes from the input stream.
Example: int x = 5; inside a method. | Example: g. Define Widening Type Casting
int x = 10; outside any method. | Widening type casting is an automatic type 2. close(): Closes the input stream and
i. Explain “garbage collection” in conversion where a smaller data type (e.g., int) releases system resources associated with it.
objectoriented methodology: Garbage is converted into a larger data type (e.g., float),
collection is the process of automatically without losing data. It happens implicitly in j. What is the use of the new keyword?
identifying and reclaiming unused or Java.
unreferenced memory occupied by objects. The new keyword is used to create new objects
This helps in efficient memory management h. | Method | Constructor in Java. It dynamically allocates memory for the
and prevents memory leaks. Languages like | A method is a block of code that performs a object on the heap. Example:
Java use a built-in garbage collector. specific task. | A constructor is a special
MyClass obj = new MyClass();
method used to initialize objects when they
j. | Subclass | are created. | | It can have a return type.
Superclass | It does not have a return type. | | It can
be called multiple times. | It is called only
once when an object is created. |
i. Define stream.