Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Questions from mock exam

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
****************************************************
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to confirm
I see sachin answers correct

Im totally agree with him
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Cherry Mathew
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
someone pleae confirm Q1 . threee objects are created or just two will pal be placed in the pool
it should be placed
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tom yang,
any answers u r the string specialist.
is "pal"placed in the string pool
Cherry
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any method that keeps count of the number of objects created?
Let me know.
Bye
Arif
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic