wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Questions from mock exam 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 "Questions from mock exam" Watch "Questions from mock exam" New topic
Author

Questions from mock exam

shabbir zakir
Ranch Hand

Joined: Nov 12, 2000
Posts: 183
how many String objects are created when we run the
following code

String s1,s2,s3,s4;
s1 = "Hello";
s2 = s1;
s3 = s2 + "Pal";
s4 = s3;

A. 1
b. 2
c. 3
d. 4
e. we can't say.
they have given the answser is C.Four string objects are created. so how come the answer is 3.CanAnybody explain?

q.2
which of the following are valid constructors of the class named 'SubClass' which extends a class with defualt constructor.
A.public SubClass (int i) {
this();
}

B.public SubClass (int i) {
super();
this();
}
C. private SubClass(int i) {
super();
}
D. protected SubClass(int i) {
this();super();
}
in Q.2 which answer is correct and why? please explain.
Q.3
1. class HasStatic
2. {
3. private static int x = 100;
4.
5. public static void main(String args[])
6. {
7. HasStatic hs1 = new HasStatic();
8. hs1.x++;
9. HasStatic hs2 = new HasStatic();
10. hs2.x++;
11. hs1 = new HasStatic();
12. hs1.x++;
13. HasStatic.x++;
14. System.out.println("x = " + x);
15. }
16. }
in Q.3 the code gets compiled and the output is 104.My question is the compiler should throw an exception at line.11 beacause of creating a object with same name.
Kirti Dhingra
Ranch Hand

Joined: Dec 13, 2000
Posts: 32
Q1. 4 objects are being created from two string literals. if i am wrong, then only 2 objects are being created. But 3 should not be the answer in any case.
Q2. A and C are correct. this() or super() have to be the first statement in the constructor.
Q3. Exception would be thrown only when you create an existing reference i.e. Object a;Object a;. But when a reference is created, it can be assigned to any object at any time.
K Singh
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
Q1. Kirti, String s1,s2,s3,s4 ; this line wont create a new object it creates a reference variable so the answer is two.
if im wrong someone please correct me
if the question is modified to be s2= "hel"+"lo"; then only one object is created
please correct me if im wrong
Q2 . answer is a,c
Q3. what kirti wrote is correct that we r not creating a new refernce we r assinging a new Object to an existing refernce u will get an error only when u redeclare a variable that is if u say Hasstatic hasstatic = new Hasstatic();
and Hasstatic hasstatic = new Hasstatic();
not when Hasstatic hasstatic = new Hasstatic();
hasstatic = new Hasstatic();
Cherry
Jai Viswanathan
Greenhorn

Joined: Jan 06, 2001
Posts: 5
Can someone confirm the answer for question 1 ? I also think it would be 2 objects.
For Qn #s 2 and 3, the answers stated by Kirthi and Cherry are definitely correct.
sachin patel
Ranch Hand

Joined: Nov 28, 2000
Posts: 75
hi ,
This is what i think.. please correct me if i am wrong...
1) 3 objects are created because of "Hello" as first object..
"Pal" as second and s2+"Pal" as the third object as strings are immutable....
2) I think (C) is the answer..... (A) is not correct because as the constructor are not inherited and as you have one constructor with int as argument so compiler wont provide default constructor..... so in SubClass you dont have constructor with no arguments .... so you cannot call this();
3) i think what kirti wrote is correct
------------------
Sachin,
****************************************************
Learn from others mistakes. Life is too short to make all yourself.
****************************************************


Sachin,<P>****************************************************<BR>Learn from others mistakes. Life is too short to make all yourself.<BR>****************************************************
Scott Kiel
Greenhorn

Joined: Jan 05, 2001
Posts: 15
Q1. I think Sachin is right. 2 objects created in the pool and one by concatenating.

Q2. Depending on what assumptions we make, I believe A is still a possible correct answer. The subclass has had a constructor explicitly created. That means the default constructor is not created. There could be an no argument constructor, if it was explicitly included by whoever wrote the class.
Morgan Subram
Greenhorn

Joined: Jan 03, 2001
Posts: 12
String s1,s2,s3,s4;
s1 = "Hello"; //Object Created
s2 = s1;
s3 = s2 + "Pal"; //Two Objects Created --StringBuffer and append
s4 = s3;
So the answer is three

Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Just to confirm
I see sachin answers correct

Im totally agree with him
Maitham H
Greenhorn

Joined: Jan 07, 2001
Posts: 29
I should correct people who said for Q2 that both answer a and c are correct
only answer c is correct.
notic that in option a, the constructor is trying to call the defualt constructor of class SubClass by using the word this(), this will produce a compile time error since the default constructor has been overriden by the constructor SubClass(int i).

A Software life cycle can be greater than its Developer's
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
someone pleae confirm Q1 . threee objects are created or just two will pal be placed in the pool
it should be placed
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53

Q1. s3=s2+"Pal"- String computed at run time are newly created and therefore distinct.(JSL)
"String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code: x = "a" + 4 + "c" is compiled to the equivalent of:
x =new StringBuffer().append("a").append(4).append("c").
toString()"(API)
So I think there are indeed 3 object created.
But, if for example s3="Hi"+"Pal"- Strings computed by constant expressions (�15.28) are computed at compile time and then treated as if they were literals.
In this case,I think only 2 object would be created
If I'm wrong in understanding this,please correct me.
Q2. The default constructor is automatically inserted by the compiler as there is no other constructor defined by the programmer explicitly.In this case u define a constructor with an argument public SubClass (int i) {this()} so a call to this() is a compile
error.
But,as Scott says, if u explicitly write a default constructor it's OK.So,(a) could be a correct answer only with this supposition.
rgds,
Cristi
Amit, Jhalani
Greenhorn

Joined: Jan 11, 2001
Posts: 21
Q1. only 2 string objects will be created "Hello" and "HelloPal"
for q# 2 and 3 kirti is right.

[This message has been edited by Amit, Jhalani (edited January 12, 2001).]
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
Amit,what about "Pal" ?
rgds,
Cristi
Originally posted by Amit, Jhalani:
Q1. only 2 string objects will be created "Hello" and "HelloPal"
for q# 2 and 3 kirti is right.

[This message has been edited by Amit, Jhalani (edited January 12, 2001).]

Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
Tom yang,
any answers u r the string specialist.
is "pal"placed in the string pool
Cherry
Son Le
Greenhorn

Joined: Jan 15, 2001
Posts: 21
Hi,
I think "pal" is placed in the string pool. The operation s3 = s2 + "Pal" happens at runtime generating another string. The answer should be 3.
Please correct me if I'm wrong.


-LHS
Weigang Gu
Ranch Hand

Joined: Jan 16, 2001
Posts: 44
Question1: Three objects are created.
At compile time, Objects "Hello" (referenced by s1) and "Pal" (no reference)are created and put in the String pool. At runtime, s2 becomes the alias of s1 referring to the same String object "Hello". another String object was created by concatenation of s1 + "Hello" and assigned to the reference variable s3. However, the new String object is not in the pool.
So the answer is right.

Arif Attar
Greenhorn

Joined: Jan 15, 2001
Posts: 12
Is there any method that keeps count of the number of objects created?
Let me know.
Bye
Arif
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Questions from mock exam