Need correction on my answers and description on my errors.


Q1
Which of the following statement(s) is true about an abstract class?
1. An abstract class cannot have any constructors.
2. An abstract class contains only abstract methods
3. An abstract class always implements an interface
4. An abstract class contains one or more methods without an implementation.
5. Objects cannot be created from an abstract class.

Q2
Which of the following statement(s) is true about the program below?

class X {
}
class Y extends X {
void foo() {}
void foo(int m) {}
}

1. method foo is overloaded in Y.
2. method foo is overridden in Y.
3. method foo is overridden in X.
4. the program will not compile.
5. method foo is overloaded in X.

Q3
How many objects can be created from a single class?
1. two
12. one
3. zero or one
4. a maximum of ten
5. any number

Q4
What is the reason that the following code will cause a compilation error?

interface Instrument {
int sound = 15;
void play();
}
class MyInstrument implements Instrument {
public void getSound() {
System.out.println(Instrument.sound);
}
}

1. A class cannot implement an interface it needs to extend it.
2. An interface cannot contain fields (i.e. sound in Instrument)
3. Method getSound needs to create an object before accessing field sound
as sound is not declared static in the interface.
4. MyInstrument does not override method play. (a guess)

Q4
What is the output of the following code?

class C {
private int i;
private int k = 10;
public static void main(String[] args) {
C a2 = new C();
C a1 = new C();
C a3 = new C();
a1.i = a3.i;
a2 = a1;
a2.i = 12;
a3.i = a3.i + 1;
a1.i = 9;
a1.k = 11;
a2.k = 12;
System.out.println(a1.i + " " + a2.i + " " + a3.i + " " + a1.k + " " + a2.k);
}
}

1. 9 9 9 11 12
2. 9 9 12 12
3. 9 9 0 12 12
4. 12 9 1 12 12
5. 9 9 1 12 1

Edited 5 Years Ago by ethio: n/a

There's a difference between doing my work and correcting my work. Clearly you're trying to tell me the first question is wrong, ok. Please have a look at the other questions.

Ok, I have run the last question and it seems the answer is 9 9 1 12 12.
I dont understand how a1.3 can be set to zero while a1.i and a2.i are 9.

Edited 5 Years Ago by ethio: n/a

This article has been dead for over six months. Start a new discussion instead.