jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock Questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock Questions" Watch "Mock Questions" New topic
Author

Mock Questions

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
What will be the result of executing the following program code with assertions enabled?

import java.util.*;

public class Q4d3f {
public static void main(String[] args) {
LinkedList lla = new LinkedList();
LinkedList llb = new LinkedList();
assert lla.size() == llb.size() : "empty";

lla.add("Hello");
assert lla.size() == 1 : "size";

llb.add("Hello");
assert llb.contains("Hello") : "contains";
assert lla.get(0).equals(llb.get(0)) : "element";
assert lla.equals(llb) : "collection";
}
}


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();
}
}


1. i.v guaranteed always to be 0 or 1
2. j.v guaranteed always to be 0 or 1
3. k.v guaranteed always to be 0 or 1
4. j.v will always be greater than or equal to k.v at any given time.
5. k.v will always be greater than or equal to j.v at any given time.



Answer is 22 how???

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();
}
}


whats the default value for the string member variable......



what will be the output.....


public class Q3a0a {
public static void main(String[] args) {
int j = 5;

for (int i = 0; i<j; i++) {
assert i < j-- : i > 0;
System.out.println(i*j);
}
}
}
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
For the linked list question,
program just runs with no output because none of the checks results in Assettion failure.
--------------------------------------
The second question u have already posted again, so better look at that
----------------------------------------

3) Here u get 22 bcoz of the following steps being followed during its run:
a) add(1) in the Base class constructor is called first which finally invokes the method of Extension Class because of the object on which this method is called.
This gives u value of i as 2
b) add(2) in the Extension class is called which results in i's value
as 6
c) finally in the method bogo u have add(8) which calls the method of Extension class and the final value of i is 6 + 8*2 = 22.
d) Print is called thereby printing the i's value.

-------------------------------------------------
4) Defualt value for the String member variable is null
-------------------------------------------------
5) Output:
0 3 4
[ May 04, 2005: Message edited by: Animesh Shrivastava ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock Questions
 
Similar Threads
Synchronization Methods
Mughal rasmussen question
One more on threads
Thread Question
synchronized methods + Threads