aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Javabeat query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Javabeat query" Watch "Javabeat query" New topic
Author

Javabeat query

ketki kalkar
Ranch Hand

Joined: May 09, 2007
Posts: 36
hi,
Following are some questions from javabeat site,mock exam number 10.
Given:
1. public class TeSet {
2. public static void main(String args[]) {
3. int m = 2;
4. int p = 1;
5. int t = 0;
6. for(;p < 5;p++) {
7. if(t++ > m) {
8. m = p + t;
9. }
10. }
11. System.out.println("t equals " + t);
12. }
13. }

What is the resulting value of t?

(1) 2
(2) 4
(3) 6
(4) 7
The answer is(2).

* Given the following code, what will be the output?
class Value{ public int i = 15;} //Value
public class Test{
public static void main(String argv[]){
Test t = new Test();
t.first(); }
public void first() {
int i = 5;
Value v = new Value();
v.i = 25;
second(v, i);
System.out.println(v.i); }
public void second(Value v, int i) {
i = 0;
v.i = 20;
Value val = new Value();
v = val;
System.out.println(v.i + " " + i);
}} // Test

(1) 15 0 20

(2) 15 0 15

(3) 20 0 20

(4) 0 15 20
answer is 1.
Please explain.
Nishant Vartak
Greenhorn

Joined: Nov 29, 2005
Posts: 15
public class TeSet {
2. public static void main(String args[]) {
3. int m = 2;
4. int p = 1;
5. int t = 0;
6. for(;p < 5;p++) {
System.out.println("p ="+p+", t ="+t);
7. if(t++ > m) {
8. m = p + t;
9. }
10. }
11. System.out.println("t equals " + t);
12. }
13. }
explaination.
In above example insert the line between 6 & 7. Its quite cleared that loop executes four times. adn t is incremented four times. Hence 't' should be four.
As far as for loop is concerned it should always have boolean condition.
other things are optional i.e declaration and iterator condition.

2.
class Value{ public int i = 15;} //Value
public class Test{
public static void main(String argv[]) {
Test t = new Test();
1.t.first(); }
public void first() {
2.int i = 5;
3.Value v = new Value();
4.v.i = 25;
second(v, i);
5.System.out.println(v.i); }
public void second(Value v, int i) {
6.i = 0;
7.v.i = 20;
8.Value val = new Value();
9.v = val;
10.System.out.println(v.i + " " + i);
}} // Test

line1: a stack is created for method 'first()'.
line 2: local variable i in stack is assigned value 5. its scope is till line5. It will not be visble when method "second" is being executed.
line3 bejct of type Value with reference 'v' is added to heap. its instnace variable i has value '15'. i.e v.i = 15;
line 4: instance variable i of Value(refered by V) is overwrriten i.e v.i = 25;
line 6: local variable i is created with value '0'. its scope exists till line 10.
line 7: inside method 'second()'. another object reference of type v points to our Value Object in heap. i.e. v.i = 25;so now two Object references points to same Value Object in heap. its modified to '20' so now v.i = 20;
line 8: New Value object is created and is referenced by 'val' i.e val.i==15.
so now our heap contains two 'Values' objects(v.i = 25 val.i = 15) and three references (v from first(), v from second(its parameter of second method) and val )
line 9: v= val; ('v' from second method will now refer to 'val' i.e v.i = 15)
line 10: local variable i of method second and v of method second.
line 5. on line 7 the state of object was modified. so now the Object reference v (of first method) is 20 . i.e v.i = 20
same code can be re written as:

class Value{ public int i = 15;} //Value
public class Test{
public static void main(String argv[]) {
Test t = new Test();
t.first(); }

public void first() {
int i1 = 5;
Value v1 = new Value();
v1.i = 25;
second(v1, i1);
System.out.println(v1.i); }

public void second(Value v2, int i2) {
i2 = 0;
v2.i = 20;
Value val = new Value();
v2 = val;
System.out.println(v2.i + " " + i2);
}} // Test
 
Consider Paul's rocket mass heater.
 
subject: Javabeat query