Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Question (Help)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a rookie in Java and have several Mock question needing your help!
(1).If ("String".replace('g','G')=="String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
1."Equal"
2."Not Equal"
3.compile eror
Ans: 2.
I think after the "String" is immune so even it's changed, the
original one should be the same so why the answer is "Not Equal"?
(2).public class xxx{
public void main(String args[]){
System.out.println(Math.abs(Integer.MIN_VLUE));
}}
1.compile error
2.the value printed on the screen is less than zero
3.the value printed on the screen is one more than Integer.MAX_VALUE
4.will throw exception due to overflow-Integer.MAX_VALUE is less in magnitude than Integer.MIN_VALUE
Ans:2
I have no idea why the answer is 2
(3).Which of the following are true about the class deined inside an intergface
1.it's not posible in the java Language
2.The class is always public
3.The class is always Static
4.the class methods cannot call the methods decalred in the interface
5.the class methods can call only the static methods declared in the interface
Ans:2,3,4
I've never seen a class defined in an interface so why the answers are 2,3,4
(4).System.out.println(Math.min(Float.NaN,Float.POSITIVE_INFINITY));
1.Nan
2.InFinity
3.RunTime Error- Nan is an illegal argument
Ans:1
I think the answer should be 3
(5).Public class xxx{
public xxx() throws IOException{
throw new IOException();}

Assume the destination of yyy begins with the line
public class yyy extends xxx
It's required that none of the constructors of yyy should throw any checked exception
1.It can be achieved by placing the call to the superclass with a super keywor,which
is placed in a try block with a catch block to handle the IOException
thrown by the super class
2.It can be achieved by avoiding the explicit calls to the base class consturctors
3.It cannot be done in the Java with the above definition of the base class
Ans:3
Can a constructor throw a exception , if so , what's the rule for
the constructor of the child class?
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Chang,
If ("String".replace('g','G')=="String".replace('g','G'))
System.out.println("Equal");
else
System.out.println("Not Equal");
1."Equal"
2."Not Equal"
3.compile eror
Here both Strings are creating two new String objects.(because strings are immutable)..
These two strings are refering to two different objects..
== operator returns true only if both objects refering to same object..
If you replace == with equals(ob), it will return true check it.
2.
public class xxx{
public void main(String args[]){
System.out.println(Math.abs(Integer.MIN_VLUE));
}}
For Q2: There is no +ve number corresponding to MIn_value. Because In Integer.. values are from -2^31 to 2^31-1.
abs(-2^31) giving result -2^31..

(3).Which of the following are true about the class deined inside an intergface
1.it's not posible in the java Language
2.The class is always public
3.The class is always Static
4.the class methods cannot call the methods decalred in the interface
5.the class methods can call only the static methods declared in the interface
Read this carefully(This is taken from Roseane Zhangs tutorial)
static and interface:
1) Inner interfaces are implicitly static, no matter you put static modifier or not. Attention: inner interfaces is very rarely used.
2) Outer interface are NOT static, just like outer class.
3) All members (attributes & methods) of interface are implicitly public.
4) All attributes defined in interface are implicitly static and final.
5) All methods defined in interface are NOT static.
6) All classes defined in an interface are implicitly static

[This message has been edited by suresh seeram (edited October 12, 2001).]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred.
1) The following is from the API for String.replace():
" If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.
"
Because two new diferent String objects are created and compared with == false is the result.
2)Integer.MIN_VALUE is -2147483648 clearly < 0
3) 3.2 y 3.3 JLS 9.5:
"Interfaces may contain member type declarations (�8.5). A member type declaration in an interface is implicitly static and public."
3.4 abstract methods can't be called directly from a class member of the interface where they are declared because they are abstract, they have no implementation. They could be called only over an instance of a class that implements (providing and implementation) the interface.
4) From the API for Math.min:
"Returns the smaller of two double values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN.
"
5)Constructor must declare all the checked exceptions declared in the base constructor, and they can add others. This exactly the opposite behaviour as the methods exhibit.
hope helps
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jose,


3.4 abstract methods can't be called directly from a class member of the interface where
they are declared because they are abstract, they have no implementation. They could be
called only over an instance of a class that implements (providing and implementation) the
interface.


I tired this:

The error I got is:
non-static method m() cannot be referenced from a static context
int i = m();
But, if I tried this:

I compiled fine.
So, I think the real reason is not because the method being
called is abstract. It is because we can't reference a
non-static member from a static context, based on the following
facts:
- the method defined in an interfact is abstract, therefore
is non-static
- inner class declared in an interface is implicitly
static.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Nain you are right
 
suresh seeram
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai Nain,
Very good point..

thanks for good discussion..
regards
Suresh
[This message has been edited by suresh seeram (edited October 12, 2001).]
 
Fred Chang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All:
Thanks you for clearing my questions.
 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
just a small question
Math.abs(neg. number) should be positive.
then Math.abs(-2^31) should be positive.
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic