This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pl explain this a bit diff code

 
jerry sharma
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends pl help me out to understand the following questions
Q1
Which of the follwing is true about static modifier.
A.static can be applied to : instance variables, methods,
code Segments and classes.
B.a static method cannot be overridden.
C.inner classes can be both static & private.
D.a static reference cannot be made through non static
method or code block.
E.abstract & static both can be applied together to a
method.

Answer : B C D
My doubt is
why not A option is true

Q2

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.0
 
Suhas Wadadekar
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1 My doubt is
why not A option is true

Q2

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.0[/QB]


For Q1, in my opinion, answer choice A is not correct because they have mentioned it can be applied to classes..
the staic modifier cannot be applied to classes, unless the class is inner (again not method-local). The answer choice simply says "classes" which certainly is not true.

For Q2 mu answer is Option C.
First String object ---> "Hello"
Second String object ---> "Pal"
Third String object ---> "HelloPal"


pls anyone correct me if I am wrong...
[ July 14, 2006: Message edited by: Suhas Wadadekar ]
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer to question 2
There will be total 3 objects

1.Hello Ref(s1,s2)
2.pal(Lost)
3.Hellopal Ref(s3,s4)
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For question 1 the option 1 can be incorrect in only one case
if class is used as top level class,cause top level class
cannot be static.If class is used as inner class then option 1
is correct.
 
jerry sharma
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all friends
for the expalination of Q2 i got it now
Q1 is given as it is, not mentioned inner or something else reg classes

so then what should be select

regards

jerry
 
Gowher Naik
Ranch Hand
Posts: 643
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if inner class is not mention then u should assume it as top level class.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Q1. There are class (static) variables and instance variables. There is no such thing as a static instance variable.
 
Asha Pathik
Ranch Hand
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

D. a static reference cannot be made through non static method or code block.


I'm not able to understand the meaning of this statement. Does this mean that we cannot refer to the static variable from the non-static method of the class? If it so, then this is wrong because we easily refer to the static variables from both type of methods : static and non-static.
 
Vijay Raj
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of question is this. The words chosen to phrase the question are really confusing.

First, what is a static instance variable.
Second, when the question says that static methods can be applied to classes, do we assume that teh question is only talking about top-level class or inner classes also.
Thirdly, option D is wrong. Static variable or methods can be referenced from non-static methods. Only the opposite is not true as we do not have the 'this' reference in non-static methods.

Do we get such questions come in the exam.

regards,
vijay.
 
Irshad Nizami
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding first question, Static variables belong to the class and are initialized when the class is loaded at runtime. Hence, we can't apply 'static' modifier to instance variables which are initialized when a particular instance of a class gets created i.e. we can't make instance variables as class-specific, since their values may differ with each instance ! That's it!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic