IAT 2 OOPS Solution
IAT 2 OOPS Solution
Date: 18/1/24 Duration 90 mins Max Marks: 50 Sem/Sec: III A,B,C OBE
:
1. a) Explain how access modifiers (protected, default) influence the visibility of [5] CO L2
classes within and outside a package. 3
Explanation - In Java, access modifiers are keywords that control the visibility of
classes, methods, and fields in a program. There are four access modifiers in Java:
public, private, protected, and the default (package-private) modifier. These modifiers
determine which other classes can access the members of a class and in what context.
Here's how the protected and default (package-private) access modifiers influence
the visibility of classes within and outside a package:
Protected Access Modifier:
If a class is declared with the protected modifier, it is accessible within its own package
and by subclasses (regardless of whether they are in the same package or a different one).
Members with protected access are also accessible within the same package and by
subclasses, whether inside or outside the package.
package com.example;
protected class ProtectedClass {
protected void protectedMethod() {
// accessible within the package and by subclasses
}
}
Default (Package-Private) Access Modifier:
If no access modifier is specified (default), it is also known as package-private. Classes
with default access are accessible only within the same package.
Members with default access are accessible only within the same
package. package com.example;
class DefaultClass {
void defaultMethod() {
// accessible only within the package
}
}
1 Describe how Java supports multiple inheritances through interfaces. Write a [5] 3 L2,
(b) java program to achieve multiple inheritances using interface concept.
Solution:
Write up Multiple Inheritance–
Java supports multiple inheritances through interfaces, which are a way to achieve
abstraction and allow a class to inherit the behaviors (method signatures) of multiple
interfaces. In Java, a class can implement multiple interfaces, allowing it to inherit
the abstract methods declared in each interface.
Example:
// Define two interfaces with abstract methods
interface Interface1 {
void method1();
}
interface Interface2 {
void method2();
}
@Override
public void method2() {
System.out.println("Implementing method2 from Interface2");
}
2 (a) Does the below Java code with abstract method compile? If yes [5] CO L3
provide implementation for method. If no write the reason and 3
Solution:
In Java, overloading refers to the ability to define multiple methods or constructors in a
class with the same name but with different parameters. This allows a class to have
multiple methods or constructors that perform similar actions but can handle different
types or numbers of arguments. Overloading is based on the concept of polymorphism
and is a fundamental feature of object-oriented programming
Method Overloading:
1. Involves defining multiple methods in a class with the same name but different
parameter lists.
2. Used for providing variations of a method based on the type or number of
parameters. 3. Return type alone is not sufficient to differentiate overloaded methods.
4. Can be inherited by subclasses.
Constructor Overloading:
1. Involves having multiple constructors in a class with different parameter
lists. 2. Used for creating objects with different initial states.
3. Constructors do not have a return type.
4. Not inherited, but can be called using the super() keyword in subclasses.
3(a). What is the output of the below Java program with an abstract class? [5] CO L3
3
final abstract class Bell
{}
class DoorBell extends Bell{
DoorBell()
{System.out.println(“DoorBell ringing..”);}}
public class AbstractClassTesting2
{public static void main(String[] args){
Bell bell = new DoorBell();}}
Output: Compile Time Error
Reason: Final classes can not be inherited.
3(b) Write a java program to create one interface CreditCard with 2 methods [5] CO L3
accptRupees() and acceptDoller(). Provide implementation for both methods 3
and print the output.
Solution :
// Define the CreditCard interface
interface CreditCard {
void acceptRupees(double amount);
void acceptDollars(double amount);
}
@Override
public void acceptDollars(double amount) {
System.out.println("Accepted Dollars: " + amount);
}
}
Solution:
In Java, exceptions are categorized into a hierarchy based on the inheritance
structure defined by the Throwable class. The two main types of exceptions in this
hierarchy are: Checked Exceptions (Compile-time Exceptions):
∙ These exceptions are checked at compile time.
∙ Subclasses of Exception that are not subclasses of RuntimeException. ∙ Developers
are required to handle or declare these exceptions using the try-catch block.
∙ Common checked exceptions include IOException, SQLException, and
FileNotFoundException.
Unchecked Exceptions (Runtime Exceptions):
∙ These exceptions are not checked at compile time and typically result from
programming errors or unexpected conditions at runtime.
∙ Subclasses of RuntimeException.
∙ Developers are not required to handle or declare these exceptions explicitly.
∙ Common unchecked exceptions include NullPointerException,
ArrayIndexOutOfBoundsException, and ArithmeticException.
∙ The Throwable class is the root class for the exception hierarchy.
4(b) What is the output of the below Java code? [5] CO L3
3
public class ExceptionTest5{
public static void main(String[] args) {
int ary[] = new int[2];
ary[10] = 5;
try { int number= 2/0;}
catch(Exception e){ System.out.println(“Divide by Zero”); }
finally{System.out.println(“Inside FINALLY block”); }}}
5 How is super used to call the constructor of the super class? Write program to [5] CO L3
(a) call super class constructor. 3
Solution:
In Java, the super keyword is used to call the constructor of the superclass. This
is typically done within the constructor of a subclass to invoke the constructor
of its immediate superclass. The super() statement must be the first statement
in the constructor of the subclass.
Example:
class Animal {
String type;
void displayInfo() {
System.out.println("Type of animal: " + type);
}
}
void displayBreed() {
System.out.println("Breed of dog: " + breed);
}
}
5 Write a java program to use static and default methods inside the interface [5] CO L3
(b) and access them. 4
Program :
interface MyInterface {
static void staticMethod() {
System.out.println("Static method in the interface");
}
void abstractMethod();
}
myObject.defaultMethod();
myObject.abstractMethod();
}
}
Solution: True
6(b) Choose a correct statement about Java Interfaces? [1] CO L1
A) Interface contains only abstract methods by default. 3
B) A Java class can implement multiple interfaces
C) An Interface can extend or inherit another Interface.
D) All the above
Solution: D
6(c) Which is the correct syntax to import a Java package below? [1] CO L1
A) 4
import PACKAGE1.*; B) import PACKAGE1.CLASS1; C) import
PACKAGE1.PACKAGE2.PACKAGE3.*; D) All the above
Solution : D
6(e) In abstract class we can write abstract methods and non abstract methods. True or [1] CO L1
false. Solution: True 3
Write syntax to call super class methods using super keyword. [1] CO L1
Solution: Super.method_name(); 3
CI CCI HOD
PO Mapping
COGNITI REVISED BLOOMS TAXONOMY KEYWORDS
VE
LEVEL
L1 List, define, tell, describe, identify, show, label, collect, examine, tabulate, quote, name,
who, when, where, etc.
L4 Analyze, separate, order, explain, connect, classify, arrange, divide, compare, select,
explain, infer.
L5 Assess, decide, rank, grade, test, measure, recommend, convince, select, judge,
explain, discriminate, support, conclude, compare, summarize.
PSO1 Develop applications using different stacks of web and programming technologies
PSO2 Design and develop secure, parallel, distributed, networked, and digital systems
PSO3 Apply software engineering methods to design, develop, test and manage software systems.