• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

confusing question

 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ranchers can any one can explain this question



45.What will be the result of compiling and running the given program?
Select one correct answer.

1. public class Q45
2. {
4. static
5. {
6. System.out.print("Java".startsWith(""));
7. }
8. static void processorB()
9. {
10. System.out.println(" is a boolean Operator.");
11. }
12. static Q45 processorA()
13. {
14. Q45 q=null;
15. if (("java").startsWith("null"))
16. return q;
17. else
18. return null;
19. }
20. public static void main(String[] args)
21. {
22. processorA().processorB();
23. }
24. }


a Compile time error at line number 22 as you can't call processorB() like this.
b NullPointerException at line number 22 as processorA() will return null.
c Program will compile correctly and print false is a boolean operator when executed.
d Program will compile correctly and print true is a boolean operator when executed.

here the ans is d

by velan vel
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi velan,



above code effectively translates to


and
will print true. As statsWith method returns true if string passed as parameter to it is a empty string.

So the output is "true is a boolean operator".

Regards,
Harshil


[/code]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harshil Mehta:
[QB]Hi velan,



above code effectively translates to


and
[CODE]
Hi donot agree with above code transalation.
above will mean processorA function is never being called.

I would like to know any better explanation
regards

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the return type of processorA() is Q45, and the processorB() method is static, then it belongs to the class and not an instance of the class, it doesn�t matter what the processorA() method actually return, even if it�s null, the processorA().processorB() statement is equal to Q45.processorB()
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Program is definately calling processorA().

processorB is a static method so you don't have to provide {object}.processorB();
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you call processorA() it returns Q45 type whose value is null.

But when you call processorB(), you are not calling it as null.processorB(). Instead what is happening is, it is looking at object type Q45 and then processorB() is been called.

Example:
Q45 q = null;
q.processorB(); This is not interpreted as null.processorB()
Instead it is processed as Q45.processorB()

I hope it is clear !

-Dinesh



Answer is d
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body explain the following statement..

 
Ranch Hand
Posts: 1277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in the 1.4 exam u wont get any question on static initialiser blocks
 
vivekkumar sharma
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alejandro Montenegro:
As the return type of processorA() is Q45, and the processorB() method is static, then it belongs to the class and not an instance of the class, it doesn�t matter what the processorA() method actually return, even if it�s null, the processorA().processorB() statement is equal to Q45.processorB()



Hi Alejandro
Thanxs,
i didn't looked at return type of function
regards
 
There's a way to do it better - find it. -Edison. A better tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic