Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

who can xplain these???

 
O Joseph
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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,
Uma...

  •  
    Praveen Kumar Mathaley
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Greenhorn
    Posts: 9
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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--
     
    O Joseph
    Ranch Hand
    Posts: 75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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;
    b=0;
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic