my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Khalid Mock Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Khalid Mock Exam" Watch "Khalid Mock Exam" New topic
Author

Khalid Mock Exam

Gaurav Chikara
Ranch Hand

Joined: Jun 09, 2000
Posts: 410
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


SCJP,SCWCD,SCBCD<br />If Opportunity doesn't knock then build the door
Uvnik Gupta
Ranch Hand

Joined: Jul 24, 2000
Posts: 32
CODE FRAGMENT A : b,d
CODE FRAGMENT B : a,b
CODE FRAGMENT C : e. It IS calling the base class constructor but the base class constructor is calling the overloaded add() method (miracles of polymorphism!!!).
Correct me if i am wrong
-Uvnik
Marcela Blei
Ranch Hand

Joined: Jun 28, 2000
Posts: 477
>Answer of question 1:
>a. Code will fail to compile
False. The code will compile fine.
>b. class C has three constructors
True.
>c. Objects of class B cannot be constructed
False. Class B doesn�t has a constructor, so the compiler creates a default constructor for it. It has a method called B that returns a boolean value.
>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
True. You have to call that A constructor explicitly for taking a non default constructor. If not, the JVM call the default constructors of the super class.
>e. At most one of the constructors of each >class is called as a result
>of constructing an object of class C.
False. "At least" is the right answer, no "at most".
>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
This isn�t clear, it has to compile, do you have the error or the complete code?
The 2nd question was further discussed a days ago, I don�t remember the link but there is no possible answer in the given onces. Try to find the discussion.
The 3rd question returns 22 because it calls the add method of the class Extension in the both constructors. It calls the Base and the Extension constructors once ans right. Notice that in constructor Base, it calls the add method of the class Extension.

[This message has been edited by Marcela Blei (edited July 24, 2000).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Khalid Mock Exam
 
Similar Threads
Synchronization Methods
One more on threads
Thread Question
Dumb question about threads
synchronized methods + Threads