0% found this document useful (0 votes)
14 views3 pages

Type Casting Needed 26 56.0 26 182.0 20 10 10 20: How Many Objects Are Eligible For Garbage Collection?

Uploaded by

shsy.mn6523
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)
14 views3 pages

Type Casting Needed 26 56.0 26 182.0 20 10 10 20: How Many Objects Are Eligible For Garbage Collection?

Uploaded by

shsy.mn6523
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/ 3

The LNM Institute of Information Technology Roll Number:

Mid Semester Exam (OOPs (using Java)

Time : 1 hour 30 minutes Max Marks: 30


PART-A
Correct the syntax errors (if any) and find the output of the following Java program segments
Note: Show the workout in the answer script. [2X10]

Q1. class Overload { Q2. class Test {


static int x; int a;
double y; int b;
static { x =10;} Test(int i, int j) {
void add(double c , double d){ a = i;
x = (x+c*d); b = j;
y = (c + d)*x; }
} void meth(Test o) {
Overload() { Test ob = new Test(o.a,o.b);
this.y = 10.0; ob.a= ob.a*2;
} ob.b = ob.b/2;
} System.out.println(ob.a + " " + ob.b);
class Output { }
public static void main(String args[]){ }
Overload obj = new Overload(); class Output {
Overload obj1 = new Overload(); public static void main(String args[])
int a = 2; double b = 3.5; {
obj.add(a, a); obj1.add(b, b); Test obj = new Test(10 , 20);
System.out.println(obj.x + " " + obj.y); obj.meth(obj);
System.out.println(obj1.x + " " + obj1.y); System.out.println(obj.a + " " + obj.b);
} }
} }
Type casting needed
20 10
26 56.0 10 20
26 182.0
Q3. Q4. class X2
class Output { { public X2 x;
public static void main(String args[]) public static void main(String [] args) {
{ X2 x2 = new X2();
short x = 0x8000; X2 x3 = new X2();
byte y = 010; x2.x = x3;
System.out.println(x + " and "+y); x3.x = x2;
x = x >>> 15; x2 = new X2();
y = y<<4; x3 = x2;
System.out.println(x + " and "+y); System.out.println(“Garbage Collection”);
} }} how many objects are eligible for
} garbage collection?
Needed Type casting None
-32768 and 8
-1 and -128

Q5. public class Test Q6. public class A


{ public static void main(String [] args) { {
String names [] = new String[5]; void A()
for (int x=0; x < args.length; x++) {
names[x] = args[x]; System.out.println("Class A");
System.out.println(names[2]); }
} public static void main(String[] args)
} {
$ java Test Hello World new A();
(if we run the program by giving the above }
command then what will be the output) }
ArrayOutofBundException Error return type of constructor cant be void
Class A

Q7. class A { Q8. interface Count


final public int GetResult(int a, int b) { short counter = 0;
{ return 0; } void countUp();
} }
class B extends A public class TestCount implements Count
{ public int GetResult(int a, int b) {return 1; } { public void countUp() {
} counter = counter + 10;
public class Test System.out.print(" " + counter);
{ public static void main(String args[]) }
{ public static void main(String [] args) {
B b = new B(); TestCount t = new TestCount();
System.out.println("x = " + b.GetResult(0, t.countUp();
1)); }
} }
} Error can not assign value to final
Error : can’t override the method GetResult Output 0
Result :1

Q9. public class ExceptTest Q10. public class MyProg


{ {
public static void main(String[] args) public static void main(String args[])
{ {
try try
{ {
return; System.out.print(args[0].substring(2));
} }
finally finally
{ {
throw new RuntimeException(); System.out.println("Finally executing ");
System.out.println("Finally"); }
} }
} }
} $java MyProg
RunTimeException (if we run the program by giving the above
command then what will be the output)
ArrayIndexOutofBound and Finally executing
PART – B [10Marks]

Q1. The Charity Collection Box contains money in different currencies - dollars-cents or pounds-pence or
rupees-paise. All of these currencies have notes and coins. The note and coin numbers are counted
when they are added based on their value (that is number of 5 rupee notes, or $1 dollar note).
 Create a super class representing Currency is created with different denomination for of notes
and coins and subclasses Dollar, Pound and Rupee has conversion methods to rupees, print()
and compute().
 Create a class called CollectionBox that allows entry of these currencies in terms of number of
notes and coins of different denomination. Create a display method that allows any of these
currency types and displays the total amount collected in terms of Rupees. (Assume1 dollar= Rs.
50 and 1 pound = 78).

You might also like