Question
1.Which statement from s1 to s6 (see below) cause compilationerror? Assumethestatementthatcauses compilation is deleted, what is the output when the program is executed? class X{
1.Which statement from s1 to s6 (see below) cause compilationerror? Assumethestatementthatcauses compilation is deleted, what is the output when the program is executed?
class X{ } void f1() { System.out.println("XXX"); }
class Y extends X{
void f1() { System.out.println("YYY"); } }
class Z extends X{
void f1() { System.out.println("ZZZ"); } }
class Test{
static void g(X a)
{ a.f1(); }
public static void main(String args[]){
Y y = new Y(); //s1
g(y); //s2
Object obj = new Y(); //s3
obj.f1(); //s4
X x = new Z(); //s5
x.f1(); //s6
}
------------------------------------------------------
2. Which of the following 10 statements (see ** ) cause compilation or runtime errors
class X{}
class Y extends X{}
class Z extends X{}
class Test{
public static void main(String args[])
{ X x = new X(); Y y = new Y(); Z z = new Z();
Object obj1 = new Y();
Object obj2 = new X();
//**The below 10 statements creating
// references o1, o2, ..., o10
X o1 = new Y(); Xo2 =z; Y o3 = new Z(); Y o4 = (Y) x;
Y o5 = (Y) obj1; Z o6 = z; Z o7 = (Z) y; Z o8 = (X) y; Object o9 = y; Object o10 = (X) obj1;
}
----------------------------------------------------------------------
3. Answer questions (i), (ii) and (iii) below:
classA {
// data/fields
int a; public int b;
private int c;
protected int d;
static private int e;
static public int f;
final int g=5;
// methods
void f1() { /* statements */ } public void f2() { /* statements */ }
protected void f3() { /* statements */ }
private void f4() { /* statements */ }
static public void f5() { /* statements */ }
final void f6(){ /* statements */ }
void f7() { /* (i): list all fields/methods in class A
that can be directly accessed here */
}
static void f8() { /* (ii): list all fields/methods in class A
} that can be directly accessed here*/
}
class B { public static void main(String[] argv)
{
AobjA=newA();
/* (iii): : list all fields/methods in class A
thatcanbeaccessedusingobjA*/
}
}
-----------------------------------------------------------------------
4. What is the output from the following program? Note: if an exception is not handled, then the
program is terminated immediately.
class Test {
static void f1() { throw new RuntimeException("hello"); }
static void f2()
{ try
{ f1(); } catch (Exception e) { System.out.println("FFF"); }
finally { System.out.println("GGG"); }
f1(); System.out.println("HHH");
}
public static void main(String[] argv)
{ try
{ f2();
f1(); }
catch (Exception e) { System.out.println("AAA"); }
try
{ f1(); } catch (Exception e) { System.out.println("BBB"); }
finally { System.out.println("CCC"); }
System.out.println("DDD");
f2(); System.out.println("EEE");
f1();
}} // END class Test
-----------------------------------------------------------------------
5. Implement replace method in linked list that replaces a given findObject by a given replaceObject
in a bag. Return false if findObject cannot be found. The header of the method is as follows:
public boolean replace(T findObject, T replaceObject)
Note: You may assume that fisrtNode is a private data in list which references to first node.
--------------------------------------------------------------
6. Assume that you have the following Bag object,
myBag, with n String data :
BagInterface myBag=
new ArrayBag();
Write Java statements that create a newBag object which contains non-duplicate data in myBag.
Example: if myBag contains data: hello, Hello, world, hello, james, james
newBag object should contain : hello, Hello, world, james
Hint: Use the following Bags methods :
int getCurrentSize ();
boolean isFull (); boolean isEmpty ();
boolean add (T newEntry); T remove (); boolean remove (T anEntry);
void clear (); int getFrequencyOf (T anEntry);
boolean contains (T anEntry);
T [] toArray ();
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started