OOP Java - IMP M 2
OOP Java - IMP M 2
rd
3 SEM Exam – Important Questions
MODULE – 2
1. Default Constructor:
A default constructor is one that takes no arguments.
If a class does not explicitly define any constructors, Java automatically provides a default
constructor with no parameters.
The default constructor initializes instance variables to their default values (0 for numeric types,
false for boolean, and null for reference types).
2. Parameterized Constructor:
A parameterized constructor is one that takes one or more parameters.
It allows you to initialize instance variables with specific values passed as arguments during
object creation.
In the above examples, the first one demonstrates a default constructor where no arguments are passed,
and it simply prints a message when an object is created. The second example shows a parameterized
constructor that initializes instance variables name and age with specific values passed during object
creation.
1. Static Variables:
Static variables, also known as class variables, are declared using the static keyword within a
class.
They are shared across all instances of the class and exist independently of any particular
instance.
Only one copy of a static variable exists in memory, regardless of how many instances of the
class are created.
Static variables are initialized only once, at the start of the program's execution, and retain
their values throughout the program's lifetime.
2. Static Methods:
Static methods, like static variables, are also declared using the static keyword within a
class.
They belong to the class rather than to any specific instance, and can be invoked using the
class name without the need to create an object.
Static methods can access only other static members of the class (variables and methods),
and cannot access instance variables or instance methods directly.
They are commonly used for utility functions that do not require access to instance-specific
data.
In the provided examples, the count variable in MyClass is a static variable, and the square method in
MathUtils is a static method. These static members can be accessed using the class name without the
need to create an object, making them useful for maintaining shared state and functionality across instances
of the class
3. Write a program to perform Stack operation using proper class and Methods.
Ans:
4. Explain use of this in JAVA with example program.
Ans:
“this” keyword:
In Java, the this keyword is a reference to the current object within a method or constructor. It can be used
to access instance variables and methods of the current object, differentiate between instance variables and
local variables with the same name, and pass the current object as a parameter to other methods or
constructors. Here's how this can be used with an example program:
The setNameAndAge method sets both the name and age of the Person object in one function
call.
Inside the method, this keyword is used to access instance variables name and age.
The display method then displays the details of the Person object, using this to access
instance variables.
5. Explain memory allocation and use of garbage collector in JAVA.
Ans:
In Java, memory allocation and management are handled by the Java Virtual Machine (JVM). The
JVM divides the memory into several areas, including the heap, method area, and stack. Here's an
explanation of memory allocation and the use of garbage collector in Java:
1. Memory Allocation:
Heap Memory: The heap memory is where objects are stored. It's a runtime data area from
which memory for all class instances and arrays are allocated. Objects in the heap can be
accessed globally from anywhere in the application.
Method Area (Non-Heap Memory): The method area stores class structures such as method
code, field names, method names, and constant pool. It's a shared resource for all threads and
contains static fields, method code, and runtime constant pool.
In summary, Java's memory allocation system manages memory by allocating objects on the heap
and class structures in the method area. The garbage collector automatically reclaims memory from
objects that are no longer in use, ensuring efficient memory utilization and preventing memory
leaks. This automatic memory management simplifies memory management for developers and
reduces the risk of memory-related errors.
6. Write a JAVA program demonstrating Method overloading.
Ans:
Method overloading in Java allows a class to have multiple methods with the same name but different
parameter lists. The compiler determines which version of the overloaded method to call based on the
number and types of arguments passed to it. Here's a Java program demonstrating method overloading:
In the above example:
We have defined three overloaded versions of the add method, each taking a different number or
type of arguments.
The first add method adds two integers, the second adds three integers, and the third adds two
doubles.
We then create an instance of the OverloadingExample class and call each version of the add
method with different arguments.
The compiler determines which version of the add method to call based on the arguments passed
to it, allowing for flexibility and code reuse.
1. Call by Value:
In call by value, a copy of the actual parameter's value is passed to the method.
Changes made to the parameter within the method do not affect the original value of the
argument.
Primitive data types (int, double, char, etc.) are passed by value in Java.
2. Call by Reference:
In call by reference, a reference to the actual parameter's memory location is passed to the
method.
Changes made to the parameter within the method directly affect the original value of the
argument.
Objects (including arrays) are passed by reference in Java.
Example Program:
In summary, call by value involves passing a copy of the value to the method, while call by reference involves
passing a reference to the original value's memory location. Primitive data types are passed by value, and
objects (including arrays) are passed by reference in Java.
1. Nested Classes:
A nested class is simply a class defined within another class, without any special access
modifiers.
Nested classes can be either static or non-static (also known as inner classes).
They can access members of the enclosing class, including private members, directly.
Nested classes are primarily used for logical grouping and code organization.
Example of Nested Class:
2. Inner Classes:
An inner class is a non-static nested class that is associated with an instance of the enclosing
class.
Inner classes have access to instance variables and methods of the outer class, even if they
are private.
Inner classes can be instantiated only within an instance of the outer class.
They are useful when you want to associate one class with another class closely.
Example of Inner Class:
In summary, nested classes are simply classes defined within another class, while inner classes
specifically refer to non-static nested classes. Both nested and inner classes can access members of
the enclosing class, but inner classes are associated with instances of the enclosing class and have
access to instance variables and methods of the outer class.
9. Distinguish between method overloading and method overriding.
10. How do you overload a constructor? Explain with a program.
Ans:
Constructor overloading in Java refers to having multiple constructors within a class, each with a different
parameter list. This allows objects of the class to be instantiated in different ways, providing flexibility in
object creation. Below is an example program demonstrating constructor overloading:
3
In the above program:
The Rectangle class has three constructors: a default constructor, a parameterized constructor
with one parameter (for creating squares), and a parameterized constructor with two parameters
(for creating rectangles with specified length and width).
Depending on how the Rectangle object is created, the appropriate constructor is called to
initialize the object's state.
The calculateArea method calculates the area of the rectangle based on its length and width.
In the main method, we demonstrate creating objects using different constructors and calculating
the area of each rectangle.
11. Define recursion. Write a recursive program to find nth Fibonacci number.
Ans:
Recursion is a programming technique where a function calls itself directly or indirectly to solve a
problem. It is a powerful concept used to break down complex problems into simpler ones and
solve them iteratively. In recursion, the function keeps calling itself with smaller instances of the
problem until a base case is reached, which provides the solution without further recursion.
Explanation:
12. What are various access specifiers in Java? List out the behaviour of each of them.
Ans:
In Java, access specifiers are keywords used to control the visibility or accessibility of classes,
methods, and variables. There are four access specifiers in Java, each with its own behavior:
1. public:
The public access specifier allows classes, methods, and variables to be accessed from any
other class or package.
Classes, methods, and variables declared as public can be accessed by any other class or
package, even outside the package where they are defined.
It provides the highest level of accessibility.
2. protected:
The protected access specifier allows classes, methods, and variables to be accessed within
the same package or by subclasses (inheritance).
Classes, methods, and variables declared as protected can be accessed by any class within
the same package or by subclasses (even if they are in a different package).
It is less restrictive than private but more restrictive than public.
3. private:
The private access specifier restricts the access of classes, methods, and variables to the
class where they are declared.
Classes, methods, and variables declared as private can only be accessed within the same
class. They are not visible to subclasses or other classes, even within the same package.
It provides the highest level of encapsulation and data hiding.
13.Explain the usage of static and final keyword. [EXTRA]
Ans:
“static” keyword:
“final” keyword: