Java Problem Solving Exercises

      52 Comments on Java Problem Solving Exercises
Java

Java Problem Solving Exercises 2017

The output of following Programs

Program 1

// filename Main.Java
public class Quiz {
 protected int a, b;
}
 
public class Main {
 public static void main(String args[]) {
Quiz  q = new Quiz();
 System.out.println(t.a + " " + t.b);
 }
}

Output
0 0

In Java, a protected member is accessible in all classes of same package and in inherited classes of other packages. Since Test and Main are in same package, no access related problem in the above program. Also, the default constructors initialize integral variables as 0 in Java (See this GFact for more details). That is why we get output as 0 0.

 Program 2

// filename Test.Java
public class Test {
 public static void main(String[] args) {
 for(int i = 0; 1; i++) {
 System.out.println("Hello");
 break;
 }
 }
}
Run on IDE
Output: Compiler Error

There is an error in condition check expression of for loop. Java differs from C++(or C) here. C++ considers all non-zero values as true and 0 as false. Unlike C++, an integer value expression cannot be placed where a boolean is expected in Java. Following is the corrected program.

// filename Quiz.java
class Quiz {
 public static void main(String[] args) {
 for(int i = 0; true; i++) {
 System.out.println("Hello Kamran");
 break;
 }
 }
}
// Output: Hello Kamran

 Program 3

// filename Main.Java
public class Main {
 public static void main(String args[]) { 
 System.out.println(Sum());
 } 
 int Sum() {
 return 90;
 } 
}
Run on IDE
Output: Compiler Error

Like C++, in Java, non-static methods cannot be called in a static method. If we make fun() static, then the program compiles fine without any compiler error. Following is the corrected program.

// filename Main.Java
public class Main {
 public static void main(String args[]) {
 System.out.println(Sum());
 } 
 static int Sum() {
 return 40;
 }
}
// Output: 40

 Program 4
// filename Quiz.java
public class Quiz {
 public static void main(String args[]) {
 System.out.println(Sum());
 }
 static int Sum() {
 static int x= 0;
 return ++x;
 }
}

Output: Compiler Error

Unlike C++, static local variables are not allowed in Java. See this GFact for details. We can have class static members to count number of function calls and other purposes that C++ local static variables serve. Following is the corrected program.

class Test {
 private static int i;
 public static void main(String args[]) {
 System.out.println(fun());
 }
 static int fun() {
 return ++i;
 }
}
// Output: 1

Java programs with output and explanation and its answers

52 thoughts on “Java Problem Solving Exercises

  1. cheap jordans

    I together with my friends ended up looking through the great hints on your web page and so all of a sudden developed a horrible suspicion I never expressed respect to you for those tips. These young men became for this reason glad to read all of them and have now really been making the most of these things. I appreciate you for actually being indeed thoughtful and also for figuring out this kind of marvelous tips millions of individuals are really wanting to know about. Our own sincere regret for not expressing appreciation to sooner.

    Reply
  2. nike roshe run

    I needed to draft you the little bit of note so as to thank you the moment again for your lovely tips you’ve shared in this case. This has been quite particularly generous with people like you giving freely what many of us might have marketed as an electronic book to end up making some cash for themselves, most importantly seeing that you could possibly have done it if you ever decided. These solutions in addition worked to become good way to be certain that someone else have a similar dream the same as my personal own to figure out a whole lot more related to this condition. Certainly there are thousands of more enjoyable periods up front for many who read your blog.

    Reply
  3. reebok outlet

    I am only commenting to let you know of the fine encounter my cousin’s princess undergone browsing your webblog. She mastered plenty of details, including what it is like to possess a very effective coaching mindset to let the others effortlessly thoroughly grasp a variety of complicated subject areas. You actually surpassed readers’ expected results. Thanks for delivering such valuable, safe, revealing and also cool guidance on your topic to Ethel.

    Reply
  4. adidas tubular

    I would like to show thanks to you for bailing me out of this particular challenge. After researching through the world-wide-web and getting solutions which were not productive, I assumed my entire life was gone. Living devoid of the solutions to the difficulties you’ve fixed as a result of your good site is a critical case, as well as the ones which may have in a negative way damaged my entire career if I hadn’t discovered your blog. Your own personal understanding and kindness in playing with a lot of stuff was important. I am not sure what I would have done if I had not encountered such a stuff like this. I can at this moment look forward to my future. Thanks so much for the high quality and effective help. I will not hesitate to suggest the website to anyone who requires support on this issue.

    Reply
  5. yeezy shoes

    I’m commenting to let you understand of the cool encounter my cousin’s girl had going through your web page. She realized a wide variety of details, most notably what it is like to have a wonderful giving mindset to let men and women quite simply fully grasp several specialized subject matter. You actually exceeded her expectations. Thank you for displaying these warm and helpful, trusted, informative as well as fun guidance on your topic to Lizeth.

    Reply
  6. yeezy shoes

    I want to express my passion for your kindness in support of folks who really need guidance on this particular subject. Your personal dedication to passing the message all through ended up being really advantageous and have made workers like me to get to their aims. Your entire informative key points entails a lot to me and especially to my mates. Many thanks; from each one of us.

    Reply
  7. michael kors uk

    I wish to show some thanks to you for bailing me out of this matter. Right after surfing throughout the online world and finding recommendations which are not productive, I figured my entire life was well over. Living without the presence of answers to the problems you have solved as a result of this article content is a crucial case, as well as those that would have badly affected my entire career if I hadn’t discovered your website. Your good capability and kindness in playing with almost everything was very useful. I am not sure what I would’ve done if I hadn’t discovered such a stuff like this. It’s possible to now relish my future. Thanks very much for this professional and sensible help. I won’t be reluctant to propose your site to anybody who should have assistance on this matter.

    Reply
  8. yeezy shoes

    I precisely had to say thanks again. I do not know the things I would have used without the ideas shared by you concerning such a field. It was before a distressing problem for me personally, but encountering your skilled fashion you solved the issue made me to jump over happiness. I am grateful for the assistance and then hope that you realize what a powerful job you were undertaking educating men and women by way of your webpage. I know that you’ve never got to know any of us.

    Reply
  9. michael kors handbags

    I in addition to my guys appeared to be looking through the good guides located on your web page then instantly developed an awful suspicion I had not expressed respect to the website owner for those strategies. My young men became for this reason excited to read through them and have now quite simply been making the most of those things. I appreciate you for simply being very kind and for considering such really good guides millions of individuals are really eager to discover. My very own honest apologies for not saying thanks to sooner.

    Reply
  10. michael kors outlet online

    I not to mention my buddies have been looking through the best information and facts from your website while before long developed a horrible suspicion I never expressed respect to the web site owner for those techniques. These boys appeared to be absolutely thrilled to study them and have in effect in fact been making the most of these things. Appreciate your actually being really kind and then for picking out such notable things millions of individuals are really desperate to be aware of. My personal sincere regret for not expressing appreciation to you earlier.

    Reply
  11. michael kors outlet

    I wanted to post you this little bit of remark to say thanks a lot as before just for the breathtaking pointers you’ve featured in this article. It has been tremendously generous of people like you giving freely what most people could have offered as an electronic book to generate some bucks for their own end, especially now that you might have tried it if you ever considered necessary. Those tips additionally acted as the good way to comprehend most people have the identical keenness the same as my very own to figure out whole lot more on the topic of this condition. I am certain there are lots of more pleasurable opportunities up front for many who scan through your blog post.

    Reply
  12. yeezy boost

    My spouse and i got very thrilled that Louis could complete his studies through the ideas he gained when using the web page. It’s not at all simplistic to just choose to be giving for free information other folks might have been making money from. And we also do understand we now have you to give thanks to because of that. Most of the illustrations you’ve made, the easy website menu, the friendships your site give support to create – it is everything remarkable, and it’s letting our son and us consider that that matter is amusing, which is extremely important. Thanks for the whole lot!

    Reply
  13. air jordan shoes

    I just wanted to write down a brief word so as to express gratitude to you for these precious points you are giving on this site. My long internet look up has at the end been rewarded with good details to exchange with my relatives. I ‘d repeat that most of us readers actually are undeniably lucky to dwell in a decent community with very many marvellous individuals with interesting methods. I feel really happy to have discovered the webpages and look forward to tons of more enjoyable minutes reading here. Thank you once more for all the details.

    Reply
  14. skechers outlet

    I truly wanted to make a simple remark in order to appreciate you for all the lovely tips and tricks you are posting on this site. My incredibly long internet research has at the end of the day been recognized with wonderful suggestions to exchange with my pals. I would point out that most of us visitors actually are very endowed to live in a perfect site with so many special individuals with helpful methods. I feel rather grateful to have encountered your webpage and look forward to so many more cool minutes reading here. Thanks once more for a lot of things.

    Reply
  15. kobe shoes

    Thank you for your own work on this web page. My mom loves participating in internet research and it is easy to understand why. Most of us know all of the lively medium you produce useful tips and hints by means of this blog and even foster contribution from visitors on the area then our favorite princess is in fact becoming educated a lot of things. Take advantage of the rest of the new year. You’re the one doing a very good job.

    Reply
  16. yeezy shoes

    I simply had to appreciate you yet again. I’m not certain the things that I would have accomplished in the absence of the actual methods contributed by you on my subject. It has been the frightening issue in my circumstances, but witnessing the very specialised style you solved it made me to weep for contentment. I will be grateful for this guidance and in addition sincerely hope you really know what a great job you are accomplishing teaching some other people with the aid of your blog. More than likely you have never encountered any of us.

    Reply
  17. air max

    I have to express some thanks to the writer just for bailing me out of this type of incident. As a result of checking through the the net and meeting strategies that were not productive, I was thinking my entire life was done. Existing devoid of the approaches to the difficulties you have fixed through this guide is a serious case, as well as the kind that could have in a negative way damaged my career if I hadn’t come across the website. Your primary training and kindness in handling the whole lot was tremendous. I’m not sure what I would have done if I hadn’t discovered such a step like this. I’m able to at this time relish my future. Thank you very much for the professional and results-oriented guide. I won’t be reluctant to propose your web sites to anybody who requires guidelines on this topic.

    Reply
  18. michael kors outlet store

    Thanks so much for giving everyone a very terrific chance to read articles and blog posts from this website. It is often very brilliant plus packed with a good time for me personally and my office fellow workers to visit the blog particularly thrice in 7 days to read the latest tips you have got. And definitely, I am just usually fascinated with all the surprising things you serve. Certain 1 areas on this page are indeed the most effective we’ve had.

    Reply
  19. yeezy boost

    I and also my pals have been checking the good tactics located on the blog while then developed a terrible feeling I had not thanked the site owner for those techniques. These men are actually for that reason happy to learn all of them and have undoubtedly been making the most of those things. Appreciate your being well considerate and then for picking out certain fine ideas millions of individuals are really desirous to know about. My sincere apologies for not expressing appreciation to earlier.

    Reply
  20. nike huarache

    Thanks a lot for providing individuals with such a superb possiblity to read from this web site. It is usually so awesome and also stuffed with amusement for me and my office fellow workers to search the blog at minimum 3 times a week to learn the fresh secrets you have. And lastly, I’m so certainly impressed with your gorgeous advice you serve. Certain two areas in this posting are in reality the most effective we have all ever had.

    Reply
  21. yeezy boost

    I am just writing to make you understand what a perfect encounter my friend’s child undergone viewing yuor web blog. She even learned such a lot of things, not to mention how it is like to have an ideal giving mindset to make a number of people really easily know just exactly various tortuous matters. You undoubtedly surpassed her expected results. Thanks for coming up with those good, safe, explanatory and also unique thoughts on that topic to Janet.

    Reply
  22. lacoste outlet

    I wanted to send you this very little remark in order to give many thanks over again regarding the marvelous suggestions you have shared on this page. This is certainly seriously open-handed of people like you giving extensively exactly what most of us would have offered for sale as an ebook to make some cash on their own, primarily given that you might well have tried it in case you desired. The concepts in addition acted like the fantastic way to understand that other people online have the identical desire just as my own to see way more related to this condition. I believe there are thousands of more pleasant opportunities up front for individuals that start reading your site.

    Reply
  23. yeezy boost

    I wanted to write you a little remark to thank you so much once again for your incredible advice you have documented in this article. This is so pretty open-handed of you to offer publicly all that most of us could have made available as an e-book to end up making some dough for their own end, principally given that you might well have done it if you considered necessary. The strategies in addition served as a great way to comprehend some people have the identical desire the same as my very own to grasp lots more regarding this problem. I am sure there are thousands of more pleasant instances ahead for individuals that read through your blog.

    Reply
  24. nike roshe uk

    A lot of thanks for all of your effort on this web site. My mom really loves managing investigations and it’s really simple to grasp why. A number of us notice all concerning the lively ways you give informative information through your blog and as well encourage contribution from some other people on the matter plus our favorite simple princess is undoubtedly becoming educated a lot of things. Have fun with the rest of the new year. Your conducting a fantastic job.

    Reply
  25. cheap basketball shoes

    I definitely wanted to jot down a brief remark to be able to say thanks to you for the lovely facts you are giving out on this site. My extensive internet search has finally been rewarded with reputable tips to share with my partners. I would state that that many of us site visitors are very much fortunate to live in a remarkable site with so many special people with great techniques. I feel extremely fortunate to have discovered the web pages and look forward to really more enjoyable moments reading here. Thanks once again for all the details.

    Reply
  26. michael kors outlet

    Thanks a lot for providing individuals with remarkably wonderful possiblity to check tips from this website. It’s always so enjoyable and as well , packed with amusement for me and my office acquaintances to visit your web site really 3 times in a week to study the newest items you have. And indeed, we’re usually amazed with all the attractive guidelines you give. Certain 3 ideas in this post are rather the very best I have had.

    Reply
  27. yeezy boost

    I am commenting to let you know of the fantastic encounter my friend’s child encountered visiting yuor web blog. She came to find plenty of issues, with the inclusion of how it is like to have an excellent helping mood to have many people very easily completely grasp some grueling subject areas. You undoubtedly surpassed readers’ expected results. Thank you for imparting such priceless, healthy, informative and as well as easy guidance on this topic to Julie.

    Reply
  28. yeezy boost

    I am only writing to make you be aware of what a useful experience my daughter found reading through your web page. She came to understand several things, with the inclusion of what it’s like to have a marvelous teaching spirit to let folks smoothly know just exactly some very confusing subject areas. You undoubtedly exceeded people’s desires. Thanks for coming up with such essential, trusted, revealing and easy tips about your topic to Mary.

    Reply
  29. adidas stan smith sneakers

    Thanks so much for giving everyone an extremely nice opportunity to read critical reviews from this blog. It’s usually so amazing and as well , packed with amusement for me and my office peers to search the blog minimum thrice weekly to see the latest guidance you have. And definitely, I’m certainly contented concerning the powerful tricks you give. Selected two ideas in this post are in reality the most suitable we’ve had.

    Reply
  30. air jordan

    I in addition to my friends have been reading the good helpful tips located on the website and so then I had a horrible suspicion I never thanked the blog owner for those secrets. All the women are already warmed to study all of them and have now very much been making the most of those things. Thank you for getting very considerate as well as for choosing this sort of ideal subject areas millions of individuals are really desperate to know about. Our sincere apologies for not expressing gratitude to earlier.

    Reply
  31. yeezy boost 350

    I would like to show my affection for your generosity for people that need guidance on the situation. Your very own dedication to getting the message around ended up being definitely functional and has always permitted individuals like me to realize their dreams. Your invaluable tips and hints means a great deal a person like me and far more to my office colleagues. Thanks a lot; from each one of us.

    Reply
  32. michael kors factory outlet

    I precisely wanted to appreciate you yet again. I’m not certain the things that I would have implemented in the absence of the actual strategies revealed by you about my field. Certainly was an absolute intimidating concern in my position, but taking note of your specialized approach you managed that made me to jump over joy. Now i’m happy for this advice and in addition pray you realize what an amazing job you were undertaking training people today via your websites. I’m certain you haven’t come across any of us.

    Reply
  33. cheapbasketballshoes.us.com

    I as well as my pals ended up checking out the good helpful hints from your website while immediately I got a horrible feeling I had not expressed respect to the web blog owner for those techniques. All the young men are already as a consequence joyful to read through all of them and already have surely been loving them. Thank you for genuinely indeed thoughtful and for considering these kinds of useful subject matter millions of individuals are really desirous to understand about. My honest regret for not expressing appreciation to sooner.

    Reply
  34. yeezy boost

    I would like to voice my respect for your kind-heartedness supporting women who really want guidance on that niche. Your personal dedication to passing the message all around turned out to be amazingly powerful and have frequently empowered guys and women like me to attain their endeavors. Your amazing valuable guide denotes a whole lot a person like me and even more to my mates. Regards; from each one of us.

    Reply
  35. michael kors outlet

    My wife and i were really more than happy Peter managed to complete his investigation out of the precious recommendations he came across from your web site. It is now and again perplexing to simply always be releasing tips and hints that some people could have been making money from. We consider we have the blog owner to appreciate because of that. The main illustrations you made, the easy web site menu, the friendships you help to create – it is many sensational, and it’s leading our son and the family understand the situation is brilliant, and that’s especially mandatory. Thanks for all!

    Reply
  36. chrome hearts online

    I would like to express my affection for your kind-heartedness in support of folks who require help with this particular area. Your personal dedication to getting the message around had become remarkably practical and have usually helped women like me to get to their pursuits. Your amazing helpful hints and tips implies this much to me and substantially more to my peers. Best wishes; from each one of us.

    Reply
  37. yeezy boost

    I wanted to send a brief word in order to express gratitude to you for some of the awesome strategies you are showing on this site. My incredibly long internet investigation has at the end of the day been rewarded with sensible ideas to exchange with my co-workers. I ‘d believe that most of us visitors actually are very much lucky to exist in a fine network with many wonderful professionals with very helpful tips. I feel very much fortunate to have come across your entire website and look forward to really more entertaining times reading here. Thanks a lot again for a lot of things.

    Reply
  38. yeezy boost

    I would like to show my respect for your kindness in support of persons who really want help with this one content. Your very own commitment to getting the solution up and down had been amazingly interesting and have constantly encouraged ladies just like me to realize their objectives. Your new interesting information denotes this much to me and further more to my colleagues. Best wishes; from each one of us.

    Reply
  39. adidas superstar

    My wife and i were very satisfied that John could do his survey from the ideas he received using your web page. It’s not at all simplistic just to always be giving away concepts that many people today may have been making money from. We already know we’ve got the blog owner to appreciate for this. The entire explanations you have made, the simple site menu, the friendships you will give support to foster – it is most incredible, and it is letting our son and the family feel that that situation is entertaining, which is certainly exceptionally indispensable. Thanks for everything!

    Reply
  40. adidas nmd

    I as well as my friends ended up analyzing the good information found on the website and then I got an awful feeling I had not expressed respect to the site owner for those techniques. My men are already as a consequence passionate to learn them and have honestly been taking pleasure in those things. I appreciate you for genuinely simply thoughtful and for utilizing variety of brilliant subject areas most people are really eager to understand about. My very own sincere apologies for not expressing gratitude to you sooner.

    Reply
  41. kobe byrant shoes

    I have to express some thanks to this writer just for rescuing me from this particular trouble. Right after researching throughout the world-wide-web and obtaining concepts which were not helpful, I believed my entire life was gone. Living without the presence of approaches to the issues you have solved all through this guideline is a serious case, and those that might have in a wrong way affected my entire career if I had not encountered your web blog. Your good ability and kindness in playing with all the details was important. I don’t know what I would’ve done if I had not come across such a point like this. I am able to at this point look forward to my future. Thanks a lot very much for this impressive and result oriented help. I will not think twice to propose the blog to any person who needs and wants counselling on this topic.

    Reply
  42. adidas ultra boost

    I am just commenting to make you know of the impressive discovery our princess obtained checking your webblog. She came to find such a lot of pieces, which included what it’s like to have a very effective giving style to let other folks easily gain knowledge of selected complex matters. You really surpassed readers’ expectations. Thanks for churning out such essential, trustworthy, informative as well as unique thoughts on your topic to Lizeth.

    Reply
  43. yeezy shoes

    I precisely had to thank you very much yet again. I’m not certain the things I would’ve achieved in the absence of the actual tricks shown by you about my subject matter. It absolutely was the alarming situation for me personally, but considering this specialized avenue you resolved that made me to weep with contentment. Extremely happier for the advice as well as wish you really know what a powerful job you happen to be putting in teaching many people using your web site. I am certain you haven’t encountered any of us.

    Reply
  44. cheap jordans

    I would like to express my thanks to you just for rescuing me from such a scenario. Because of surfing around throughout the online world and coming across ideas which were not helpful, I was thinking my entire life was well over. Being alive without the presence of answers to the problems you have sorted out by way of the website is a critical case, as well as the ones which could have negatively damaged my career if I had not noticed your web blog. That expertise and kindness in dealing with every item was invaluable. I don’t know what I would’ve done if I had not discovered such a step like this. I can also at this time relish my future. Thank you very much for the skilled and sensible guide. I won’t think twice to recommend your blog to anybody who should get guide about this topic.

    Reply
  45. michael kors handbags

    I intended to draft you a very small observation so as to say thanks over again with your pleasant thoughts you’ve provided above. This is simply strangely open-handed with people like you to deliver freely what many people could possibly have sold as an ebook in making some profit for their own end, and in particular considering that you might well have done it in the event you desired. These strategies likewise acted to become easy way to be sure that some people have the identical desire just as my personal own to learn great deal more regarding this problem. Certainly there are thousands of more pleasant opportunities in the future for people who scan your blog post.

    Reply
  46. yeezy boost

    I and my guys were going through the great things found on your web site and then suddenly got a terrible feeling I never expressed respect to the site owner for them. The young men were definitely thrilled to read through them and have now sincerely been taking advantage of them. Thank you for turning out to be well accommodating as well as for using this sort of smart subject areas most people are really wanting to be informed on. Our honest regret for not expressing gratitude to sooner.

    Reply
  47. michael kors outlet online

    I needed to put you that very small word just to say thanks the moment again for the pleasing thoughts you’ve featured at this time. This has been simply particularly generous with you to provide easily just what a lot of people could possibly have sold as an electronic book to end up making some money on their own, most notably considering the fact that you could have done it in the event you considered necessary. These tricks likewise worked like a fantastic way to know that most people have similar passion the same as my personal own to understand more and more on the topic of this problem. Certainly there are lots of more fun instances up front for folks who go through your website.

    Reply
  48. adidas neo

    Thanks for each of your hard work on this web page. Gloria really likes participating in investigation and it’s really easy to understand why. My spouse and i learn all regarding the powerful ways you give great guidelines through the web site and in addition improve response from other people on that subject so our girl is really understanding a lot of things. Enjoy the rest of the new year. You’re doing a splendid job.

    Reply
  49. pandora jewelry

    I would also like to mention that most individuals that find themselves without health insurance are typically students, self-employed and those that are without a job. More than half on the uninsured are under the age of Thirty five. They do not feel they are needing health insurance as they are young plus healthy. Their particular income is typically spent on houses, food, in addition to entertainment. Many people that do represent the working class either complete or part time are not given insurance by means of their jobs so they move without due to the rising tariff of health insurance in the us. Thanks for the ideas you discuss through this site.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

WordPress spam blocked by CleanTalk.