0% found this document useful (0 votes)
22 views

IAT 2 OOPS Solution

Java supports multiple inheritances through interfaces which allow a class to inherit behaviors from multiple interfaces. A class can implement multiple interfaces and inherit abstract methods from each. Access modifiers like protected and default influence the visibility of classes within and outside packages. Protected allows inheritance within and outside the package while default is only within the package.

Uploaded by

pohv22cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

IAT 2 OOPS Solution

Java supports multiple inheritances through interfaces which allow a class to inherit behaviors from multiple interfaces. A class can implement multiple interfaces and inherit abstract methods from each. Access modifiers like protected and default influence the visibility of classes within and outside packages. Protected allows inheritance within and outside the package while default is only within the package.

Uploaded by

pohv22cs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

USN

Internal Assessment Test II – Jan 2024

Sub: OOPS WITH JAVA Sub Code: BCS306A Branch CSE


:

Date: 18/1/24 Duration 90 mins Max Marks: 50 Sem/Sec: III A,B,C OBE
:

IVE FULL ARK CO RB


T

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();
}

// Implement both interfaces in a class


class MyClass implements Interface1, Interface2 {
@Override
public void method1() {
System.out.println("Implementing method1 from Interface1");
}

@Override
public void method2() {
System.out.println("Implementing method2 from Interface2");
}

// Additional methods specific to MyClass


public void additionalMethod() {
System.out.println("Additional method in MyClass");
}
}

public class Main {


public static void main(String[] args) {
// Create an instance of MyClass
MyClass myObject = new MyClass();

// Call methods from both interfaces


myObject.method1();
myObject.method2();

// Call an additional method from MyClass


myObject.additionalMethod();
}
}

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

correct the error. class Puppy


{ abstract void showName();}
Solution:
No.
Correct code:
Class should be abstract
Abstract class Puppy{abstract void showName();}

2(b) What is overloading. Write the difference between method [5] CO L2


overloading and constructor overloading. (Min 4 points). 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);
}

// Implement the CreditCard interface


class CreditCardImpl implements CreditCard {
@Override
public void acceptRupees(double amount) {
System.out.println("Accepted Rupees: " + amount);
}

@Override
public void acceptDollars(double amount) {
System.out.println("Accepted Dollars: " + amount);
}
}

// Main class to demonstrate the program


public class Main {
public static void main(String[] args) {
// Create an instance of CreditCardImpl
CreditCardImpl myCreditCard = new CreditCardImpl();

// Call acceptRupees() method


myCreditCard.acceptRupees(5000.75);

// Call acceptDollars() method


myCreditCard.acceptDollars(100.50);
}
}
4(a) Explain the hierarchy of exceptions in Java. How are checked and [5] CO L2
unchecked exceptions related in the hierarchy with diagram? 4

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”); }}}

Output: Inside FINALLY block


Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 10 out of bounds for length 2
at ExceptionTest5.main(ExceptionTest5.java:5)

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;

// Constructor of the superclass


Animal(String type) {
this.type = type;
System.out.println("Animal constructor called");
}

void displayInfo() {
System.out.println("Type of animal: " + type);
}
}

class Dog extends Animal {


String breed;

// Constructor of the subclass


Dog(String type, String breed) {
// Calling the constructor of the superclass using super
super(type);
this.breed = breed;
System.out.println("Dog constructor called");
}

void displayBreed() {
System.out.println("Breed of dog: " + breed);
}
}

public class Main {


public static void main(String[] args) {
// Creating an instance of the subclass Dog
Dog myDog = new Dog("Mammal", "Labrador");

// Calling methods from both the superclass and subclass


myDog.displayInfo();
myDog.displayBreed();
}
}

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");
}

default void defaultMethod() {


System.out.println("Default method in the interface");
}

void abstractMethod();
}

// Implement the interface in a class


class MyClass implements MyInterface {

public void abstractMethod() {


System.out.println("Implemented abstract method");
}
}

public class Main {


public static void main(String[] args) {
// Call the static method using the interface name
MyInterface.staticMethod();

MyClass myObject = new MyClass();

myObject.defaultMethod();

myObject.abstractMethod();
}
}

6(a) An object of multi-level inherited abstract class cannot be created in [1] CO L2


memory? State True / false 3

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(d) In java, can an abstract class be instantiated. Yes or no. [1] CO L1


Solution: No 3

6(e) In abstract class we can write abstract methods and non abstract methods. True or [1] CO L1
false. Solution: True 3

6(f) Which keyword used to declare a package? [1] CO L1


Solution:Package 4

6(g) Finally block is related to Exception in java. True or false. [1] CO L1


Solution: True 4

Write syntax to call super class methods using super keyword. [1] CO L1
Solution: Super.method_name(); 3

6(i) Write difference between Exception and Errors. [1] CO L1


Solution: 4
Exceptions are events that occur during the execution of a program and can be handled
programmatically, while errors are typically unrecoverable and arise from critical failures
in the system or the application.

6(j) Method overriding is a compile time polymorphism. True/False [1] CO L1


Solution: True 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.

L2 summarize, describe, interpret, contrast, predict, associate, distinguish, estimate,


differentiate, discuss, extend

L3 Apply, demonstrate, calculate, complete, illustrate, show, solve, examine, modify,


relate, change, classify, experiment, discover.

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.

PROGRAM OUTCOMES (PO), PROGRAM SPECIFIC OUTCOMES (PSO) CORRELATIO


N LEVELS

PO1 Engineering knowledge PO7 Environment and sustainability 0 No Correlation


PO2 Problem analysis PO8 Ethics 1 Slight/Low

PO3 Design/development of solutions PO9 Individual and team work 2 Moderate/


Medium

PO4 Conduct investigations of PO10 Communication 3 Substantial/


complex problems High

PO5 Modern tool usage PO11 Project management and finance

PO6 The Engineer and society PO12 Life-long learning

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.

PSO4 Develop intelligent applications for business and industry

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy