Hi All
there are few questions from Khaalid Mock Exam
Since answers are not provided I would be thnakful if you give your views
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(
String msg) { return false; }
}
class C extends B {
private C() { super(); }
public C(String msg) { this(); }
public C(int i) {}
}
a. Code will fail to compile
b. class C has three constructors
c. Objects of class B cannot be constructed
d. Constructor of A that takes int as an argument will never be called
as a result of calling the constructor of class B or class C
e. At most one of the constructors of each class is called as a result
of constructing an object of class C.
Code fails to compile only if we have public static void main()
method else it compiles nicely
so no idea what is the correct answer
Given the following code, which statements concerning the objects referenced through the member variables i, j and k are true, given that any
thread may call the methods a, b and c at any time?
class Counter {
int v = 0;
synchronized void inc() { v++; }
synchronized void dec() { v--; }
}
public class Q7ed5 {
Counter i;
Counter j;
Counter k;
public synchronized void a() {
i.inc();
System.out.println("a");
i.dec();
}
public synchronized void b() {
i.inc(); j.inc(); k.inc();
System.out.println("b");
i.dec(); j.dec(); k.dec();
}
public void c() {
k.inc();
System.out.println("c");
k.dec();
}
}
a. i.v is guaranteed always to be 0 or 1.
b. j.v is guaranteed always to be 0 or 1.
c. k.v is guaranteed always to be 0 or 1.
d. j.v will always be greater than or equal to k.v at any given time.
e. k.v will always be greater than or equal to j.v at any given time.
d and e should be correct answers what r your views ?
class Base {
int i;
Base() {
add(1);
}
void add(int v) {
i += v;
}
void print() {
System.out.println(i);
}
}
class Extension extends Base {
Extension() {
add(2);
}
void add(int v) {
i += v*2;
}
}
public class Qd073 {
public static void main(String args[]) {
bogo(new Extension());
}
static void bogo(Base b) {
b.add(8);
b.print();
}
}
a.9
b.18
c.20
d.21
e.22
This program calls constructor of extension two times and gives
22 as output can any one pls tell why it is not calling base class constructor