The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes who can xplain these??? 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 "who can xplain these???" Watch "who can xplain these???" New topic

who can xplain these???

O Joseph
Ranch Hand

Joined: Jun 27, 2003
Posts: 75
Question 13

What is the result of attempting to compile and run the above program?
a. Prints: A
b. Prints: B
c. Prints: C
d. Prints: D
e. Prints: E
f. Runtime Exception
g. Compiler Error
h. None of the Above

answer says D, i've tried my best but i still dont understand the logic behind it and similar questions.
secondly: if a variable is declared within an inner class as private. is it visible outside the class? please Explain.
[ edited to preserve formatting using the [code] and [/code] UBB tags -ds ]
[ September 10, 2003: Message edited by: Dirk Schreckmann ]

Uma Balu
Ranch Hand

Joined: Aug 22, 2003
Posts: 61

Let me try explaining.
A static boolean variable declared and not initialized will get a default value "false".
At 1, the condition is checked for if(b), in sense check if( b==true),
at 2, it is if(b=false), that is value false will be assigned to the boolean variable b and then evaluated as if(b), which is similar to the condition at 1.
Condition at 3 is also similar, all these conditions will be satisfied only if b is true.
At 4, since the condition is if(!b), the expression evaluates to true.
that is,
!b --> true , since b is false.
Hence, D is printed.
Hope this helps,

  • Praveen Kumar Mathaley
    Ranch Hand

    Joined: Apr 14, 2003
    Posts: 45
    when if (b=false) statement is executed b is assigned false( note here = operator does not compare the value in java!) in jave to compare the equality operator is ==
    The outer class instance can access the private instance variables of its inner class
    Nishu Batra

    Joined: Sep 09, 2003
    Posts: 9
    Uma is right about the answer.
    Two things
    default value of b is false.
    (b = false ) would assign the value false to b and do a comparison if b is true or not. But since b was false it went to the next comparison.
    now when (!b) is dealt with this condition is true and hence we get the output to be 'D'.

    --nb-- <p>Life is short love life live life..
    O Joseph
    Ranch Hand

    Joined: Jun 27, 2003
    Posts: 75
    thanks y'll,
    please tell me if i'm right to approach it this way:
    0 stands for false while 1 for true.
    and since 0 and 0 ==0
    1 and 0 =1;
    and any other combination with b that is 0 = false.
    so we need !0 to give us true.
    which is why D is the correct answer.
    I agree. Here's the link:
    subject: who can xplain these???
    jQuery in Action, 3rd edition