0% found this document useful (0 votes)
7 views4 pages

CSE1115 Mid 191

The document is a mid-term exam paper for the Object-Oriented Programming course at United International University, detailing various programming tasks and questions. It includes code snippets that require students to analyze outputs, fix errors, and implement classes based on given specifications. The exam covers concepts such as inheritance, method overriding, and class design in Java.

Uploaded by

Tanvir
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)
7 views4 pages

CSE1115 Mid 191

The document is a mid-term exam paper for the Object-Oriented Programming course at United International University, detailing various programming tasks and questions. It includes code snippets that require students to analyze outputs, fix errors, and implement classes based on given specifications. The exam covers concepts such as inheritance, method overriding, and class design in Java.

Uploaded by

Tanvir
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/ 4

United International University (UIU)

Dept. of Computer Science and Engineering (CSE)


MID TERM EXAM :: Spring 2019
Course Code: CSI 211 Course Title: Object-Oriented Programming
Date: 7/3/19 Total Marks: 30 Time: 1 Hour 45 mins

1. Write the output of the following code: [5]

public class Zoo { class Magpie extends Bird{


public static void main(String[] args) { Magpie(){
Animal a = new Animal(); System.out.println("A magpie is created.");
Bird b = new Magpie(); }
Magpie c = (Magpie) b; void fly(){
System.out.println("Magpie is flying...");
a.fly(); }
b.fly(); void fly(int speed){
((Magpie)b).fly(15); System.out.println( "Magpie is flying at speed: "
c.fly(); + speed);
}
a.eat(); void eat(){
b.eat(); System.out.println( "Magpie is eating.");
c.eat(); }
} }
}

class Bird extends Animal{ class Animal{


Bird(){ Animal() {
System.out.println("A bird is created."); System.out.println( "An animal is created.");
} }
void fly(){ void fly(){
System.out.println("Flying..."); System.out.println("Don't know if I can fly!");
} }
} void eat(){
System.out.println("Eating...");
}
}

2. Consider the following FruitShop class. Now, write a class Fruit in such a way that [5]
FruitShop class will give expected output as shown below.

Class FruitShop Output


public class FruitShop { Fruit Details:
public static void main(String[] args) { Name: Apple
Fruit fruit1 = new Fruit("Apple", 3.5, 110); Weight: 3.5kg
Fruit fruit2 = new Fruit("Mango", 5, 90); Price per kg: 100.0
Total price: 350.0
fruit1.reducePricePerKG(10);
fruit2.increasePricePerKG(20); Fruit Details:
Name: Mango
fruit1.printDetails(); Weight: 5.0kg
fruit2.printDetails(); Price per kg: 110.0
} Total price: 550.0
}
3 a. Consider the following classes. Will the code run without errors? If so, please specify the [4]
errors. Otherwise, write the output of the code.

class B{ public class D{


void a(){ public static void main(String[] args) {
System.out.println("I am first A inside B"); B obj = new C();
} obj.a();
} obj.a(5);
}
class C extends B{ }
void a(int a){
System.out.println("I am second a inside C, my
val is " + a);
}
}

b. Is it a must for abstract classes to have abstract method/methods? [1]

4 a. Write the output of the following code: [3]

public class Course { public class Application {


String name, id; public static void main(String[] args) {
int credit; Course c1=new Course("OOP","CSI211",3);
public Course(String name, String id, int credit) { Course c2=new Course("SPL","CSI121",1);
this.name = name; c1.display();
this.id = id; c2.display();
this.credit = credit; c1.updateCourse(c2);
} c1.display();
public void display() { c2.display();
System.out.printf("%s-%s-%d\n", name, id, credit); }
} }
public void updateCourse(Course c1){
this.name = c1.name;
this.id = c1.id;
c1.credit--;
}
}

b. Fix the following code. You cannot remove any lines. You can only add or edit existing [2]
lines.

package zoo; package zoo.birds;

class Animal { class Bird extends Animal{


String name; String color;
protected int legs;
Bird(){ }
Animal(String name, int legs){
this.name = name; Bird(String name, int legs, String color){
this.legs = legs; this(name, legs);
} super.color = color;
}
void print(){
System.out.println(name + " " + legs); void print(){
} System.out.println(name + " " +
} legs + " " + color);
}
}
5. a. Will the code run without errors? If so, please specify the errors. Otherwise, write the [2.5]
output of the following code.

class CrazyVars{ public class Execution{

static int i; public static void main(String[] args){


int j; CrazyVars workerOne = new CrazyVars();
CrazyVars workerTwo = new CrazyVars();
void incrementI(){ workerOne.incrementI();
CrazyVars.i++; workerTwo.incrementJ();
} workerTwo.incrementI();
workerOne.incrementJ();
void incrementJ(){ System.out.println(workerOne.i + workerTwo.j);
this.j++; }
} }
}

b. Explain what is wrong with this code. Fix the code. You CANNOT add/delete any line. [2.5]

interface ABC { class C implements ABC{


String s = "123"; String s = "new";
void method1(int a); public void greeter() {
void greeter(); System.out.println("Hi");
} }
}

6. a. Write the output of the following code: [2.5]

public class RefFunc {


int a;
int b;
class SwapFunc { public RefFunc(int a, int b) {
this.a = a;
static void swapReference(RefFunc a, RefFunc b) { this.b = b;
RefFunc c = a; }
a = b; public static void main(String[] args) {
b = c; RefFunc obj1 = new RefFunc(5, 6);
RefFunc obj2 = new RefFunc(7, 8);
} SwapFunc.swapReference(obj1, obj2);
System.out.println(obj1.a + "\t" + obj2.b);
} }
}

b. Will the code run without errors? If so, please specify the errors. Otherwise, write the [2.5]
output of the following code.

abstract class Bike { public class Runner extends Bike {


int brandType; protected void firstCall() {
private abstract void velocity(); super.firstCall();
abstract void showType() { System.out.println("A Runner is being made");
System.out.println("brandType is " + }
brandType); private void velocity() {
} System.out.println("Max velocity is 125");
public void firstCall() { }
System.out.println("A bike is being made"); public static void main(String[] args) {
} Bike obj = new Runner();
} obj.firstCall();

}
}

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