ASSIGNMENT
OBJECT ORIENTED PROGRAMMING WITH JAVA
Submission Date: 24 April
SECTION A
1. Attempt all parts.
Q. Question
(a) Predict the output:
class A {
int x = 10;
}
class B extends A {
int x = 20;
void show() {
[Link](x + super.x);
}
}
class Main {
public static void main(String[] args) {
B obj = new B();
[Link]();
}
}
(b) A programmer says:
“Since Java does not support multiple inheritance, it is impossible for a class to inherit behavior
from more than one source.”
Comment on this statement.
(c) Differentiate between method overloading and method overriding
(d) State difference between Thread class and Runnable interface.
(e) Identify the of method that can be called without creating object.
(f) Define functional interface.
SECTION B
2. Attempt all parts.
Q. Question
I. Create an abstract class 'Bank' with an abstract method 'getBalance' $100, $150 and $200 are
deposited in banks A, B and C respectively. 'BankA', 'BankB' and 'BankC' are subclasses of class
'Bank', each having a method named 'getBalance'. Call this method by creating an object of each of
(a)
the three classes.
II. Analyze a situation where method overloading leads to unexpected compile-time behavior and
suggest improvements.
I. The following code fragment contains error. Can you point it out and explain it?
(b)
II. Explain the lifecycle of multithreading with the help of a diagram. Write the program to create a
thread to print the factorial of a number using the runnable interface.
I. A multithreaded application shows inconsistent results. Diagnose possible causes related to
thread life cycle and synchronization.
(c)
II. A student writes a program to perform two tasks simultaneously but notices that both tasks
execute one after another.
He confirms that he has written a run() method.
● What mistake might he have made?
● Explain why the tasks are not running concurrently.
I. Evaluate the impact of removing functional interfaces on lambda expression usability.
II. Consider the following Java program:
class A {
void show() {
[Link]("Class A");
}
}
class B {
void show() {
[Link]("Class B");
}
}
(d) class C extends A, B {
}
public class Main {
public static void main(String[] args) {
C obj = new C();
[Link]();
}
}
Will this code compile?
● If yes, then what will be the output
● If not, identify the error, Rewrite the program to achieve similar functionality using a valid
approach in Java.
SECTION C
3. Attempt all parts.
Q. Question
(a) Create a class named 'Shape' with a method to print "This is This is shape". Then create two other
classes named 'Rectangle', 'Circle' inheriting the Shape class, both having a method to print "This
is rectangular shape" and "This is circular shape" respectively. Create a subclass 'Square' of
'Rectangle' having a method to print "Square is a rectangle". Now call the method of 'Shape and
'Rectangle' class by the object of 'Square' class.
(b) Predict the output of the following codes. Also, give an explanation.
a) abstract class A {
public void disp() {
[Link]("Concrete method of parent class");
}
abstract public void disp2();
}
class B extends A {
public void disp2() {
[Link]("Overriding abstract method");
}
}
class Main {
public static void main(String args[]) {
B obj = new B();
[Link]();
}
}
b) class Main {
public static void main(String[] args) throws Exception {
[Link](3000);
[Link]("sleep");
}
}
4. Attempt all parts.
Q. Question
(a) A developer creates a class where:
● Internal data is hidden
● Different objects respond differently to the same method call
● Only essential features are exposed to the user
I. Identify all applicable concepts among and justify your answer:
● Polymorphism
● Encapsulation
● Abstraction
II. Explain above each concept briefly.
(b) In a banking application, two users try to withdraw money from the same account at the same time.
Sometimes incorrect balance is shown. Identify the problem in terms of multithreading
Suggest the concept used in Java to solve it.